diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 8b62d107..5f3d234a 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -134,6 +134,7 @@ $routes->get("delete_invoice/(:any)", "Invoice::delete_invoice/$1"); $routes->add('approve_invoice/(:num)', 'Invoice::approve_invoice/$1'); $routes->get('generate_invoice_pdf/(:num)', 'Invoice::generate_invoice_pdf/$1'); $routes->get('generate_invoice_pdf_preview/(:any)', 'Invoice::generate_invoice_pdf_preview/$1'); +$routes->get('generate_invoice_print_preview/(:any)', 'Invoice::generate_invoice_print_preview/$1'); $routes->get('print_address/(:num)', 'Invoice::print_address/$1'); @@ -173,6 +174,14 @@ $routes->get('subscription_inactive', 'Invoice::subscription_inactive'); # Api integration Routes $routes->post('api/(:any)', 'ApiIntegration::api_integration/$1'); + +# Get Country List + +$routes->get('get_country', 'Customer::get_country_list'); +$routes->post('save_address_from_model', 'Customer::save_address_from_model'); + + + // $routes->group("api", function ($routes) { // // $routes->post("create_products/", "ApiIntegration::save_book_details"); // // $routes->match(['put', 'post', 'get', 'delete'], 'products', 'ApiIntegration::book_api_integration'); diff --git a/app/Controllers/Customer.php b/app/Controllers/Customer.php index 3150760f..585212bf 100644 --- a/app/Controllers/Customer.php +++ b/app/Controllers/Customer.php @@ -276,6 +276,13 @@ class Customer extends BaseController $country_details = $model->orderBy('country_id', 'ASC')->findAll(); return $country_details; } + public function get_country_list() + { + $model = new CustomerModel(); + $model->setTable('countries'); + $country_details = $model->orderBy('country_id', 'ASC')->findAll(); + return json_encode($country_details); + } public function get_state_details() { @@ -548,81 +555,126 @@ 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(); + { + 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') ? $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') ? $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, + $data = [ 'first_name' => $this->request->getPost('cus_first_name'), 'last_name' => $this->request->getPost('cus_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') + '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 ]; - // Call the method to store both addresses - $result = $model->storeCustomerAddresses($billingAddress, $shippingAddress); - if($customerId){ - $cus_first_name = $this->request->getPost('cus_first_name'); - $cus_last_name = $this->request->getPost('cus_last_name'); - $name = $cus_first_name; - if ($cus_last_name !== null) { - $name .= ' ' . $cus_last_name; + 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') ? $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') ? $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($customerId){ + $cus_first_name = $this->request->getPost('cus_first_name'); + $cus_last_name = $this->request->getPost('cus_last_name'); + $name = $cus_first_name; + if ($cus_last_name !== null) { + $name .= ' ' . $cus_last_name; + } } + + if ($customerId) { + // Success response + $results = ['status' => true, 'message' => 'Customer and addresses saved successfully', 'cus_id' => $customerId,'cus_name'=>$name,"billing_addr_id"=>$result['billing_addr_id'],"shipping_addr_id"=>$result['shipping_addr_id']]; + } 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']; } - if ($customerId) { - // Success response - $results = ['status' => true, 'message' => 'Customer and addresses saved successfully', 'cus_id' => $customerId,'cus_name'=>$name,"billing_addr_id"=>$result['billing_addr_id'],"shipping_addr_id"=>$result['shipping_addr_id']]; - } 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]); } - return $this->response->setJSON(['data' => $results]); -} + + public function save_address_from_model(){ + // print_r($this->request->getPost());die; + + + $requestDataBilling = $this->request->getPost('billing'); + $requestDataShipping = $this->request->getPost('shipping'); + + $customer_id_for_addresses = $this->request->getPost('customer_id'); + + // $bill_addr = $this->save_customer_addresses_model($customer_id_for_addresses, $requestDataBilling, 'b'); + // $ship_addr = $this->save_customer_addresses_model($customer_id_for_addresses, $requestDataBilling, 's'); + + $billingData = [ + 'first_name' => $this->request->getPost('first_name'), + 'last_name' => $this->request->getPost('last_name'), + 'address_1'=> $requestDataBilling['address'], + 'address_2'=> '', + 'country' => $requestDataBilling['country'], + 'state' => $requestDataBilling['state'], + 'city' => $requestDataBilling['state'], + 'postal_code' => $requestDataBilling['postalCode'], + ]; + + $shippingData = [ + 'first_name' => $this->request->getPost('first_name'), + 'last_name' => $this->request->getPost('last_name'), + 'address_1'=> $requestDataShipping['address'], + 'address_2'=> '', + 'country' => $requestDataShipping['country'], + 'state' => $requestDataShipping['state'], + 'city' => $requestDataShipping['state'], + 'postal_code' => $requestDataShipping['postalCode'], + ]; + + $model = new CustomerModel(); + + $insertedId = $model->updateAddress($customer_id_for_addresses, $billingData, 'b'); + $insertedId = $model->updateAddress($customer_id_for_addresses, $shippingData, 's'); + + } + + + } diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index 5b300572..67a60e9e 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -5,6 +5,7 @@ namespace App\Controllers; use App\Models\EventModel; use App\Models\CustomerModel; use App\Models\InvoiceModel; +use App\Models\BusinessModel; use App\Models\BooksModel; use App\Helpers\NotificationHelper; @@ -252,7 +253,7 @@ class Invoice extends BaseController 'event_id' => (int)$this->request->getPost('event_id'), 'business_id' => (int)get_business_id(), 'status' => $invoice_status, - 'isactive' => 1 + 'isactive' => 1, ]; ## Based on the invoice ID, we designated Insert or Update on Details... @@ -426,6 +427,7 @@ class Invoice extends BaseController if (!empty($requestData['item_details'][$x])) { $invoiceitem_arr[$x]['invoice_id'] = $id; $invoiceitem_arr[$x]['product'] = (int)$requestData['item_details'][$x]; + $invoiceitem_arr[$x]['description'] = $requestData['description'][$x]; $invoiceitem_arr[$x]['quantity'] = (int)$requestData['quantity'][$x]; $invoiceitem_arr[$x]['tax'] = (float)$requestData['tax'][$x]; $invoiceitem_arr[$x]['unit_price'] = (float)$requestData['rate'][$x]; @@ -689,6 +691,7 @@ class Invoice extends BaseController $invoice_type = $data[0]->invoice_type; // Create an mPDF object + $mpdf = new Mpdf([ 'mode' => '', 'format' => [148, 210], // Set custom width and height in millimeters @@ -732,11 +735,16 @@ class Invoice extends BaseController $mpdf->showWatermarkText = true; } + $BusinessModel = new BusinessModel(); + $business = $BusinessModel->where('business_id',$data[0]->business_id)->first(); + // Generate the PDF content (HTML) with data - $html = view('invoice_pdf_template', ['data' => $data, 'invoiceItems' => $invoiceItems, 'invoice_type' => $invoice_type, 'invoiceTerms' => $data]); + $html = view('invoice_pdf_template', ['data' => $data, 'invoiceItems' => $invoiceItems,'business' => $business, 'invoice_type' => $invoice_type, 'invoiceTerms' => $data]); // echo $html;die; // Load HTML into the mPDF instance + // print_r($html);die; + $mpdf->WriteHTML($html); // Output the PDF to the browser for download @@ -748,7 +756,38 @@ class Invoice extends BaseController { // Load required model $model = new InvoiceModel(); + $BusinessModel = new BusinessModel(); $data = $model->getInvoiceData($id); + + $business = $BusinessModel->where('business_id',$data[0]->business_id)->first(); + $invoiceItems = $model->getInvoiceItems($id, 'groupby'); + + foreach ($invoiceItems as $index => $singleItem) { + $productImgs = $model->getProductImgs($singleItem->product); + $invoiceItems[$index]->imgs = $productImgs; + } + + + $invoice_type = $data[0]->invoice_type; + + // Get status data + $status = $data[0]->status; + + // Load view with data + $html = view('invoice_pdf_template', ['data' => $data, 'invoiceItems' => $invoiceItems, 'invoice_type' => $invoice_type,'business' => $business, 'invoiceTerms' => $data, 'status' => $status]); + + // Return HTML content + echo $html; + } + + public function generate_invoice_print_preview($id) + { + // Load required model + $model = new InvoiceModel(); + $data = $model->getInvoiceData($id); + $BusinessModel = new BusinessModel(); + + $business = $BusinessModel->where('business_id',$data[0]->business_id)->first(); // print_r($data);die; $invoiceItems = $model->getInvoiceItems($id, 'groupby'); @@ -763,13 +802,12 @@ class Invoice extends BaseController $status = $data[0]->status; // Load view with data - $html = view('invoice_pdf_template', ['data' => $data, 'invoiceItems' => $invoiceItems, 'invoice_type' => $invoice_type, 'invoiceTerms' => $data, 'status' => $status]); + $html = view('invoice_print_preview_template', ['data' => $data, 'invoiceItems' => $invoiceItems,'business'=>$business, 'invoice_type' => $invoice_type, 'invoiceTerms' => $data, 'status' => $status]); // Return HTML content echo $html; } - public function print_address($id) { // Fetch the invoice data based on $id diff --git a/app/Models/CustomerModel.php b/app/Models/CustomerModel.php index a96343ed..ad491b43 100644 --- a/app/Models/CustomerModel.php +++ b/app/Models/CustomerModel.php @@ -59,6 +59,45 @@ class CustomerModel extends Model return $this->db->insertID(); // Return the last inserted ID } + public function updateAddress($id, $addressData, $addressType) { + + + if ($addressType == 'b') { + $addressTypeValue = 1; + } else { + $addressTypeValue = 2; + } + $isActive = 1; + + // Check if an active address of the specified type already exists for the customer + $existingAddress = $this->db->table('customer_addresses') + ->where('customer_id', $id) + ->where('isactive', $isActive) + ->where('address_type', $addressTypeValue) + ->get() + ->getRow(); + + // If an active address of the specified type exists, update it + if ($existingAddress) { + $this->db->table('customer_addresses') + ->where('customer_id', $id) + ->where('address_type', $addressTypeValue) + ->update($addressData); + return $id; // Return the customer ID + } else { + // If no active address of the specified type exists, insert a new one + $addressData['customer_id'] = $id; + $addressData['address_type'] = $addressTypeValue; + $addressData['isactive'] = $isActive; + $this->db->table('customer_addresses')->insert($addressData); + return $this->db->insertID(); // Return the last inserted ID + } + } + + + + + public function insertAddressBatch($addressesDataArray) { $this->db->table('customer_addresses')->insertBatch($addressesDataArray); return $this->db->insertID(); // Note: insertID() might not be applicable for batch inserts diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php index de400331..2389f44a 100644 --- a/app/Models/InvoiceModel.php +++ b/app/Models/InvoiceModel.php @@ -18,6 +18,7 @@ public function saveInvoiceItemDetails($data){ if($id != ''){ unset($row['invoice_child_id']); // Remove the id from the data to avoid updating it unset($row['created_by']); // bcoz here data are Updating here. + $this->db->table('invoiceitems')->where('invoice_child_id', $id)->update($row);// Update the row with the specified id $affectedRows = $this->db->affectedRows(); $statement[$i] = "Invoice Item - ".$id." ".$affectedRows ? " Updated":" Not Updated"; @@ -254,7 +255,7 @@ public function getJoinedData($where, $orderby = []) { if ($stringflag == 'groupby') { - $select = 'invoiceitems.*, B.*, GROUP_CONCAT(C.name SEPARATOR \',\') as name'; + $select = ' B.*, GROUP_CONCAT(C.name SEPARATOR \',\') as name,invoiceitems.*'; $group_by = 'invoiceitems.invoice_child_id'; } else { $select = 'invoiceitems.*, B.*, C.name as category_name'; @@ -270,7 +271,6 @@ public function getJoinedData($where, $orderby = []) ->groupBy($group_by) // Fixed the variable name here ->get() ->getResult(); - //->orderBy("book_img_id", "asc") // Order by book_img_id in ascending order //->limit(1,0) diff --git a/app/Views/invoice_form.php b/app/Views/invoice_form.php index c19a4539..fddefd83 100644 --- a/app/Views/invoice_form.php +++ b/app/Views/invoice_form.php @@ -6,6 +6,7 @@ .blueText { color: blue !important; } +