diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 5058dea6..97a24a24 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -356,8 +356,8 @@ 'data_type' => '', 'format' => null, 'allowed_values' => null, - 'custom' => null, - 'params' => null + 'custom' => 'check_gst_percentage', + 'params' => ['row'] ], 'cgst' => [ @@ -368,8 +368,8 @@ 'data_type' => '', 'format' => null, 'allowed_values' => null, - 'custom' => null, - 'params' => null + 'custom' => 'check_gst_percentage', + 'params' => ['row'] ], 'sgst' => [ @@ -380,8 +380,9 @@ 'data_type' => '', 'format' => null, 'allowed_values' => null, - 'custom' => null, - 'params' => null + 'custom' => 'check_gst_percentage', + 'params' => ['row'] + ], 'stamp_duty' => [ @@ -4152,6 +4153,26 @@ $expected_amount = trim($row[33]); $rewards = trim($row[34]); + $calculation = calculateMotorPolicyAmounts([ + 'base_premium' => trim($row[16]), + 'non_commission_premium_amount' => trim($row[17]), + 'tp_premium' => trim($row[18]), + 'igst' => trim($row[19]), + 'cgst' => trim($row[20]), + 'sgst' => trim($row[21]), + 'stamp_duty' => trim($row[22]), + 'agreed_amount' => trim($row[24]), + 'agreed_bp_percentage' => trim($row[25]), + 'agreed_tp_percentage' => trim($row[26]), + 'actual_bp_brokerage_amount' => trim($row[31]), + 'actual_tp_brokerage_amount' => trim($row[32]), + 'standard_bp_percentage' => 15.00, + 'standard_tp_percentage' => 2.5 + ]); + + log_message('error', 'Motor Policy Calculation: ' . json_encode($calculation)); + + $current_insurer_data = check_insurer_exist($row, $insurer_data)['insurer'] ?? null; $current_insurer_branch_data = check_insurer_branch_exist($row, $insurer_branch_data, $current_insurer_data)['branch'] ?? null; @@ -4212,7 +4233,7 @@ 'co_share_per' => 100, 'bp_amt' => $base_premium, - 'bp_gst_amt' => $gst_amt, + 'bp_gst_amt' => $calculation['gst_amount'] ?? $gst_amt, 'bp_igst' => $igst , 'bp_sgst' => $sgst, 'bp_cgst' => $cgst, @@ -4254,14 +4275,14 @@ 'variance' => 0, 'remark' => null, - 'amount' => $total, + 'amount' => $calculation['total_amount'] ?? $total, 'stamp_duty' => $stamp_duty, 'cop_amt' => $base_premium, 'cotp_amt' => $tp_premium, 'cotep_amt' => 0.00, - 'exp_amt' => $expected_amount, + 'exp_amt' => $calculation['expected_amount'] ?? $expected_amount, 'statement_id' => null, 'follower_policy_no' => null, diff --git a/app/Helpers/excel_util_helper.php b/app/Helpers/excel_util_helper.php index b5334313..28ec3083 100755 --- a/app/Helpers/excel_util_helper.php +++ b/app/Helpers/excel_util_helper.php @@ -3329,6 +3329,149 @@ if (!function_exists('calculate_gst_amount')) { } } +if (!function_exists('check_gst_percentage')) { + + function check_gst_percentage($row) + { + $igst = (float)trim($row[19]); + $cgst = (float)trim($row[20]); + $sgst = (float)trim($row[21]); + + // CASE 1: IGST is entered → CGST & SGST must be zero + if ($igst > 0) { + + if ($cgst > 0 || $sgst > 0) { + return [ + 'status' => false, + 'error' => "Invalid GST configuration: When IGST is entered, CGST and SGST must be 0." + ]; + } + + return [ + 'status' => true, + 'error' => null, + 'gst_type' => "IGST" + ]; + } + + // CASE 2: IGST = 0 → CGST and SGST must be entered together + if ($cgst > 0 || $sgst > 0) { + + if ($cgst == 0 || $sgst == 0) { + return [ + 'status' => false, + 'error' => "Invalid GST configuration: CGST and SGST must both be entered when IGST is 0." + ]; + } + + return [ + 'status' => true, + 'error' => null, + 'gst_type' => "CGST+SGST" + ]; + } + + // CASE 3: No GST provided at all + return [ + 'status' => false, + 'error' => "GST values missing: Enter either IGST or both CGST and SGST." + ]; + } +} + +if (!function_exists('calculateMotorPolicyAmounts')) { + + function calculateMotorPolicyAmounts(array $data) + { + try { + + // Parse incoming values (fallback to 0) + $bp = floatval($data['base_premium'] ?? 0); + $ncpa = floatval($data['non_commission_premium_amount'] ?? 0); + $tp = floatval($data['tp_premium'] ?? 0); + $igst = floatval($data['igst'] ?? 0); + $cgst = floatval($data['cgst'] ?? 0); + $sgst = floatval($data['sgst'] ?? 0); + $stamp = floatval($data['stamp_duty'] ?? 0); + + $agreed_amount = floatval($data['agreed_amount'] ?? 0); + + $ag_bp_per = floatval($data['agreed_bp_percentage'] ?? 0); + $ag_tp_per = floatval($data['agreed_tp_percentage'] ?? 0); + + $std_bp_per = floatval($data['standard_bp_percentage'] ?? 0); + $std_tp_per = floatval($data['standard_tp_percentage'] ?? 0); + + $actual_bp_amt = floatval($data['actual_bp_brokerage_amount'] ?? 0); + $actual_tp_amt = floatval($data['actual_tp_brokerage_amount'] ?? 0); + + // Determine GST % + $gst_per = ($igst > 0) ? $igst : ($cgst + $sgst); + + // Motor Policy Premium Logic + $tpTotal = $bp + $tp; + + // GST without NCPA + $gst_per_amt = ($tpTotal * $gst_per) / 100; + + // GST with NCPA + $ncpaTotal = $tpTotal + $ncpa; + $ncpa_gst_per_amt = ($ncpaTotal * $gst_per) / 100; + + // Choose correct GST amount + $gst_amount = ($ncpa != 0) ? $ncpa_gst_per_amt : $gst_per_amt; + + // Total amount payable + $total_amount = $tpTotal + $gst_amount + $stamp; + + // Expected Amount + $sum_agreed = $ag_bp_per + $ag_tp_per; + $expected_amount = 0; + + if ($agreed_amount > 0) { + + $expected_amount = $agreed_amount; // Formula 1 + + } elseif ($sum_agreed > 0) { + + // Formula 2 – Agreed % + $expected_amount = + (($bp * $ag_bp_per) / 100) + + (($tp * $ag_tp_per) / 100); + + } else { + + // Formula 3 – Standard % + $expected_amount = + (($bp * $std_bp_per) / 100) + + (($tp * $std_tp_per) / 100); + } + + // Variance + $variance = + ($actual_bp_amt + $actual_tp_amt) - $expected_amount; + + // Final return array + return [ + 'tp_total' => round($tpTotal, 2), + 'gst_per' => round($gst_per, 2), + 'gst_amount' => round($gst_amount, 2), + 'total_amount' => round($total_amount, 2), + 'expected_amount' => round($expected_amount, 2), + 'variance' => round($variance, 2), + 'actual_bp_amount'=> $actual_bp_amt, + 'actual_tp_amount'=> $actual_tp_amt, + ]; + + } catch (\Throwable $e) { + + log_message('error', 'Motor Policy Calculation Error: ' . $e->getMessage()); + return []; + } + } +} + +