nhance/app/Models/ClaimReportModel.php

203 lines
6.8 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
class ClaimReportModel extends Model
{
protected $table = 'claim_report';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $allowedFields = [
'tpa_id',
'client_id',
'client_policy_id',
'file_id',
'ticket_id',
'source_table',
'source_row_id',
'claim_number',
'emp_code',
'tpa_no',
'emp_id',
'insured_emp_id',
'claim_amount',
'approved_amount',
'incurred_amount',
'si_amt',
'tpa_claim_status',
'claim_status_id',
'tpa_claim_type',
'tpa_ailments',
'doa',
'dod',
'date_of_intimat',
'settled_date',
'approved_date',
'claim_dump_date',
'hospital_name',
'hospital_city',
'hospital_state',
'hospital_pin_code',
'hospital_address',
'gender',
'age',
'relation',
'is_active',
'created_at',
'updated_at',
];
/**
* Push ticket_master fields into matching claim_report row(s).
* Match by ticket_id first; fallback to (client_policy_id, claim_number).
* Never creates new claim_report rows — only updates existing ones.
* Failures are swallowed so ticket updates are never blocked.
*/
public function syncFromTicketId(int $ticketId): bool
{
if ($ticketId <= 0) {
return true;
}
try {
if (!$this->db->tableExists('claim_report')) {
return true;
}
$ticket = $this->db->table('ticket_master')
->where('id', $ticketId)
->get()
->getRowArray();
if (empty($ticket)) {
return true;
}
$update = $this->mapTicketToClaimReport($ticket);
if ($update === []) {
return true;
}
$exists = $this->db->table('claim_report')
->where('ticket_id', $ticketId)
->countAllResults() > 0;
if ($exists) {
$this->db->table('claim_report')
->where('ticket_id', $ticketId)
->update($update);
return true;
}
// Fallback: match by unique key when ticket_id not set on claim_report yet
$claimNumber = trim((string) ($ticket['claim_number'] ?? ''));
$clientPolicyId = (int) ($ticket['client_policy_id'] ?? 0);
if ($claimNumber === '' || $clientPolicyId <= 0) {
return true;
}
$this->db->table('claim_report')
->where('client_policy_id', $clientPolicyId)
->where('claim_number', $claimNumber)
->update($update);
return true;
} catch (\Throwable $e) {
log_message('error', 'claim_report syncFromTicketId failed for ticket {id}: {msg}', [
'id' => $ticketId,
'msg' => $e->getMessage(),
]);
return false;
}
}
/**
* Sync multiple ticket IDs (e.g. after updateBatch).
*
* @param list<int|string> $ticketIds
*/
public function syncFromTicketIds(array $ticketIds): void
{
$ids = array_values(array_unique(array_filter(array_map('intval', $ticketIds))));
foreach ($ids as $id) {
$this->syncFromTicketId($id);
}
}
/**
* @param array<string, mixed> $ticket
* @return array<string, mixed>
*/
protected function mapTicketToClaimReport(array $ticket): array
{
$now = date('Y-m-d H:i:s');
$relation = $ticket['relation'] ?? null;
if ($relation === null || $relation === '') {
$relation = $ticket['relationship'] ?? null;
}
$approved = $ticket['approved_amount'] ?? null;
$claimed = $ticket['claim_amount'] ?? null;
$incurred = $ticket['incurred_amount'] ?? null;
if ($incurred === null || $incurred === '') {
$incurred = $approved !== null && $approved !== '' ? $approved : $claimed;
}
$map = [
'tpa_id' => $ticket['tpa_id'] ?? null,
'client_id' => $ticket['client_id'] ?? null,
'client_policy_id' => $ticket['client_policy_id'] ?? null,
'file_id' => $ticket['file_id'] ?? null,
'ticket_id' => $ticket['id'] ?? null,
'claim_number' => isset($ticket['claim_number']) ? trim((string) $ticket['claim_number']) : null,
'emp_code' => $ticket['emp_code'] ?? null,
'tpa_no' => $ticket['tpa_no'] ?? null,
'emp_id' => $ticket['emp_id'] ?? null,
'insured_emp_id' => $ticket['insured_emp_id'] ?? null,
'claim_amount' => $claimed,
'approved_amount' => $approved,
'incurred_amount' => $incurred,
'si_amt' => $ticket['si_amt'] ?? null,
'tpa_claim_status' => $ticket['tpa_claim_status'] ?? null,
'claim_status_id' => $ticket['claim_status_id'] ?? null,
'tpa_claim_type' => $ticket['tpa_claim_type'] ?? null,
'tpa_ailments' => $ticket['tpa_ailments'] ?? null,
'doa' => $ticket['doa'] ?? null,
'dod' => $ticket['dod'] ?? null,
'date_of_intimat' => $ticket['date_of_intimat'] ?? null,
'settled_date' => $ticket['settled_date'] ?? null,
'approved_date' => $ticket['approved_date'] ?? null,
'claim_dump_date' => $ticket['claim_dump_date'] ?? null,
'hospital_name' => $ticket['hospital_name'] ?? null,
'hospital_city' => $ticket['hospital_city'] ?? null,
'hospital_state' => $ticket['hospital_state'] ?? null,
'hospital_pin_code' => $ticket['hospital_pin_code'] ?? null,
'hospital_address' => $ticket['hospital_address'] ?? null,
'relation' => $relation,
'is_active' => isset($ticket['is_active']) ? (int) $ticket['is_active'] : 1,
'updated_at' => $now,
];
// Do not overwrite claim_number with empty string
if ($map['claim_number'] === '') {
unset($map['claim_number']);
}
// Keep source_table / source_row_id as-is (provenance from dump sync)
return $map;
}
}