diff --git a/app/Controllers/EnquiryController.php b/app/Controllers/EnquiryController.php index 17182fe..4e9209c 100644 --- a/app/Controllers/EnquiryController.php +++ b/app/Controllers/EnquiryController.php @@ -218,7 +218,7 @@ class EnquiryController extends ResourceController 'mobile' => $data['mobile'], 'email' => $data['email'], 'reg_no' => $data['reg_no'], - 'vehicle_type_id' => $data['vehicle_type_id'], + 'vehicle_type_id' => $data['vehicle_type_id'] ?? 0, 'rc_file_name' => $rcFileName, 'id_proof_file_name' => $idProofFileName, 'previous_policy_file_name' => $previousPolicyFileName, @@ -230,7 +230,7 @@ class EnquiryController extends ResourceController 'assigned_to' => $data['assigned_to'] ?? 0, 'insurer_id' => $data['insurer_id'] ?? 0, 'insurer_branch_id'=> $insurerBranchresult['id'] ?? 0, - 'broker_id' => $data['broker_id'] ?? 1, + 'broker_id' => $data['broker_id'] ?? 0, ]; diff --git a/app/Controllers/InvoiceController.php b/app/Controllers/InvoiceController.php new file mode 100644 index 0000000..75fedb3 --- /dev/null +++ b/app/Controllers/InvoiceController.php @@ -0,0 +1,185 @@ +PolicyModel = new PolicyModel(); + $this->QuotationModel = new QuotationModel(); + $this->EnquiryModel = new EnquiryModel(); + $this->InvoiceModel = new InvoiceModel(); + $this->InvoiceItemModel = new InvoiceItemModel(); + $this->InvoiceUtrModel = new InvoiceUtrModel(); + $this->db = \Config\Database::connect(); + } + + public function invoiceList() + { + try { + + $data = $this->InvoiceModel->where('is_active', 1)->orderBy('id', 'DESC')->findAll(); + + return $this->respond(['status' => 'success', 'code' => 200,'data' => $data ], 200); + + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + + public function findInvoiceWithItems() + { + try { + $id = $this->request->getGet('id'); + + if (!$id) { + return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 400); + } + + $invoice = $this->InvoiceModel->where('id', $id)->first(); + + if (!$invoice) { + return $this->respond(['status' => 'failed', 'code' => 404, 'data' => 'Invoice not found'], 404); + } + + $items = $this->InvoiceItemModel->where('invoice_id', $id)->where('is_active', 1)->findAll(); + + return $this->respond([ + 'status' => 'success', + 'code' => 200, + 'data' => [ + 'invoice' => $invoice, + 'items' => $items + ] + ], 200); + + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + + public function createOrUpdateInvoice() + { + $this->db->transBegin(); + + try { + $input = $this->request->getJSON(true); + + // Basic invoice data + $invoiceData = [ + 'invoice_no' => $input['invoice_no'] ?? '', + 'invoice_amount' => $input['invoice_amount'] ?? 0, + 'agent_id' => $input['agent_id'] ?? '', + 'invoice_date' => $input['invoice_date'] ?? '', + 'payout_status' => $input['payout_status'] ?? 0, + 'is_active' => 1, + 'updated_at' => date('Y-m-d H:i:s'), + 'updated_by' => $input['updated_by'] ?? 0 + ]; + + $invoiceId = $input['id'] ?? null; + + if (!$invoiceId) { + + $invoiceData['created_at'] = date('Y-m-d H:i:s'); + $invoiceData['created_by'] = $input['created_by'] ?? 0; + + $invoiceId = $this->InvoiceModel->insert($invoiceData); + + if (!$invoiceId) { throw new DataException("Failed to create invoice"); } + + } else { + $this->InvoiceModel->update($invoiceId, $invoiceData); + } + + // Insert or update items + if (!empty($input['items']) && is_array($input['items'])) { + + foreach ($input['items'] as $item) { + + $itemData = [ + 'invoice_id' => $invoiceId, + 'policy_id' => $item['policy_id'], + 'policy_no' => $item['policy_no'], + 'commission_amount' => $item['commission_amount'] ?? 0, + 'is_active' => $item['is_active'] ?? 1, + 'updated_at' => date('Y-m-d H:i:s'), + 'updated_by' => $input['updated_by'] ?? 0 + ]; + + // UPDATE (if id exists) + if (!empty($item['id'])) { + + $this->InvoiceItemModel->update($item['id'], $itemData); + + }else { // INSERT (if id missing) + + $itemData['created_at'] = date('Y-m-d H:i:s'); + $itemData['created_by'] = $input['created_by'] ?? 0; + $this->InvoiceItemModel->insert($itemData); + } + } + } + + + if ($this->db->transStatus() === false) { + $this->db->transRollback(); + throw new DataException("Transaction failed"); + } + + $this->db->transCommit(); + + return $this->respond([ + 'status' => 'success', + 'code' => 200, + 'data' => ['invoice_id' => $invoiceId] + ], 200); + + } catch (\Exception $e) { + $this->db->transRollback(); + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + + public function deleteInvoice() + { + try { + $id = $this->request->getGet('id'); + + if (!$id) { + return $this->respond(['status' => 'failed', 'code' => 400, 'data' => 'ID is required'], 400); + } + + $this->InvoiceModel->update($id, ['is_active' => 0]); + $this->InvoiceItemModel->where('invoice_id', $id)->set(['is_active' => 0])->update(); + + return $this->respond(['status' => 'success', 'code' => 200, 'data' => 'Invoice deleted'], 200); + + } catch (\Exception $e) { + return $this->respond(['status' => 'failed', 'code' => 500, 'data' => $e->getMessage()], 500); + } + } + + + + + + +} diff --git a/app/Controllers/PolicyController.php b/app/Controllers/PolicyController.php index 00ab21a..fa32007 100644 --- a/app/Controllers/PolicyController.php +++ b/app/Controllers/PolicyController.php @@ -127,7 +127,7 @@ class PolicyController extends ResourceController 'model' => $data['model'] ?? null, 'cubic_capacity' => $data['cubic_capacity'] ?? null, 'vehicle_type' => $data['vehicle_type'] ?? null, - 'created_by' => $data['created_by'] + 'created_by' => $data['created_by'] ]; @@ -137,13 +137,15 @@ class PolicyController extends ResourceController //update enquiry status $quotationData = $this->QuotationModel->where('id',$data['quotation_id'])->first(); if (!empty($quotationData)){ - $this->EnquiryModel->update($quotationData['enquiry_id'], ['status'=> 'Policy Created' ]); + $enquiryArray = ['vehicle_type_id' => $data['vehicle_type_id'] ?? 0 , 'broker_id' => $data['broker_id'] ?? 0 , 'status'=> 'Policy Created' ]; + $this->EnquiryModel->update($quotationData['enquiry_id'], $enquiryArray ); } // Call helper to create BDS record after creating client,clientPolicy,vehicle records - $policyData = $this->PolicyModel->select('partner_policy.*,Q.insurer_id,Q.insurer_branch_id,E.name as client_name,E.mobile as client_mobile,E.email as client_email,E.reg_no,E.vehicle_type_id') + $policyData = $this->PolicyModel->select('partner_policy.*,Q.insurer_id,Q.insurer_branch_id,E.name as client_name,E.mobile as client_mobile,E.email as client_email,E.reg_no,E.vehicle_type_id,A.id as agent_id,A.agent_code') ->join('partner_quotation Q', 'Q.id = partner_policy.quotation_id AND Q.status = "Accepted"', 'left') ->join('partner_enquiry E', 'E.id = partner_policy.enquiry_id', 'left') + ->join('partner_agent A', 'A.id = partner_policy.agent_id', 'left') ->where('partner_policy.id',$policyId) ->first(); $bdsLogs = createBDS($policyData, $policyId); @@ -293,8 +295,9 @@ class PolicyController extends ResourceController //update enquiry status $quotationData = $this->QuotationModel->where('id',$data['quotation_id'])->first(); - if (!empty($quotationData)){ - $this->EnquiryModel->update($quotationData['enquiry_id'], ['status'=> 'Policy Created' ]); + if (!empty($quotationData)){ + $enquiryArray = ['vehicle_type_id' => $data['vehicle_type_id'] ?? 0 , 'broker_id' => $data['broker_id'] ?? 0 , 'status'=> 'Policy Created' ]; + $this->EnquiryModel->update($quotationData['enquiry_id'], $enquiryArray ); } }else{ diff --git a/app/Helpers/common_helper.php b/app/Helpers/common_helper.php index a51d543..8514c03 100644 --- a/app/Helpers/common_helper.php +++ b/app/Helpers/common_helper.php @@ -123,6 +123,8 @@ if (!function_exists('createBDS')) { 'endorse_eff_date' => null, 'pre_payable_by' => 1, 'bro_payable_by' => 1, + 'agent_id' => $policyData['agent_id'], + 'agent_code' => $policyData['agent_code'], ]; $policyTransactionId = $policyTransactionModel->insert($policyTransactionData, true); $log[] = "Policy transaction created (ID: {$policyTransactionId})"; diff --git a/app/Models/InvoiceModel.php b/app/Models/InvoiceModel.php index fce8f79..d5c946e 100644 --- a/app/Models/InvoiceModel.php +++ b/app/Models/InvoiceModel.php @@ -18,6 +18,7 @@ class InvoiceModel extends Model 'invoice_amount', 'agent_id', 'invoice_date', + 'payout_status', 'is_active', 'created_at', 'created_by', diff --git a/app/Models/InvoiceUtrModel.php b/app/Models/InvoiceUtrModel.php index 9f0c44c..98b2ddc 100644 --- a/app/Models/InvoiceUtrModel.php +++ b/app/Models/InvoiceUtrModel.php @@ -16,6 +16,7 @@ class InvoiceUtrModel extends Model protected $allowedFields = [ 'invoice_id', 'utr_no', + 'utr_date', 'amount', 'is_active', 'created_at',