376 lines
17 KiB
PHP
376 lines
17 KiB
PHP
<!-- Client form content modal-->
|
||
<div class="modal fade" id="upload_enrollment_model" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" data-backdrop="static">
|
||
<div class="modal-dialog modal-dialog-centered">
|
||
<div class="modal-content">
|
||
<div class="modal-header">
|
||
<h4 class="modal-title" id="myCenterModalLabel">Add New Client</h4>
|
||
<button type="button" id="close_btn" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||
</div>
|
||
<div class="modal-body" style="overflow-y: auto;height: 90vh;">
|
||
<form class="parsley-examples" method="post" id="client_form" enctype="multipart/form-data">
|
||
<input type="hidden" name="from_modal" value="1">
|
||
<div class="form-group">
|
||
|
||
<div class="form-row">
|
||
<div class="form-group col-md-12">
|
||
<label for="client_name_for_modal">Client Name<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="client_name_for_modal" name="client_name" required>
|
||
</div>
|
||
<div class="form-group col-md-12">
|
||
<label for="client_short_name_for_modal">Client Short name<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="client_short_name_for_modal" name="short_name" onkeyup="validateInputForClient(this, 'clients', 'short_name')" required>
|
||
</div>
|
||
|
||
<div class="form-group col-md-12">
|
||
<label for="cost_center">PAN</label>
|
||
<input type="text" class="form-control" id="pan" placeholder="Enter PAN No" name="pan" onchange="validateInputForClient(this, 'clients', 'pan')">
|
||
</div>
|
||
</div>
|
||
|
||
<hr>
|
||
|
||
<!-- Client Type -->
|
||
<div class="form-row clienttypediv" style="display: none;">
|
||
|
||
<!-- <div class="form-group col-md-6">
|
||
<label for="dob">Date of Birth<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="dob" placeholder="DD/MM/YYYY" name="dob" >
|
||
</div>
|
||
|
||
<div class="form-group col-md-6">
|
||
<label for="cost_center">Mobile<span class="text-danger"></span></label>
|
||
<input type="text" class="form-control" id="phone" placeholder="Enter Mobile Number" maxlength="10" name="phone"
|
||
onkeypress="return onlyNumbers(event)" onchange="validateInputForClient(this, 'clients', 'phone')">
|
||
</div> -->
|
||
|
||
<!-- <div class="form-group col-md-12">
|
||
<label for="email2">Email<span class="text-danger">*</span></label>
|
||
<input type="email" class="form-control" id="email2" placeholder="Enter Email" name="email2">
|
||
</div>
|
||
<div class="form-group col-md-12">
|
||
<label for="aadhar">Aadhar</label>
|
||
<input type="text" class="form-control" id="aadhar" placeholder="Enter Aadher No" name="aadhar" maxlength="12" onchange="validateInputForClient(this, 'clients', 'aadhar')">
|
||
</div> -->
|
||
|
||
</div>
|
||
|
||
<!-- branch -->
|
||
<div class="form-row branchdiv">
|
||
|
||
<div class="form-group col-md-12">
|
||
<label for="short_name">Entity Type<span class="text-danger">*</span></label>
|
||
<select class="form-control" id="entity_type_id_for_client" name="entity_type_id" required>
|
||
<option value="" selected>Select Entity</option>
|
||
<?php
|
||
if (isset($entity) && count($entity)) {
|
||
foreach ($entity as $key => $value) {
|
||
echo "<option value=" . $value['id'] . ">" . $value['name'] . "</option>";
|
||
}
|
||
}
|
||
?>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="form-group col-md-6">
|
||
<label for="client_name">Branch Name<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="branch_name" name="branch_name" required>
|
||
</div>
|
||
|
||
<div class="form-group col-md-6">
|
||
<label for="branch_code">Branch Code<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="branch_code" name="branch_code" required>
|
||
</div>
|
||
|
||
<div class="form-group col-md-6">
|
||
<label for="branch_code">Name<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="name" name="name" required>
|
||
</div>
|
||
|
||
<div class="form-group col-md-6">
|
||
<label for="branch_code">Mobile<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="mobile" maxlength="10" name="mobile" onkeypress="return onlyNumbers(event)" oninput="this.value = this.value.replace(/[^0-9]/g, '');" required>
|
||
</div>
|
||
<div class="form-group col-md-12">
|
||
<label for="email">Email<span class="text-danger">*</span></label>
|
||
<input type="email" class="form-control" id="email" placeholder="Enter Email" name="email" required>
|
||
</div>
|
||
|
||
<div class="form-group col-md-12">
|
||
<label for="gst">GST<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="gst" placeholder="Enter GST Number" oninput="validateInputForClient(this, 'client_branch', 'gst')"
|
||
data-parsley-error-message="Invalid GST Number. Example: 12ABCDE1234F5Z6" name="gst" data-parsley-trigger="change" data-parsley-pattern="^\d{2}[A-Z]{5}\d{4}[A-Z]{1}[A-Z\d]{1}[Z]{1}[A-Z\d]{1}$" required>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
<div class="form-group text-right m-b-0" id="hide_smbt_btn">
|
||
<button type="submit" id="client_add_modal_submit_btn" class="btn btn-primary waves-effect waves-light mr-1 your">Submit</button>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div><!-- /.modal-content -->
|
||
</div><!-- /.modal-dialog -->
|
||
</div>
|
||
|
||
<script>
|
||
|
||
let isGSTValidating = false;
|
||
let isGSTValid = false;
|
||
|
||
function validateInputForClient(input, table, field) {
|
||
|
||
let owner_type = $('#Owner_type').val();
|
||
let client_type = $('#client_type').val();
|
||
|
||
console.log('owner_type', owner_type);
|
||
console.log('client_type', client_type);
|
||
|
||
if((client_type == 2 || owner_type == 2) && field == 'short_name'){
|
||
return;
|
||
}
|
||
|
||
let value = $(input).val();
|
||
if (!value) return;
|
||
|
||
let label = $(input).closest('.form-group').find('label').text().replace('*', '').trim();
|
||
let message = label ? label + " already exists!" : "Value is duplicate!";
|
||
|
||
// Set validating state
|
||
isGSTValidating = true;
|
||
isGSTValid = false;
|
||
$('#hide_smbt_btn button').prop('disabled', true);
|
||
|
||
checkDuplicateTableFieldValue(table, field, value, function(isDuplicate) {
|
||
isGSTValidating = false;
|
||
if (isDuplicate) {
|
||
toastr.warning(message, 'Warning');
|
||
// $(input).val('');
|
||
isGSTValid = false;
|
||
$('#client_add_modal_submit_btn').prop('disabled', true);
|
||
} else {
|
||
isGSTValid = true;
|
||
$('#client_add_modal_submit_btn').prop('disabled', false);
|
||
}
|
||
});
|
||
|
||
$('#hide_smbt_btn button').prop('disabled', false);
|
||
}
|
||
|
||
$("#client_form").submit(function(event) {
|
||
|
||
event.preventDefault();
|
||
|
||
// If GST is currently validating, wait for it to complete
|
||
if (isGSTValidating) {
|
||
if($('#Owner_type').val() == 1){
|
||
toastr.info('Please wait while we validate your GST number...', 'Validating');
|
||
return;
|
||
}else if ($('#client_type').val() == 1){
|
||
toastr.info('Please wait while we validate your GST number...', 'Validating');
|
||
return;
|
||
}
|
||
}
|
||
|
||
// If GST validation failed, prevent submission
|
||
if ($('#gst').val() && !isGSTValid) {
|
||
toastr.warning('Please fix the GST number before submitting', 'Validation Error');
|
||
return;
|
||
}
|
||
|
||
let form_type = $(this).data('id') || 0;
|
||
|
||
console.log('onsubmit event', this);
|
||
console.log('onsubmit event get data value',form_type);
|
||
console.log('client_type', $('#client_type').val());
|
||
|
||
event.preventDefault();
|
||
|
||
|
||
$('#aadhar').attr('data-parsley-required', 'false');
|
||
$('#aadhar').removeAttr('required');
|
||
|
||
if($('#Owner_type').val() == 2){
|
||
$('#gst').removeAttr('required');
|
||
}
|
||
|
||
var isValid = $('#client_form').parsley().validate();
|
||
|
||
if (!isValid) {
|
||
var emptyFieldIds = [];
|
||
|
||
$('#client_form').parsley().fields.forEach(function(field) {
|
||
if (!field.isValid()) {
|
||
var elem = field.$element;
|
||
var id = elem.attr('id') || elem.attr('name');
|
||
emptyFieldIds.push(id);
|
||
}
|
||
});
|
||
|
||
console.log('Empty/Invalid Fields:', emptyFieldIds);
|
||
return;
|
||
}
|
||
|
||
isClientFormSubmitting = true;
|
||
|
||
form_action = '<?= base_url("util/createClientWithMinimalData"); ?>';
|
||
|
||
$('.loader').fadeIn();
|
||
$('.loader-mask').fadeIn();
|
||
|
||
var client_type = $('#client_type').val() || $('#Owner_type').val() || 1;
|
||
console.log('client_type:', client_type);
|
||
|
||
//FORM Data
|
||
var formData = new FormData($('#client_form')[0]);
|
||
formData.append('client_type', client_type);
|
||
|
||
// return
|
||
$.ajax({
|
||
data:formData,
|
||
url: form_action,
|
||
type: "POST",
|
||
dataType: 'json',
|
||
processData: false,
|
||
contentType: false,
|
||
success: function(res) {
|
||
|
||
console.log('create_Client_With_Minimal_Data', res)
|
||
|
||
if(res.status == true){
|
||
|
||
getClientAndBranchAndPolicy(false);
|
||
|
||
appendClients(res.clients, res.client_id);
|
||
appendBranch(res.branches, res.branch_id);
|
||
|
||
$('#client_name').val($('#client_name_for_modal').val());
|
||
$('#client_short_name').val($('#client_short_name_for_modal').val());
|
||
$('#contact_person_name').val(res.data.name);
|
||
|
||
$('#gst').val(res.data.gst);
|
||
$('#branch_name').val(res.data.branch_name);
|
||
$('#branch_code').val(res.data.branch_code);
|
||
$('#contact_person_mobile').val(res.data.mobile);
|
||
$('#contact_person_email').val(res.data.email);
|
||
|
||
$('#contact_person_summary').empty();
|
||
$('#contact_person_summary').append($('<option>', {
|
||
value: "",
|
||
text: "Select a Contact"
|
||
}));
|
||
|
||
if (res.data.name && res.data.email) {
|
||
|
||
// let display_value = `${res.data.name} - ${res.data.email} - ${res.data.mobile}`;
|
||
let display_value = res.data.name;
|
||
|
||
$('#contact_person_summary').append($('<option>', {
|
||
value: 0, // index or key in array/object
|
||
text: display_value,
|
||
selected: true
|
||
}));
|
||
|
||
}
|
||
|
||
toastr.success(res.message, 'Success');
|
||
|
||
}else{
|
||
toastr.error(res.message, 'Error');
|
||
}
|
||
|
||
$('#client_form')[0].reset();
|
||
$('.close').click();
|
||
|
||
$('.loader').fadeOut();
|
||
$('.loader-mask').delay(350).fadeOut('slow');
|
||
|
||
isClientFormSubmitting = false;
|
||
},
|
||
error: function (xhr, status, error) {
|
||
console.error(xhr.responseText);
|
||
console.error(status, error);
|
||
setTimeout(function() {
|
||
$('.loader').fadeOut();
|
||
$('.loader-mask').delay(350).fadeOut('slow');
|
||
// $('#client_add_modal_submit_btn').prop('disabled', false);
|
||
if (xhr.status === 400) {
|
||
let response = JSON.parse(xhr.responseText);
|
||
let errorMessages = "";
|
||
let seenMessages = []; // Array to store unique messages
|
||
if (response.errors) {
|
||
$.each(response.errors, function (field, message) {
|
||
if (!seenMessages.includes(message)) {
|
||
errorMessages += `• ${message}<br>`;
|
||
seenMessages.push(message); // Mark this message as "seen"
|
||
}
|
||
});
|
||
toastr.error(errorMessages, 'Validation Error', { "allowHtml": true });
|
||
} else {
|
||
toastr.warning(response.message || 'Validation failed', 'Warning');
|
||
}
|
||
} else if (xhr.status === 403) {
|
||
let response = JSON.parse(xhr.responseText);
|
||
toastr.error(response.message, 'Security Policy');
|
||
} else if (xhr.status === 404) {
|
||
toastr.warning('Resource not found', 'Warning');
|
||
} else if (xhr.status === 500) {
|
||
toastr.error('Something went wrong . Please try again later.', 'Server Error');
|
||
} else {
|
||
toastr.error('An unexpected error occurred. Please try again later.', 'Error');
|
||
}
|
||
}, 1000);
|
||
}
|
||
});
|
||
});
|
||
|
||
$('#client_name_for_modal').on('input', function() {
|
||
clearTimeout(shortNameTimer);
|
||
shortNameTimer = setTimeout(generateShortNameModal, 200);
|
||
});
|
||
|
||
function generateShortNameModal() {
|
||
let clientInput = $("#client_name_for_modal");
|
||
let shortInput = $("#client_short_name_for_modal");
|
||
let newClientName = clientInput.val().trim();
|
||
|
||
console.log(`LN : NEW - ${newClientName}`);
|
||
if (newClientName.length == 0) {
|
||
shortInput.val('');
|
||
return;
|
||
}
|
||
|
||
let shortName = newClientName.substring(0, 10).replace(/\s+/g, '').toUpperCase();
|
||
console.log(`LN : NEW - ${shortName}`);
|
||
makeUniqueShortNameModal(shortName);
|
||
}
|
||
|
||
function makeUniqueShortNameModal(baseName) {
|
||
let input = $("#client_short_name_for_modal")[0];
|
||
|
||
checkDuplicateTableFieldValue("clients", "short_name", baseName, function(isDuplicate) {
|
||
if (isDuplicate) {
|
||
let counter = 1;
|
||
function tryNext() {
|
||
let padded = String(counter).padStart(3, '0'); // 001, 002, 003
|
||
let newName = baseName + padded;
|
||
|
||
checkDuplicateTableFieldValue("clients", "short_name", newName, function(exists) {
|
||
if (exists) {
|
||
counter++;
|
||
tryNext();
|
||
} else {
|
||
$("#client_short_name_for_modal").val(newName);
|
||
validateInput(input, "clients", "short_name");
|
||
}
|
||
});
|
||
}
|
||
tryNext();
|
||
} else {
|
||
$("#client_short_name_for_modal").val(baseName);
|
||
validateInput(input, "clients", "short_name");
|
||
}
|
||
});
|
||
}
|
||
|
||
</script>
|