From c23d345f354310919b279e6de959a3b3fee6957c Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Thu, 29 Feb 2024 12:14:58 +0530 Subject: [PATCH] REFACTOR_CLIENT_POLICY_MASTER FEAT_DATE_AUTOSELECT : RV --- app/Controllers/ClientController.php | 6 +- app/Models/ClientPolicyModel.php | 2 + app/Views/client_policy.php | 257 ++++++++++++++++----------- 3 files changed, 158 insertions(+), 107 deletions(-) diff --git a/app/Controllers/ClientController.php b/app/Controllers/ClientController.php index 554c8a2f..e30e4989 100644 --- a/app/Controllers/ClientController.php +++ b/app/Controllers/ClientController.php @@ -419,12 +419,12 @@ class ClientController extends AdminController $data['client_id'] = $this->request->getPost('client_id'); - $data['insured'] = $this->request->getPost('insured'); $data['no_of_lives'] = $this->request->getPost('no_of_lives'); $data['policy_status'] = $this->request->getPost('policy_status'); $data['no_of_employees'] = $this->request->getPost('no_of_employees'); $data['earned_premium_date'] = change_date_format($this->request->getPost('earned_premium_date'), 'd-m-Y', 'Y-m-d'); - $data['claims_incurred_date'] = change_date_format($this->request->getPost('claims_incurred_date'), 'd-m-Y', 'Y-m-d'); $data['incurred_claims_ratio'] = $this->request->getPost('incurred_claims_ratio'); + $data['claims_incurred_date'] = change_date_format($this->request->getPost('claims_incurred_date'), 'd-m-Y', 'Y-m-d'); + $data['incurred_claims_ratio'] = $this->request->getPost('incurred_claims_ratio'); $data['no_lives_at_inception'] = $this->request->getPost('no_lives_at_inception'); $data['premium_paid_at_inception'] = $this->request->getPost('premium_paid_at_inception'); $data['claims_experience_for_last_3_years'] = $this->request->getPost('claims_experience_for_last_3_years'); @@ -630,7 +630,7 @@ class ClientController extends AdminController $client_policy_data = $this->clientPolicyModel->where(['id' => $id, 'is_active' => 1])->first(); $insurer_id = $client_policy_data['insurer_id']; $polices = $this->policesModel->where(['insurer_id' => $insurer_id, 'is_active' => 1])->findAll(); - echo json_encode(array("status" => true , 'data' => $client_policy_data, 'policy' => $polices)); + echo json_encode(array("insurer_id" => $client_policy_data['insurer_id'], "status" => true , 'data' => $client_policy_data, 'policy' => $polices)); }else{ echo json_encode(array("status" => false)); } diff --git a/app/Models/ClientPolicyModel.php b/app/Models/ClientPolicyModel.php index 2d1eff5d..0849618a 100644 --- a/app/Models/ClientPolicyModel.php +++ b/app/Models/ClientPolicyModel.php @@ -16,6 +16,8 @@ class ClientPolicyModel extends Model "insurer_branch_id", "tpa_id", "tpa_branch_id", + "policy_type_id", + "policy_start_date", "policy_end_date", diff --git a/app/Views/client_policy.php b/app/Views/client_policy.php index 01c386c9..9eb267f2 100644 --- a/app/Views/client_policy.php +++ b/app/Views/client_policy.php @@ -38,7 +38,7 @@ - +
@@ -109,20 +109,34 @@ var policy_client = $('#policy_PrimaryKey').val(); $(document).ready(function () { - //Configure Flatpicker for the policy start date datepicker. - flatpickr("#start_date", { + // Get today's date + var today = new Date(); + + // Initialize Flatpickr for the start date with today's date + var startDatePicker = flatpickr("#start_date", { dateFormat: "d-m-Y", - defaultDate: "today", + defaultDate: today, allowInput: false, + onChange: function(selectedDates, dateStr, instance) { + var endDate = new Date(selectedDates[0]); + endDate.setFullYear(endDate.getFullYear() + 1); + + endDatePicker.setDate(endDate); + } }); - //Configure Flatpicker for the policy end date datepicker. - flatpickr("#end_date", { + // Calculate the end date (today + 1 year) + var endDate = new Date(today); + endDate.setFullYear(endDate.getFullYear() + 1); + + // Initialize Flatpickr for the end date with the calculated end date + var endDatePicker = flatpickr("#end_date", { dateFormat: "d-m-Y", - defaultDate: "today", + defaultDate: endDate, allowInput: false }); + $('#add_form').hide(); $('.btnBack').hide(); @@ -181,79 +195,81 @@ $(document).ready(function () { //for form submit using AJAX $("#policy_form").submit(function(event) { -event.preventDefault(); + event.preventDefault(); - policy_PrimaryKey = $('#client_id_policy').val(); - policy_client = $('#policy_PrimaryKey').val(); + policy_PrimaryKey = $('#client_id_policy').val(); + policy_client = $('#policy_PrimaryKey').val(); -if (policy_PrimaryKey === '' && policy_client === '') { - toastr.error('Client is required', 'Error'); - $('#insurer').val(''); - $('#tpa').val(''); - $('#policy').html(''); - return; -} - -var isValid = $('#policy_form').parsley().validate(); -if (!isValid) { - console.log('Form is Empty', 'Warning'); - return ; -} - -var formData = new FormData($('#policy_form')[0]); -var policy_form_action = $('#policy_form_action').val(); - -$.ajax({ - data: formData, - url: policy_form_action, - type: "POST", - dataType: 'json', - processData: false, - contentType: false, - success: function(res) { - - if (res.status === false) { - toastr.error('Policy Dose Not Create', 'Error'); - return; + if (policy_PrimaryKey === '' && policy_client === '') { + toastr.error('Client is required', 'Error'); + $('#insurer').val(''); + $('#tpa').val(''); + $('#policy').html(''); + return; } - console.log(res); - var message = (policy_PrimaryKey === '') ? 'Policy Created successfully' : 'Policy Updated Successfully'; - toastr.success(message, 'Success'); + var isValid = $('#policy_form').parsley().validate(); + if (!isValid) { + console.log('Form is Empty', 'Warning'); + return ; + } - $('#policy_table tr').remove(); - var policyTable = ''; - $.each(res.data, function(index, item) { - policyTable += ` - - ${item.insurer_short} - ${item.insurer_branch_name} - ${item.policy_name} - ${item.tpa_short} - ${item.tpa_branch_code} - -    - - - - `; + var formData = new FormData($('#policy_form')[0]); + var policy_form_action = $('#policy_form_action').val(); + + $.ajax({ + data: formData, + url: policy_form_action, + type: "POST", + dataType: 'json', + processData: false, + contentType: false, + success: function(res) { + + if (res.status === false) { + toastr.error('Policy Dose Not Create', 'Error'); + return; + } + + console.log(res); + var message = (policy_PrimaryKey === '') ? 'Policy Created successfully' : 'Policy Updated Successfully'; + toastr.success(message, 'Success'); + + $('#policy_table tr').remove(); + var policyTable = ''; + $.each(res.data, function(index, item) { + policyTable += ` + + ${item.insurer_short} - ${item.insurer_branch_name} + ${item.policy_name} + ${item.tpa_short} - ${item.tpa_branch_code} + +    + + + + `; + }); + $('#policy_table').append(policyTable); + $('#add_form').hide(); + $('#table_list').show(); + $('.btnBack').hide(); + $('.btnAdd').show(); + }, + error: function(xhr, status, error) { + console.error(xhr.responseText); + console.error(status, error); + } }); - $('#policy_table').append(policyTable); - $('#add_form').hide(); - $('#table_list').show(); - $('.btnBack').hide(); - $('.btnAdd').show(); - }, - error: function(xhr, status, error) { - console.error(xhr.responseText); - console.error(status, error); - } -}); -}); + }); //To get th POLICY base on Insurer $('#insurer').change(function() { var id = $(this).val(); - var url = "" + id; + var parts = id.split('-'); + var secondPart = parts[1]; + var url = "" + secondPart; $.get(url, function(res) { res = JSON.parse(res) @@ -274,13 +290,17 @@ $.ajax({ // set the Policy_Type_id for Form Submit $('#policy').change(function(){ + var dataId = $(this).children('option:selected').attr('data-id'); + console.log('dataId', dataId) $('#policy_type_id').val(dataId); if(dataId == 1){ - appendPolicyFormFields(1) - }else{ - appendPolicyFormFields(2) + appendPolicyFormFields(1); + }else if (dataId >= 1){ + appendPolicyFormFields(2); } + + console.log('second', $('#policy_type_id').val()); }); @@ -288,16 +308,18 @@ $.ajax({ $('body').on('click', '.btnPolicyEdit', function () { var policy_form_action = ''; - var policy_id = $(this).data('id'); - console.log('1', policy_id) - var policyId = $(this).attr('data-id'); - console.log('2', policyId) + // var policy_id = $(this).data('id'); + // console.log('1', policy_id) + var policy_id = $(this).attr('data-id'); + console.log('2', policy_id) $.ajax({ url: '' + policy_id, type: "GET", dataType: 'json', success: function (res) { + + console.log(res); $('#policy_form_action').val(''); @@ -321,12 +343,13 @@ $.ajax({ if(res.data.policy_type_id == 1){ appendPolicyFormFields(1, res.data); - }else { + }else if(res.data.policy_type_id >= 2) { appendPolicyFormFields(2, res.data); } var policeOptionHTML = ''; $.each(res.policy, function(index, item) { + console.log(item) policeOptionHTML += ''; }); $('#policy').html(policeOptionHTML); @@ -338,7 +361,6 @@ $.ajax({ }); }); - function appendPolicyFormFields(policy_type, data = false){ var html = ''; @@ -351,24 +373,6 @@ $.ajax({ html = `
-
-
- - -
-
- - -
-
- - -
-
- - -
-
@@ -389,6 +393,21 @@ $.ajax({
+
+
+ + +
+
+ + +
+
+ + +
+
+
@@ -398,7 +417,7 @@ $.ajax({
- +
@@ -413,19 +432,15 @@ $.ajax({ html = `
-
- - -
-
+
-
+
-
+
@@ -450,6 +465,40 @@ $.ajax({ }); } + $('#earned_premium_amount').keyup(function(){ + + var PA = parseFloat($(this).val()); + var CA = parseFloat($('#claims_incurred_amount').val()); + // console.log('earned_premium_amount',PA) + // console.log('claims_incurred_amount',CA) + + if (!isNaN(PA) && !isNaN(CA) && CA !== 0) { + var incurredClaimRatio = CA / PA; + // console.log(incurredClaimRatio) + $('#incurred_claims_ratio').val(incurredClaimRatio.toFixed(2)); + } else { + $('#incurred_claims_ratio').val('0'); + } + }); + + + $('#claims_incurred_amount').keyup(function(){ + + var CA = $(this).val() + var PA = $('#earned_premium_amount').val() + // console.log('earned_premium_amount',PA) + // console.log('claims_incurred_amount',CA) + + if (!isNaN(PA) && !isNaN(CA) && CA !== 0) { + var incurredClaimRatio = CA / PA; + // console.log(incurredClaimRatio) + $('#incurred_claims_ratio').val(incurredClaimRatio.toFixed(2)); + } else { + $('#incurred_claims_ratio').val('0'); + } + + }) + }