FIX_POS_SAVING_ISSUE

This commit is contained in:
VENKATESHWARAN 2026-04-22 11:31:38 +05:30
parent 553ba9069b
commit 0fd039d3bd
2 changed files with 306 additions and 188 deletions

View File

@ -3540,6 +3540,7 @@ class MasterController extends AdminController
$method = $this->request->getMethod();
if ($this->request->getMethod() === 'post') {
try {
$rules = [
'manager_id' => [
@ -3632,25 +3633,22 @@ class MasterController extends AdminController
]
],
'aadhar_file_name' => [
'rules' => 'permit_empty|uploaded[aadhar_file_name]|max_size[aadhar_file_name,5120]|ext_in[aadhar_file_name,pdf,jpg,jpeg,png]',
'rules' => 'if_exist|max_size[aadhar_file_name,5120]|ext_in[aadhar_file_name,pdf,jpg,jpeg,png]',
'errors' => [
'uploaded' => 'Invalid Aadhaar file',
'max_size' => 'Aadhaar file size should not exceed 5MB',
'ext_in' => 'Aadhaar must be PDF or image (jpg, jpeg, png)'
]
],
'pan_file_name' => [
'rules' => 'permit_empty|uploaded[pan_file_name]|max_size[pan_file_name,5120]|ext_in[pan_file_name,pdf,jpg,jpeg,png]',
'rules' => 'if_exist|max_size[pan_file_name,5120]|ext_in[pan_file_name,pdf,jpg,jpeg,png]',
'errors' => [
'uploaded' => 'Invalid PAN file',
'max_size' => 'PAN file size should not exceed 5MB',
'ext_in' => 'PAN must be PDF or image (jpg, jpeg, png)'
]
],
'certificate_file_name' => [
'rules' => 'permit_empty|uploaded[certificate_file_name]|max_size[certificate_file_name,5120]|ext_in[certificate_file_name,pdf,jpg,jpeg,png]',
'rules' => 'if_exist|max_size[certificate_file_name,5120]|ext_in[certificate_file_name,pdf,jpg,jpeg,png]',
'errors' => [
'uploaded' => 'Invalid Certificate file',
'max_size' => 'Certificate file size should not exceed 5MB',
'ext_in' => 'Certificate must be PDF or image (jpg, jpeg, png)'
]
@ -3674,17 +3672,29 @@ class MasterController extends AdminController
try {
// Certificate
$certificate = $this->uploadPOSFile('certificate_file_name', 'pos_certificate_files');
if ($certificate !== null) { $sanitized_post_data['certificate_file_name'] = $certificate; } else { unset($sanitized_post_data['certificate_file_name']); }
if ($certificate !== null) {
$sanitized_post_data['certificate_file_name'] = $certificate;
} else {
unset($sanitized_post_data['certificate_file_name']);
}
// PAN file
$panFile = $this->uploadPOSFile('pan_file_name', 'pos_certificate_files');
if ($panFile !== null) { $sanitized_post_data['pan_file_name'] = $panFile; } else { unset($sanitized_post_data['pan_file_name']);}
if ($panFile !== null) {
$sanitized_post_data['pan_file_name'] = $panFile;
} else {
unset($sanitized_post_data['pan_file_name']);
}
// Aadhaar file
$aadharFile = $this->uploadPOSFile('aadhar_file_name', 'pos_certificate_files');
if ($aadharFile !== null) { $sanitized_post_data['aadhar_file_name'] = $aadharFile; } else { unset($sanitized_post_data['aadhar_file_name']);}
} catch (\RuntimeException $e) {
if ($aadharFile !== null) {
$sanitized_post_data['aadhar_file_name'] = $aadharFile;
} else {
unset($sanitized_post_data['aadhar_file_name']);
}
} catch (\Exception $e) {
log_message('error', 'File upload error: ' . $e->getMessage() . ' | POST data: ' . json_encode($sanitized_post_data) . $e->getTraceAsString());
return $this->respond(['status' => false, 'code' => 400, 'message' => $e->getMessage(), 'data' => $sanitized_post_data], 400);
}
@ -3696,6 +3706,7 @@ class MasterController extends AdminController
// INSERT / UPDATE
try {
if (empty($id)) {
$status = $posModel->insert($sanitized_post_data);
} else {
@ -3706,9 +3717,52 @@ class MasterController extends AdminController
return $this->respond(['status' => true, 'code' => 200, 'message' => 'POS updated successfully', 'data' => $sanitized_post_data], 200);
}
return $this->respond([ 'status' => false, 'code' => 400, 'message' => 'Failed to update', 'data' => $sanitized_post_data ], 400);
$modelErrors = method_exists($posModel, 'errors') ? $posModel->errors() : [];
return $this->respond([
'status' => false,
'code' => 400,
'message' => 'Failed to update',
'errors' => $modelErrors,
'data' => $sanitized_post_data
], 400);
} catch (\Throwable $e) {
log_message('error', 'partnerPOS DB error: ' . $e->getMessage() . ' | payload: ' . json_encode($sanitized_post_data) . ' | trace: ' . $e->getTraceAsString());
$message = 'Something went wrong while saving POS details.';
$statusCode = 500;
if (stripos($e->getMessage(), 'Duplicate entry') !== false) {
$statusCode = 400;
if (stripos($e->getMessage(), 'email') !== false) {
$message = 'Email already exists.';
} elseif (stripos($e->getMessage(), 'mobile') !== false) {
$message = 'Mobile number already exists.';
} else {
$message = 'Duplicate data found. Please check email/mobile.';
}
elseif ($method === 'get') {
}
return $this->respond([
'status' => false,
'code' => $statusCode,
'message' => $message
], $statusCode);
}
} catch (\Throwable $e) {
log_message(
'error',
'partnerPOS fatal error: ' . $e->getMessage() .
' | file: ' . $e->getFile() .
' | line: ' . $e->getLine() .
' | trace: ' . $e->getTraceAsString()
);
return $this->respond([
'status' => false,
'code' => 500,
'message' => 'Internal server error while processing POS request.'
], 500);
}
} elseif ($method === 'get') {
$id = $this->request->getGet('pk') ?? null;
@ -3747,7 +3801,6 @@ class MasterController extends AdminController
return $this->loadLayout('pos_list', ['data' => $data, 'tab_name' => 'POS details', 'page_name' => 'POS details']);
} elseif ($method === 'delete') {
// $input = $this->request->getRawInput();
@ -3786,15 +3839,39 @@ class MasterController extends AdminController
{
$file = $this->request->getFile($fieldName);
if (!$file || !$file->isValid()) { return null; }
if (!$file) {
return null;
}
$allowedMime = [ 'image/jpg', 'image/jpeg', 'image/png', 'image/webp', 'application/pdf'];
// No file selected for this optional input.
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return null;
}
if (!in_array($file->getMimeType(), $allowedMime)) { throw new \RuntimeException('Invalid file format'); }
if (!$file->isValid()) {
throw new \RuntimeException('File upload failed for ' . $fieldName);
}
$allowedMime = [
'image/jpg',
'image/jpeg',
'image/pjpeg',
'image/png',
'image/x-png',
'image/webp',
'application/pdf'
];
$mimeType = (string) $file->getMimeType();
if (!in_array($mimeType, $allowedMime, true)) {
throw new \RuntimeException('Invalid file format for ' . $fieldName);
}
$path = ROOTPATH . 'public/uploads/' . $uploadDir . '/';
if (!is_dir($path)) { mkdir($path, 0755, true); }
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
$newName = $file->getRandomName();
$file->move($path, $newName);

View File

@ -233,6 +233,7 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
<input type="file" class="form-control" name="aadhar_file_name" id="aadhar_file_name" accept=".pdf,.jpg,.jpeg,.png" data-parsley-file-validation>
<i class="mdi mdi-upload additional-icon"></i>
</div>
<small id="aadhar_file_name_display" class="text-muted d-block" style="display:none !important;"></small>
<small class="text-muted d-block">Allowed: PDF, PNG, JPG, JPEG.</small>
</div>
<div class="form-group col-md-4">
@ -241,6 +242,7 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
<input type="file" class="form-control" name="pan_file_name" id="pan_file_name" accept=".pdf,.jpg,.jpeg,.png" data-parsley-file-validation>
<i class="mdi mdi-upload additional-icon"></i>
</div>
<small id="pan_file_name_display" class="text-muted d-block" style="display:none !important;"></small>
<small class="text-muted d-block">Allowed: PDF, PNG, JPG, JPEG.</small>
</div>
<div class="form-group col-md-4">
@ -249,6 +251,7 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
<input type="file" class="form-control" name="certificate_file_name" id="certificate_file_name" accept=".pdf,.jpg,.jpeg,.png" data-parsley-file-validation>
<i class="mdi mdi-upload additional-icon"></i>
</div>
<small id="certificate_file_name_display" class="text-muted d-block" style="display:none !important;"></small>
<small class="text-muted d-block">Allowed: PDF, PNG, JPG, JPEG.</small>
</div>
</div>
@ -433,11 +436,17 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
},
error: function (xhr) {
$('.loader, .loader-mask').fadeOut();
let response = null;
try {
response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
} catch (e) {
response = null;
}
if (xhr.status === 400) {
let response = JSON.parse(xhr.responseText);
let errorMessages = "";
let seenMessages = []; // Array to store unique messages
if (response.errors) {
if (response && response.errors) {
$.each(response.errors, function (field, message) {
if (!seenMessages.includes(message)) {
errorMessages += `• ${message}<br>`;
@ -446,16 +455,15 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
});
toastr.error(errorMessages, 'Validation Error', { "allowHtml": true });
} else {
toastr.warning(response.message || 'Validation failed', 'Warning');
toastr.warning((response && response.message) ? response.message : 'Validation failed', 'Warning');
}
} else if (xhr.status === 403) {
let response = JSON.parse(xhr.responseText);
toastr.error(response.message, 'Security Policy');
toastr.error((response && response.message) ? response.message : 'Security policy error', 'Security Policy');
} else if (xhr.status === 500) {
toastr.error('Something went wrong . Please try again later.', 'Server Error');
toastr.error((response && response.message) ? response.message : 'Something went wrong . Please try again later.', 'Server Error');
} else {
toastr.error('An unexpected error occurred. Please try again later.', 'Error');
toastr.error((response && response.message) ? response.message : 'An unexpected error occurred. Please try again later.', 'Error');
}
console.error('Unexpected error:', xhr.responseText);
@ -470,9 +478,32 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
$('#partnerPOSForm').parsley().reset();
}
$('#manager_id').val(null).trigger('change');
updateFileNameDisplay('#aadhar_file_name', '#aadhar_file_name_display', '');
updateFileNameDisplay('#pan_file_name', '#pan_file_name_display', '');
updateFileNameDisplay('#certificate_file_name', '#certificate_file_name_display', '');
}
function updateFileNameDisplay(inputSelector, displaySelector, existingName = '') {
const $display = $(displaySelector);
let fileName = '';
if (existingName && String(existingName).trim() !== '') {
fileName = String(existingName).trim();
} else {
const inputEl = $(inputSelector)[0];
if (inputEl && inputEl.files && inputEl.files.length > 0) {
fileName = inputEl.files[0].name || '';
}
}
if (fileName) {
$display.text('Uploaded: ' + fileName).show();
} else {
$display.text('').hide();
}
}
function appendEditData(data){
$('#pos_id').val(data.id);
@ -481,14 +512,12 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
$('#mobile').val(data.mobile);
$('#pos_code').val(data.pos_code);
// Instead of setting the file input
$('#current_file_name').text(data['certificate_file_name'] || 'No file uploaded');
$('#aadhar').val(data.aadhar);
$('#aadhar_file_name').text(data['aadhar_file_name'] || 'No file uploaded');
updateFileNameDisplay('#aadhar_file_name', '#aadhar_file_name_display', data['aadhar_file_name'] || '');
$('#pan').val(data.pan);
$('#pan_file_name').text(data['pan_file_name'] || 'No file uploaded');
updateFileNameDisplay('#pan_file_name', '#pan_file_name_display', data['pan_file_name'] || '');
updateFileNameDisplay('#certificate_file_name', '#certificate_file_name_display', data['certificate_file_name'] || '');
$('#bank_name').val(data.bank_name);
$('#account_holder_name').val(data.account_holder_name);
@ -508,6 +537,18 @@ $pos_col_width_px = nhance_dt_column_widths_px($pos_header_labels, $pos_col_max_
});
}
$('#aadhar_file_name').on('change', function () {
updateFileNameDisplay('#aadhar_file_name', '#aadhar_file_name_display');
});
$('#pan_file_name').on('change', function () {
updateFileNameDisplay('#pan_file_name', '#pan_file_name_display');
});
$('#certificate_file_name').on('change', function () {
updateFileNameDisplay('#certificate_file_name', '#certificate_file_name_display');
});
$('#aadhar').on('input', function () {
this.value = this.value.replace(/[^0-9]/g, '');