diff --git a/app/Controllers/EmployeeController.php b/app/Controllers/EmployeeController.php index 2f05a1d4..ae8fb1a4 100755 --- a/app/Controllers/EmployeeController.php +++ b/app/Controllers/EmployeeController.php @@ -1225,8 +1225,278 @@ class EmployeeController extends AdminController } - //truncateFileDataIn DB (de activate rows) + //truncateFileDataIn DB (de activate rows) NEW FUNCTION public function truncateFileData($file_id, $role_id = null) + { + $this->myLogger->logme('error', '---- truncateFileData Function called ----'); + $file_id = $this->request->uri->getSegment(3); + // $file_id = 747; + + $this->myLogger->logme('error', 'File id for truncate : -- FILE ID : {data} --', ['data' => $file_id]); + + //get the files data + $file = $this->fileModel->find($file_id); + + $client_id = $file['client_id']; + $client_policy_id = $file['policy_id']; + $loggedInUserID = get_session_userid(); + + //get the client policy data + $policy_data = $this->clientPolicyModel->where('id', $client_policy_id)->first(); + + //get the cd transaction data based on the insurer and client + $cd_tranction = $this->cashDepositModel + ->where('client_id', $client_id) //client + ->where('insurer_id', $policy_data['insurer_id']) //insurer + ->where('event_name',$file['action']) //event + ->where('client_policy_id', $client_policy_id)//policy + ->orderBy('id', 'desc') + ->first(); + + // print_r($file); die; + if ($file['action'] == 'inception' || $file['action'] == 'dependent_addition' || $file['action'] == 'addition' || $file['action'] == 'enrollment' || $file['action'] == 'missed_inception') { + + + if($file['action'] == 'enrollment'){ + $result = $this->employeeModel->getEmpListByFileId(file_id: $file_id, emp_status: ['draft','enrolled'], policy_status: ['draft','enrolled']); + }else{ + $result = $this->employeeModel->getEmpListByFileId(file_id: $file_id, emp_status: ['active'], policy_status: ['active']); + } + + // print_r($result); die; + if (count($result) && $role_id == null) { + + if(get_role_id() == 1 || get_role_id() == 5){ + return $this->respond(['role' => get_role_id(), 'dataStatus' => false, 'code' => 404, 'message' => 'File data already processed. Do you want to truncate data from uploaded file?'], 200); + }else{ + return $this->respond(['role' => get_role_id(), 'dataStatus' => false, 'code' => 404, 'message' => 'File data already processed'], 200); + } + + } else { + + $this->myLogger->logme('error', '---- TRUNCATED STARTED WITH ADMIN OR HEAD PERMISSION ----'); + $this->myLogger->logme('error', '---- Event Type : -- {data} ----', ['data' => $file['action']]); + + if($file['action'] != 'enrollment') + { + $employee_policy_data_with_file_id = $this->employeePolicyModel->where('file_id', $file_id)->findAll(); + + if(!empty($employee_policy_data_with_file_id) && count($employee_policy_data_with_file_id) > 0){ + + // GET THE CD TRANSACTION AMOUNT + $cd_amount_total = $this->employeePolicyModel->calculateCdTranctionAmount($file_id); + $cd_amount = $cd_amount_total['total'] ?? 0; + $this->myLogger->logme('error', '---- CD Amount : {data} ----', ['data' => $cd_amount]); + + //STEP: 1 - Update employee policy table + $db = db_connect(); + $query = " + UPDATE employee_polices + SET employee_polices.status = 'truncated', employee_polices.is_active = 0 + WHERE employee_polices.file_id = $file_id + "; + $db->query($query); + $affectedRows = $db->affectedRows(); + $this->myLogger->logme('error', '---- employee_polices table update query : {data} ----', ['data' => $query]); + $this->myLogger->logme('error', '---- employee_polices table updated - Affected Rows : {data} ----', ['data' => $affectedRows]); + // print_r($db->getLastQuery()); die; + + //STEP:2 - Update emp_endorsement Table + if(in_array($file['action'], ['addition', 'dependent_addition'])){ + $this->empEndorsementModel->where('file_id', $file_id)->set(['status' => 'truncated','is_active' => 0])->update(); + $this->myLogger->logme('error', '---- emp_endorsement table updated ----'); + } + + //STEP:3 - Update files table status + $this->fileModel->where('id', $file_id)->set(['status' => 'truncated'])->update(); + $this->myLogger->logme('error', '---- files table updated ----'); + + //FINAL STEP - Update reverse entry in cash_deposite table + if(!empty($cd_amount)){ + + $cd_data = [ + 'amount' => $cd_amount, + 'sub_type_id' => 8, + 'endorsement_no' => null, + 'insurer_id' =>$policy_data['insurer_id'], + 'description' => 'The policy Truncated by Admin or Head', + 'transaction_type' => 'Credit', + 'updated_by' => 1, + 'client_id' => $client_id, + 'client_policy_id' => $client_policy_id, + 'cd_ac_no' => $policy_data['cd_ac_no'] ?? null, + 'cd_ac_pk' => $policy_data['cd_ac_pk'] ?? null, + 'event_name' => $file['action'], + ]; + + $response = DepositHelper::saveDeposit($cd_data, $loggedInUserID); + $this->myLogger->logme('error', 'cash_deposite table updated -- cd transaction'); + } + + }else{ + + $this->myLogger->logme('error', '---- There is no data to truncate ----'); + return $this->respond(['role' => get_role_id(), 'dataStatus' => false, 'code' => 404, 'message' => 'There is no data to truncate'], 200); + } + + } + else + { + // echo 'came here...1'; + //check all dependents added by self and other dependent policies + $emp_codes = $this->employeeModel->select('emp_code') + ->where('file_id', $file_id) + ->findAll(); + $emp_codes = array_column($emp_codes,'emp_code'); + // Kint::dump($emp_codes);//die; + + $dependent_policies = $this->clientPolicyModel->select('id') + ->where('base_policy', $client_policy_id) + ->findAll(); + // $dependent_policies = [ [10],[25],[35] ]; + // Kint::dump($dependent_policies); + // Kint::dump(array_column($dependent_policies,'id')); + if(count($dependent_policies)) + { + $dependent_policies = array_column($dependent_policies,'id'); + // Kint::dump($dependent_policies); + $second_level_dependent_policies = $this->clientPolicyModel->select('id') + ->whereIn('base_policy', $dependent_policies) + ->findAll(); + // dd($second_level_dependent_policies); + if(count($second_level_dependent_policies)) + { + $second_level_dependent_policies = array_column($second_level_dependent_policies,'id'); + } + + $dependent_policies = array_merge($dependent_policies,$second_level_dependent_policies); + } + $dependent_policies = array_merge($dependent_policies,[$client_policy_id]); + // Kint::dump($dependent_policies); + + //update emp and emp plocies + $db = db_connect(); + $emp_codes = '(' . implode(',', array_map(fn($code) => "'$code'", $emp_codes)) . ')'; + $dependent_policies = '(' . implode(',', $dependent_policies) . ')'; + $query = " + UPDATE employee_polices + JOIN employees ON employees.id = employee_polices.employee_id and employees.emp_code in $emp_codes + SET employee_polices.status = 'truncated', employee_polices.is_active = 0 + WHERE employee_polices.client_policy_id in $dependent_policies + "; + // print_r($query); die; + $db->query($query); + $affectedRows = $db->affectedRows(); + + // dd($affectedRows); + $affectedRows = $affectedRows * 2; + $this->fileModel->where('id', $file_id)->set(['status' => 'truncated'])->update(); + + } + + $this->myLogger->logme('error', 'SUCCESSFULLY TRUNCATED'); + + return $this->respond(['dataStatus' => true, 'code' => 200, 'data' => round($affectedRows / 2)], 200); + } + + } else if ($file['action'] == 'si_enhancement' || $file['action'] == 'correction' || $file['action'] == 'deletion') { + + $res = $this->empEndorsementModel->select(['count(id) as count']) + ->where('emp_endorsement.file_id', $file_id) + ->groupStart() + ->where('emp_endorsement.endorsement_id is not null') + ->orwhereIn('emp_endorsement.status', ['complete']) + ->groupEnd() + ->get() + ->getResult(); + + // print_r($this->empEndorsementModel->getLastQuery()); + // echo $res[0]->count;die(); + + if ($res[0]->count == 0 || $role_id == 5 || $role_id == 1) { + + $this->myLogger->logme('error', 'TRUNCATED STARTED WITH ADMIN OR HEAD PERMISSION'); + $this->myLogger->logme('error', 'Event Type : -- {data} --', ['data' => $file['action']]); + + $transaction_type = 'Credit'; + $cd_amount = $cd_tranction['amount']; + + if($file['action'] == 'deletion'){ + $transaction_type = 'Debit'; + //get cd amount for the particular file id to reverse entry to the cash deposite for only deletion + $cd_amount_total = $this->employeePolicyModel->getDeletionDataForTruncated($file['id']); + $totalSum = array_sum(array_column($cd_amount_total, 'total')); + $cd_amount = $totalSum; + + $this->myLogger->logme('error', 'Deletion CD Amount : {data}', ['data' => $cd_amount]); + } + + $this->myLogger->logme('error', 'Endorsemnt CD Amount : {data}', ['data' => $cd_amount]); + + + //STEP 1: + //update truncated status to the Emp_endorsement table + $this->empEndorsementModel->where('file_id', $file_id) + ->set(['status' => 'truncated','is_active' => 0]) + ->update(); + + $this->myLogger->logme('error', 'emp_endorsement table updated'); + + //STEP 2: + if($file['action'] == 'deletion') { + //update the employee policy table reverse the data + $this->employeePolicyModel->updateEmployeePolicyTruncateReverse($file_id); + $this->myLogger->logme('error', 'employee_polices table updated for deletion'); + } + + if($file['action'] == 'correction'){ + + } + + // STEP 3: + //update files table status to "truncated" + $this->fileModel->where('id', $file_id)->set(['status' => 'truncated'])->update(); + $this->myLogger->logme('error', 'files table updated'); + + + if($cd_tranction && $file['action'] != 'correction'){ + + $cd_data = [ + 'amount' => $cd_amount, + 'sub_type_id' => 8, + 'endorsement_no' => null, + 'insurer_id' =>$policy_data['insurer_id'], + 'description' => 'The policy Truncated by Admin or Head', + 'transaction_type' => $transaction_type, + 'updated_by' => 1, + 'client_id' => $client_id, + 'client_policy_id' => $client_policy_id, + 'cd_ac_no' => $policy_data['cd_ac_no'] ?? null, + 'cd_ac_pk' => $policy_data['cd_ac_pk'] ?? null, + 'event_name' => $file['action'], + ]; + + $response = DepositHelper::saveDeposit($cd_data, $loggedInUserID); + $this->myLogger->logme('error', 'cash_deposite table updated -- cd transaction'); + + } + + $this->myLogger->logme('error', 'SUCCESSFULLY TRUNCATED'); + + return $this->respond(['dataStatus' => true, 'code' => 200, 'data' => 'success'], 200); + + } else { + if(get_role_id() == 1 || get_role_id() == 5){ + return $this->respond(['role' => get_role_id(), 'dataStatus' => false, 'code' => 404, 'message' => 'File data already processed. Do you want to truncate data from uploaded file?'], 200); + }else{ + return $this->respond(['dataStatus' => false, 'code' => 404, 'message' => 'File data already processed'], 200); + } + } + } + } + + //truncateFileData OLD FUNCTION + public function truncateFileDataOld($file_id, $role_id = null) { $file_id = $this->request->uri->getSegment(3); // $file_id = 747; @@ -1288,7 +1558,7 @@ class EmployeeController extends AdminController if($file['action'] != 'enrollment') { - //STEP: 1 - Update employee policy table + //STEP: 1 - Update employee policy table //update emp and emp plocies $db = db_connect(); $query = " @@ -1331,13 +1601,13 @@ class EmployeeController extends AdminController // Kint::dump($emp_codes);//die; $dependent_policies = $this->clientPolicyModel->select('id') - ->where('base_policy', $client_policy_id) - ->findAll(); + ->where('base_policy', $client_policy_id) + ->findAll(); // $dependent_policies = [ [10],[25],[35] ]; - // Kint::dump($dependent_policies); - // Kint::dump(array_column($dependent_policies,'id')); - if(count($dependent_policies)) - { + // Kint::dump($dependent_policies); + // Kint::dump(array_column($dependent_policies,'id')); + if(count($dependent_policies)) + { $dependent_policies = array_column($dependent_policies,'id'); // Kint::dump($dependent_policies); $second_level_dependent_policies = $this->clientPolicyModel->select('id') @@ -1350,11 +1620,11 @@ class EmployeeController extends AdminController } $dependent_policies = array_merge($dependent_policies,$second_level_dependent_policies); - } - $dependent_policies = array_merge($dependent_policies,[$client_policy_id]); - // Kint::dump($dependent_policies); + } + $dependent_policies = array_merge($dependent_policies,[$client_policy_id]); + // Kint::dump($dependent_policies); - //update emp and emp plocies + //update emp and emp plocies $db = db_connect(); $emp_codes = '(' . implode(',', array_map(fn($code) => "'$code'", $emp_codes)) . ')'; $dependent_policies = '(' . implode(',', $dependent_policies) . ')'; @@ -1402,7 +1672,7 @@ class EmployeeController extends AdminController } } else if ($file['action'] == 'si_enhancement' || $file['action'] == 'correction' || $file['action'] == 'deletion') { - + $res = $this->empEndorsementModel->select(['count(id) as count']) ->where('emp_endorsement.file_id', $file_id) ->groupStart() @@ -1425,7 +1695,7 @@ class EmployeeController extends AdminController if($file['action'] == 'deletion'){ $transaction_type = 'Debit'; - //get cd amount for the particular file id to reverse entry to the cash deposite for only deletion + //get cd amount for the particular file id to reverse entry to the cash deposite for only deletion $cd_amount_total = $this->employeePolicyModel->getDeletionDataForTruncated($file['id']); $totalSum = array_sum(array_column($cd_amount_total, 'total')); $cd_amount = $totalSum; diff --git a/app/Models/EmployeePolicyModel.php b/app/Models/EmployeePolicyModel.php index 8c98dc21..3a4d852d 100755 --- a/app/Models/EmployeePolicyModel.php +++ b/app/Models/EmployeePolicyModel.php @@ -1682,4 +1682,29 @@ class EmployeePolicyModel extends Model $query = "UPDATE emp_endorsement SET status = $caseStatus, endorsement_id = $caseEndorsementId WHERE id IN ($ids);"; // Execute the query $this->db->query($query); } + + //calculate the CD Transaction sum of the amount for the inception, addition, dependent addition, missed inception + public function calculateCdTranctionAmount($file_id) + { + $query = $this->db->query(" + + SELECT + ROUND(SUM(ep.rata_premimum + ep.gst), 2) AS total + FROM + employee_polices ep + WHERE + ep.file_id = $file_id + AND ep.uhid IS NOT NULL' + AND ep.status != 'truncated' + AND ep.status != 'truncated' + AND ep.is_active = 1; + "); + + + $result = $query->getResultArray(); + // dd($this->db->getLastQuery(), $result); + // dd($result); + + return $result[0]; + } } \ No newline at end of file diff --git a/app/Views/view_deposit.php b/app/Views/view_deposit.php index 1ec2d6c7..7abbf0aa 100755 --- a/app/Views/view_deposit.php +++ b/app/Views/view_deposit.php @@ -265,11 +265,13 @@ $(document).ready(function() { $(document).ready(function() { $('#tickets-table').DataTable({ - dom: "<'row'<'col-sm-0'f><'col-sm-9 text-right'B>>", + dom: "<'row'<'col-sm-0'f><'col-sm-7 text-right'B>>" + + "<'row'<'col-sm-12'tr>>" + + "<'row'<'col-sm-5'i><'col-sm-7'p>>", buttons: [{ extend: 'csv', text: 'CSV', - title: 'TransactionDetails', + title: 'CD TransactionDetails', exportOptions: { columns: '' } @@ -283,17 +285,15 @@ $(document).ready(function() { } } ], - - language: { search: "_INPUT_", searchPlaceholder: "Search..." }, - + ordering: false, + paging: true , }); }); -