340 lines
15 KiB
PHP
340 lines
15 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">
|
||
<div class="form-group">
|
||
|
||
<div class="form-row">
|
||
<div class="form-group col-md-12">
|
||
<label for="client_name">Client Name<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="client_name" name="client_name" onkeypress="return restrictCharacters(event, /[a-zA-Z0-9 ,\\/_-]/)" required>
|
||
</div>
|
||
<div class="form-group col-md-12">
|
||
<label for="short_name">Client Short name<span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" id="short_name" name="short_name" onkeyup="validateInputForClient(this, 'clients', 'short_name')" onkeypress="return restrictCharacters(event, /[a-zA-Z0-9,\\/_-]/)" 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')" onkeypress="return restrictCharacters(event, /[A-Z0-9]/)" oninput="this.value = this.value.toUpperCase()">
|
||
</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')" onkeypress="return onlyNumbers(event)">
|
||
</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">
|
||
<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" onkeypress="return restrictCharacters(event, /[a-zA-Z0-9 ,\\/_-]/)" 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" onkeypress="return restrictCharacters(event, /[a-zA-Z0-9,\\/_-]/)" 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" onkeypress="return restrictCharacters(event, /[a-zA-Z\s]/)" 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)" 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}$" onkeypress="return restrictCharacters(event, /[A-Z0-9]/)" oninput="this.value = this.value.toUpperCase()" 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 restrictCharacters(event, pattern) {
|
||
// Allow control keys (Backspace, Tab, Enter, etc.) which have charCode < 32
|
||
// or are navigation keys.
|
||
if (event.which < 32 || event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "Delete" || event.key === "Backspace" || event.key === "Tab") {
|
||
return true;
|
||
}
|
||
const char = String.fromCharCode(event.which);
|
||
if (!pattern.test(char)) {
|
||
event.preventDefault();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
function onlyNumbers(event) {
|
||
const charCode = (event.which) ? event.which : event.keyCode;
|
||
// Allow control keys
|
||
if (event.which < 32 || event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "Delete" || event.key === "Backspace" || event.key === "Tab") {
|
||
return true;
|
||
}
|
||
// Allow numbers 0-9
|
||
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
|
||
event.preventDefault();
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
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);
|
||
console.log("formData : ",formData);
|
||
// return
|
||
$.ajax({
|
||
data:formData,
|
||
url: form_action,
|
||
type: "POST",
|
||
dataType: 'json',
|
||
processData: false,
|
||
contentType: false,
|
||
success: function(res) {
|
||
|
||
$('.loader').fadeOut();
|
||
$('.loader-mask').delay(350).fadeOut('slow');
|
||
|
||
console.log('create_Client_With_Minimal_Data', res)
|
||
|
||
if(res.status == true){
|
||
getClientAndBranchAndPolicy(res.client_id, res.branch_id);
|
||
|
||
if(form_type != 1){
|
||
|
||
setTimeout(function(){
|
||
|
||
$('#client_id').val(res.client_id).change();
|
||
$('#client_id').prepend($('<option>', {
|
||
value: 'add_client',
|
||
text: '+ Add Client'
|
||
}));
|
||
|
||
setTimeout(function(){
|
||
|
||
$('#client_branch_id').val(res.branch_id).change();
|
||
$('.loader').fadeOut();
|
||
$('.loader-mask').delay(350).fadeOut('slow');
|
||
|
||
}, 1000)
|
||
|
||
}, 3000)
|
||
|
||
}else{
|
||
|
||
console.log("Couldn't Add Client","Error");
|
||
}
|
||
|
||
toastr.success(res.message, 'Success');
|
||
|
||
}else{
|
||
toastr.error(res.message, 'Error');
|
||
}
|
||
|
||
$('#client_form')[0].reset();
|
||
$('.close').click();
|
||
|
||
if(form_type == 1){
|
||
|
||
var myModal = new bootstrap.Modal(document.getElementById('vehicle_modal'));
|
||
myModal.show();
|
||
|
||
$('#client_form').removeAttr('data-id');
|
||
|
||
// Load form data into #vehicle_form from localStorage
|
||
setFormDataFromLocalStorage("#vehicle_form", "vehicleFormData", res.client_id);
|
||
|
||
setTimeout(function(){
|
||
$('#owner').val(res.client_id).change();
|
||
$('#owner_branch').val(res.branch_id).change();
|
||
|
||
$('.loader').fadeOut();
|
||
$('.loader-mask').delay(350).fadeOut('slow');
|
||
|
||
}, 2000)
|
||
}
|
||
|
||
isClientFormSubmitting = false;
|
||
},
|
||
error: function (xhr, status, error) {
|
||
console.error(xhr.responseText);
|
||
console.error(status, error);
|
||
isClientFormSubmitting = false;
|
||
$('.loader').fadeOut();
|
||
$('.loader-mask').delay(350).fadeOut('slow');
|
||
}
|
||
});
|
||
});
|
||
|
||
</script>
|