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 $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 $ticket * @return array */ 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; } }