FEAT_SI_MAPPING_SRINIVAS
This commit is contained in:
parent
180812a544
commit
2d59886d45
@ -337,6 +337,13 @@ $routes->group("/util", ["filter" => "authMVC"], function ($routes) {
|
||||
$routes->get('transformMailContent', 'LeadsController::transformMailContent');
|
||||
$routes->get('getRackRateSIAmountAndAutoSiDataForAutoSI', 'ClientController::getRackRateSIAmountAndAutoSiDataForAutoSI');
|
||||
$routes->post('createAutoSI', 'ClientController::createAutoSI');
|
||||
$routes->get('getPolicySIRacketData','ClientController::getPolicySIRacketData');
|
||||
$routes->get('fetchPolicySIDataForParentPolicy','ClientController::fetchPolicySIDataForParentPolicy');
|
||||
$routes->post('saveSIMapping','ClientController::saveSIMapping');
|
||||
$routes->post('checkSIMapping','ClientController::checkSIMapping');
|
||||
$routes->post('deleteMapping','ClientController::deleteMapping');
|
||||
|
||||
|
||||
});
|
||||
|
||||
$routes->group("policy_tranction", ["filter" => "authMVC"], function ($routes) {
|
||||
|
||||
@ -40,6 +40,7 @@ use App\Models\PolicyTransactionStatusModel;
|
||||
use App\Models\VehicleModel;
|
||||
use App\Models\LeadsModel;
|
||||
use App\Models\AutoSIModel;
|
||||
use App\Models\SIMappingModel;
|
||||
|
||||
use App\Controllers\EmpDataServiceController;
|
||||
use App\Controllers\GoogleDriveController;
|
||||
@ -82,6 +83,7 @@ class ClientController extends AdminController
|
||||
protected $vehicleModel;
|
||||
protected $leadsModel;
|
||||
protected $AutoSIModel;
|
||||
protected $SIMappingModel;
|
||||
|
||||
|
||||
|
||||
@ -119,6 +121,7 @@ class ClientController extends AdminController
|
||||
$this->vehicleModel = new VehicleModel();
|
||||
$this->leadsModel = new LeadsModel();
|
||||
$this->AutoSIModel = new AutoSIModel();
|
||||
$this->SIMappingModel = new SIMappingModel();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------
|
||||
@ -4038,4 +4041,110 @@ class ClientController extends AdminController
|
||||
return $this->respond(['status' => false, 'code' => 404, 'message' => 'No data to found. Client Policy Not found'], 200);
|
||||
}
|
||||
}
|
||||
|
||||
public function getPolicySIRacketData(){
|
||||
$base_policy = $this->request->getGet('client_base_policy_id');
|
||||
$policy_data = $this->clientPolicyModel->getBasePolicyByPrimaryKey($base_policy);
|
||||
// log_message('error','data '.$base_policy);
|
||||
$si_amounts = $this->policyPremium2Model->where('client_id',$policy_data['client_id'])->where('client_policy_id',$policy_data['id'])->findAll();
|
||||
|
||||
// log_message('error','Policy data for first'.json_encode($si_amounts));
|
||||
return $this->respond(['status' => true,'code' => 200,'data' => $si_amounts ], 200);
|
||||
}
|
||||
|
||||
public function fetchPolicySIDataForParentPolicy(){
|
||||
$client_policy_id = $this->request->getGet('client_policy_id');
|
||||
$policy_data = $this->clientPolicyModel->getBasePolicyByPrimaryKey($client_policy_id);
|
||||
$si_amounts = $this->policyPremium2Model->where('client_id',$policy_data['client_id'])->where('client_policy_id',$policy_data['id'])->findAll();
|
||||
// log_message('error','Policy data for second'.json_encode($policy_data));
|
||||
return $this->respond(['status' => true,'code' => 200,'data' => $si_amounts ], 200);
|
||||
|
||||
}
|
||||
|
||||
public function saveSIMapping(){
|
||||
$formData = $_POST;
|
||||
log_message('error','received data'.json_encode($_POST));
|
||||
// print_rr($formData);
|
||||
try{
|
||||
$si_mapping_array = $formData['si_mapping'];
|
||||
foreach($si_mapping_array as $mappings){
|
||||
log_message('error','si mapping form data '.json_encode($mappings));
|
||||
if(isset($mappings['pk']) && $mappings['pk'] != ''){
|
||||
$data = [
|
||||
'id' => $mappings['pk'],
|
||||
'policy_id' => $formData['client_policy_id'],
|
||||
'policy_si_amounts' => json_encode($mappings['policy_si_amounts']),
|
||||
'base_policy_si_amount' => $mappings['base_policy_si_amount'],
|
||||
'base_policy_id' => $formData['client_base_policy_id'],
|
||||
'updated_by' => get_session_userid(),
|
||||
'is_active' => 1
|
||||
];log_message('error','the data for update'.json_encode($data));
|
||||
$result = $this->SIMappingModel->save($data);
|
||||
|
||||
}else{
|
||||
$data = [
|
||||
'policy_id' => $formData['client_policy_id'],
|
||||
'policy_si_amounts' => json_encode($mappings['policy_si_amounts']),
|
||||
'base_policy_si_amount' => $mappings['base_policy_si_amount'],
|
||||
'base_policy_id' => $formData['client_base_policy_id'],
|
||||
'created_by' => get_session_userid(),
|
||||
'is_active' => 1
|
||||
];log_message('error','the data for insert'.json_encode($data));
|
||||
$result = $this->SIMappingModel->save($data);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ($result){
|
||||
return $this->respond(['status' => true,'code' => 200],200);
|
||||
}
|
||||
}
|
||||
catch (\Exception $e) {
|
||||
$this->myLogger->logme('error' , $e->getMessage());
|
||||
return $this->respond(['status' => false,'code' => 404,'message'=>'Check for Empty Fields'],200);
|
||||
}
|
||||
}
|
||||
|
||||
public function checkSIMapping(){
|
||||
$policy_id = $this->request->getPost('client_policy_id');
|
||||
$base_policy_id = $this->request->getPost('client_base_policy_id');
|
||||
|
||||
$results = $this->SIMappingModel->where('policy_id',$policy_id)->where('base_policy_id',$base_policy_id)->where('is_active',1)->findAll();
|
||||
if($results){
|
||||
$response_array = [];
|
||||
$count = 0;
|
||||
foreach($results as $result){
|
||||
$response_array[$count] = [
|
||||
'pk' => $result['id'],
|
||||
'policy_si_amounts' => $result['policy_si_amounts'],
|
||||
'base_policy_si_amount' => $result['base_policy_si_amount']
|
||||
];
|
||||
$count+=1;
|
||||
}
|
||||
return $this->respond(['status' => true,'code' => 200,'data'=> $response_array],200);
|
||||
|
||||
}else{
|
||||
$message = 'no data found';
|
||||
return $this->respond(['status' => false, 'code' => 350,'data'=>$message],200);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function deleteMapping(){
|
||||
$primary_key = $this->request->getPost('pk');
|
||||
|
||||
try{
|
||||
if(isset($primary_key) && $primary_key != ''){
|
||||
$result = $this->SIMappingModel->where('id',$primary_key) ->set('is_active',0)->update();
|
||||
}
|
||||
if($result){
|
||||
return $this->respond(['status'=> true,'code'=>200,'message'=>'Mapping Deleted Successfully'],200);
|
||||
}
|
||||
}
|
||||
catch(\Exception $e){
|
||||
$this->myLogger->logme('error',$e->getMessage());
|
||||
return $this->respond(['status'=> false,'code'=>404,'message'=>'Mapping Deletion Failed'],200);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -220,6 +220,10 @@ class ClientPolicyModel extends Model
|
||||
return $query->getRow();
|
||||
}
|
||||
|
||||
public function getBasePolicyByPrimaryKey($id){
|
||||
return $this->db->table('client_policy')->where('id',$id)->get()->getRowArray();
|
||||
}
|
||||
|
||||
|
||||
public function getDepositData($clientId, $insurerId)
|
||||
{
|
||||
|
||||
22
app/Models/SIMappingModel.php
Normal file
22
app/Models/SIMappingModel.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
|
||||
class SIMappingModel extends Model
|
||||
{
|
||||
protected $table = 'si_mapping';
|
||||
protected $primaryKey = 'id';
|
||||
protected $allowedFields = [
|
||||
"id",
|
||||
"policy_id",
|
||||
'base_policy_id',
|
||||
'policy_si_amounts',
|
||||
'base_policy_si_amount',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'is_active'
|
||||
];
|
||||
|
||||
}
|
||||
@ -53,9 +53,9 @@ table.dataTable thead th {
|
||||
<div class="col-6" style="align-self: center;">
|
||||
<h4 style="position: relative;">Client List</h4>
|
||||
</div>
|
||||
<div class="col-2" style="text-align: right; position: relative;top: 56px; left: 303px;">
|
||||
<!-- <div class="col-2" style="text-align: right; position: relative;top: 56px; left: 303px;">
|
||||
<a class="btn btn-primary waves-effect waves-light" onclick="showModal()">Add From Lead</a>
|
||||
</div>
|
||||
</div> -->
|
||||
<div class="col-1" style="text-align: right; position: relative;top: 56px; left: 294px;">
|
||||
<a href="<?= base_url("client/create"); ?>" type="button" id="btnAdd" class="btn btn-primary waves-effect waves-light" data-toggle="" data-placement="top" title="Add" data-trigger="hover">Add</a>
|
||||
</div>
|
||||
|
||||
@ -227,6 +227,7 @@
|
||||
<?php include('policy_gpa_terms.php'); ?>
|
||||
<?php include('other_policy_terms.php'); ?>
|
||||
<?php include('rack_rate_auto_si.php'); ?>
|
||||
<?php include('si_mapping.php');?>
|
||||
|
||||
<!-- Center modal content -->
|
||||
<div class="modal fade" id="lead_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
|
||||
@ -402,11 +403,16 @@ $(document).ready(function() {
|
||||
}
|
||||
|
||||
var policy_name_data = `${item.policy_type_name ?? ''}` + ' - ' + `${item.policy_no ?? ''}`;
|
||||
|
||||
// console.log("Inside of item "+ JSON.stringify(item,null,2));
|
||||
let auto_si_menu = '';
|
||||
let si_mapping = '';
|
||||
if(item.policy_type_id == 2){
|
||||
auto_si_menu = `<a href="#" data-id="${item.id}" data-typeid="${item.policy_type_id}" class="dropdown-item" onclick="showModal('rack_rate_auto_si_modal'); getRackRateSIAmountAndAutoSiDataForAutoSI('${item.id}')"><i class="mdi mdi-autorenew mr-2 text-muted font-18 vertical-middle"></i>Auto SI</a>`;
|
||||
}
|
||||
if (item.policy_type_id == 3 && item.base_policy != null && item.base_policy != ''){
|
||||
console.log('base policy '+ item.base_policy);
|
||||
si_mapping = `<a href="#" data-id="${item.id}" data-typeid="${item.policy_type_id}" class="dropdown-item" onclick="showModal('si_mapping'); loadModal('${item.id}','${item.base_policy}');"><i class="mdi mdi-swap-horizontal"></i> SI Mapping</a>`;
|
||||
}
|
||||
|
||||
|
||||
policyTable +=
|
||||
@ -426,7 +432,8 @@ $(document).ready(function() {
|
||||
<a href="#" data-id="${item.id}" id="${item.policy_id}" class="dropdown-item btnPolicyEdit"><i class="mdi mdi-lead-pencil mr-2 text-muted font-18 vertical-middle"></i>Edit</a>
|
||||
<a href="#" data-id="${item.id}" id="${search_term}" data-typeid="${item.policy_type_id}" class="dropdown-item btnPolicyMaster" data-toggle="modal"><i class="mdi mdi-wrench mr-2 text-muted font-18 vertical-middle"></i>Terms</a>
|
||||
<a href="#" data-id="${item.id}" id="${search_term}" data-typeid="${item.policy_type_id}" class="dropdown-item btnPolicyModel" data-toggle="modal" data-target="#bs-example-modal-lg"><i class="mdi mdi-book-open mr-2 text-muted font-18 vertical-middle"></i>Rack Rate</a>
|
||||
${auto_si_menu}`;
|
||||
${auto_si_menu}
|
||||
${si_mapping}`;
|
||||
// Conditionally render the delete option based on the role
|
||||
if (role !== 3 && role !== 4) {
|
||||
policyTable +=
|
||||
@ -2032,11 +2039,17 @@ function getClientPolicyDataForEdit(client_policy_id) {
|
||||
}
|
||||
|
||||
function showModal($opener) {
|
||||
console.log('showModal triggered with opener:', $opener);
|
||||
if($opener == 'lead_modal'){
|
||||
var myModal = new bootstrap.Modal(document.getElementById('lead_modal'));
|
||||
myModal.show();
|
||||
}else if($opener == 'rack_rate_auto_si_modal'){
|
||||
}
|
||||
else if ($opener == 'si_mapping') {
|
||||
var si_mapping_modal = new bootstrap.Modal(document.getElementById('si_mapping'));
|
||||
si_mapping_modal.show();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
function featchClient() {
|
||||
|
||||
@ -240,7 +240,7 @@ function getFormDataWithJSON() {
|
||||
return familyComposition;
|
||||
}
|
||||
|
||||
function checkCDAmountForBasePremium(client_policy_id) {
|
||||
function getRackRateSIAmountAndAutoSiDataForAutoSI(client_policy_id) {
|
||||
|
||||
$('.loader').fadeIn();
|
||||
$('.loader-mask').fadeIn();
|
||||
|
||||
466
app/Views/si_mapping.php
Normal file
466
app/Views/si_mapping.php
Normal file
@ -0,0 +1,466 @@
|
||||
<div id="si_mapping" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="fullWidthModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-full-width">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header" style="padding: 10px 15px">
|
||||
<h4 class="modal-title" id="fullWidthModalLabel">SI Mapping</h4>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true" onclick="resetFormAndRemoveRows()">×</button>
|
||||
</div>
|
||||
<div class="modal-body" style="padding: 5px 15px;">
|
||||
<form role="form" class="parsley-examples" id="si_mapping_form" enctype="multipart/form-data">
|
||||
<input type="hidden" id="auto_si_pk" name="id">
|
||||
<input type="hidden" id="client_policy_pk" name="client_policy_id">
|
||||
<div class="form-row" style="margin-top:-2%">
|
||||
<div class="form-group col-md-6">
|
||||
<label id='choose_base_policy_label' for="choose_base_policy">Base Policy<span class="text-danger">*</span></label>
|
||||
<select class="form-control" id="choose_base_policy" name="choose_base_policy">
|
||||
<option value="">Select a Base Policy</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<input type="hidden" id="client_policy_id_form">
|
||||
<input type="hidden" id="base_policy_id_form">
|
||||
<div id="si-mapping-container">
|
||||
<div class="form-row si-mapping-row">
|
||||
<input name="primary_key" type="hidden" id='pk' value="">
|
||||
<div class="form-group col-md-3">
|
||||
<label>Base Policy SI Amount<span class="text-danger">*</span></label>
|
||||
<select class="form-control" id="choose_si_amount_from_base_policy" name="choose_si_amount_from_base_policy">
|
||||
<!-- <option value="">Choose SI Amount</option> -->
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group col-md-3">
|
||||
<label>Policy SI Amount</label><br />
|
||||
<select class="form-control" name="choose_si_amount_for_policy[]" id="choose_si_amount_for_policy" multiple>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="btn-group" style="margin-top: 45px;">
|
||||
<button type="button" id="deleteButton" class="btn btn-danger remove-row" style="margin-right: 5px;">x</button>
|
||||
<button type="button" id="addButton" class="btn btn-success add-row">+</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-light" data-dismiss="modal" onclick="resetFormAndRemoveRows()">Close</button>
|
||||
<button type="button" class="btn btn-primary" id="submitFormButton" onclick="submitForm()">Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function loadModal(client_policy_id, base_policy_id) {
|
||||
$('.loader').fadeIn();
|
||||
$('.loader-mask').fadeIn();
|
||||
$('#choose_base_policy').hide();
|
||||
$('#choose_base_policy_label').hide();
|
||||
checkExistingMapping(client_policy_id, base_policy_id);
|
||||
|
||||
$('#base_policy_id_form').val(base_policy_id);
|
||||
$('#client_policy_id_form').val(client_policy_id);
|
||||
$('.loader').fadeOut();
|
||||
$('.loader-mask').delay(350).fadeOut('slow');
|
||||
};
|
||||
|
||||
var si_amt_base_policy = new Set();
|
||||
|
||||
function checkExistingMapping(client_policy_id, base_policy_id) {
|
||||
$.ajax({
|
||||
url: '<?= base_url("util/checkSIMapping") ?>',
|
||||
method: 'POST',
|
||||
data: {
|
||||
client_policy_id: client_policy_id,
|
||||
client_base_policy_id: base_policy_id
|
||||
},
|
||||
success: function(response) {
|
||||
if (response.status === true && response.code === 200 && response.data.length > 0) {
|
||||
// Clear existing rows except the first one
|
||||
$('.si-mapping-row:not(:first)').remove();
|
||||
|
||||
// First, ensure base policy and parent policy data is loaded
|
||||
fetchBasePolicy(client_policy_id, base_policy_id);
|
||||
fetchParentPolicy(client_policy_id, base_policy_id);
|
||||
|
||||
// Wait for dropdowns to be populated
|
||||
setTimeout(() => {
|
||||
response.data.forEach((mapping, index) => {
|
||||
if (index === 0) {
|
||||
// Handle first row
|
||||
const firstRow = $('.si-mapping-row:first');
|
||||
|
||||
// Clear existing values
|
||||
firstRow.find('select[name="choose_si_amount_from_base_policy"]')
|
||||
.val('')
|
||||
.trigger('change');
|
||||
firstRow.find('select[name="choose_si_amount_for_policy[]"]')
|
||||
.val([])
|
||||
.trigger('change');
|
||||
|
||||
// Set base policy SI amount
|
||||
firstRow.find('select[name="choose_si_amount_from_base_policy"]')
|
||||
.val(mapping.base_policy_si_amount)
|
||||
.trigger('change');
|
||||
|
||||
// Set policy SI amounts (parse JSON string to array)
|
||||
const policyAmounts = JSON.parse(mapping.policy_si_amounts);
|
||||
firstRow.find('select[name="choose_si_amount_for_policy[]"]')
|
||||
.val(policyAmounts)
|
||||
.trigger('change');
|
||||
|
||||
// Store pk value
|
||||
firstRow.find('input[name="primary_key"]').val(mapping.pk);
|
||||
} else {
|
||||
// Add new row for subsequent mappings
|
||||
addRow();
|
||||
$('#addButton').trigger("click");
|
||||
|
||||
// Wait for the new row to be properly initialized
|
||||
setTimeout(() => {
|
||||
const newRow = $('.si-mapping-row:last');
|
||||
|
||||
// Clear existing values
|
||||
newRow.find('select[name="choose_si_amount_from_base_policy"]')
|
||||
.val('')
|
||||
.trigger('change');
|
||||
newRow.find('select[name="choose_si_amount_for_policy[]"]')
|
||||
.val([])
|
||||
.trigger('change');
|
||||
|
||||
// Set base policy SI amount
|
||||
newRow.find('select[name="choose_si_amount_from_base_policy"]')
|
||||
.val(mapping.base_policy_si_amount)
|
||||
.trigger('change');
|
||||
|
||||
// Set policy SI amounts
|
||||
const policyAmounts = JSON.parse(mapping.policy_si_amounts);
|
||||
newRow.find('select[name="choose_si_amount_for_policy[]"]')
|
||||
.val(policyAmounts)
|
||||
.trigger('change');
|
||||
|
||||
// Store pk value
|
||||
newRow.find('input[name="primary_key"]').val(mapping.pk);
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
}, 1000); // Wait for fetchBasePolicy and fetchParentPolicy to complete
|
||||
}else{
|
||||
fetchBasePolicy(client_policy_id, base_policy_id);
|
||||
fetchParentPolicy(client_policy_id, base_policy_id);
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.log("Error during AJAX request:", error);
|
||||
toastr.error("Failed to fetch existing mappings");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function fetchBasePolicy(client_policy_id, base_policy_id) {
|
||||
$.ajax({
|
||||
url: '<?php echo base_url("util/getPolicySIRacketData")?>',
|
||||
method: 'GET',
|
||||
data: {
|
||||
client_policy_id: client_policy_id,
|
||||
client_base_policy_id: base_policy_id
|
||||
},
|
||||
success: function(response) {
|
||||
console.log(response); // Log the entire response
|
||||
|
||||
if (response.status === true) {
|
||||
// Reset the Set to avoid duplication when re-fetching
|
||||
si_amt_base_policy.clear();
|
||||
console.log("Data to be added:", response.data);
|
||||
|
||||
// Loop through the response.data array and add 'si' values to the Set
|
||||
for (let i = 0; i < response.data.length; i++) {
|
||||
si_amt_base_policy.add(response.data[i].si); // Add to Set, duplicates will be ignored
|
||||
}
|
||||
|
||||
// Convert Set back to an array to use in the dropdown
|
||||
si_amt_base_policy = Array.from(si_amt_base_policy);
|
||||
console.log("Unique SI amounts:", si_amt_base_policy); // Check the result
|
||||
|
||||
// Clear existing options in the dropdown
|
||||
// $('#choose_si_amount_from_base_policy').empty();
|
||||
$('#choose_si_amount_from_base_policy').append('<option value="">Select SI Amount</option>');
|
||||
|
||||
// Append the new options
|
||||
for (let i = 0; i < si_amt_base_policy.length; i++) {
|
||||
console.log("Appending option:", si_amt_base_policy[i]); // Log each SI value
|
||||
$('#choose_si_amount_from_base_policy').append(`<option value="${si_amt_base_policy[i]}">${si_amt_base_policy[i]}</option>`);
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("Error: Response status is false");
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.log("Error during AJAX request:", error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var si_amt_parent_policy = new Set();
|
||||
function fetchParentPolicy(client_policy_id, base_policy_id) {
|
||||
$.ajax({
|
||||
url: '<?= base_url("util/fetchPolicySIDataForParentPolicy")?>',
|
||||
method: 'GET',
|
||||
data: {
|
||||
client_policy_id: client_policy_id,
|
||||
client_base_policy_id: base_policy_id
|
||||
},
|
||||
success: function(response) {
|
||||
if (response.status === true) {
|
||||
// Reset the Set to avoid duplication when re-fetching
|
||||
si_amt_parent_policy.clear();
|
||||
console.log("Data to be added:", response.data);
|
||||
|
||||
// Loop through the response.data array and add 'si' values to the Set
|
||||
for (let i = 0; i < response.data.length; i++) {
|
||||
si_amt_parent_policy.add(response.data[i].si); // Add to Set, duplicates will be ignored
|
||||
}
|
||||
|
||||
// Convert Set back to an array to use in the dropdown
|
||||
si_amt_parent_policy = Array.from(si_amt_parent_policy);
|
||||
console.log("Unique SI amounts:", si_amt_parent_policy); // Check the result
|
||||
|
||||
// Clear existing options in the dropdown
|
||||
// $('#choose_si_amount_for_policy').empty();
|
||||
$('#choose_si_amount_for_policy').append('<option value="">Select SI Amount</option>');
|
||||
|
||||
// Append the new options
|
||||
for (let i = 0; i < si_amt_parent_policy.length; i++) {
|
||||
console.log("Appending option:", si_amt_parent_policy[i]); // Log each SI value
|
||||
$('#choose_si_amount_for_policy').append(`<option value="${si_amt_parent_policy[i]}">${si_amt_parent_policy[i]}</option>`);
|
||||
}
|
||||
|
||||
} else {
|
||||
console.log("Error: Response status is false");
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.log("Error during AJAX request:", error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
// Initialize select2 for the initial select field
|
||||
$("#choose_si_amount_for_policy").select2({
|
||||
placeholder: 'Select SI Amount'
|
||||
});
|
||||
$("textarea.select2-search__field").attr('rows', '1');
|
||||
$("textarea.select2-search__field").css('resize', 'none');
|
||||
|
||||
// Function to get all selected base policy values
|
||||
function getSelectedBasePolicies() {
|
||||
var selectedBasePolicies = [];
|
||||
$('.si-mapping-row').each(function() {
|
||||
var selectedBasePolicy = $(this).find('select[name="choose_si_amount_from_base_policy"]').val();
|
||||
if (selectedBasePolicy) {
|
||||
selectedBasePolicies.push(selectedBasePolicy);
|
||||
}
|
||||
});
|
||||
return selectedBasePolicies;
|
||||
}
|
||||
|
||||
function addRow() {
|
||||
// Get selected base policies from all previous rows
|
||||
var selectedBasePolicies = getSelectedBasePolicies();
|
||||
|
||||
// Check if si_amt_base_policy has been populated
|
||||
if (si_amt_base_policy.length === 0) {
|
||||
console.log("SI amounts not yet loaded.");
|
||||
return; // Prevent adding the row if the data isn't ready
|
||||
}
|
||||
|
||||
// Create a new row with the same structure as the existing row
|
||||
var newRow = $('<div class="form-row si-mapping-row">');
|
||||
|
||||
// Generate unique IDs for the new row's selects
|
||||
var uniqueIdForBasePolicy = 'choose_base_policy_' + Date.now();
|
||||
var uniqueIdForSIAmountForPolicy = 'choose_si_amount_for_policy_' + Date.now();
|
||||
|
||||
newRow.append(
|
||||
'<input name = "primary_key" type="hidden" id="'+uniqueIdForBasePolicy+'" value="">'+
|
||||
'<div class="form-group col-md-3">' +
|
||||
'<label>Base Policy SI Amount<span class="text-danger">*</span></label>' +
|
||||
'<select class="form-control" id="' + uniqueIdForBasePolicy + '" name="choose_si_amount_from_base_policy">' +
|
||||
'<option value="">Choose SI Amount</option>' +
|
||||
'</select>' +
|
||||
'</div>'
|
||||
);
|
||||
|
||||
newRow.append(
|
||||
'<div class="form-group col-md-3">' +
|
||||
'<label>Policy SI Amount</label><br />' +
|
||||
'<select class="form-control" id="' + uniqueIdForSIAmountForPolicy + '" name="choose_si_amount_for_policy[]" multiple>' +
|
||||
'</select>' +
|
||||
'</div>'
|
||||
);
|
||||
|
||||
newRow.append(
|
||||
'<div class="form-group">' +
|
||||
'<div class="btn-group" style="margin-top: 45px;">' +
|
||||
'<button type="button" class="btn btn-danger remove-row" style="margin-right: 5px;">x</button>' +
|
||||
'<button type="button" class="btn btn-success add-row">+</button>' +
|
||||
'</div>' +
|
||||
'</div>'
|
||||
);
|
||||
|
||||
// Append the new row to the container
|
||||
$('#si-mapping-container').append(newRow);
|
||||
|
||||
// Loop through and append SI amounts to the new row's 'choose_si_amount_from_base_policy' dropdown
|
||||
si_amt_base_policy.forEach(function(value) {
|
||||
newRow.find('select[name="choose_si_amount_from_base_policy"]').append('<option value="'+value+'">'+value+'</option>');
|
||||
});
|
||||
|
||||
// Remove selected base policies from the new row's 'choose_si_amount_from_base_policy' options
|
||||
newRow.find('select[name="choose_si_amount_from_base_policy"] option').each(function() {
|
||||
var value = $(this).val();
|
||||
if (selectedBasePolicies.includes(value)) {
|
||||
$(this).remove();
|
||||
}
|
||||
});
|
||||
|
||||
// Append the unique SI amounts to 'choose_si_amount_for_policy[]'
|
||||
si_amt_parent_policy.forEach(function(value) {
|
||||
newRow.find('select[name="choose_si_amount_for_policy[]"]').append('<option value="'+value+'">'+value+'</option>');
|
||||
});
|
||||
|
||||
// Re-initialize select2 for the new row's dropdowns
|
||||
newRow.find('select[name="choose_si_amount_for_policy[]"]').select2({
|
||||
placeholder: 'Select SI Amount'
|
||||
});
|
||||
|
||||
newRow.find('select[name="choose_si_amount_from_base_policy"]').select2({
|
||||
placeholder: 'Choose SI Amount'
|
||||
});
|
||||
|
||||
$("textarea.select2-search__field").attr('rows', '1');
|
||||
$("textarea.select2-search__field").css('resize', 'none');
|
||||
}
|
||||
|
||||
// Use event delegation to bind the click event for .add-row
|
||||
$(document).on('click', '.add-row', function() {
|
||||
addRow();
|
||||
});
|
||||
|
||||
// Remove an SI Mapping row
|
||||
$(document).on('click', '.remove-row', function() {
|
||||
var pk = $(this).closest('.si-mapping-row').find('input[name="primary_key"]').val();
|
||||
console.log('The primary key is ' + pk);
|
||||
|
||||
if (pk) {
|
||||
var delete_status = deleteROW(pk);
|
||||
if ($('.si-mapping-row').length > 1) {
|
||||
$(this).closest('.si-mapping-row').remove();
|
||||
|
||||
}
|
||||
} else {
|
||||
if ($('.si-mapping-row').length > 1) {
|
||||
$(this).closest('.si-mapping-row').remove();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
function deleteROW(pk){
|
||||
$.ajax({
|
||||
url: "<?= base_url('util/deleteMapping')?>",
|
||||
data:{
|
||||
pk:pk
|
||||
},
|
||||
method:'POST',
|
||||
success:function(response){
|
||||
console.log(response);
|
||||
toastr.success(response.message);
|
||||
// return true;
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.log('Mapping Deletion failed:', error);
|
||||
toastr.error("Mapping Deletion failed");
|
||||
// return false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function submitForm() {
|
||||
|
||||
|
||||
// Initialize formData as an object
|
||||
var formData = {
|
||||
client_policy_id: $('#client_policy_id_form').val(),
|
||||
client_base_policy_id: $('#base_policy_id_form').val(),
|
||||
si_mapping: [] // Array to hold all SI mapping rows
|
||||
};
|
||||
|
||||
// Iterate through each `.si-mapping-row`
|
||||
$('.si-mapping-row').each(function() {
|
||||
var basePolicyAmount = $(this).find('select[name="choose_si_amount_from_base_policy"]').val();
|
||||
var policyAmounts = $(this).find('select[name="choose_si_amount_for_policy[]"]').val(); // Multiple values
|
||||
var pk = $(this).find('input[name="primary_key"]').val(); // Get the `pk` value for this row
|
||||
|
||||
// Add the data for this row to the si_mapping array
|
||||
formData.si_mapping.push({
|
||||
pk: pk, // Include the pk value
|
||||
base_policy_si_amount: basePolicyAmount,
|
||||
policy_si_amounts: policyAmounts
|
||||
});
|
||||
});
|
||||
|
||||
// Debugging: Log the full formData object
|
||||
console.log('Form Data:', JSON.stringify(formData));
|
||||
$('.loader').fadeIn();
|
||||
$('.loader-mask').fadeIn();
|
||||
$('.loader').fadeOut();
|
||||
$('.loader-mask').delay(350).fadeOut('slow');
|
||||
|
||||
// Submit the form data using AJAX
|
||||
$.ajax({
|
||||
url: '<?= base_url("util/saveSIMapping") ?>',
|
||||
method: 'POST',
|
||||
data: formData,
|
||||
success: function(response) {
|
||||
if(response.status == true && response.code == 200){
|
||||
$('.loader').fadeOut();
|
||||
$('.loader-mask').delay(350).fadeOut('slow');
|
||||
toastr.success('SI Mapped Successfully');
|
||||
}
|
||||
else {
|
||||
var message = response.message;
|
||||
toastr.error(message);
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.log('Form submission failed:', error);
|
||||
toastr.error("Failed to Map SI");
|
||||
$('.loader').fadeOut();
|
||||
$('.loader-mask').delay(250).fadeOut('slow');
|
||||
}
|
||||
});
|
||||
}
|
||||
function resetFormAndRemoveRows() {
|
||||
// Reset the form fields
|
||||
$('#si_mapping_form')[0].reset(); // Reset all form fields
|
||||
|
||||
// Clear hidden input fields
|
||||
$('#client_policy_id_form').val('');
|
||||
$('#base_policy_id_form').val('');
|
||||
|
||||
// Remove all rows except the first one
|
||||
$('#si-mapping-container .si-mapping-row:not(:first)').remove();
|
||||
|
||||
// Optionally, reset the select options to default values if needed
|
||||
$('#choose_base_policy').val('').trigger('change');
|
||||
$('#choose_si_amount_from_base_policy').val('').trigger('change');
|
||||
$('#choose_si_amount_for_policy').val('').trigger('change');
|
||||
}
|
||||
|
||||
|
||||
</script>
|
||||
Loading…
Reference in New Issue
Block a user