diff --git a/app/Config/Constants.php b/app/Config/Constants.php index c95970b7..20716889 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -115,8 +115,8 @@ define('order_column', ["invoice_id","wp_api_order_id","invoice_child_id"]); define('order_child','invoiceitems'); define('order_child_column', ["invoice_id","wp_api_line_items_id","customer_id"]); -define('WAAI_TOKEN', '650be030cedb3'); -define('WAAI_INSTANCE', '650C383B67BC7'); +define('WAAI_TOKEN', '65156a8cc4358'); +define('WAAI_INSTANCE', '65156ADE3E24F'); define('SEND_WAAI_URL', 'https://waai.in/api/send'); diff --git a/app/Controllers/Invoice.php b/app/Controllers/Invoice.php index 5d784ef5..6fe26b8a 100644 --- a/app/Controllers/Invoice.php +++ b/app/Controllers/Invoice.php @@ -1,194 +1,236 @@ (int)$session_bid,'I.isactive' => 1]; - }else{ $where = ['I.business_id != '=>NULL,'I.isactive != ' => NULL];} - - $model = new InvoiceModel(); - $data['page_name'] = 'Invoice Details'; - $data['invoice'] = $model->getJoinedData($where,['status']); - // print_r($data);die(); - $this->render_page('invoice_list', $data); - }else { - return redirect()->to('login'); + $this->logger->info("Invoice: Listing In Admin BID = " . $session_bid); + $where = ['I.business_id' => (int)$session_bid, 'I.isactive' => 1]; + } else { + $this->logger->info("Invoice: Listing In the Super-Admin "); + $where = ['I.business_id != ' => NULL, 'I.isactive != ' => NULL]; + } + + $model = new InvoiceModel(); + $data['page_name'] = 'Invoice Details'; + $data['invoice'] = $model->getJoinedData($where, ['status']); + $this->logger->info("Invoice: Listing Count ." . count($data['invoice'])); + $this->render_page('invoice_list', $data); + } else { + return redirect()->to('login'); + } } -} - - + + ## To Load Invoice ADD/EDIT page... public function new_invoice($id) { helper('session'); - $model = new InvoiceModel(); - $where = ['business_id'=>(int)get_business_id()]; - // Get customer names for the dropdown - $data['customers'] = $model->getData('customers',$where); - $data['events']= $model->getData('events',$where); - $data['books']= $model->getData('books',$where); + $where = ['business_id' => (int)get_business_id()]; + + ## Get customer-names for the dropdown , events-details and books-details + $data['customers'] = $model->getData('customers', $where); + $data['events'] = $model->getData('events', $where); + $data['books'] = $model->getData('books', $where); + if ($id === '0') { + $this->logger->info("Invoice: In Add Page"); $data['page_name'] = 'Add Invoice Details'; $data['invoice_details'] = []; $data['invoice_item_details'] = []; } else if ($id !== '0') { + $this->logger->info("Invoice: In Edit Page ID =" . $id); $data['page_name'] = 'Edit Invoice Details'; - $data['invoice_details'] = $model->where(['invoice_id' => $id,'isactive' => 1])->first(); + $data['invoice_details'] = $model->where(['invoice_id' => $id, 'isactive' => 1])->first(); $data['invoice_item_details'] = $this->get_invoice_item($id); } $this->render_page('invoice_form', $data); } - + + ## For Ajax Call To Fetch/Retrive All Address Details Based On Customer... public function load_details1() { - $id = $this->request->getPost('selectedValue'); $where = ['isactive' => 1, 'customer_id' => (int)$id]; $where['address_type'] = 1; - $data['customer_billing'] = $this->get_customer_address($where,[]); - $select = ["customer_address_id","CONCAT(address_1,' ',address_2) as address"]; + $data['customer_billing'] = $this->get_customer_address($where, []); + $select = ["customer_address_id", "CONCAT(address_1,' ',address_2) as address"]; $where['address_type'] = 2; - $data['customer_shipping'] = $this->get_customer_address($where,$select); + $data['customer_shipping'] = $this->get_customer_address($where, $select); return $this->response->setJSON(['data' => $data]); } + ## For Ajax Call To Fetch/Retrive Shipping Address Details Only Based On Customer... public function load_details2() { $customer_id = $this->request->getPost('customerValue'); $address_id = $this->request->getPost('selectedValue'); - $where = ['isactive' => 1, 'customer_id' => (int)$customer_id,'address_type'=>2,'customer_address_id'=>(int)$address_id]; - $data['customer_shipping'] = $this->get_customer_address($where,[]); + $where = ['isactive' => 1, 'customer_id' => (int)$customer_id, 'address_type' => 2, 'customer_address_id' => (int)$address_id]; + $data['customer_shipping'] = $this->get_customer_address($where, []); return $this->response->setJSON(['data' => $data]); } - public function get_customer_address($where,$select){ + ## For Gethering Address Details... + public function get_customer_address($where, $select) + { $model = new CustomerModel(); - $model->setTable('customer_addresses'); - if(!empty($select)){ + $model->setTable('customer_addresses'); + if (!empty($select)) { $address_details = $model->select($select)->where($where)->findAll(); - }else{ + } else { $address_details = $model->where($where)->findAll(); } return $address_details; } - public function save_invoice(){ - - helper('session'); - $model = new InvoiceModel(); - - // print_r($this->request->getPost()); - // echo "

"; - // Array ( [customer_name] => 1 [shipping_address] => 2 [event_next_id] => 134 [event_id] => 1 [invoice_number] => NBF2023-INV-00133 [order_number] => 12 [invoice_date] => 2023-09-07 [due_date] => 2023-09-07 [item_details] => Array ( [0] => 1 ) [quantity] => Array ( [0] => 2 [1] => 2 ) [rate] => Array ( [0] => 3 [1] => 3 ) [tax] => [amount] => Array ( [0] => 5 [1] => 5 ) [itemDetails] => Array ( [0] => 1 ) [subtotal] => [discount] => 9 [total] => [terms] => i don't like... ) - $duedate = $this->request->getVar('due_date'); - $invdate = $this->request->getVar('invoice_date'); - $data = [ - 'invoice_number' => $this->request->getPost('invoice_number'), - 'customer_id' => (int)$this->request->getPost('customer_name'), - 'billing_address_id' => (int)$this->request->getPost('billing_address_id'), - 'billing_address' => $this->request->getPost('billing_address'), - 'shipping_address_id' => (int)$this->request->getPost('shipping_address_id'), - 'shipping_address' => $this->request->getPost('shipping_address'), - 'invoice_date' => date("Y-m-d", strtotime($invdate)), - 'due_date' => date("Y-m-d", strtotime($duedate)), - 'subtotal' => (int)$this->request->getPost('sub_total'), - 'tax' => (int)$this->request->getPost('invoice_tax'), - 'discount'=> (int)$this->request->getPost('discount'), - 'total_amount'=> (int)$this->request->getPost('grand_total'), - 'order_number'=> $this->request->getPost('order_number'), - 'payment_method'=> 'Cash on Delivery',// "Credit Card," "Cash on Delivery," "PayPal","PayTm","Gpay" - 'payment_status'=> 'Pending',//"Paid," "Pending," "Failed," "Refunded," "Canceled," "Authorized," and "Completed." - 'event_id'=> (int)$this->request->getPost('event_id'), - 'business_id'=> (int)get_business_id(), - 'status' => $this->request->getPost('status'), - 'isactive'=> 1]; - $invoice_id = $this->request->getPost('invoice_id'); + ## To insert or update the details of the invoice + public function save_invoice() + { + $this->logger->info("Invoice: Insert/Update Details"); + try { - - if (empty($invoice_id)) { - $data['created_by'] = (int)get_logged_user_id(); - $model->insert($data); - $IID = $model->insertID(); - // $IID = 1; - } else { - $data['updated_by'] = (int)get_logged_user_id(); - $model->update($invoice_id, $data); - $IID = $invoice_id; - // $IID = 1; + ## Declarions + helper('session'); + $model = new InvoiceModel(); + // Array ( [customer_name] => 1 [shipping_address] => 2 [event_next_id] => 134 [event_id] => 1 [invoice_number] => NBF2023-INV-00133 [order_number] => 12 [invoice_date] => 2023-09-07 [due_date] => 2023-09-07 [item_details] => Array ( [0] => 1 ) [quantity] => Array ( [0] => 2 [1] => 2 ) [rate] => Array ( [0] => 3 [1] => 3 ) [tax] => [amount] => Array ( [0] => 5 [1] => 5 ) [itemDetails] => Array ( [0] => 1 ) [subtotal] => [discount] => 9 [total] => [terms] => i don't like... ) + $duedate = $this->request->getVar('due_date'); + $invdate = $this->request->getVar('invoice_date'); + $invoice_status = $this->request->getPost('status'); + $invoice_id = (!empty($this->request->getPost('invoice_id'))) ? $this->request->getPost('invoice_id') : ""; + $customer_id = (int)$this->request->getPost('customer_name'); + + ## Array Formation For Invoice Details.. + $data = [ + 'invoice_number' => $this->request->getPost('invoice_number'), + 'customer_id' => $customer_id, + 'billing_address_id' => (int)$this->request->getPost('billing_address_id'), + 'billing_address' => $this->request->getPost('billing_address'), + 'shipping_address_id' => (int)$this->request->getPost('shipping_address_id'), + 'shipping_address' => $this->request->getPost('shipping_address'), + 'invoice_date' => (!empty($invdate)) ? date("Y-m-d", strtotime($invdate)) : NULL, + 'due_date' => (!empty($duedate)) ? date("Y-m-d", strtotime($duedate)) : NULL, + 'subtotal' => (int)$this->request->getPost('sub_total'), + 'tax' => (int)$this->request->getPost('invoice_tax'), + 'discount' => (int)$this->request->getPost('discount'), + 'total_amount' => (int)$this->request->getPost('grand_total'), + 'order_number' => $this->request->getPost('order_number'), + 'payment_method' => 'Cash on Delivery', // "Credit Card," "Cash on Delivery," "PayPal","PayTm","Gpay" + 'payment_status' => 'Pending', //"Paid," "Pending," "Failed," "Refunded," "Canceled," "Authorized," and "Completed." + 'event_id' => (int)$this->request->getPost('event_id'), + 'business_id' => (int)get_business_id(), + 'status' => $this->request->getPost('status'), + 'isactive' => 1 + ]; + ## Based on the invoice ID, we designated Insert or Update on Details... + if (empty($invoice_id)) { + $data['created_by'] = (int)get_logged_user_id(); + if ($model->insert($data)) { + $invoice_id = $model->insertID(); + + session()->setFlashdata('success', 'Invoice has been added successfully.'); + $this->logger->info("Invoice: has been added successfully. Inserted ID = " . $invoice_id); + } else { + session()->setFlashdata('error', 'Invoice could not be added. Please try again.'); + $this->logger->error("Invoice: Err Occur could not be added. Please try again."); + } + } else { + $data['updated_by'] = (int)get_logged_user_id(); + if ($model->update($invoice_id, $data)) { + session()->setFlashdata('success', 'Invoice has been updated successfully.'); + $this->logger->info("Invoice: has been updated successfully. Updated ID = " . $invoice_id); + } else { + session()->setFlashdata('error', 'Invoice update failed. Please try again.'); + $this->logger->error("Invoice: Err Failed to update ID =" . $invoice_id); + } + } + + $requestData = $this->request->getPost(); + + ## Array Formation For Events Details And Updating Events also Here.. + $update_events = [ + 'id' => $this->request->getPost('event_id'), + 'next_id' => $this->request->getPost('event_next_id'), + 'business_id' => get_business_id(), + 'updated_by' => get_logged_user_id() + ]; + $this->update_events($update_events); + + + ## For Invoice Item Details Insert/Update.. + $this->save_invoice_item($invoice_id, $requestData); + + ## Notification For Approve.. + if ($invoice_status == 'Approved' && !$invoice_id) { + $this->approve_notifications((int)$invoice_id); + } + } catch (\Exception $e) { + $this->logger->error("Invoice: Err Occur =" . $e->getMessage()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); } - // print_r($data); - // echo "

"; - $requestData = $this->request->getPost(); - $update_events=['id'=>$this->request->getPost('event_id'), - 'next_id' => $this->request->getPost('event_next_id'), - 'business_id' => get_business_id(), - 'updated_by' => get_logged_user_id()]; - $this->update_events($update_events); - // print_r($update_events);echo "

"; - $this->save_invoice_item($IID,$requestData);//die(); - // print_r($IID); - // print_r($requestData);die(); - return redirect()->route('invoice_list'); } - - public function update_events($update_events){ + + ## For Updating Events Details .. + public function update_events($update_events) + { // `id``event_name``business_id``updated_by``updated_on``next_id` $model = new InvoiceModel(); $model->setTable('events'); - $where = ['isactive' => 1, 'id' => (int)$update_events['id'],'business_id'=>(int)$update_events['business_id'],'next_id'=>$update_events['next_id']]; + $where = ['isactive' => 1, 'id' => (int)$update_events['id'], 'business_id' => (int)$update_events['business_id'], 'next_id' => $update_events['next_id']]; $details = $model->where($where)->findAll(); - if(empty($details)){ - $update_where = ['id' => (int)$update_events['id'],'business_id'=>(int)$update_events['business_id']]; - $update_data = ['next_id'=>$update_events['next_id'],'updated_by'=>$update_events['updated_by']]; + if (empty($details)) { + $update_where = ['id' => (int)$update_events['id'], 'business_id' => (int)$update_events['business_id']]; + $update_data = ['next_id' => $update_events['next_id'], 'updated_by' => $update_events['updated_by']]; $model->updateData('events', $update_data, $update_where); } - - } - public function save_invoice_item($id,$requestData){ - - // print_r($requestData['invoice_child_id']); - + + ## To insert or update invoice item details based on invoice ID + public function save_invoice_item($id, $requestData) + { + + ## Get Invoice item details (to checking purpose exist or not based on invoice ID) $getInvoiceItemDetails = $this->get_invoice_item($id); - $itemid = $requestData['invoice_child_id']; - + + ## Declaration $statement = ""; $model = new InvoiceModel(); + $itemid = $requestData['invoice_child_id']; - // IF Any Missing value Means that values are Inactive here.... - if(!empty($itemid)){ - $filteringInvoiceItemIds = []; - for ($y = 0; $y < count($getInvoiceItemDetails); $y++) { - $filteringInvoiceItemIds[$y] = $getInvoiceItemDetails[$y]['invoice_child_id']; + ## IF Any Missing value Means that values are Inactive here.... + if (!empty($itemid)) { + $filteringInvoiceItemIds = []; + for ($y = 0; $y < count($getInvoiceItemDetails); $y++) { + $filteringInvoiceItemIds[$y] = $getInvoiceItemDetails[$y]['invoice_child_id']; + } + + if (!empty($filteringInvoiceItemIds)) { + $A = $filteringInvoiceItemIds; + $B = $itemid; + $missingValues = array_diff($A, $B); + + if (!empty($missingValues)) { + $where = ['isactive' => 1, 'invoice_id' => (int)$id]; + $model->inactiveMissingInvoiceItemDetails($where, $missingValues); } - - if(!empty($filteringInvoiceItemIds)){ - $A = $filteringInvoiceItemIds; - $B = $itemid; - $missingValues = array_diff($A,$B); - - if(!empty($missingValues)){ - $where = ['isactive'=>1,'invoice_id'=>(int)$id]; - $model->inactiveMissingInvoiceItemDetails($where,$missingValues); - } - } - } + } + } // echo "
...................."; $count = count($itemid); $invoiceitem_arr = []; @@ -210,10 +252,11 @@ class Invoice extends BaseController $statement = $model->saveInvoiceItemDetails($invoiceitem_arr); } return $statement; - } - public function get_invoice_item($id){ + ## To Retrive Invoice item details based on invoice ID + public function get_invoice_item($id) + { $model = new InvoiceModel(); $model->setTable('invoiceitems'); $where = ['isactive' => 1, 'invoice_id' => (int)$id]; @@ -221,12 +264,16 @@ class Invoice extends BaseController return $details; } - public function delete_invoice($id){ - + ## To Inactive Invoice details based on invoice ID Including Invoice Item Details also + public function delete_invoice($id) + { helper('session'); - $model = new InvoiceModel(); - $where = ['isactive' => 1, 'business_id' => (int)get_business_id(),'invoice_id' => (int)$id]; - $existed = $model->where($where)->findAll(); + $session_uid = get_logged_user_id(); + try { + $model = new InvoiceModel(); + $where = ['isactive' => 1, 'business_id' => (int)get_business_id(), 'invoice_id' => (int)$id]; + $existed = $model->where($where)->findAll(); + $this->logger->Info("Invoice : Going to Inactive ID = " . $id); if ($existed) { $data['isactive'] = 0; @@ -238,91 +285,175 @@ class Invoice extends BaseController $model->updateData('invoiceitems', $data, $update_where); } } -print_r($getInvoiceItemDetails);die(); + return redirect()->route('invoice_list'); } + + ## To Approve Invoice details based on invoice ID public function approve_invoice($id) { - // Validate $id (e.g., check if it's a valid invoice ID) - - // Update the status in the database to "Approved" - $model = new InvoiceModel(); - $data = ['status' => 'Approved']; - $model->update($id, $data); - + + try { + if (!$id) { + throw new \Exception("Invoice can't able to Approved. Because ID can't Found"); + } + $model = new InvoiceModel(); + helper('session'); + $where = ['isactive' => 1, 'status' => 'Approved', 'invoice_id' => (int)$id]; + $details = $model->where($where)->findAll(); + if (!empty($details)) { // change + // print_r($details);die; + $data = ['status' => 'Approved', 'updated_by' => get_logged_user_id()]; + if ($model->update($id, $data)) { + $this->approve_notifications((int)$id); + session()->setFlashdata('success', 'Invoice has been Approved Successfully.'); + $this->logger->info("Invoice: has been Approved successfully. ID = " . $id); + } else { + $this->logger->error("Invoice: Does Not Exist To Approved"); + throw new \Exception("Invoice can't Approved"); + } + } else { + $this->logger->error("Invoice: already Approved ID = " . $id); + throw new \Exception("Invoice already Approved"); + } + } catch (\Exception $e) { + $this->logger->error("Invoice: Err Occur = " . $e->getMessage()); + session()->setFlashdata('error', 'Message: ' . $e->getMessage()); + } // Redirect back to the invoice list return redirect()->route('invoice_list'); } + public function approve_notifications($invoice_id) + { + $model = new InvoiceModel(); + $where = ['I.business_id' => (int)get_business_id(), 'I.invoice_id' => $invoice_id, 'I.isactive' => 1]; + $details = $model->getJoinedData($where); + $reference_number = ""; + $invoice_serial_number = ""; + $recipient_name = ""; + $approval_date = ""; + $approved_by = ""; + $recipient_email = ""; + $recipient_mobile = ""; + if (isset($details) && gettype($details) === 'object') { + $details = [$details]; + $this->logger->info("Approve : Request data type = " . gettype($details)); + } + helper('notification'); + $notification = new NotificationHelper(); - -public function generate_invoice_pdf($id) -{ - // Fetch the invoice data based on $invoice_id - $model = new InvoiceModel(); - $data = $model->getInvoiceData($id); - $invoiceItems = $model->getInvoiceItems($id); - -// print_r($invoiceItems);die(); + foreach ($details as $rec) { + $reference_number = $rec->order_number; + $invoice_serial_number = $rec->invoice_number; + $recipient_name = $rec->customer_name; + $approval_date = $rec->updated_on; + $approved_by = $rec->updated_by_name; + $recipient_email = $rec->customer_email; + $recipient_mobile = $rec->customer_mobile; + $records['recipient'] = $rec->customer_email; + $records['mobile'] = $rec->customer_mobile; + } - // Create an mPDF object - $mpdf = new Mpdf(); + $records['subject'] = $invoice_serial_number . " - Approval Notification"; + $records['description'] = "VBP Approve Information +

Dear $recipient_name,

+

   We are pleased to inform you that your Invoice has been approved.

+

Details:

+
+

Best regards,

+