feat(claims): add tracking number and status history for claims
- Introduced `tracking_number` field in the `claims` table to store the insurance tracking number. - Created `claim_status_logs` table to maintain a history of status changes for claims, including who made the change and when. - Implemented `ClaimStatusLog` entity and repository for managing status log entries. - Updated `ClaimService` to log transitions and handle tracking numbers during claim submissions. - Added new API endpoint for fetching claims by patient, including detailed claim history and status logs. - Enhanced frontend with a new `ClaimPatientDetailPage` to display claims and their status history. - Added tests to ensure correct aggregation of claims and proper handling of status transitions.
This commit is contained in:
@@ -119,6 +119,216 @@ class ClaimRepository extends ServiceEntityRepository
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
/**
|
||||
* تجمیع مطالبات بر اساس بیمار — نمای سطحاول داشبورد.
|
||||
*
|
||||
* مبالغ خدمات/سهم بیمار از صورتحسابهای **یکتا** جمع میشوند، نه از مطالبات؛ یک
|
||||
* صورتحساب میتواند دو مطالبه (پایه و مکمل) داشته باشد و جمعزدن از سمت مطالبه
|
||||
* مبلغ خدمات را دوبار میشمرد.
|
||||
*
|
||||
* @param array<string, mixed> $filters
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function aggregateByPatient(string $entityType, int $entityId, array $filters, string $sort, string $dir, int $page, int $limit): array
|
||||
{
|
||||
[$where, $params] = $this->patientAggregateFilters($filters);
|
||||
|
||||
$orderBy = match ($sort) {
|
||||
'claims_count' => 'claims_count',
|
||||
'total_services_rials' => 'total_services_rials',
|
||||
'total_insurance_rials' => 'total_insurance_rials',
|
||||
'last_activity_at' => 'last_activity_at',
|
||||
default => 'full_name',
|
||||
};
|
||||
$direction = strtolower($dir) === 'asc' ? 'ASC' : 'DESC';
|
||||
|
||||
// LIMIT/OFFSET بهصورت مقدار درج میشوند: MariaDB پارامتر رشتهای در LIMIT نمیپذیرد.
|
||||
// هر دو از قبل به int تبدیل شدهاند، پس تزریقی ممکن نیست.
|
||||
$offset = ($page - 1) * $limit;
|
||||
$sql = $this->patientAggregateSql($where)
|
||||
. " ORDER BY {$orderBy} {$direction} LIMIT {$limit} OFFSET {$offset}";
|
||||
|
||||
$params['type'] = $entityType;
|
||||
$params['id'] = $entityId;
|
||||
|
||||
$rows = $this->getEntityManager()->getConnection()->executeQuery($sql, $params)->fetchAllAssociative();
|
||||
|
||||
return array_map(static function (array $r): array {
|
||||
$statuses = array_filter(explode(',', (string) $r['statuses']));
|
||||
|
||||
return [
|
||||
'patient_uuid' => $r['patient_uuid'],
|
||||
'record_uuid' => $r['record_uuid'],
|
||||
'full_name' => $r['full_name'],
|
||||
'mobile' => $r['mobile'],
|
||||
'national_code' => $r['national_code'],
|
||||
'claims_count' => (int) $r['claims_count'],
|
||||
'total_services_rials' => (int) $r['total_services_rials'],
|
||||
'total_insurance_rials' => (int) $r['total_insurance_rials'],
|
||||
'total_patient_rials' => (int) $r['total_patient_rials'],
|
||||
'total_approved_rials' => (int) $r['total_approved_rials'],
|
||||
'total_paid_rials' => (int) $r['total_paid_rials'],
|
||||
'overall_status' => count($statuses) === 1 ? reset($statuses) : 'mixed',
|
||||
'last_activity_at' => (int) $r['last_activity_at'],
|
||||
];
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
public function countPatientsWithClaims(string $entityType, int $entityId, array $filters): int
|
||||
{
|
||||
[$where, $params] = $this->patientAggregateFilters($filters);
|
||||
$params['type'] = $entityType;
|
||||
$params['id'] = $entityId;
|
||||
|
||||
$sql = 'SELECT COUNT(*) FROM (' . $this->patientAggregateSql($where) . ') agg';
|
||||
|
||||
return (int) $this->getEntityManager()->getConnection()->executeQuery($sql, $params)->fetchOne();
|
||||
}
|
||||
|
||||
private function patientAggregateSql(string $where): string
|
||||
{
|
||||
return <<<SQL
|
||||
WITH claim_map AS (
|
||||
SELECT c.id AS claim_id, MIN(inv.id) AS invoice_id, MIN(inv.patient_record_id) AS record_id
|
||||
FROM claims c
|
||||
JOIN claim_items ci ON ci.claim_id = c.id
|
||||
JOIN invoice_items ii ON ii.id = ci.invoice_item_id
|
||||
JOIN invoices inv ON inv.id = ii.invoice_id
|
||||
WHERE c.entity_type = :type AND c.entity_id = :id
|
||||
GROUP BY c.id
|
||||
)
|
||||
SELECT
|
||||
u.uuid AS patient_uuid,
|
||||
pr.uuid AS record_uuid,
|
||||
u.real_name AS full_name,
|
||||
u.mobile_number AS mobile,
|
||||
u.national_code AS national_code,
|
||||
COUNT(DISTINCT c.id) AS claims_count,
|
||||
COALESCE(SUM(c.total_claimed_rials), 0) AS total_insurance_rials,
|
||||
COALESCE(SUM(c.total_approved_rials), 0) AS total_approved_rials,
|
||||
COALESCE(SUM(c.total_paid_rials), 0) AS total_paid_rials,
|
||||
COALESCE((
|
||||
SELECT SUM(i2.total_rials) FROM invoices i2
|
||||
WHERE i2.id IN (SELECT DISTINCT cm2.invoice_id FROM claim_map cm2 WHERE cm2.record_id = pr.id)
|
||||
), 0) AS total_services_rials,
|
||||
COALESCE((
|
||||
SELECT SUM(i3.patient_rials) FROM invoices i3
|
||||
WHERE i3.id IN (SELECT DISTINCT cm3.invoice_id FROM claim_map cm3 WHERE cm3.record_id = pr.id)
|
||||
), 0) AS total_patient_rials,
|
||||
GROUP_CONCAT(DISTINCT c.status) AS statuses,
|
||||
MAX(c.updated_at) AS last_activity_at
|
||||
FROM claim_map cm
|
||||
JOIN claims c ON c.id = cm.claim_id
|
||||
JOIN invoices inv ON inv.id = cm.invoice_id
|
||||
JOIN patient_records pr ON pr.id = cm.record_id
|
||||
JOIN users u ON u.id = pr.user_id
|
||||
{$where}
|
||||
GROUP BY pr.id, u.uuid, pr.uuid, u.real_name, u.mobile_number, u.national_code
|
||||
SQL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $filters
|
||||
* @return array{0: string, 1: array<string, mixed>}
|
||||
*/
|
||||
private function patientAggregateFilters(array $filters): array
|
||||
{
|
||||
$conditions = [];
|
||||
$params = [];
|
||||
|
||||
if (!empty($filters['status'])) {
|
||||
$conditions[] = 'c.status = :status';
|
||||
$params['status'] = $filters['status'];
|
||||
}
|
||||
if (!empty($filters['insurance_id'])) {
|
||||
$conditions[] = 'c.insurance_id = :insId';
|
||||
$params['insId'] = (int) $filters['insurance_id'];
|
||||
}
|
||||
if (!empty($filters['from'])) {
|
||||
$conditions[] = 'c.created_at >= :from';
|
||||
$params['from'] = (int) $filters['from'];
|
||||
}
|
||||
if (!empty($filters['to'])) {
|
||||
$conditions[] = 'c.created_at <= :to';
|
||||
$params['to'] = (int) $filters['to'];
|
||||
}
|
||||
if (!empty($filters['doctor_id'])) {
|
||||
$conditions[] = 'EXISTS (SELECT 1 FROM patient_sessions ps '
|
||||
. 'JOIN appointments a ON a.id = ps.appointment_id '
|
||||
. 'WHERE ps.id = inv.patient_session_id AND a.doctor_id = :docId)';
|
||||
$params['docId'] = (int) $filters['doctor_id'];
|
||||
}
|
||||
if (!empty($filters['payment_status'])) {
|
||||
$conditions[] = $filters['payment_status'] === 'paid'
|
||||
? 'c.status = \'paid\''
|
||||
: 'c.status <> \'paid\'';
|
||||
}
|
||||
if (!empty($filters['search'])) {
|
||||
$conditions[] = '(u.real_name LIKE :search OR u.mobile_number LIKE :search OR u.national_code LIKE :search)';
|
||||
$params['search'] = '%' . trim((string) $filters['search']) . '%';
|
||||
}
|
||||
|
||||
return [$conditions === [] ? '' : 'WHERE ' . implode(' AND ', $conditions), $params];
|
||||
}
|
||||
|
||||
/**
|
||||
* مطالبات یک بیمار با جزئیات نمایشی (پزشک، سرویس، تاریخ مراجعه، سهمها).
|
||||
*
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
public function detailsForPatient(string $entityType, int $entityId, int $recordId, array $filters): array
|
||||
{
|
||||
[$where, $params] = $this->patientAggregateFilters($filters);
|
||||
$where = $where === '' ? 'WHERE pr.id = :recordId' : $where . ' AND pr.id = :recordId';
|
||||
|
||||
$params['type'] = $entityType;
|
||||
$params['id'] = $entityId;
|
||||
$params['recordId'] = $recordId;
|
||||
|
||||
$sql = <<<SQL
|
||||
WITH claim_map AS (
|
||||
SELECT c.id AS claim_id, MIN(inv.id) AS invoice_id, MIN(inv.patient_record_id) AS record_id
|
||||
FROM claims c
|
||||
JOIN claim_items ci ON ci.claim_id = c.id
|
||||
JOIN invoice_items ii ON ii.id = ci.invoice_item_id
|
||||
JOIN invoices inv ON inv.id = ii.invoice_id
|
||||
WHERE c.entity_type = :type AND c.entity_id = :id
|
||||
GROUP BY c.id
|
||||
)
|
||||
SELECT
|
||||
c.id AS claim_id,
|
||||
c.uuid,
|
||||
c.insurance_id,
|
||||
c.insurance_kind,
|
||||
c.status,
|
||||
c.total_claimed_rials,
|
||||
c.total_approved_rials,
|
||||
c.total_paid_rials,
|
||||
c.reject_reason,
|
||||
c.tracking_number,
|
||||
c.submitted_at,
|
||||
c.settled_at,
|
||||
c.created_at,
|
||||
inv.uuid AS invoice_uuid,
|
||||
inv.total_rials AS service_base_rials,
|
||||
inv.patient_rials AS patient_share_rials,
|
||||
ps.session_at AS visit_date,
|
||||
d.name AS doctor_name
|
||||
FROM claim_map cm
|
||||
JOIN claims c ON c.id = cm.claim_id
|
||||
JOIN invoices inv ON inv.id = cm.invoice_id
|
||||
JOIN patient_records pr ON pr.id = cm.record_id
|
||||
JOIN users u ON u.id = pr.user_id
|
||||
LEFT JOIN patient_sessions ps ON ps.id = inv.patient_session_id
|
||||
LEFT JOIN appointments a ON a.id = ps.appointment_id
|
||||
LEFT JOIN doctors d ON d.id = a.doctor_id
|
||||
{$where}
|
||||
ORDER BY c.created_at DESC, c.id DESC
|
||||
SQL;
|
||||
|
||||
return $this->getEntityManager()->getConnection()->executeQuery($sql, $params)->fetchAllAssociative();
|
||||
}
|
||||
|
||||
/** آیا برای این صورتحساب از قبل مطالبهای ساخته شده؟ (از طریق آیتمهای صورتحساب) */
|
||||
public function existsForInvoice(int $invoiceId): bool
|
||||
{
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace App\Billing\Repository;
|
||||
|
||||
use App\Billing\Entity\ClaimStatusLog;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class ClaimStatusLogRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, ClaimStatusLog::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* تاریخچهی چند مطالبه در یک رفتوآمد، گروهبندیشده بر اساس claim_id.
|
||||
*
|
||||
* @param int[] $claimIds
|
||||
* @return array<int, array<int, array<string, mixed>>>
|
||||
*/
|
||||
public function timelinesForClaims(array $claimIds): array
|
||||
{
|
||||
if ($claimIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$logs = $this->createQueryBuilder('l')
|
||||
->where('l.claimId IN (:ids)')
|
||||
->setParameter('ids', $claimIds)
|
||||
->orderBy('l.createdAt', 'ASC')
|
||||
->addOrderBy('l.id', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
|
||||
$grouped = [];
|
||||
foreach ($logs as $log) {
|
||||
$grouped[$log->getClaimId()][] = $log->toArray();
|
||||
}
|
||||
|
||||
return $grouped;
|
||||
}
|
||||
|
||||
public function save(ClaimStatusLog $entity, bool $flush = true): void
|
||||
{
|
||||
$this->getEntityManager()->persist($entity);
|
||||
if ($flush) {
|
||||
$this->getEntityManager()->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user