From d6db9f3541e19557996131787b71096064d09e88 Mon Sep 17 00:00:00 2001 From: "venkatesh.r" Date: Tue, 18 Nov 2025 11:23:07 +0530 Subject: [PATCH] FEAT_CHECK_DUBLICAT --- app/Config/Routes.php | 1 + app/Controllers/RuleImportController.php | 15 +++ app/Views/commission_file_upload.php | 158 ++++++++++++++++------- 3 files changed, 129 insertions(+), 45 deletions(-) diff --git a/app/Config/Routes.php b/app/Config/Routes.php index f397284f..634252a7 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -783,5 +783,6 @@ $routes->group('commission', function($routes) { $routes->get('sample_file',"RuleImportController::downloadSampleCommissionFileUploadExcel"); $routes->get('downloadErrorFile',"RuleImportController::downloadErrorFile"); $routes->get("deleteCommissionData/(:any)", "RuleImportController::deleteCommissionData/$1"); + $routes->get('checkSameEntry',"RuleImportController::checkSameEntry"); }); diff --git a/app/Controllers/RuleImportController.php b/app/Controllers/RuleImportController.php index d181284e..678e59c6 100644 --- a/app/Controllers/RuleImportController.php +++ b/app/Controllers/RuleImportController.php @@ -102,6 +102,7 @@ class RuleImportController extends AdminController $insurerId = $this->request->getPost('insurer_id'); $department = $this->request->getPost('department'); $commissionMonth = $this->request->getPost('commission_month'); + $overwrite = $this->request->getPost('overwrite') ?? 0; $createdBy = get_session_userid(); if (empty($insurerId) || empty($department) || empty($commissionMonth)) { @@ -424,5 +425,19 @@ class RuleImportController extends AdminController return $this->respond(['status' => true, 'code' => 200, 'message' => "File removed successfully"], 200); } + public function checkSameEntry() + { + $data = $this->request->getGet(); + $commissionMonth = $data['commission_month'] . '-01'; + $data['commission_month'] = change_date_format($commissionMonth, 'Y-M-d', 'Y-m-d'); + + $count = $this->commissionFilesModel->where($data)->where('is_active', 1)->countAllResults(); + if($count > 0){ + return $this->respond(['status' => true, 'code' => 200, 'data' => $count, 'message' => ""], 200); + }else{ + return $this->respond(['status' => false, 'code' => 404, 'data' => $count, 'message' => ""], 200); + } + } + } diff --git a/app/Views/commission_file_upload.php b/app/Views/commission_file_upload.php index 72fc1ca3..29961e3c 100644 --- a/app/Views/commission_file_upload.php +++ b/app/Views/commission_file_upload.php @@ -217,7 +217,7 @@
-
- +
- + let isDublicateFound = false; + document.addEventListener("DOMContentLoaded", function() { const table = document.getElementById("tickets-table"); @@ -466,62 +468,91 @@ return; } - $('#btnSubmit').prop('disabled', true).text('Submitting...'); - // Create FormData object var formData = new FormData($(this)[0]); - $('.loader').fadeIn(); - $('.loader-mask').fadeIn(); + // FUNCTION to actually submit AJAX + function submitAjax(formData) { + $('#btnSubmit').prop('disabled', true).text('Submitting...'); + $('.loader').fadeIn(); + $('.loader-mask').fadeIn(); - $.ajax({ - url: $(this).attr("action"), - type: "POST", - data: formData, - processData: false, // Prevent jQuery from automatically processing the data - contentType: false, // Let jQuery handle the content type - headers: { - // "Content-Type":"multipart/form-data", - "X-Requested-With": "XMLHttpRequest" - }, - success: function(response) { + $.ajax({ + url: $('#commission_upload_form').attr("action"), + type: "POST", + data: formData, + processData: false, + contentType: false, + headers: { + "X-Requested-With": "XMLHttpRequest" + }, + success: function(response) { - console.log(response); - $('#commission_upload_form')[0].reset(); + console.log(response); + $('#commission_upload_form')[0].reset(); + + if (response.code === 200 && response.status === true) { + toastr.success('File upload success', 'SUCCESS'); + } else if (response.code === 404 && response.status === false) { + toastr.error(response.message, 'FAILED'); + } else { + toastr.error('Something went wrong! Try later', 'ERROR'); + } - if (response.code === 200 && response.status === true) { - toastr.success('File upload successs','SUCCESS'); $('.close').click(); - window.location.reload(true); - } else if (response.code === 404 && response.status === false) { - console.error('no data found', response); - toastr.error(response.message, 'FAILED'); - window.location.reload(true); - } else { - console.error('Something went wrong!'); + $('.loader').fadeOut(); + $('.loader-mask').delay(10).fadeOut('slow'); + + $('#btnSubmit').prop('disabled', false).text('Submit'); + + window.location.reload(true); + }, + error: function(xhr, status, error) { + console.error("Request failed:", status, error); toastr.error('Something went wrong! Try later', 'ERROR'); - window.location.reload(true); + + $('.close').click(); + $('.loader').fadeOut(); + $('.loader-mask').delay(10).fadeOut('slow'); + + $('#btnSubmit').prop('disabled', false).text('Submit'); + + window.location.reload(true); } - $('.close').click() - $('.loader').fadeOut(); - $('.loader-mask').delay(10).fadeOut('slow'); + }); + } - $('#btnSubmit').prop('disabled', false).text('Submit'); + // Duplicate check + if (isDublicateFound) { + Swal.fire({ + title: "Duplicate found. What do you want?", + showCancelButton: true, + confirmButtonText: "Overwrite", + cancelButtonText: "Append", + confirmButtonColor: "#ff3333", + }).then((result) => { - }, - error: function(xhr, status, error) { - // Request failed, handle error - console.error("Request failed:", status, error); - toastr.error('Something went wrong! Try later', 'Error'); - $('.close').click() - $('.loader').fadeOut(); - $('.loader-mask').delay(10).fadeOut('slow'); - window.location.reload(true); - } - }); + if (result.isConfirmed) { + formData.append('overwrite', 1); + } else { + formData.append('overwrite', 0); + } + + isDublicateFound = false; + + // 🔥 Continue with form submission AFTER append + submitAjax(formData); + }); + + return; + } + + // If no duplicate, submit normally + submitAjax(formData); }); + // open the file upload modal function showFileUploadModal(input) { var myModal = new bootstrap.Modal(document.getElementById('file_upload')); @@ -587,4 +618,41 @@ }); } + + // avoid dublicate entry + function checkSameEntry() + { + + let insurer_id = $('#insurer_id').val() + let commission_month = $('#commission_month').val() + let department = $('#department').val() + + let url = ''; + + // Data to send in the AJAX request + let requestData = { + insurer_id: insurer_id, + commission_month: commission_month, + department: department, + }; + + // Send AJAX request + sendAjaxRequestForGlobal(url, 'GET', requestData, function(response) { + + console.log('Data fetched successfully:', response); + + if (response.status == true) { + isDublicateFound = true; + }else{ + isDublicateFound = false; + } + + }, function(xhr, status, error) { + isDublicateFound = false; + console.error('Error fetching data:', error); + console.error(xhr.responseText); + console.log('An error occurred while fetching the data.', 'ERROR'); + }); + } +