92 lines
2.5 KiB
PHP
92 lines
2.5 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 for a policy, formatted as dd-mm-yyyy.
|
|
* Returns null when no dump file has a claim_dump_date.
|
|
*/
|
|
public function getGeneratedAtForPolicy(int $policyId): ?string
|
|
{
|
|
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);
|
|
if ($ts === false) {
|
|
return null;
|
|
}
|
|
|
|
return date('d-m-Y', $ts);
|
|
}
|
|
}
|