diff --git a/app/Controllers/QuotationController.php b/app/Controllers/QuotationController.php index d83826e..66e8ba9 100644 --- a/app/Controllers/QuotationController.php +++ b/app/Controllers/QuotationController.php @@ -252,7 +252,7 @@ class QuotationController extends ResourceController - public function proceedQuotation() + public function proceedQuotationOld() { try { $data = $this->request->getPost(); @@ -338,6 +338,154 @@ class QuotationController extends ResourceController } + public function proceedQuotation() + { + try { + $data = $this->request->getPost(); + $quotationId = $data['id'] ?? null; // For update + $policyId = $data['policy_id'] ?? null; // For policy update + $policyPdf = $this->request->getFile('policy_pdf_file_name'); + + /* ------------------------------------------------------ + * CASE 1: Update Policy (policy_id + file uploaded) + * ------------------------------------------------------ */ + if (!empty($policyId) && $policyPdf && $policyPdf->isValid()) { + + $uploadPath = WRITEPATH . 'uploads/policy/policy_pdf/'; + if (!is_dir($uploadPath)) { + mkdir($uploadPath, 0777, true); + } + + $pdfFileName = time() . '_' . $policyPdf->getRandomName(); + $policyPdf->move($uploadPath, $pdfFileName); + + $updateData = [ + 'policy_pdf_file_name' => $pdfFileName, + 'updated_by' => $data['created_by'], + ]; + + $this->PolicyModel->update($policyId, $updateData); + + } + + + /* ------------------------------------------------------ + * Get insurer_branch_id + * ------------------------------------------------------ */ + $query = $this->db->query( + "SELECT id FROM insurer_branch WHERE insurer_id = ? LIMIT 1", + [$data['insurer_id']] + ); + $insurerBranch = $query->getRowArray(); + $insurerBranchId = $insurerBranch['id'] ?? 0; + + + /* ------------------------------------------------------ + * Prepare common quotation data + * ------------------------------------------------------ */ + $quotationData = [ + 'enquiry_id' => $data['enquiry_id'], + 'insured_declared_value' => $data['insured_declared_value'], + 'premium_amount' => $data['premium_amount'], + 'insurance_plan_type_id' => $data['insurance_plan_type_id'], + 'insurer_id' => $data['insurer_id'], + 'insurer_branch_id' => $insurerBranchId, + 'manager_id' => $data['manager_id'], + 'payment_mode_id' => $data['payment_mode_id'] ?? 0, + ]; + + + /* ------------------------------------------------------ + * CASE 2: Update Quotation + * ------------------------------------------------------ */ + if (!empty($quotationId)) { + + $quotationData['updated_by'] = $data['created_by']; + + $this->QuotationModel->update($quotationId, $quotationData); + + return $this->respond([ + 'status' => 'success', + 'code' => 200, + 'msg' => 'Quotation updated successfully' + ], 200); + } + + + /* ------------------------------------------------------ + * CASE 3: Create New Quotation + * ------------------------------------------------------ */ + $quotationData['created_by'] = $data['created_by']; + $quotationData['status'] = 'Accepted'; + + $newQuotationId = $this->QuotationModel->insert($quotationData, true); + + + /* -------------------------------------------- + * Update Enquiry (Quotation Accepted) + * -------------------------------------------- */ + $this->EnquiryModel->update($data['enquiry_id'], [ + 'broker_id' => $data['broker_id'] ?? 0, + 'vehicle_type_id' => $data['vehicle_type_id'] ?? 0, + 'status' => 'Proposal Accepted' + ]); + + + /* ------------------------------------------------------ + * If file uploaded → Create Policy + * ------------------------------------------------------ */ + if ($policyPdf && $policyPdf->isValid()) { + + $uploadDir = WRITEPATH . 'uploads/policy/policy_pdf/'; + if (!is_dir($uploadDir)) { + mkdir($uploadDir, 0777, true); + } + + $pdfFileName = time() . '_' . $policyPdf->getRandomName(); + $policyPdf->move($uploadDir, $pdfFileName); + + // fetch enquiry + agent + $quot = $this->QuotationModel + ->select('partner_quotation.*,E.agent_id,E.name') + ->join('partner_enquiry E', 'E.id = partner_quotation.enquiry_id') + ->where('partner_quotation.id', $newQuotationId) + ->first(); + + $policyInsert = [ + 'enquiry_id' => $quot['enquiry_id'], + 'quotation_id' => $newQuotationId, + 'insured_name' => $quot['name'], + 'manager_id' => $quot['manager_id'], + 'agent_id' => $quot['agent_id'], + 'policy_pdf_file_name' => $pdfFileName, + 'created_by' => $data['created_by'], + ]; + + $newPolicyId = $this->PolicyModel->insert($policyInsert); + + // Update enquiry status → Policy Created + $this->EnquiryModel->update($quot['enquiry_id'], ['status' => 'Policy Created']); + } + + + return $this->respond([ + 'status' => 'success', + 'code' => 200, + 'msg' => 'Quotation created successfully' + ], 200); + + + } catch (\Exception $e) { + return $this->respond([ + 'status' => 'failed', + 'code' => 500, + 'error' => $e->getMessage() + ], 500); + } + } + + +