diff --git a/app/Controllers/EmpDataServiceController.php b/app/Controllers/EmpDataServiceController.php index 1c22f967..a2143b50 100755 --- a/app/Controllers/EmpDataServiceController.php +++ b/app/Controllers/EmpDataServiceController.php @@ -43,6 +43,7 @@ use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Reader\Exception as SpreadsheetReaderException; use PhpParser\Node\Expr\Cast\Double; use Kint\Kint; +use PhpParser\Node\Stmt\TraitUseAdaptation; use function PHPUnit\Framework\returnSelf; @@ -2179,18 +2180,18 @@ class EmpDataServiceController extends BaseController 'user_id' => $user_id, ]]); - // $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ - // 'client_policy_id' => $client_policy_id ?? null, - // 'endorsement_no' => $endorsement_id ?? null, - // 'emp_count' => $emp_count ?? null, - // 'action_type' => $file['event_type'] ?? null, - // 'no_of_insured' => count($no_of_insured ?? []) ?? null, - // 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, - // 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, - // 'gst' => $base_bremium_and_gst['gst'] ?? null, - // 'policy_issue_date' => $policy_issue_date ?? null, - // 'created_by' => $file['created_by'] ?? null, - // ]]); + $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ + 'client_policy_id' => $client_policy_id ?? null, + 'endorsement_no' => $endorsement_id ?? null, + 'emp_count' => $emp_count ?? null, + 'action_type' => $file['event_type'] ?? null, + 'no_of_insured' => count($no_of_insured ?? []) ?? null, + 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, + 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, + 'gst' => $base_bremium_and_gst['gst'] ?? null, + 'policy_issue_date' => $policy_issue_date ?? null, + 'created_by' => $file['created_by'] ?? null, + ]]); // $this->cashDepositCalculationForInception($depositeData); // $this->sendMailForDownloadingECard($emp_policy_ids); @@ -2620,14 +2621,14 @@ class EmpDataServiceController extends BaseController $file_data = $this->getDataByFileId($file_id, 'success'); $this->setPullNotification($file_data); - // $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ - // 'client_policy_id' => $client_policy_id ?? null, - // 'endorsement_no' => $endorsement_id[0] ?? null, - // 'emp_count' => $emp_count ?? null, - // 'action_type' => $file['event_type'] ?? null, - // 'policy_issue_date' => $policy_issue_date ?? null, - // 'created_by' => $file['created_by'] ?? null, - // ]]); + $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ + 'client_policy_id' => $client_policy_id ?? null, + 'endorsement_no' => $endorsement_id[0] ?? null, + 'emp_count' => $emp_count ?? null, + 'action_type' => $file['event_type'] ?? null, + 'policy_issue_date' => $policy_issue_date ?? null, + 'created_by' => $file['created_by'] ?? null, + ]]); //import file to upload Google Drive @@ -3354,18 +3355,18 @@ class EmpDataServiceController extends BaseController 'user_id' => $user_id, ]]); - // $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ - // 'client_policy_id' => $client_policy_id ?? null, - // 'endorsement_no' => $endorsement_id ?? null, - // 'emp_count' => $emp_count ?? null, - // 'action_type' => $file['event_type'] ?? null, - // 'no_of_insured' => count($no_of_insured ?? []) ?? null, - // 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, - // 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, - // 'gst' => $base_bremium_and_gst['gst'] ?? null, - // 'policy_issue_date' => $policy_issue_date ?? null, - // 'created_by' => $file['created_by'] ?? null, - // ]]); + $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ + 'client_policy_id' => $client_policy_id ?? null, + 'endorsement_no' => $endorsement_id ?? null, + 'emp_count' => $emp_count ?? null, + 'action_type' => $file['event_type'] ?? null, + 'no_of_insured' => count($no_of_insured ?? []) ?? null, + 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, + 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, + 'gst' => $base_bremium_and_gst['gst'] ?? null, + 'policy_issue_date' => $policy_issue_date ?? null, + 'created_by' => $file['created_by'] ?? null, + ]]); } @@ -3865,18 +3866,18 @@ class EmpDataServiceController extends BaseController 'user_id' => $user_id, ]]); - // $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ - // 'client_policy_id' => $client_policy_id ?? null, - // 'endorsement_no' => $endorsement_id ?? null, - // 'emp_count' => $emp_count ?? null, - // 'action_type' => $file['event_type'] ?? null, - // 'no_of_insured' => count($no_of_insured ?? []) ?? null, - // 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, - // 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, - // 'gst' => $base_bremium_and_gst['gst'] ?? null, - // 'policy_issue_date' => $policy_issue_date ?? null, - // 'created_by' => $file['created_by'] ?? null, - // ]]); + $r = Jobs::addJob(['job_name' => 'makeEntryForBDSPolicyTransaction', 'payload' => [ + 'client_policy_id' => $client_policy_id ?? null, + 'endorsement_no' => $endorsement_id ?? null, + 'emp_count' => $emp_count ?? null, + 'action_type' => $file['event_type'] ?? null, + 'no_of_insured' => count($no_of_insured ?? []) ?? null, + 'no_of_dependent' => count($no_of_dependent ?? []) ?? null, + 'base_premium' => $base_bremium_and_gst['base_premium'] ?? null, + 'gst' => $base_bremium_and_gst['gst'] ?? null, + 'policy_issue_date' => $policy_issue_date ?? null, + 'created_by' => $file['created_by'] ?? null, + ]]); } $file_data = $this->getDataByFileId($file_id, 'success'); @@ -5170,6 +5171,7 @@ class EmpDataServiceController extends BaseController public function makeEntryForBDSPolicyTransaction($params) { try { + $this->myLogger->logme("error", "makeEntryForBDSPolicyTransaction() started: " . json_encode(['params' => $params])); if (empty($params) || !isset($params['client_policy_id']) || empty($params['client_policy_id'])) { @@ -5182,29 +5184,31 @@ class EmpDataServiceController extends BaseController return ['status' => "Failed", 'message' => "Action type is missing or empty", 'data' => ['params' => $params]]; } + //get the policy data + $policy_data = $this->clientPolicyModel->where('id', $params['client_policy_id'])->where('is_active', 1)->first(); + $params['policy_no'] = $policy_data['policy_no'] ?? null; + + //check if the entries available in the policy transaction table + $check_policy_exist = $this->checkPolicyTransactionExist($params); + + if($check_policy_exist['status'] == true) { + return $check_policy_exist; + } + //Function call for if the old policy transaction entries exisit than active the Policy transaction entries $existing_policy_transaction_entry = $this->retrivePolicyTransactionEntries($params); - if($existing_policy_transaction_entry != false){ + if($existing_policy_transaction_entry != false) { return $existing_policy_transaction_entry; } - //get the policy data - $policy_data = $this->clientPolicyModel - ->where('id', $params['client_policy_id']) - ->where('is_active', 1) - ->first(); - if (empty($policy_data)) { $this->myLogger->logme("error", "No policy data found: " . json_encode(['client_policy_id' => $params['client_policy_id']])); return ['status' => "Failed", 'message' => "No policy data found for the given client policy ID", 'data' => ['params' => $params]]; } //get the leads data - $lead_data = $this->leadsModel - ->where('is_policy_created', $params['client_policy_id']) - ->where('is_active', 1) - ->first(); + $lead_data = $this->leadsModel->where('is_policy_created', $params['client_policy_id'])->where('is_active', 1)->first(); if (empty($lead_data)) { $this->myLogger->logme("error", "No lead data found: " . json_encode(['client_policy_id' => $params['client_policy_id']])); @@ -5227,14 +5231,13 @@ class EmpDataServiceController extends BaseController $this->myLogger->logme("error", "Policy transaction inserted successfully: " . json_encode(['insert_id' => $insert_id])); //do not remove this commented item - $coShareDetails = $this->ConstructPTShareData($policy_data, $insert_id, $params); + $coShareDetails = $this->ConstructPTShareData($policy_data, $insert_id, $params, $lead_data); $pt_co_share_id = $this->PTCOShareDetailsModel->insert($coShareDetails); - - // $this->myLogger->logme("error", "PT Co share data inserted successfully: " . json_encode(['pt_co_share_id' => $pt_co_share_id])); + $this->myLogger->logme("error", "PT Co share data inserted successfully: " . json_encode(['pt_co_share_id' => $pt_co_share_id])); // get the lead installment data - if(!empty($lead_data)){ + $lead_installment_data = $this->leadInstallmentPaymentDetailesModel ->select('lead_id, installment_amount, payment_date, utr_no') ->where('lead_id', $lead_data['id']) @@ -5292,7 +5295,7 @@ class EmpDataServiceController extends BaseController 'closure_date' => $policy_data['closure_date'] ?? null, 'emp_count' => $params['no_of_insured'] ?? null, 'dependent_count' => $params['no_of_dependent'] ?? null, - 'revenue_type' => isset($lead_data['lead_type']) ?($lead_data['lead_type'] == 1 ? "NA" : ($lead_data['lead_type'] == 2 ? "EA" : "EANR") ) : null, + 'revenue_type' => isset($lead_data['lead_type']) ?($lead_data['lead_type'] == 1 ? "NA" : ($lead_data['lead_type'] == 2 ? "EA" : "EANR") ) : "NA", 'co_share' => $policy_data['co_share'] ?? 0, 'pre_payable_by' => $policy_data['pre_payable_by'] ?? 1, 'renewal_date' => $policy_data['policy_end_date'] ?? null, @@ -5312,6 +5315,7 @@ class EmpDataServiceController extends BaseController 'tsi' => generate_tsi_code($lead_data['lead_type'] ?? 1) ?? null, 'installment' => $lead_data['no_of_installment'] ?? null, 'created_by' => $params['created_by'] ?? null, + 'entry_from' => 2, ]; $this->myLogger->logme("error", "Constructed policy transaction data: " . json_encode($policyTransactionData)); @@ -5319,7 +5323,7 @@ class EmpDataServiceController extends BaseController return $policyTransactionData; } - private function ConstructPTShareData($policy_data, $pt_id, $params) + private function ConstructPTShareData($policy_data, $pt_id, $params, $lead_data) { $policyTypeModel = new PolicyTypeModel(); $policy_type_data = $policyTypeModel->where('is_active', 1)->where('id', $policy_data['policy_type_id'])->first(); @@ -5339,7 +5343,11 @@ class EmpDataServiceController extends BaseController 'amount' => ($params['base_premium'] ?? 0) + ($params['gst'] ?? 0), ]; - $coShareData['exp_amt'] = (($params['base_premium'] ?? 0) * ($policy_type_data['ebp'] ?? 0)) / 100; + if(isset($lead_data['agreed_percentage']) && !empty($lead_data['agreed_percentage'])){ + $coShareData['exp_amt'] = (($params['base_premium'] ?? 0) * ($lead_data['agreed_percentage'] ?? 0)) / 100; + }else{ + $coShareData['exp_amt'] = (($params['base_premium'] ?? 0) * ($policy_type_data['ebp'] ?? 0)) / 100; + } $this->myLogger->logme("error", "Constructed PT co-share data: " . json_encode($coShareData)); @@ -5391,8 +5399,7 @@ class EmpDataServiceController extends BaseController $activated_pt_co_share_ids[] = $value['id']; } - $this->myLogger->logme("error", "Activated pt_co_share_ids : ". json_encode($activated_pt_co_share_ids)); - + $this->myLogger->logme("error", "Activated pt_co_share_ids : ". json_encode($activated_pt_co_share_ids)); $update_return = $this->policyTransactionModel->where('id', $policy_transaction_data['id'])->set(['is_active' => 1])->update(); @@ -5405,6 +5412,30 @@ class EmpDataServiceController extends BaseController } } + + public function checkPolicyTransactionExist($params) + { + // Trim the input + $policy_no = trim($params['policy_no']); + + // Base query + $policy_transaction_data = $this->policyTransactionModel->where('is_active', 1)->where('TRIM(policy_no)', $policy_no, false); + + // Add endorsement condition only if not inception + if ($params['action_type'] !== "inception") { + $policy_transaction_data = $policy_transaction_data ->where('TRIM(endorsement_no)', trim($params['endorsement_no']), false); + } + + $policy_transaction_data = $policy_transaction_data->first(); + + // If record exists → log and return true + if (!empty($policy_transaction_data)) { + $this->myLogger->logme("error", "Policy Transaction already exists. Input Parameters: " . json_encode($params) ); + return ['status' => true, 'message' => "This entry already available in the Policy Transaction table", 'data' => $policy_transaction_data['id']]; + } + + return ['status' => false, 'message' => "This entry not available in the Policy Transaction table", 'data' => '']; + } public function removeBDSPolicyTransactionEntryFromTruncate($params) { diff --git a/app/Models/PolicyTransactionModel.php b/app/Models/PolicyTransactionModel.php index 519b2848..1d752149 100644 --- a/app/Models/PolicyTransactionModel.php +++ b/app/Models/PolicyTransactionModel.php @@ -84,6 +84,7 @@ 'agent_id', 'agent_code', 'file_id', + 'entry_from', ];