nhance/app/Views/client_kyc.php
2026-07-15 15:49:13 +05:30

630 lines
28 KiB
PHP
Executable File

<style> .card-body{ margin-top: 0px !important; } </style>
<div class="tab-pane fade" id="KYC-DOC-tab">
<div class="col-lg-12">
<div class="card" id="collapseOne">
<div class="card-body">
<small class="text-danger"> * Allowed: PDF, JPG, JPEG, PNG. </small>
<div class="table-responsive">
<table data-custom-table-css="second-table" id="kyc_table" class="table table-sm mb-0">
<thead>
<tr>
<th>S. No</th>
<th>Document Name</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody id="tbody">
<?= isset($client_kyc_primary_table) ? $client_kyc_primary_table : "" ?>
</tbody>
</table>
</div> <!-- end table-responsive-->
<!-- <button class="btn btn-primary waves-effect waves-light btn-sm" id="btn_other_docs">Other Documents</button> -->
</div>
</div> <!-- end card -->
</div> <!-- end col -->
<div class="row" id="others">
<div class="col-12">
<div class="card" id="collapseOne" style="margin-left: 12px;margin-right: -12px;">
<div class="card-body">
<h3>Additional Documents</h3>
<form role="form" class="parsley-examples" method="post" id="kyc_form"
enctype="multipart/form-data">
<input type="hidden" class="form-control" value="<?= csrf_hash() ?>" name="<?= csrf_token() ?>" />
<input type="hidden" name="PrimaryKey" id="kyc_PrimaryKey" value="<?= isset($client['id']) ? $client['id'] : '' ?>"/>
<input type="hidden" name="client_id" id="client_id_kyc" value="<?= isset($client['id']) ? $client['id'] : '' ?>"/>
<input type="hidden" name="kyc_doc_type_id" value="" />
<div class="form-group">
<div id="additional_docs_rows">
<div class="form-row additional-doc-row" data-row-index="0">
<div class="form-group col-md-4">
<label>Document Name<span class="text-danger">*</span></label>
<input type="text" class="form-control other-doc-name-field" placeholder="Document Name" name="other_docs_name[]" data-parsley-inceptioncharset="true" data-parsley-trigger="input change" data-parsley-pattern="^[a-zA-Z0-9\\s_-]+$" data-parsley-pattern-message="Only letters, numbers, spaces, underscore (_) and hyphen (-) are allowed." required>
</div>
<div class="form-group col-md-4">
<label>File<span class="text-danger">*</span></label>
<input class="form-control other-doc-file-field" type="file" name="file_name[]" required accept=".pdf, .jpeg, .jpg, .png">
<small class="mt-1 text-muted d-block">Allowed: PDF, JPG, JPEG, PNG.</small>
<div class="other-doc-file-error text-danger small mt-1"></div>
</div>
<div class="form-group col-md-4" style="<?= isset($client['id']) ? 'margin-top: 41px;' : 'margin-top: 30px;' ?>">
<button type="button" class="btn btn-outline-danger remove-additional-doc-row" style="display:none;">Remove</button>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<button type="button" class="btn btn-outline-primary waves-effect waves-light mr-1" id="add_more_other_docs">Add More</button>
<button type="submit" class="btn btn-primary waves-effect waves-light mr-1" id="btnSubmit">Save</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div> <!-- end col-->
</div> <!-- end row -->
<div class="col-lg-12" id="other_docs_table">
<div class="card" id="collapseOne">
<div class="card-body">
<div class="table-responsive">
<table data-custom-table-css="second-table" id="kyc_table" class="table table-sm mb-0">
<thead>
<tr>
<th>Document Name</th>
<th>File Name</th>
<th>Action</th>
</tr>
</thead>
<tbody id="other_docs">
<?= isset($client_kyc_other_table) ? $client_kyc_other_table : "" ?>
</tbody>
</table>
</div> <!-- end table-responsive-->
</div>
</div> <!-- end card -->
</div> <!-- end col -->
</div>
<!-- end -->
<!-- <script src="<?= base_url('public/assets/js/pages/policy_transaction_inception_form_validation.js') ?>"></script> -->
<script>
var kycPrimaryKey = $('#client_id_kyc').val();
var allowedExtensions = ['pdf', 'png', 'jpeg', 'jpg'];
var maxImageSizeInBytes = 200 * 1024;
var maxPdfSizeInBytes = 25 * 1024 * 1024;
function resetOtherDocFileField($field) {
$field.val('');
}
function validateOtherDocFileField($field, showToastr) {
var $row = $field.closest('.additional-doc-row');
var $scope = $row.length ? $row : $field.closest('form');
var $error = $scope.find('.other-doc-file-error, .kyc-primary-file-error').first();
var file = $field[0].files && $field[0].files[0];
if ($error.length) {
$error.text('');
}
$field.removeClass('is-invalid');
if (!file) {
return true;
}
var fileName = file.name || '';
var fileExtension = fileName.split('.').pop().toLowerCase();
var sizeErrorMessage = '';
if (allowedExtensions.indexOf(fileExtension) === -1) {
var typeMessage = 'Only PDF, JPG, JPEG, PNG files are allowed.';
if ($error.length) {
$error.text(typeMessage);
}
$field.addClass('is-invalid');
resetOtherDocFileField($field);
if (showToastr) {
toastr.warning(typeMessage, 'Invalid file type');
}
return false;
}
if (fileExtension === 'pdf' && file.size > maxPdfSizeInBytes) {
sizeErrorMessage = 'PDF size should be up to 25MB.';
} else if (['png', 'jpg', 'jpeg'].indexOf(fileExtension) !== -1 && file.size > maxImageSizeInBytes) {
sizeErrorMessage = 'Image size should be up to 200KB.';
}
if (sizeErrorMessage) {
if ($error.length) {
$error.text(sizeErrorMessage);
}
$field.addClass('is-invalid');
resetOtherDocFileField($field);
if (showToastr) {
toastr.warning(sizeErrorMessage, 'Invalid file size');
}
return false;
}
return true;
}
function getAdditionalDocRowTemplate(index) {
return `
<div class="form-row additional-doc-row" data-row-index="${index}">
<div class="form-group col-md-4">
<label>Document Name<span class="text-danger">*</span></label>
<input type="text" class="form-control other-doc-name-field" placeholder="Document Name" name="other_docs_name[]" data-parsley-inceptioncharset="true" data-parsley-trigger="input change" data-parsley-pattern="^[a-zA-Z0-9\\s_-]+$" data-parsley-pattern-message="Only letters, numbers, spaces, underscore (_) and hyphen (-) are allowed." required>
</div>
<div class="form-group col-md-4">
<label>File<span class="text-danger">*</span></label>
<input class="form-control other-doc-file-field" type="file" name="file_name[]" required accept=".pdf, .jpeg, .jpg, .png">
<small class="mt-1 text-muted d-block">Allowed: PDF, JPG, JPEG, PNG.</small>
<div class="other-doc-file-error text-danger small mt-1"></div>
</div>
<div class="form-group col-md-4" style="<?= isset($client['id']) ? 'margin-top: 41px;' : 'margin-top: 30px;' ?>">
<button type="button" class="btn btn-outline-danger remove-additional-doc-row">Remove</button>
</div>
</div>
`;
}
$(document).ready(function(){
kycPrimaryKey = $('#kyc_PrimaryKey').val();
$(document).on('click', '.submit', function(e) {
e.preventDefault();
kycPrimaryKey = $('#id_for_kyc_file').val();
var form = $(this).closest('form');
var formData = new FormData(form[0]);
var fileInputs = form.find('input[type="file"]');
if (kycPrimaryKey === '') {
toastr.error('Client is required', 'Error');
form.trigger('reset')
return;
}
var isFormEmpty = true;
var hasInvalidFile = false;
fileInputs.each(function() {
var $currentFileInput = $(this);
var files = $currentFileInput[0].files;
if (files.length > 0) {
if (!validateOtherDocFileField($currentFileInput, true)) {
hasInvalidFile = true;
return false;
}
isFormEmpty = false;
}
});
if (hasInvalidFile) {
return;
}
if (isFormEmpty) {
form.find('.kyc-primary-file-error').text('Please select a file.');
fileInputs.addClass('is-invalid');
toastr.warning('Form is Empty', 'Warning');
return;
}
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
$.ajax({
url: '<?= base_url("client/kyc/create"); ?>',
type: 'POST',
data: formData,
processData: false,
contentType: false,
success: function (res) {
console.log('KYC PRIMARY SUBMIT', res);
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
if(res.status == true){
$('#tbody').empty();
$('#tbody').append(res.data);
toastr.success('File uploaded successfully', 'Success');
}else{
toastr.warning('File uploaded failed', 'Warning');
}
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
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');
}
}
});
});
$(document).on('click', '#add_more_other_docs', function () {
var nextIndex = $('#additional_docs_rows .additional-doc-row').length;
$('#additional_docs_rows').append(getAdditionalDocRowTemplate(nextIndex));
$('#additional_docs_rows .remove-additional-doc-row').show();
});
$(document).on('click', '.remove-additional-doc-row', function () {
$(this).closest('.additional-doc-row').remove();
if ($('#additional_docs_rows .additional-doc-row').length <= 1) {
$('#additional_docs_rows .remove-additional-doc-row').hide();
}
});
})
/*** for Others documents form submit ***/
$("#kyc_form").submit(function(event) {
event.preventDefault();
var isValid = $('#kyc_form').parsley().validate();
var form_action = '';
$('#kyc_form .other-doc-name-field, #kyc_form .other-doc-file-field').removeClass('is-invalid');
$('#kyc_form .other-doc-file-error').text('');
$('#additional_docs_rows .additional-doc-row').each(function() {
var docNameField = $(this).find('.other-doc-name-field');
var fileField = $(this).find('.other-doc-file-field');
var docNameValue = (docNameField.val() || '').trim();
var hasSelectedFile = !!(fileField[0].files[0]);
if (docNameValue === '' || !hasSelectedFile) {
isValid = false;
if (docNameValue === '') {
docNameField.addClass('is-invalid');
}
if (!hasSelectedFile) {
fileField.addClass('is-invalid');
}
return;
}
if (!validateOtherDocFileField(fileField, true)) {
isValid = false;
}
});
if (isValid) {
if(kycPrimaryKey === ''){
form_action = '<?= base_url("client/kyc/create"); ?>';
}else{
form_action = '<?= base_url("client/kyc/edit"); ?>';
}
var formData = new FormData($('#kyc_form')[0]);
formData.append("form_type", "others");
$('.loader').fadeIn();
$('.loader-mask').fadeIn();
$.ajax({
data: formData,
url: '<?= base_url("client/kyc/create"); ?>',
type: "POST",
dataType: 'json',
processData: false,
contentType: false,
success: function(res) {
console.log('KYC OTHERS SUBMIT', res);
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
if(res.status == true){
$('#other_docs').empty();
$('#other_docs').append(res.data);
toastr.success('File uploaded successfully', 'Success');
}else{
toastr.warning('File uploaded failed', 'Warning');
}
$('#kyc_form').trigger('reset');
$('#additional_docs_rows .additional-doc-row').not(':first').remove();
$('#additional_docs_rows .remove-additional-doc-row').hide();
},
error: function(xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
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');
}
}
});
} else {
toastr.warning('Please fill all Additional Document rows and upload valid files.', 'Warning');
}
});
$('body').on('click', '.btnKycDelete', function () {
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
var kyc_id = $(this).attr('data-id');
var client_id = $(this).attr('data-client-id') || $('#client_id_kyc').val();
$.get('<?php echo base_url('client/kyc/delete/');?>'+kyc_id, { client_id: client_id }, function (data) {
if(data && data.status){
if (data.data) {
$('#tbody').empty();
$('#tbody').append(data.data);
}
toastr.success('Document deleted successfully', 'Success');
}
})
}
});
});
$('body').on('click', '.btnKycOtherDelete', function () {
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: "Yes, delete it!"
}).then((result) => {
if (result.isConfirmed) {
var kyc_id = $(this).attr('data-id');
var client_id = $(this).attr('data-client-id') || $('#client_id_kyc').val();
$.get('<?php echo base_url('util/kyc-other-docs-delete/');?>'+kyc_id, { client_id: client_id }, function (data) {
if(data && data.status){
if (data.data) {
$('#other_docs').empty();
$('#other_docs').append(data.data);
} else {
$('#kyc-'+ kyc_id).remove();
}
toastr.success('Document deleted successfully', 'Success');
}
})
}
});
});
$(document).on('change', '.other-doc-file-field', function () {
validateOtherDocFileField($(this), true);
});
$(document).on('change', '.file-input__input', function () {
validateOtherDocFileField($(this), true);
});
function getKYCEntityDocument(entity_type_id, client_id){
$.ajax({
url: '<?= base_url("client/kyc/list/"); ?>' + entity_type_id,
type: "GET",
dataType: 'json',
processData: false,
contentType: false,
success: function (res) {
console.log("KYC DOCUMENTS TABLE RSPONSE", res);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
}, 1000);
var tbody = $('#tbody');
tbody.empty();
$.each(res.data, function(index, item) {
var row = `<tr>
<td>${index+1}</td>
<td>${item.file_name}</td>
<td id="form_${item.id}">
<form class="ajax">
<input class="file-input__input" type="file" name="file_name"
accept=".pdf,.jpg,.jpeg,.png">
<input type="hidden" class="form-control" value="<?= csrf_hash() ?>" name="<?= csrf_token() ?>">
<input type="hidden" class="form-control" value="${item.id}" name="kyc_doc_type_id">
<input type="hidden" name="client_id" value="${client_id}">
<button class="btn btn-sm btn-primary submit" style="position: relative; right:0px;">Submit</button>
<small class="mt-1 text-muted d-block">Allowed: PDF, JPG, JPEG, PNG.</small>
<div class="kyc-primary-file-error text-danger small mt-1"></div>
</form>
</td>
<td id="name_${item.id}" style="display:none;"></td>
<td>
<a id="download_${item.id}" data-id="${item.id}" class="mdi mdi-download" style="font-size:18px; display: none" download></a>
</td>
</tr>`;
tbody.append(row);
});
//<i id="delete_${item.id}" data-id="${item.id}" class="mdi mdi-delete btnKycDelete" style="font-size:18px; display: none"></i>
},
error: function (xhr, status, error) {
console.error(xhr.responseText);
console.error(status, error);
setTimeout(function() {
$('.loader').fadeOut();
$('.loader-mask').delay(350).fadeOut('slow');
toastr.warning('Something Wrong!', 'Warning');
}, 1000);
}
});
}
function appendKycTableListData(data){
// console.log('-----------------------------------------------------------')
// console.log('appendKycTableListData function called')
// console.log('-----------------------------------------------------------')
var table = '';
$('#other_docs tr').remove();
var other_docs_count = 0;
$.each(data, function(index, item) {
if(item.other_docs_name != ""){
other_docs_count++;
}
// var url = "<?= base_url('/downloadGdriveFile'); ?>" +
// "?client_id=" + item.client_id +
// "&file_type=kyc" +
// "&file_name=" + item.file_name +
// "&client_policy_id=client_policy_id";
var url = '<?= base_url('download-kyc-docs/') ?>' + item.id;
if(item.kyc_doc_type_id == '0'){
table += `
<tr id="kyc-${item.id}">
<td>${item.other_docs_name}</td>
<td>${item.file_name ? item.file_name.replace(/^\d+_[a-f0-9]+_/i, '') : ''}</td>
<td>
<a href = "${url}" data-id="${item.id}" class="mdi mdi-download" style="font-size:18px;" download></a>
</td>
</tr>
`;
}
// <i data-id="${item.id}" class="mdi mdi-delete btnKycOtherDelete" style="font-size:18px;"></i>
});
$('#other_docs').append(table);
if(other_docs_count > 0){
$('#others').show()
$('#other_docs_table').show();
}
$.each(data, function(index, item) {
// console.log(item.kyc_doc_type_id);
// console.log('name_' + item.kyc_doc_type_id);
// console.log(document.getElementById('name_' + item.kyc_doc_type_id));
setTimeout(function() {
$('#name_'+item.kyc_doc_type_id).show();
$('#name_'+item.kyc_doc_type_id).html(item.file_name ? item.file_name.replace(/^\d+_[a-f0-9]+_/i, '') : '');
$('#form_'+item.kyc_doc_type_id).hide();
// console.log('step 1')
// var url = "<?= base_url('/downloadGdriveFile'); ?>" +
// "?client_id=" + item.client_id +
// "&file_type=kyc" +
// "&file_name=" + item.file_name +
// "&client_policy_id=client_policy_id";
var url = '<?= base_url('download-kyc-docs/') ?>' + item.id;
if(item.file_name != null && item.file_name != ""){
// console.log('step 2')
$('#download_'+item.kyc_doc_type_id).show();
$('#download_' + item.kyc_doc_type_id).attr('href', url).removeAttr('target').attr('download', '');
$('#delete_'+item.kyc_doc_type_id).show();
}
else{
// console.log('step 3')
$('#download_'+item.kyc_doc_type_id).hide();
$('#download_' + item.kyc_doc_type_id).attr('href', '#');
$('#delete_'+item.kyc_doc_type_id).hide();
}
// console.log('step 4')
}, 1000);
});
}
</script>