279 lines
12 KiB
PHP
279 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Tests\unit;
|
|
|
|
use CodeIgniter\Test\CIUnitTestCase;
|
|
use App\Controllers\PolicyTransactionController;
|
|
use App\Models\PolicyTransactionModel;
|
|
|
|
class PolicyTransactionControllerTest extends CIUnitTestCase
|
|
{
|
|
/**
|
|
* Helper to build a fully initialised controller instance.
|
|
*/
|
|
protected function makeController(): PolicyTransactionController
|
|
{
|
|
$controller = new PolicyTransactionController();
|
|
|
|
$request = \Config\Services::request();
|
|
$response = \Config\Services::response();
|
|
$logger = \Config\Services::logger();
|
|
|
|
$controller->initController($request, $response, $logger);
|
|
|
|
return $controller;
|
|
}
|
|
|
|
/**
|
|
* Helper to inject a stubbed PolicyTransactionModel into the controller.
|
|
*
|
|
* @param PolicyTransactionController $controller
|
|
* @param array $stubData
|
|
*/
|
|
protected function injectBdsStubModel(PolicyTransactionController $controller, array $stubData): void
|
|
{
|
|
$stubModel = new class($stubData)
|
|
{
|
|
private array $data;
|
|
|
|
public function __construct(array $data)
|
|
{
|
|
$this->data = $data;
|
|
}
|
|
|
|
public function getBDSReportList(
|
|
$start_date = 0,
|
|
$end_date = 0,
|
|
$client_id = 0,
|
|
$insurer_id = 0,
|
|
$policy_type_id = 0,
|
|
$date_type = 0,
|
|
$issuer = 0,
|
|
$client_branch_id = 0,
|
|
$insurer_branch_id = 0,
|
|
$client_policy_id = 0,
|
|
$user_id = 0,
|
|
$where = [],
|
|
) {
|
|
return $this->data;
|
|
}
|
|
};
|
|
|
|
$refClass = new \ReflectionClass($controller);
|
|
$prop = $refClass->getProperty('policyTransactionModel');
|
|
$prop->setAccessible(true);
|
|
$prop->setValue($controller, $stubModel);
|
|
}
|
|
|
|
public function testCronDailyBDSReportReturnsUnauthorizedWithoutKey(): void
|
|
{
|
|
putenv('cron.secretKey=unit-test-secret');
|
|
|
|
$_GET = [];
|
|
|
|
$controller = $this->makeController();
|
|
|
|
$response = $controller->cronDailyBDSReport();
|
|
|
|
$this->assertSame(401, $response->getStatusCode());
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertIsArray($body);
|
|
$this->assertSame('error', $body['status'] ?? null);
|
|
}
|
|
|
|
public function testCronDailyBDSReportNoRecordsReturnsSuccessMessage(): void
|
|
{
|
|
putenv('cron.secretKey=unit-test-secret');
|
|
putenv('bds.reportEmails='); // no recipients for this test
|
|
|
|
$_GET['key'] = 'unit-test-secret';
|
|
|
|
$controller = $this->makeController();
|
|
|
|
$this->injectBdsStubModel($controller, []);
|
|
|
|
$response = $controller->cronDailyBDSReport();
|
|
|
|
$this->assertSame(200, $response->getStatusCode());
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
$this->assertIsArray($body);
|
|
$this->assertSame('success', $body['status'] ?? null);
|
|
$this->assertStringContainsString(
|
|
'No BDS records for today',
|
|
$body['message'] ?? ''
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Integration-style test: generate an Excel file for today's BDS data
|
|
* and store it in the user's Downloads folder as BDS_DAILY_REPORT_TEST_CASE.xlsx.
|
|
*
|
|
* This reuses the same transformation logic as cronDailyBDSReport.
|
|
*/
|
|
public function testGenerateDailyBdsReportExcelToDownloads(): void
|
|
{
|
|
helper(['excel_import_export_helper', 'utility_helper']);
|
|
|
|
// Sample BDS row to generate a test Excel (no DB dependency)
|
|
$today = date('Y-m-d');
|
|
$reportList = [
|
|
[
|
|
'user_name' => 'Test User',
|
|
'policy_issue_month' => 'Mar 2026',
|
|
'revenue_type' => 'Fresh',
|
|
'client_type' => 'Group',
|
|
'client_name' => 'Test Client',
|
|
'action_type' => 'Policy',
|
|
'policy_type' => 'Health',
|
|
'bap' => 'BAP-GRP',
|
|
'vehicle_no' => 'TN01AB1234',
|
|
'policy_no' => 'P-TEST-001',
|
|
'endorsement_no' => 'E-TEST-001',
|
|
'insurer_branch_name' => 'Chennai Branch',
|
|
'endorse_eff_date' => $today,
|
|
'policy_start_date' => $today,
|
|
'policy_end_date' => $today,
|
|
'ref' => 'REF-001',
|
|
'remarks' => 'Test remark',
|
|
'bp_amt' => 1000,
|
|
'tp_or_ter' => 500,
|
|
'premium_wo_gst' => 1500,
|
|
'total_premium' => 1770,
|
|
'agreed_bp_per' => 10,
|
|
'agreed_tp_or_ter_per' => 5,
|
|
'reward' => 100,
|
|
'total_irda_amt' => 800,
|
|
'billed_amt' => 300,
|
|
'salse_person_name' => 'Sales Person',
|
|
'service_person_name' => 'Service Person',
|
|
'nhance_branch' => 'Nhance Chennai',
|
|
'installment' => '1',
|
|
'data_received_date' => $today,
|
|
'renewal_date' => $today,
|
|
'co_share' => 'No',
|
|
'bro_payable_by' => 'No',
|
|
'salse_manager_name' => 'Sales Manager',
|
|
'service_manager_name' => 'Service Manager',
|
|
'service_branch' => 'Service Branch',
|
|
'rollover_date' => $today,
|
|
'policy_holder_name' => 'Holder Name',
|
|
'same_as_proposer' => 'Yes',
|
|
'follower_policy_no' => 'F-TEST-001',
|
|
'co_share_per' => 0,
|
|
'non_comm_per_amt' => 0,
|
|
'bp_cgst' => 0,
|
|
'bp_sgst' => 0,
|
|
'bp_igst' => 0,
|
|
'stamp_duty' => 0,
|
|
'standerd_bp_per' => 0,
|
|
'standerd_tp_per' => 0,
|
|
'actual_bp_amt' => 0,
|
|
'actual_tp_amt' => 0,
|
|
'actual_bp_per' => 0,
|
|
'actual_tp_per' => 0,
|
|
'actual_tep_brokerage_amt' => 0,
|
|
'actual_tp_brokerage_amt' => 0,
|
|
'cd_ac_no' => 'CD-001',
|
|
],
|
|
];
|
|
|
|
$headers = [
|
|
'S. No', 'User', 'Month', 'Business Type', 'Client Type', 'Insured Name', 'Transaction Type',
|
|
'Policy Type', 'BAP Group', 'Vehicle Number', 'Policy No', 'Endorsement No', 'Insurer Branch',
|
|
'Endorsement Effective Date', 'Policy Effective Date', 'Policy Expiry Date', 'Reference', 'Remarks',
|
|
'BP Premium', 'TP Premium', 'Premium (without GST)', 'Total Premium', 'Agreed BP %', 'Agreed TP %',
|
|
'Rewards', 'Agreed Amount', 'Invoiced Amount', 'Outstanding Amount',
|
|
'Salse Person', 'Service Person', 'Salse Person Branch', 'Installment', 'Data Received Date', 'Renewal Date',
|
|
'Co-Premium', 'Remuneration Pay By Leader', 'Salse Person Manager', 'Service Person Manager',
|
|
'Service Person Branch', 'Rollover Date', 'Policyholder Name', 'Insured (Same as Proposer)',
|
|
'Follower Policy No', 'Co-Share %', 'Non Commissional Premium Amount', 'CGST', 'SGST', 'IGST',
|
|
'Stamp Duty', 'Standard BP %', 'Standard TP %', 'Actual BP Amount', 'Actual TP Amount',
|
|
'Actual BP %', 'Actual TP %', 'Actual BP Remuneration Amount', 'Actual TP Remuneration Amount',
|
|
'CD Account No'
|
|
];
|
|
|
|
$excelData = [];
|
|
foreach ($reportList as $idx => $row) {
|
|
$totalIrda = (float)($row['total_irda_amt'] ?? 0);
|
|
$billedAmt = (float)($row['billed_amt'] ?? 0);
|
|
$unbilledAmt = $totalIrda - $billedAmt;
|
|
if ($totalIrda == 0) {
|
|
$unbilledAmt = abs($unbilledAmt);
|
|
}
|
|
$unbilledAmt = ($unbilledAmt == 0 && $billedAmt == 0) ? $totalIrda : $unbilledAmt;
|
|
|
|
$hasIrda = ($totalIrda != 0);
|
|
|
|
$excelData[] = [
|
|
$idx + 1,
|
|
$row['user_name'] ?? 'N/A',
|
|
$row['policy_issue_month'] ?? 'N/A',
|
|
$row['revenue_type'] ?? 'N/A',
|
|
$row['client_type'] ?? 'N/A',
|
|
$row['client_name'] ?? 'N/A',
|
|
$row['action_type'] ?? 'N/A',
|
|
$row['policy_type'] ?? 'N/A',
|
|
$row['bap'] ?? 'N/A',
|
|
$row['vehicle_no'] ?? 'N/A',
|
|
$row['policy_no'] ?? 'N/A',
|
|
$row['endorsement_no'] ?? 'N/A',
|
|
$row['insurer_branch_name'] ?? 'N/A',
|
|
!empty($row['endorse_eff_date']) ? change_date_format($row['endorse_eff_date'], 'Y-m-d', 'd/m/Y') : 'N/A',
|
|
!empty($row['policy_start_date']) ? change_date_format($row['policy_start_date'], 'Y-m-d', 'd/m/Y') : 'N/A',
|
|
!empty($row['policy_end_date']) ? change_date_format($row['policy_end_date'], 'Y-m-d', 'd/m/Y') : 'N/A',
|
|
$row['ref'] ?? 'N/A',
|
|
$row['remarks'] ?? 'N/A',
|
|
$hasIrda ? ($row['bp_amt'] ?? '0.00') : '0.00',
|
|
$hasIrda ? ($row['tp_or_ter'] ?? '0.00') : '0.00',
|
|
$hasIrda ? ($row['premium_wo_gst'] ?? '0.00') : '0.00',
|
|
$hasIrda ? ($row['total_premium'] ?? '0.00') : '0.00',
|
|
$hasIrda ? ($row['agreed_bp_per'] ?? '0.00') . '%' : '0.00%',
|
|
$hasIrda ? ($row['agreed_tp_or_ter_per'] ?? '0.00') . '%' : '0.00%',
|
|
$row['reward'] ?? '0.00',
|
|
$row['total_irda_amt'] ?? '0.00',
|
|
!empty($row['billed_amt']) ? $row['billed_amt'] : '0.00',
|
|
number_format((float)$unbilledAmt, 2, '.', ''),
|
|
$row['salse_person_name'] ?? 'N/A',
|
|
$row['service_person_name'] ?? 'N/A',
|
|
$row['nhance_branch'] ?? 'N/A',
|
|
$row['installment'] ?? 'N/A',
|
|
!empty($row['data_received_date']) ? change_date_format($row['data_received_date'], 'Y-m-d', 'd/m/Y') : 'N/A',
|
|
!empty($row['renewal_date']) ? change_date_format($row['renewal_date'], 'Y-m-d', 'd/m/Y') : 'N/A',
|
|
$row['co_share'] ?? 'No',
|
|
$row['bro_payable_by'] ?? 'No',
|
|
$row['salse_manager_name'] ?? 'N/A',
|
|
$row['service_manager_name'] ?? 'N/A',
|
|
$row['service_branch'] ?? 'N/A',
|
|
!empty($row['rollover_date']) ? change_date_format($row['rollover_date'], 'Y-m-d', 'd/m/Y') : 'N/A',
|
|
$row['policy_holder_name'] ?? 'N/A',
|
|
$row['same_as_proposer'] ?? 'No',
|
|
$row['follower_policy_no'] ?? 'N/A',
|
|
number_format((float)($row['co_share_per'] ?? 0), 2),
|
|
number_format((float)($row['non_comm_per_amt'] ?? 0), 2),
|
|
number_format((float)($row['bp_cgst'] ?? 0), 2),
|
|
number_format((float)($row['bp_sgst'] ?? 0), 2),
|
|
number_format((float)($row['bp_igst'] ?? 0), 2),
|
|
number_format((float)($row['stamp_duty'] ?? 0), 2),
|
|
number_format((float)($row['standerd_bp_per'] ?? 0), 2),
|
|
number_format((float)($row['standerd_tp_per'] ?? 0), 2),
|
|
number_format((float)($row['actual_bp_amt'] ?? 0), 2),
|
|
number_format((float)($row['actual_tp_amt'] ?? 0), 2),
|
|
number_format((float)($row['actual_bp_per'] ?? 0), 2),
|
|
number_format((float)($row['actual_tp_per'] ?? 0), 2),
|
|
number_format((float)($row['actual_tep_brokerage_amt'] ?? 0), 2),
|
|
number_format((float)($row['actual_tp_brokerage_amt'] ?? 0), 2),
|
|
$row['cd_ac_no'] ?? 'N/A'
|
|
];
|
|
}
|
|
|
|
$filePath = '/home/venkat/Downloads/BDS_DAILY_REPORT_TEST_CASE.xlsx';
|
|
|
|
$generated = generate_excel($headers, $excelData, $filePath);
|
|
|
|
$this->assertTrue($generated, 'Failed to generate BDS_DAILY_REPORT_TEST_CASE.xlsx in Downloads');
|
|
}
|
|
}
|
|
|