diff --git a/app/Controllers/EmpDataServiceController.php b/app/Controllers/EmpDataServiceController.php index 3cb36091..96537b76 100644 --- a/app/Controllers/EmpDataServiceController.php +++ b/app/Controllers/EmpDataServiceController.php @@ -140,15 +140,30 @@ class EmpDataServiceController extends BaseController // Fetch employee data for export from the database $objects = $this->employeePolicyModel->getInceptionEmployeeDataForExportExcel($export_data); - $insurer_id = $this->clientPolicyModel->where('id', $export_data['client_policy_id'])->first(); - $cash_balance = $this->clientDepositModel->where('client_id', $export_data['client_id'])->where('insurer_id', $insurer_id['insurer_id'])->orderBy('id', 'DESC')->first(); + $policy_details = $this->clientPolicyModel->where('id', $export_data['client_policy_id'])->first(); - if($cash_balance == null){ + // dd($export_data, $policy_details['cd_ac_no']); + + if ($policy_details['cd_ac_no'] == null) { + // dd("The policy does not have a CD account number"); + $this->myLogger->logme('error', 'The policy does not have a CD account number.'); + return 5; + } + + $cash_balance = $this->clientDepositModel->where('cd_ac_no', $policy_details['cd_ac_no'])->orderBy('id', 'DESC')->first(); + + if ($cash_balance == null) { + + $balance = $this->CDMasterModel + ->where('client_id', $export_data['client_id']) + ->where('insurer_id', $policy_details['insurer_id']) + ->where('cd_ac_no', $policy_details['cd_ac_no']) + ->first(); - $balance = $this->CDMasterModel->where('client_id', $export_data['client_id'])->where('insurer_id', $insurer_id['insurer_id'])->orderBy('id', 'DESC')->first(); $cash_balance['balance'] = $balance['opening_bal']; } + // dd($cash_balance); // Calculate the total amount from the objects $totals = array_reduce($objects, function ($carry, $item) { @@ -158,9 +173,8 @@ class EmpDataServiceController extends BaseController $totals = round($totals, 2); if (!empty($cash_balance)) { - + if ((int) $cash_balance['balance'] < (int) $totals) { - $this->myLogger->logme('error', 'Inception export failed due to insufficient deposit amount.'); $this->myLogger->logme('error', 'Inception export failed due to insufficient deposit amount. CASH BALANCE : {balance} and TOTAL AMOUNT : {total}', ['balance' => $cash_balance['balance'], 'total' => $totals]); return 0; @@ -363,6 +377,26 @@ class EmpDataServiceController extends BaseController $ids = []; $objects = $this->employeePolicyModel->getSIEnhancementEmployeesDataForExportExcel($export_data); + $policy_details = $this->clientPolicyModel->where('id', $export_data['client_policy_id'])->first(); + + if ($policy_details['cd_ac_no'] == null) { + $this->myLogger->logme('error', 'The policy does not have a CD account number.'); + return 1; + } + + $cash_balance = $this->clientDepositModel->where('cd_ac_no', $policy_details['cd_ac_no'])->orderBy('id', 'DESC')->first(); + + if ($cash_balance == null) { + + $balance = $this->CDMasterModel + ->where('client_id', $export_data['client_id']) + ->where('insurer_id', $policy_details['insurer_id']) + ->where('cd_ac_no', $policy_details['cd_ac_no']) + ->first(); + + $cash_balance['balance'] = $balance['opening_bal']; + } + $totals = 0; foreach ($objects as $obj) { @@ -374,6 +408,14 @@ class EmpDataServiceController extends BaseController // echo '
';
         // print_r($ids); die;
 
+        if (!empty($cash_balance)) {
+            if ((int) $cash_balance['balance']  < (int) $rounded_totals) {
+                $this->myLogger->logme('error', 'Inception export failed due to insufficient deposit amount.');
+                $this->myLogger->logme('error', 'Inception export failed due to insufficient deposit amount.  CASH BALANCE : {balance}  and  TOTAL AMOUNT : {total}', ['balance' => $cash_balance['balance'], 'total' => $totals]);
+                return 0;
+            }
+        }
+
         $count = count($objects);
         $export_data['count'] = $count;
         $export_data['amount'] = $rounded_totals;
@@ -545,7 +587,7 @@ class EmpDataServiceController extends BaseController
     }
 
 
-
+    //not in use
     public function generateExcelForAdditionAndDependentAddition($export_data)
     {
         $ids = [];
@@ -628,7 +670,7 @@ class EmpDataServiceController extends BaseController
             }
         }
     }
-
+    //end not in use
 
     /**
      * The below functions are Imports data from an Excel file for :
@@ -2788,7 +2830,7 @@ class EmpDataServiceController extends BaseController
 
     //Endorsement Addition and Dependent Addition
 
-
+    //not in use
     public function AdditionAndDependentAddition($params)
     {
 
@@ -3179,6 +3221,7 @@ class EmpDataServiceController extends BaseController
 
     }
 
+    //end not in use
 
     /**
      * The below functions are Calculates and records cash deposits for employee policies at inception.
diff --git a/app/Controllers/EmployeeController.php b/app/Controllers/EmployeeController.php
index c2447397..ae32b9a4 100644
--- a/app/Controllers/EmployeeController.php
+++ b/app/Controllers/EmployeeController.php
@@ -411,10 +411,15 @@ class EmployeeController extends AdminController
 
                 $return = $empDataServiceController->generateExcelForAdditionandInception($batch_data);
 
-                if ($return == 0) {
+                if ($return === 0) {
 
                     session()->setFlashdata('error', "Insufficient deposit amount.");
                     return redirect()->to(base_url('employee/upload'));
+
+                }else if($return === 5){
+
+                    session()->setFlashdata('error', "The policy does not have a CD account number.");
+                    return redirect()->to(base_url('employee/upload'));
                 }
 
                 if (!$return) {
@@ -442,10 +447,14 @@ class EmployeeController extends AdminController
 
                 $return = $empDataServiceController->generateExcelForSIEnhancement($batch_data);
 
-                if ($return == 0) {
+                if ($return === 0) {
 
                     session()->setFlashdata('error', "Insufficient deposit amount.");
                     return redirect()->to(base_url('employee/upload'));
+                }else if($return === 5){
+
+                    session()->setFlashdata('error', "The policy does not have a CD account number.");
+                    return redirect()->to(base_url('employee/upload'));
                 }
 
                 if (!$return) {
@@ -1059,33 +1068,34 @@ class EmployeeController extends AdminController
     public function viewECard()
     {
 
-        $this->loadLayout('ecard_template/default_ecard');
+        // $this->loadLayout('ecard_template/default_ecard');
         
-        // Load the session library if it's not autoloaded
-        // $session = \Config\Services::session();
-        
-        // Access session data
-        $sessionData = session()->get();
-        
-        // Check if session data exists and if expiration time is set
-        if (!empty($sessionData) && isset($sessionData['isLoggedIn']) && isset($sessionData['session_expiration'])) {
-            // Get the session expiration timestamp
-            $expirationTimestamp = $sessionData['session_expiration'];
-        
-            // Get the current timestamp
-            $currentTimestamp = time();
-        
-            // Check if the current time is greater than the expiration time
-            if ($currentTimestamp > $expirationTimestamp) {
-                // Session has expired
-                echo "Session has expired";
-            } else {
-                // Session is active
-                echo "Session is active";
-            }
-        } else {
-            // Session data is not set or session is not started
-            echo "Session is not started or data is not set";
+        $empDataServiceController = new EmpDataServiceController();
+        $file_name = generate_filename("TCS", 'inception', 'export', 'insurer', 'policy', 'branch');
+
+        $batch_data = [
+            'client_id' => 12,
+            'client_policy_id' => 12,
+            'client_branch_id' => 1,
+            'insurer_or_tpa' => 'insurer',
+            'event_type' => 'inception',
+            'actions' => 'export',
+            'file_name' => $file_name,
+        ];
+
+        $return = $empDataServiceController->generateExcelForAdditionandInception($batch_data);
+
+        if ($return == 0) {
+
+           dd('error', "Insufficient deposit amount.");
+
+        }else if($return == 1){
+
+            dd('error', "The policy does not have a CD account number.");
+        }
+
+        if (!$return) {
+            dd('error', "No data was found");
         }
         
     }
diff --git a/app/Helpers/DepositHelper.php b/app/Helpers/DepositHelper.php
index 7d3c713d..f6d14a7c 100644
--- a/app/Helpers/DepositHelper.php
+++ b/app/Helpers/DepositHelper.php
@@ -33,7 +33,7 @@ class DepositHelper
     public static function saveDeposit(array $data, int $loggedInUserID): array
     {
         // Retrieve the last known balance
-        $lastBalance = self::calculateLastBalance($data['client_id'], $data['insurer_id']);
+        $lastBalance = self::calculateLastBalance($data['client_id'], $data['insurer_id'], $data['cd_ac_no']);
 
         // Calculate the new balance based on the transaction type
         $newBalance = self::calculateBalance(
@@ -87,24 +87,26 @@ class DepositHelper
      *
      * @return float The last known balance.
      */
-    public static function calculateLastBalance(int $clientId, int $insurerId): float
+    public static function calculateLastBalance(int $clientId, int $insurerId, $cd_ac_no): float
     {
         $model = new ClientDepositModel();
 
-        $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE client_id = ? AND insurer_id = ? ORDER BY created_at DESC LIMIT 1";
-        $getLastBalanceParams = [$clientId, $insurerId];
+        // $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE client_id = ? AND insurer_id = ? ORDER BY created_at DESC LIMIT 1";
+        // $getLastBalanceParams = [$clientId, $insurerId];
 
-        $lastBalance = $model->query($getLastBalanceQuery, $getLastBalanceParams)->getRow()->balance;
+        $getLastBalanceQuery = "SELECT balance FROM cash_deposit WHERE cd_ac_no = ? ORDER BY created_at DESC LIMIT 1";
+        $getLastBalanceParams = [$cd_ac_no];
 
-        if(!$lastBalance){
+        $lastBalance = $model->query($getLastBalanceQuery, $getLastBalanceParams)->getRow()->balance ?? null;
+
+        if(empty($lastBalance) && $lastBalance == null){
 
             $cd_model = new ClientDepositModel();
 
-            $getLastBalanceQuery = "SELECT opening_bal FROM cd_master WHERE client_id = ? AND insurer_id = ? ORDER BY created_at DESC LIMIT 1";
-            $getLastBalanceParams = [$clientId, $insurerId];
+            $getLastBalanceQuery = "SELECT opening_bal FROM cd_master WHERE client_id = ? AND insurer_id = ? AND cd_ac_no = ? ORDER BY created_at DESC LIMIT 1";
+            $getLastBalanceParams = [$clientId, $insurerId, $cd_ac_no];
     
             $lastBalance = $cd_model->query($getLastBalanceQuery, $getLastBalanceParams)->getRow()->opening_bal ?? 0;
-    
         }
 
         return $lastBalance;
diff --git a/app/Views/client_policy.php b/app/Views/client_policy.php
index fd79a58b..611fadd3 100644
--- a/app/Views/client_policy.php
+++ b/app/Views/client_policy.php
@@ -264,7 +264,7 @@
                     
                         ${item.insurer_short} - ${item.insurer_branch_name}
                         ${item.policy_name}  (${item.policy_type_name})
-                        ${item.branch_name}
+                        ${item.branch_name ? item.branch_name : ' - '}
                         ${tpaValue}
                         ${(item.policy_start_date)} /   ${(item.policy_end_date)}
                         ${(enrollmentStatus)}
diff --git a/composer.json b/composer.json
index 2dd9d301..9f2ec667 100644
--- a/composer.json
+++ b/composer.json
@@ -20,7 +20,7 @@
         "laminas/laminas-escaper": "^2.9",
         "php-amqplib/php-amqplib": "^2.8",
         "phpmailer/phpmailer": "^6.9",
-        "phpoffice/phpspreadsheet": "^2.0",
+        "phpoffice/phpspreadsheet": "^2.1",
         "psr/log": "^1.1",
         "slim/slim": "^4.13",
         "zircote/swagger-php": "^4.8"