CHANGE_DELETE_FILE_JSON

This commit is contained in:
VENKATESHWARAN 2025-11-18 18:40:33 +05:30
parent 8099ef3373
commit 7b56067adc
2 changed files with 142 additions and 69 deletions

View File

@ -77,7 +77,7 @@ class RuleImportController extends AdminController
return $this->response->setJSON($result); return $this->response->setJSON($result);
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->myLogger->logme('critical', 'RuleImportController::upload ' . $e->getMessage()); $this->myLogger->logme('error', 'RuleImportController::upload ' . $e->getMessage());
return $this->response->setJSON(['status'=>false,'message'=>$e->getMessage()]); return $this->response->setJSON(['status'=>false,'message'=>$e->getMessage()]);
} }
} }
@ -417,7 +417,7 @@ class RuleImportController extends AdminController
'message' => $result['message'] 'message' => $result['message']
], 200); ], 200);
} catch (\Throwable $ex) { } catch (\Throwable $ex) {
$this->myLogger->logme('critical', 'RuleImportController::upload exception: ' . $ex->getMessage(), [ $this->myLogger->logme('error', 'RuleImportController::upload exception: ' . $ex->getMessage(), [
'trace' => $ex->getTraceAsString() 'trace' => $ex->getTraceAsString()
]); ]);
@ -486,10 +486,82 @@ class RuleImportController extends AdminController
public function deleteCommissionData($id) public function deleteCommissionData($id)
{ {
$this->commissionFilesModel->where('id', $id)
->set(['is_active' => 0]) $return = $this->updateCommissionRules($id);
->update();
if($return['status'] == true){
$this->commissionFilesModel->where('id', $id)->set(['is_active' => 0])->update();
return $this->respond(['status' => true, 'code' => 200, 'message' => "File removed successfully"], 200); return $this->respond(['status' => true, 'code' => 200, 'message' => "File removed successfully"], 200);
}else{
return $this->respond(['status' => false, 'code' => 400, 'message' => "Failed to remove file"], 200);
}
}
public function updateCommissionRules($id)
{
// 1. Fetch commission record
$commission_data = $this->commissionFilesModel
->where('is_active', 1)
->where('id', $id)
->first();
if (!$commission_data) {
$this->myLogger->logme("error","Commission record not found for ID: $id");
return ['status' => false, 'message' => 'Commission record not found'];
}
// 2. Convert commission_month → OCT2025
$month = date("M", strtotime($commission_data['commission_month']));
$year = date("Y", strtotime($commission_data['commission_month']));
$monthFolder = strtoupper($month . $year); // OCT2025
// 3. Build file name & path
$fileName = $commission_data['insurer_id'] . '_' . $commission_data['department'] . '.json';
$filePath = WRITEPATH . "uploads/commission/rules/" . $monthFolder . "/" . $fileName;
if (!file_exists($filePath)) {
$this->myLogger->logme("error","Rule file not found: $filePath");
return ['status' => false, 'message' => 'Rule file not found'];
}
// 4. Read file
$json = file_get_contents($filePath);
$rules = json_decode($json, true);
// dd($rules);
if (!is_array($rules)) {
$this->myLogger->logme("error","Invalid JSON structure in file: $filePath");
return ['status' => false, 'message' => 'Invalid rule file'];
}
// 5. Remove rule where file_id == commission_data id
$updatedRules = array_filter($rules, function ($rule) use ($id) {
return isset($rule['file_id']) && $rule['file_id'] != $id;
});
$updatedRules = array_values($updatedRules);
// 6. If empty → delete file
if (empty($updatedRules)) {
unlink($filePath);
$this->myLogger->logme("error","Rule removed. File deleted because no rules left: $filePath");
return [
'status' => true,
'message' => 'Rule deleted and file removed (no rules left)'
];
}
// 7. Write updated JSON
file_put_contents($filePath, json_encode($updatedRules, JSON_PRETTY_PRINT));
$this->myLogger->logme("error","Rule removed successfully and file updated: $filePath");
return [
'status' => true,
'message' => 'Rule removed and file updated successfully'
];
} }
public function checkSameEntry() public function checkSameEntry()

View File

@ -185,7 +185,7 @@
<a href="javascript: void(0);" class="dropdown-toggle arrow-none btn btn-light btn-sm" data-toggle="dropdown" aria-expanded="false"><i class="mdi mdi-dots-horizontal"></i></a> <a href="javascript: void(0);" class="dropdown-toggle arrow-none btn btn-light btn-sm" data-toggle="dropdown" aria-expanded="false"><i class="mdi mdi-dots-horizontal"></i></a>
<div class="dropdown-menu dropdown-menu-right"> <div class="dropdown-menu dropdown-menu-right">
<?php if($row['file_status'] == "failed") : ?> <?php if($row['file_status'] == "failed") : ?>
<a href="<?= base_url('commission/downloadErrorFile?file_id=') . $row['id'] ?>" class="dropdown-item" target="_blank"><i class="mdi mdi-delete mr-2 text-muted font-18 vertical-middle"></i>Download Error File</a> <a href="<?= base_url('commission/downloadErrorFile?file_id=') . $row['id'] ?>" class="dropdown-item" target="_blank"><i class="mdi mdi-download mr-2 text-muted font-18 vertical-middle"></i>Download Error File</a>
<?php endif; ?> <?php endif; ?>
<a class="dropdown-item btnEdit" data-id="<?= $row['id']; ?>" onclick="deleteCommissionData(<?= $row['id']; ?>)"> <a class="dropdown-item btnEdit" data-id="<?= $row['id']; ?>" onclick="deleteCommissionData(<?= $row['id']; ?>)">
<i class="mdi mdi-delete mr-2 text-muted font-18 vertical-middle"></i>Delete <i class="mdi mdi-delete mr-2 text-muted font-18 vertical-middle"></i>Delete
@ -285,84 +285,85 @@
let isDublicateFound = false; let isDublicateFound = false;
document.addEventListener("DOMContentLoaded", function() { // document.addEventListener("DOMContentLoaded", function() {
const table = document.getElementById("tickets-table"); // const table = document.getElementById("tickets-table");
// Create custom dropdown // // Create custom dropdown
function createCustomDropdown(row) { // function createCustomDropdown(row) {
const originalDropdown = row.querySelector('.dropdown-menu'); // const originalDropdown = row.querySelector('.dropdown-menu');
if (!originalDropdown) return null; // console.log('originalDropdown', originalDropdown)
// if (!originalDropdown) return null;
const customDropdown = document.createElement('div'); // const customDropdown = document.createElement('div');
customDropdown.className = 'custom-dropdown-menu'; // customDropdown.className = 'custom-dropdown-menu';
customDropdown.innerHTML = originalDropdown.innerHTML; // customDropdown.innerHTML = originalDropdown.innerHTML;
// Remove inline onclick handlers and store them in data attributes // // Remove inline onclick handlers and store them in data attributes
const originalItems = originalDropdown.querySelectorAll('.dropdown-item'); // const originalItems = originalDropdown.querySelectorAll('.dropdown-item');
customDropdown.querySelectorAll('.dropdown-item').forEach((item, index) => { // customDropdown.querySelectorAll('.dropdown-item').forEach((item, index) => {
const originalOnclick = originalItems[index].getAttribute('onclick'); // const originalOnclick = originalItems[index].getAttribute('onclick');
item.removeAttribute('onclick'); // Remove the inline handler // item.removeAttribute('onclick'); // Remove the inline handler
item.setAttribute('data-onclick', originalOnclick); // Store in data attribute // item.setAttribute('data-onclick', originalOnclick); // Store in data attribute
}); // });
return customDropdown; // return customDropdown;
} // }
let activeDropdown = null; // let activeDropdown = null;
// Add click event listener to rows // // Add click event listener to rows
table.querySelectorAll("tbody tr").forEach(row => { // table.querySelectorAll("tbody tr").forEach(row => {
const customDropdown = createCustomDropdown(row); // const customDropdown = createCustomDropdown(row);
if (!customDropdown) return; // if (!customDropdown) return;
document.body.appendChild(customDropdown); // document.body.appendChild(customDropdown);
row.addEventListener("click", function(event) { // row.addEventListener("click", function(event) {
// Ignore clicks on the first column // // Ignore clicks on the first column
if (event.target.closest('td:first-child')) return; // if (event.target.closest('td:first-child')) return;
if (activeDropdown) activeDropdown.style.display = 'none'; // if (activeDropdown) activeDropdown.style.display = 'none';
const rect = event.target.getBoundingClientRect(); // const rect = event.target.getBoundingClientRect();
customDropdown.style.display = 'block'; // customDropdown.style.display = 'block';
customDropdown.style.position = 'fixed'; // customDropdown.style.position = 'fixed';
customDropdown.style.left = `${rect.left}px`; // customDropdown.style.left = `${rect.left}px`;
customDropdown.style.top = `${rect.bottom + 5}px`; // customDropdown.style.top = `${rect.bottom + 5}px`;
activeDropdown = customDropdown; // activeDropdown = customDropdown;
event.stopPropagation(); // event.stopPropagation();
}); // });
// Handle custom dropdown clicks // // Handle custom dropdown clicks
customDropdown.querySelectorAll('.dropdown-item').forEach(item => { // customDropdown.querySelectorAll('.dropdown-item').forEach(item => {
item.addEventListener('click', function(e) { // item.addEventListener('click', function(e) {
e.preventDefault(); // e.preventDefault();
// Execute the original onclick from data attribute // // Execute the original onclick from data attribute
const onclickAttr = this.getAttribute('data-onclick'); // const onclickAttr = this.getAttribute('data-onclick');
if (onclickAttr) eval(onclickAttr); // if (onclickAttr) eval(onclickAttr);
// Handle href navigation // // Handle href navigation
const href = this.getAttribute('href'); // const href = this.getAttribute('href');
if (href && href !== '#') window.location.href = href; // if (href && href !== '#') window.location.href = href;
if (activeDropdown) { // if (activeDropdown) {
activeDropdown.style.display = 'none'; // activeDropdown.style.display = 'none';
activeDropdown = null; // activeDropdown = null;
} // }
e.stopPropagation(); // e.stopPropagation();
}); // });
}); // });
}); // });
// Close dropdown on outside click // // Close dropdown on outside click
document.addEventListener("click", function() { // document.addEventListener("click", function() {
if (activeDropdown) { // if (activeDropdown) {
activeDropdown.style.display = 'none'; // activeDropdown.style.display = 'none';
activeDropdown = null; // activeDropdown = null;
} // }
}); // });
}); // });
//datatable //datatable
$(document).ready(function() { $(document).ready(function() {