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:
@@ -5,6 +5,7 @@ namespace App\Billing\Controller;
|
||||
use App\Auth\Entity\User;
|
||||
use App\Billing\Entity\Claim;
|
||||
use App\Billing\Repository\ClaimRepository;
|
||||
use App\Billing\Repository\ClaimStatusLogRepository;
|
||||
use App\Billing\Repository\InvoiceItemRepository;
|
||||
use App\Billing\Repository\InvoiceRepository;
|
||||
use App\Billing\Service\ClaimService;
|
||||
@@ -35,6 +36,7 @@ class BillingController extends BaseController
|
||||
private readonly PatientSessionRepository $sessionRepo,
|
||||
private readonly PatientRecordRepository $recordRepo,
|
||||
private readonly InsuranceRepository $insuranceRepo,
|
||||
private readonly ClaimStatusLogRepository $statusLogRepo,
|
||||
private readonly PatientRecordScopeResolver $scopeResolver,
|
||||
) {}
|
||||
|
||||
@@ -279,6 +281,120 @@ class BillingController extends BaseController
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* نمای سطحاول داشبورد مطالبات: یک ردیف بهازای هر بیمار با جمعهای تجمیعی.
|
||||
* فیلترها همان فیلترهای مطالبهاند و روی count هم اعمال میشوند.
|
||||
*/
|
||||
#[Route('/api/v1/billing/claims/by-patient', methods: ['GET'])]
|
||||
public function claimsByPatient(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
|
||||
}
|
||||
|
||||
$filters = $this->claimFilters($request);
|
||||
$page = max(1, (int) $request->query->get('page', 1));
|
||||
$limit = min(100, max(1, (int) $request->query->get('limit', 20)));
|
||||
$sort = (string) $request->query->get('sort', 'last_activity_at');
|
||||
$dir = (string) $request->query->get('dir', 'desc');
|
||||
|
||||
$rows = $this->claimRepo->aggregateByPatient($entityType, $entityId, $filters, $sort, $dir, $page, $limit);
|
||||
$total = $this->claimRepo->countPatientsWithClaims($entityType, $entityId, $filters);
|
||||
|
||||
return $this->paginated($rows, $total, $page, $limit);
|
||||
}
|
||||
|
||||
/** جزئیات کامل مطالبات یک بیمار، بههمراه تاریخچهی تغییر وضعیت هر مطالبه. */
|
||||
#[Route('/api/v1/billing/claims/by-patient/{patientUuid}', methods: ['GET'])]
|
||||
public function claimsForPatient(string $patientUuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
|
||||
}
|
||||
|
||||
$record = $this->recordRepo->findByUuid($patientUuid);
|
||||
if ($record === null || $record->getEntityType() !== $entityType || $record->getEntityId() !== $entityId) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'پرونده بیمار یافت نشد', 404);
|
||||
}
|
||||
|
||||
$rows = $this->claimRepo->detailsForPatient($entityType, $entityId, (int) $record->getId(), $this->claimFilters($request));
|
||||
$timelines = $this->statusLogRepo->timelinesForClaims(array_map(static fn(array $r) => (int) $r['claim_id'], $rows));
|
||||
|
||||
$insuranceNames = $this->insuranceNamesFor(array_map(static fn(array $r) => (int) $r['insurance_id'], $rows));
|
||||
|
||||
$items = array_map(function (array $r) use ($timelines, $insuranceNames) {
|
||||
$claimed = (int) $r['total_claimed_rials'];
|
||||
$base = (int) $r['service_base_rials'];
|
||||
|
||||
return [
|
||||
'uuid' => $r['uuid'],
|
||||
'invoice_uuid' => $r['invoice_uuid'],
|
||||
'visit_date' => $r['visit_date'] !== null ? (int) $r['visit_date'] : null,
|
||||
'doctor_name' => $r['doctor_name'],
|
||||
'insurance_id' => (int) $r['insurance_id'],
|
||||
'insurance_name' => $insuranceNames[(int) $r['insurance_id']] ?? null,
|
||||
'insurance_kind' => $r['insurance_kind'],
|
||||
'service_base_rials' => $base,
|
||||
'coverage_percent' => $base > 0 ? round($claimed * 100 / $base, 2) : 0.0,
|
||||
'insurance_share_rials'=> $claimed,
|
||||
'patient_share_rials' => (int) $r['patient_share_rials'],
|
||||
'total_approved_rials' => $r['total_approved_rials'] !== null ? (int) $r['total_approved_rials'] : null,
|
||||
'total_paid_rials' => $r['total_paid_rials'] !== null ? (int) $r['total_paid_rials'] : null,
|
||||
'status' => $r['status'],
|
||||
'tracking_number' => $r['tracking_number'],
|
||||
'reject_reason' => $r['reject_reason'],
|
||||
'submitted_at' => $r['submitted_at'] !== null ? (int) $r['submitted_at'] : null,
|
||||
'settled_at' => $r['settled_at'] !== null ? (int) $r['settled_at'] : null,
|
||||
'created_at' => (int) $r['created_at'],
|
||||
'allowed_transitions' => Claim::transitionsFrom($r['status']),
|
||||
'logs' => $timelines[(int) $r['claim_id']] ?? [],
|
||||
];
|
||||
}, $rows);
|
||||
|
||||
return $this->success([
|
||||
'patient' => [
|
||||
'uuid' => $record->getUser()->getUuid(),
|
||||
'record_uuid' => $record->getUuid(),
|
||||
'full_name' => $record->getUser()->getRealName(),
|
||||
'mobile' => $record->getUser()->getMobileNumber(),
|
||||
'national_code' => $record->getUser()->getNationalCode(),
|
||||
],
|
||||
'claims' => $items,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @param int[] $ids @return array<int, string> */
|
||||
private function insuranceNamesFor(array $ids): array
|
||||
{
|
||||
$unique = array_values(array_unique($ids));
|
||||
if ($unique === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$names = [];
|
||||
foreach ($this->insuranceRepo->findBy(['id' => $unique]) as $insurance) {
|
||||
$names[$insurance->getId()] = $insurance->getName();
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function claimFilters(Request $request): array
|
||||
{
|
||||
return [
|
||||
'status' => $request->query->get('status') ?: null,
|
||||
'insurance_id' => $request->query->get('insurance_id') ?: null,
|
||||
'doctor_id' => $request->query->get('doctor_id') ?: null,
|
||||
'payment_status' => $request->query->get('payment_status') ?: null,
|
||||
'from' => $request->query->get('from') ?: null,
|
||||
'to' => $request->query->get('to') ?: null,
|
||||
'search' => $request->query->get('search') ?: null,
|
||||
];
|
||||
}
|
||||
|
||||
#[Route('/api/v1/billing/claims/{uuid}/{action}', methods: ['POST'], requirements: ['action' => 'submit|approve|reject|pay'])]
|
||||
public function transitionClaim(string $uuid, string $action, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
@@ -296,10 +412,6 @@ class BillingController extends BaseController
|
||||
'pay' => Claim::STATUS_PAID,
|
||||
};
|
||||
|
||||
if ($action === 'reject' && trim($data['reason'] ?? '') === '') {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'دلیل رد الزامی است', 422);
|
||||
}
|
||||
|
||||
// Bound the financial figures: approved/paid cannot be negative, approved
|
||||
// cannot exceed the claimed total, and paid cannot exceed approved.
|
||||
if ($action === 'approve' && isset($data['approved_rials'])) {
|
||||
@@ -317,10 +429,12 @@ class BillingController extends BaseController
|
||||
}
|
||||
|
||||
$this->claimService->transition($claim, $target, [
|
||||
'approved_rials' => isset($data['approved_rials']) ? (int) $data['approved_rials'] : null,
|
||||
'paid_rials' => isset($data['paid_rials']) ? (int) $data['paid_rials'] : null,
|
||||
'reason' => trim($data['reason'] ?? ''),
|
||||
]);
|
||||
'approved_rials' => isset($data['approved_rials']) ? (int) $data['approved_rials'] : null,
|
||||
'paid_rials' => isset($data['paid_rials']) ? (int) $data['paid_rials'] : null,
|
||||
'reason' => trim($data['reason'] ?? ''),
|
||||
'tracking_number' => isset($data['tracking_number']) ? trim((string) $data['tracking_number']) : null,
|
||||
'note' => isset($data['note']) ? trim((string) $data['note']) : null,
|
||||
], $user);
|
||||
|
||||
return $this->success(['data' => $this->enrichClaims([$claim])[0]]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user