FEAT_COND_COMMISSION_FILE_UPLOAD_JSON_STRUCTURE_CHANGE

This commit is contained in:
velz 2025-11-19 17:08:52 +05:30
parent 4121fee9aa
commit e6d9d073bd
3 changed files with 38 additions and 6 deletions

View File

@ -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");
});

View File

@ -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);
}
}

View File

@ -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'],
];