diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index cfdacc90..24ba63ae 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -548,38 +548,73 @@ class Customer extends BaseController ## For Ajax Call To Save Invoice Customer... public function save_invoice_customer() - { - helper('session'); - $session_bid = get_business_id(); - $session_uid = get_logged_user_id(); - $model = new CustomerModel(); - - $data = [ - 'first_name' => $this->request->getPost('cus_first_name'), - 'last_name' => $this->request->getPost('cus_last_name'), - 'mobile_no' => $this->request->getPost('cus_mobile_no'), - 'email' => $this->request->getPost('cus_email'), - 'date_of_birth' => NULL, - 'type' => "customer", - 'mode' => "Offline", - 'business_id' => $session_bid, - 'isactive' => 1, - 'created_by' => $session_uid - ]; - - if ($model->insert($data)) { - $this->logger->info("Invoice Customer : has been added successfully. Inserted ID = " . $model->insertID()); - $results = ['status' => true, 'message' => 'customer has been added successfully','cus_id' => $model->insertID()]; - }else { - $this->logger->error("Invoice Customer: Err could not be added. Please try again."); - $results = ['status' => false, 'message' => 'customer could not be added']; - } +{ + helper('session'); + $session_bid = get_business_id(); + $session_uid = get_logged_user_id(); + $model = new CustomerModel(); - //get all customerss - $CustomerModel = new CustomerModel(); - $where = ['isactive' => 1, 'business_id' => (int)$session_bid]; - $results['customers'] = $CustomerModel->where($where)->orderBy('customer_id', 'DESC')->findAll(); - return $this->response->setJSON(['data' => $results]); + $data = [ + 'first_name' => $this->request->getPost('cus_first_name'), + 'last_name' => $this->request->getPost('cus_last_name'), + 'mobile_no' => $this->request->getPost('cus_mobile_no'), + 'email' => $this->request->getPost('cus_email'), + 'date_of_birth' => NULL, + 'type' => "customer", + 'mode' => "Offline", + 'business_id' => $session_bid, + 'isactive' => 1, + 'created_by' => $session_uid + ]; + + if ($model->insert($data)) { + $customerId = $model->insertID(); + + $this->logger->info("Invoice Customer : has been added successfully. Inserted ID = " . $customerId); + + // Store both billing and shipping addresses + $billingAddress = [ + 'customer_id' => $customerId, + 'first_name' => $this->request->getPost('cus_first_name'), + 'last_name' => $this->request->getPost('cus_last_name'), + 'address_type' => 1, // Billing address type + 'billing_address1' => $this->request->getPost('baddress1'), + 'billing_address2' => $this->request->getPost('baddress2'), + 'billing_city' => $this->request->getPost('bcity'), + 'billing_state' => $this->request->getPost('bstate'), + 'billing_country' => $this->request->getPost('bcountry'), + 'billing_pincode' => $this->request->getPost('bzip') + ]; + + $shippingAddress = [ + 'customer_id' => $customerId, + 'first_name' => $this->request->getPost('cus_first_name'), + 'last_name' => $this->request->getPost('cus_last_name'), + 'address_type' => 2, // Shipping address type + 'shipping_address1' => $this->request->getPost('saddress1'), + 'shipping_address2' => $this->request->getPost('saddress2'), + 'shipping_city' => $this->request->getPost('scity'), + 'shipping_state' => $this->request->getPost('sstate'), + 'shipping_country' => $this->request->getPost('scountry'), + 'shipping_pincode' => $this->request->getPost('szip') + ]; + + // Call the method to store both addresses + $result = $model->storeCustomerAddresses($billingAddress, $shippingAddress); + + if ($result) { + // Success response + $results = ['status' => true, 'message' => 'Customer and addresses saved successfully', 'cus_id' => $customerId]; + } else { + // Error response + $results = ['status' => false, 'message' => 'Failed to save customer and addresses']; + } + } else { + $this->logger->error("Invoice Customer: Error could not be added. Please try again."); + $results = ['status' => false, 'message' => 'Customer could not be added']; } - + + return $this->response->setJSON(['data' => $results]); +} + } diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index 5c3da750..8af5a9fd 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -753,7 +753,7 @@ $encodedUrl = urlencode($url); // Load required model $model = new InvoiceModel(); $data = $model->getInvoiceData($id); - + // print_r($data);die; $invoiceItems = $model->getInvoiceItems($id,'groupby'); foreach($invoiceItems as $index => $singleItem) diff --git a/app/Models/CustomerModel.php b/app/Models/CustomerModel.php index 28f211b0..61137c40 100644 --- a/app/Models/CustomerModel.php +++ b/app/Models/CustomerModel.php @@ -174,6 +174,46 @@ return $this->db->table('customer_groups as CG' ) ->getResultArray(); } +public function storeCustomerAddresses($billingAddress, $shippingAddress) +{ + // $this->db->transStart(); // Begin transaction + + // Insert billing address + $billingData = [ + 'customer_id' => $billingAddress['customer_id'], + 'first_name'=> $billingAddress['first_name'], + 'last_name'=> $billingAddress['last_name'], + 'address_1' => $billingAddress['billing_address1'], + 'address_2' => $billingAddress['billing_address2'], + 'city' => $billingAddress['billing_city'], + 'state' => $billingAddress['billing_state'], + 'country' => $billingAddress['billing_country'], + 'postal_code' => $billingAddress['billing_pincode'], + 'address_type' => 1 // Billing address type + ]; + $this->db->table('customer_addresses')->insert($billingData); + + // Insert shipping address + $shippingData = [ + 'customer_id' => $shippingAddress['customer_id'], + 'first_name'=> $shippingAddress['first_name'], + 'last_name'=> $shippingAddress['last_name'], + 'address_1' => $shippingAddress['shipping_address1'], + 'address_2' => $shippingAddress['shipping_address2'], + 'city' => $shippingAddress['shipping_city'], + 'state' => $shippingAddress['shipping_state'], + 'country' => $shippingAddress['shipping_country'], + 'postal_code' => $shippingAddress['shipping_pincode'], + 'address_type' => 2 // Shipping address type + ]; + $this->db->table('customer_addresses')->insert($shippingData); + return true; // Return true indicating successful insertion + + // Rollback transaction in case of an error + + + // Returns transaction status +} } ?> \ No newline at end of file diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php index dd2899b3..29e8b9e1 100644 --- a/app/Models/InvoiceModel.php +++ b/app/Models/InvoiceModel.php @@ -121,10 +121,9 @@ public function getSubscriptionInvoiceDetail($where) } - public function getJoinedData($where) - { - - return $this->db->table($this->table.' as I' ) +public function getJoinedData($where) +{ + $query = $this->db->table($this->table.' as I' ) ->join('customers as C', 'C.customer_id = I.customer_id', 'left') ->join('subscription as S', 'S.invoice_id = I.invoice_id', 'left') ->join('events as E', 'E.event_id = I.event_id', 'left') @@ -136,12 +135,16 @@ public function getSubscriptionInvoiceDetail($where) // ->like('C.first_name',"%John%") ->orderBy('I.invoice_id', 'DESC') ->groupBy('I.invoice_id') - ->get() - ->getResultArray(); - // print_r($this->db->getLastQuery());die; - - // I.shipping_address_id, I.billing_address_id, - } + ->get(); + + $resultArray = $query->getResultArray(); + + // Print the last executed query + // echo $this->db->getLastQuery();die; + + return $resultArray; +} + ## subscription_inactive public function subscription_inactive(){ @@ -236,8 +239,9 @@ public function getSubscriptionInvoiceDetail($where) ->select('invoice.*,DATE_FORMAT(invoice.invoice_date, "%d/%m/%Y") AS formatted_invoice_date,DATE_FORMAT(invoice.due_date, "%d/%m/%Y") AS formatted_due_date ,concat(C.first_name," ",C.last_name) as customer_name,C.mobile_no,A.address_1, A.address_2,A.postal_code,A.city, A.state,C.mobile_no as customer_mobile') ->select('B.title as company_name,B.business_logo,B.terms as company_terms,B.address as company_address,B.city as company_city,B.state as company_state,B.postal_code as company_postal_code,B.email as company_email,B.mobile_no as company_mobile_no') ->select('COALESCE(NULLIF(states.state_name, ""), A.state) AS customer_bill_state') - ->select('concat(A.address_1," ", A.address_2) as customer_bill_address,A.postal_code as customer_bill_postal_code ,A.city as customer_bill_city, countries.country_name as customer_bill_country') - ->select('concat(S.address_1," ", S.address_2) as customer_ship_address,S.postal_code as customer_ship_postal_code ,S.city as customer_ship_city, countries.country_name as customer_ship_country') + ->select('COALESCE(NULLIF(states.state_name, ""), S.state) AS customer_ship_state') + ->select('concat(A.address_1," ", A.address_2) as customer_bill_address,A.postal_code as customer_bill_postal_code ,A.city as customer_bill_city, countries.country_name as customer_bill_country,A.country as bcountry,A.state as bstate') + ->select('concat(S.address_1," ", S.address_2) as customer_ship_address,S.postal_code as customer_ship_postal_code ,S.city as customer_ship_city, countries.country_name as customer_ship_country,S.country as scountry,S.state as sstate') ->get() ->getResult(); diff --git a/app/Views/invoice_form.php b/app/Views/invoice_form.php index 24a9f5ef..994eb475 100644 --- a/app/Views/invoice_form.php +++ b/app/Views/invoice_form.php @@ -161,7 +161,7 @@