nhance/app/Models/ClaimDumpFileModel.php
2026-08-07 10:22:44 +05:30

102 lines
2.8 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class ClaimDumpFileModel extends Model
{
protected $table = 'claim_dump_files';
protected $primaryKey = 'id';
protected $allowedFields = [
"id",
"client_id",
"client_policy_id",
"tpa_id",
"claim_dump_date",
"file_name",
"status",
"reason",
"created_by",
"created_at",
"updated_by",
"updated_at",
"is_active",
];
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = ["checkAndADDCreatedByValue"];
protected $afterInsert = [];
protected $beforeUpdate = ["checkAndUpdateUpdatedByValue"];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
protected function checkAndADDCreatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['created_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['created_by'] = get_session_userid();
}
return $data;
}
protected function checkAndUpdateUpdatedByValue(array $data)
{
// Check if 'updated_by' value is null or empty
if (empty($data['data']['updated_by'])) {
// Set 'updated_by' value to the current session user ID
$data['data']['updated_by'] = get_session_userid();
}
return $data;
}
/**
* Latest claim_dump_date timestamp for a policy (active rows only).
*/
public function getLatestClaimDumpTimestamp(int $policyId): ?int
{
if ($policyId <= 0) {
return null;
}
$row = $this->select('claim_dump_date')
->where('client_policy_id', $policyId)
->where('is_active', 1)
->where('claim_dump_date IS NOT NULL', null, false)
->where("TRIM(claim_dump_date) != ''", null, false)
->orderBy('claim_dump_date', 'DESC')
->orderBy('id', 'DESC')
->first();
$raw = trim((string) ($row['claim_dump_date'] ?? ''));
if ($raw === '') {
return null;
}
$ts = strtotime($raw);
return $ts === false ? null : $ts;
}
/**
* Latest claim_dump_date for a policy, formatted.
* Returns null when no dump file has a claim_dump_date.
*/
public function getGeneratedAtForPolicy(int $policyId, string $format = 'd-m-Y'): ?string
{
$ts = $this->getLatestClaimDumpTimestamp($policyId);
if ($ts === null) {
return null;
}
return date($format, $ts);
}
}