diff --git a/.env.sample b/.env.sample index 49343c62..50244384 100755 --- a/.env.sample +++ b/.env.sample @@ -129,4 +129,7 @@ HEALTH_INDIA_TOKEN_URL = HEALTH_INDIA_USERNAME = HEALTH_INDIA_PASSWORD = -HEALTH_INDIA_PRIMARY_KEY_CONSTANT = \ No newline at end of file +HEALTH_INDIA_PRIMARY_KEY_CONSTANT = + +# BDS Daily Report Emails Configuration +bds.dailyReportEmails = diff --git a/app/Config/Routes.php b/app/Config/Routes.php index cf88f2b9..e07b1e01 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -451,6 +451,8 @@ $routes->group("/util", ["filter" => "authMVC"], function ($routes) { $routes->post("policy_tranction/sendInstallmentRemainderMail","PolicyTransactionController::sendInstallmentRemainderMail"); $routes->cli("cli/sendInstallmentRemainderMail","PolicyTransactionController::sendInstallmentRemainderMail"); +$routes->cli("cli/cronDailyBDSReport", "PolicyTransactionController::cronDailyBDSReport"); + $routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) { @@ -498,6 +500,7 @@ $routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) { //$routes->post('failedStatement',"PolicyTransactionController::failedStatementList"); }); + $routes->get("cronDailyBDSReport", "PolicyTransactionController::cronDailyBDSReport"); }); $routes->group("leads", ["filter" => "authMVC"], function ($routes) { diff --git a/app/Controllers/PolicyTransactionController.php b/app/Controllers/PolicyTransactionController.php index 59c115fe..3dfd21b6 100644 --- a/app/Controllers/PolicyTransactionController.php +++ b/app/Controllers/PolicyTransactionController.php @@ -6098,8 +6098,188 @@ class PolicyTransactionController extends BaseController return array('status' => false, 'message' => 'file_id is required'); } - $this->policyTransactionModel->select()->findAll(); + $data['is_active'] = 0; + $this->policyTransactionModel->where('file_id', $file_id)->set($data)->update(); + $this->PTCOShareDetailsModel->where('file_id', $file_id)->set($data)->update(); + return $this->respond(['status' => true, 'code' => 200, 'message' => 'BDS bulk upload data truncated successfully'], 200); + } + + // ------------------------------------------------------------------------------------------------------------------ + + /** + * Cron job: Daily BDS Report + * Fetches today's BDS entries, generates Excel (matching report_bds.php column order), and emails to recipients from .env. + * Recipients: comma-separated emails in bds.reportEmails + * File stored temporarily in writable/tmp/ and deleted after sending. + */ + public function cronDailyBDSReport() + { + + helper('excel_import_export_helper'); + + $filePath = null; + try { + + $today = date('Y-m-d'); + $reportList = $this->policyTransactionModel->getBDSReportList($today, $today,0,0,0,'created_at',0,0,0,0,0,[]); + + if (empty($reportList)) { + $this->myLogger->logme('error', "cronDailyBDSReport: No BDS records for {$today}"); + return $this->respond(['status' => 'success', 'message' => 'No BDS records for today. No report sent.'], 200); + } + + // Column headers exactly matching report_bds.php order (including display:none columns) + $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' + ]; + } + + $fileName = 'BDS_Daily_Report_' . $today . '.xlsx'; + $tmpDir = WRITEPATH . 'tmp' . DIRECTORY_SEPARATOR; + if (!is_dir($tmpDir)) { + mkdir($tmpDir, 0755, true); + } + $filePath = $tmpDir . $fileName; + + $generated = generate_excel($headers, $excelData, $filePath); + if (!$generated) { + $this->myLogger->logme('error', 'cronDailyBDSReport: Excel generation failed'); + return $this->respond(['status' => false, 'message' => 'Excel generation failed'], 500); + } + + $emailList = getenv('bds.dailyReportEmails') ?: ''; + $recipientEmails = array_filter(array_map('trim', explode(',', $emailList))); + if (empty($recipientEmails)) { + @unlink($filePath); + $this->myLogger->logme('error', 'cronDailyBDSReport: No recipients in bds.dailyReportEmails. Report generated but not sent.'); + return $this->respond([ + 'status' => 'success', + 'message' => 'Report generated. No recipients configured (set bds.dailyReportEmails in .env).', + 'file' => $fileName + ], 200); + } + + $subject = "Daily BDS Report - {$today}"; + $message = "

Please find attached the daily BDS report for {$today}.

"; + $message .= "

Total records: " . count($reportList) . "

"; + + $attachments = [ + ['filePath' => $filePath, 'fileName' => $fileName] + ]; + + $res = MailHelper::send_email([ + 'mail' => $recipientEmails, + 'subject' => $subject, + 'message' => $message, + 'attachments' => $attachments + ]); + + @unlink($filePath); + + $resDecoded = is_string($res) ? json_decode($res, true) : $res; + if (isset($resDecoded['status']) && $resDecoded['status'] === 'success') { + $this->myLogger->logme('error', "cronDailyBDSReport: Report sent to " . count($recipientEmails) . " recipients"); + return $this->respond([ + 'status' => true, + 'message' => 'Report generated and emailed successfully.', + 'recipients' => count($recipientEmails) + ], 200); + } + + $this->myLogger->logme('error', 'cronDailyBDSReport: Email send failed - ' . json_encode($resDecoded)); + return $this->respond(['status' => false, 'message' => 'Report generated but email send failed'], 500); + } catch (Exception $e) { + if ($filePath && is_file($filePath)) { + @unlink($filePath); + } + $this->myLogger->logme('error', 'cronDailyBDSReport Exception: ' . $e->getMessage()); + return $this->respond(['status' => false, 'message' => $e->getMessage()], 500); + } } } diff --git a/app/Libraries/MyGoogleDrive.php b/app/Libraries/MyGoogleDrive.php index 566098eb..2fea0f9e 100644 --- a/app/Libraries/MyGoogleDrive.php +++ b/app/Libraries/MyGoogleDrive.php @@ -16,7 +16,7 @@ class MyGoogleDrive { $this->myLogger = \Config\Services::mylogger(); $this->client = new Google_Client(); - $this->client->setAuthConfig(ROOTPATH . 'nhance-app-google-drive.json'); // App credentials + $this->client->setAuthConfig(ROOTPATH . 'nhance-ee8d1-e3c5269b1ec7.json'); // App credentials // putenv('GOOGLE_APPLICATION_CREDENTIALS=' . ROOTPATH . 'nhance-app-google-drive.json'); $this->client->useApplicationDefaultCredentials();