diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 4f0580df..f4413409 100755 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -787,5 +787,6 @@ $routes->group('commission', function($routes) { $routes->get('downloadErrorFile',"RuleImportController::downloadErrorFile"); $routes->get("deleteCommissionData/(:any)", "RuleImportController::deleteCommissionData/$1"); $routes->get('checkSameEntry',"RuleImportController::checkSameEntry"); + $routes->get('checkRuleUsage',"RuleImportController::checkRuleUsage"); }); diff --git a/app/Controllers/RuleImportController.php b/app/Controllers/RuleImportController.php index f5579aac..09d01c5a 100644 --- a/app/Controllers/RuleImportController.php +++ b/app/Controllers/RuleImportController.php @@ -4,6 +4,9 @@ namespace App\Controllers; use CodeIgniter\API\ResponseTrait; use App\Models\CommissionFilesModel; use App\Models\InsurerModel; +use App\Models\PartnerPolicyModel; + + class RuleImportController extends AdminController { @@ -13,6 +16,7 @@ class RuleImportController extends AdminController protected $commissionFilesModel; protected $departments; protected $insurerModel; + protected $partnerPolicyModel; public function __construct() { @@ -20,7 +24,7 @@ class RuleImportController extends AdminController $this->myLogger = \Config\Services::mylogger(); $this->ruleImportService = \Config\Services::ruleImportService(); - + $this->partnerPolicyModel = new partnerPolicyModel(); $this->commissionFilesModel = new CommissionFilesModel(); $this->insurerModel = new InsurerModel(); $this->departments = [ @@ -194,6 +198,8 @@ class RuleImportController extends AdminController 'insurer_id' => (int)$insurerId, 'department' => $department, 'commission_month' => $commissionMonth, + 'created_by' => (int)$createdBy, + ]; $this->myLogger->logme('info', 'RuleImportController::upload - Calling ruleImportService->processUpload', ['payload' => $payload]); @@ -262,10 +268,22 @@ class RuleImportController extends AdminController // handle existing JSON file based on $override (bool) if (file_exists($jsonPath)) { if ($overwrite) { - // delete existing file before writing (deterministic overwrite) - if (!@unlink($jsonPath)) { - $this->myLogger->logme('warning', 'RuleImportController::upload - Failed to delete existing JSON before overwrite', ['json_path' => $jsonPath]); - // proceed to overwrite anyway by writing to the same path + // rename existing file before overwrite + if (file_exists($jsonPath)) { + + $backupPath = $jsonPath . '.' . date('YmdHis') . '.bak'; + + if (!@rename($jsonPath, $backupPath)) { + $this->myLogger->logme( + 'warning', + 'RuleImportController::upload - Failed to rename existing JSON before overwrite', + [ + 'json_path' => $jsonPath, + 'backup_path' => $backupPath + ] + ); + // continue anyway; writing to same path will overwrite + } } $finalJson = $jsonData; } else { @@ -641,5 +659,14 @@ class RuleImportController extends AdminController } } + public function checkRuleUsage() + { + $rule_id = $this->request->getGet('rule_id'); + + $count = $this->partnerPolicyModel->where('commission_applied_rule', $ruleName) + ->countAllResults(); + return $this->respond(['status' => true, 'code' => 200, 'count' => $count, 'message' => ""], 200); + } + } diff --git a/app/Libraries/RuleImportService.php b/app/Libraries/RuleImportService.php index a4169916..ff0f5fe5 100644 --- a/app/Libraries/RuleImportService.php +++ b/app/Libraries/RuleImportService.php @@ -916,7 +916,11 @@ class RuleImportService 'is_deleted' => false, 'file_id' => $this->uploadedCommissionFileID, 'conditions' => $conditions, - 'calculation' => $calculation + 'calculation' => $calculation, + 'created_at' => date('Y-m-d H:i:s'), + 'updated_at' => date('Y-m-d H:i:s'), + 'created_by' => $this->incomingData['created_by'], + 'updated_by' => $this->incomingData['created_by'], ];