Files
clinicpro/src/Billing/Entity/Claim.php
T
hamed 20bdc49e89 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.
2026-07-18 23:38:02 +03:30

213 lines
8.0 KiB
PHP

<?php
namespace App\Billing\Entity;
use App\Billing\Repository\ClaimRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;
#[ORM\Entity(repositoryClass: ClaimRepository::class)]
#[ORM\Table(name: 'claims')]
#[ORM\Index(columns: ['entity_type', 'entity_id'], name: 'idx_claim_tenant')]
#[ORM\Index(columns: ['insurance_id', 'status'], name: 'idx_claim_insurance_status')]
class Claim
{
public const STATUS_PENDING = 'pending';
public const STATUS_SUBMITTED = 'submitted';
public const STATUS_APPROVED = 'approved';
public const STATUS_REJECTED = 'rejected';
public const STATUS_PAID = 'paid';
public const KIND_BASE = 'base';
public const KIND_SUPPLEMENTARY = 'supplementary';
private const TRANSITIONS = [
self::STATUS_PENDING => [self::STATUS_SUBMITTED],
self::STATUS_SUBMITTED => [self::STATUS_APPROVED, self::STATUS_REJECTED],
self::STATUS_APPROVED => [self::STATUS_PAID],
self::STATUS_REJECTED => [],
self::STATUS_PAID => [],
];
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private ?int $id = null;
#[ORM\Column(type: 'string', length: 36, unique: true)]
private string $uuid;
#[ORM\Column(name: 'entity_type', type: 'string', length: 10)]
private string $entityType;
#[ORM\Column(name: 'entity_id', type: 'integer')]
private int $entityId;
#[ORM\Column(name: 'insurance_id', type: 'integer')]
private int $insuranceId;
#[ORM\Column(name: 'insurance_kind', type: 'string', length: 15)]
private string $insuranceKind;
#[ORM\Column(name: 'total_claimed_rials', type: 'integer')]
private int $totalClaimedRials = 0;
#[ORM\Column(name: 'total_approved_rials', type: 'integer', nullable: true)]
private ?int $totalApprovedRials = null;
#[ORM\Column(name: 'total_paid_rials', type: 'integer', nullable: true)]
private ?int $totalPaidRials = null;
#[ORM\Column(type: 'string', length: 15)]
private string $status = self::STATUS_PENDING;
#[ORM\Column(name: 'reject_reason', type: 'text', nullable: true)]
private ?string $rejectReason = null;
/** شماره پرونده/پیگیری نزد بیمه‌گر — هنگام ارسال وارد می‌شود. */
#[ORM\Column(name: 'tracking_number', type: 'string', length: 60, nullable: true)]
private ?string $trackingNumber = null;
#[ORM\Column(name: 'submitted_at', type: 'integer', nullable: true)]
private ?int $submittedAt = null;
#[ORM\Column(name: 'settled_at', type: 'integer', nullable: true)]
private ?int $settledAt = null;
#[ORM\Column(name: 'created_at', type: 'integer')]
private int $createdAt;
#[ORM\Column(name: 'updated_at', type: 'integer')]
private int $updatedAt;
#[ORM\OneToMany(targetEntity: ClaimItem::class, mappedBy: 'claim', cascade: ['persist', 'remove'], orphanRemoval: true)]
private Collection $items;
public function __construct(string $entityType, int $entityId, int $insuranceId, string $insuranceKind)
{
$this->uuid = Uuid::v4()->toRfc4122();
$this->entityType = $entityType;
$this->entityId = $entityId;
$this->insuranceId = $insuranceId;
$this->insuranceKind = $insuranceKind;
$this->createdAt = time();
$this->updatedAt = time();
$this->items = new ArrayCollection();
}
public function getId(): ?int { return $this->id; }
public function getUuid(): string { return $this->uuid; }
public function getEntityType(): string { return $this->entityType; }
public function getEntityId(): int { return $this->entityId; }
public function getInsuranceId(): int { return $this->insuranceId; }
public function getInsuranceKind(): string { return $this->insuranceKind; }
public function getStatus(): string { return $this->status; }
public function getTotalClaimedRials(): int { return $this->totalClaimedRials; }
public function getTotalApprovedRials(): ?int { return $this->totalApprovedRials; }
public function getTotalPaidRials(): ?int { return $this->totalPaidRials; }
/** @return Collection<int, ClaimItem> */
public function getItems(): Collection { return $this->items; }
public function addItem(ClaimItem $item): self
{
$this->items->add($item);
$this->totalClaimedRials += $item->getClaimedRials();
$this->updatedAt = time();
return $this;
}
public function canTransitionTo(string $status): bool
{
return in_array($status, self::TRANSITIONS[$this->status] ?? [], true);
}
/**
* انتقال‌های مجاز از وضعیت فعلی. پنل دکمه‌ها را از همین می‌سازد تا فهرست
* مجاز فقط یک‌جا تعریف شده باشد.
*
* @return string[]
*/
public function allowedTransitions(): array
{
return self::transitionsFrom($this->status);
}
/**
* همان جدول انتقال، برای مسیرهایی که ردیف خام (array hydration) دارند و
* موجودیت را هیدریت نمی‌کنند.
*
* @return string[]
*/
public static function transitionsFrom(string $status): array
{
return self::TRANSITIONS[$status] ?? [];
}
public function getTrackingNumber(): ?string { return $this->trackingNumber; }
public function getRejectReason(): ?string { return $this->rejectReason; }
public function getSubmittedAt(): ?int { return $this->submittedAt; }
public function getSettledAt(): ?int { return $this->settledAt; }
public function getCreatedAt(): int { return $this->createdAt; }
public function setTrackingNumber(?string $v): self
{
$this->trackingNumber = $v !== null && trim($v) !== '' ? trim($v) : null;
$this->updatedAt = time();
return $this;
}
public function submit(): void
{
$this->status = self::STATUS_SUBMITTED;
$this->submittedAt = time();
$this->updatedAt = time();
}
public function approve(?int $approvedRials = null): void
{
$this->status = self::STATUS_APPROVED;
$this->totalApprovedRials = $approvedRials ?? $this->totalClaimedRials;
$this->updatedAt = time();
}
public function reject(string $reason): void
{
$this->status = self::STATUS_REJECTED;
$this->rejectReason = $reason;
$this->settledAt = time();
$this->updatedAt = time();
}
public function pay(?int $paidRials = null): void
{
$this->status = self::STATUS_PAID;
$this->totalPaidRials = $paidRials ?? $this->totalApprovedRials ?? $this->totalClaimedRials;
$this->settledAt = time();
$this->updatedAt = time();
}
public function toArray(): array
{
return [
'uuid' => $this->uuid,
'entity_type' => $this->entityType,
'entity_id' => $this->entityId,
'insurance_id' => $this->insuranceId,
'insurance_kind' => $this->insuranceKind,
'total_claimed_rials' => $this->totalClaimedRials,
'total_approved_rials' => $this->totalApprovedRials,
'total_paid_rials' => $this->totalPaidRials,
'status' => $this->status,
'reject_reason' => $this->rejectReason,
'tracking_number' => $this->trackingNumber,
'allowed_transitions' => $this->allowedTransitions(),
'submitted_at' => $this->submittedAt,
'settled_at' => $this->settledAt,
'created_at' => $this->createdAt,
'items' => array_map(fn(ClaimItem $i) => $i->toArray(), $this->items->toArray()),
];
}
}