feat: insurance & medical billing system (6 phases)

Multi-tenant insurance contracts, service coverage, versioned tariffs,
invoice calculation, and insurance claims with debt reporting.

- TenantInsurance: per-tenant insurance contracts (coverage/franchise/ceiling,
  versioning, soft-deactivate) + active guard
- ServiceItem.insuranceCovered + TenantServiceCoverage per-service overrides
- Tariff: versioned yearly tariffs with fallback to ServiceItem price
- Billing domain: Money/ShareBreakdown VOs, BillingCalculator (unit-tested),
  Invoice/InvoiceItem aggregate, InvoiceService.createFromSession
- Claim/ClaimItem with state machine (pending->submitted->approved/rejected->paid),
  ClaimService, insurance-debt report
- ClaimSubmitterInterface + ManualClaimSubmitter (future insurance API ready)
- Admin UI: insurance-pricing page, claims page, service tariff modal,
  service insurance toggle; routes + sidebar entries
- Architecture doc + billing/insurance/clinic-services API docs

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
hamed
2026-06-23 15:05:24 +03:30
co-authored by Claude Opus 4.8
parent 5b1dfe9b40
commit 89191eee57
54 changed files with 4233 additions and 10 deletions
@@ -7,6 +7,8 @@ use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceSection;
use App\ClinicService\Repository\ServiceItemRepository;
use App\ClinicService\Repository\ServiceSectionRepository;
use App\ClinicService\Repository\TariffRepository;
use App\ClinicService\Service\TariffService;
use App\Clinic\Repository\ClinicRepository;
use App\Doctor\Repository\DoctorRepository;
use App\Shared\Constant\ErrorCodes;
@@ -32,6 +34,8 @@ class ClinicServiceController extends BaseController
private readonly SubscriptionService $subscriptionService,
private readonly DoctorRepository $doctorRepo,
private readonly ClinicRepository $clinicRepo,
private readonly TariffRepository $tariffRepo,
private readonly TariffService $tariffService,
) {}
// ── Service Sections ─────────────────────────────────────────────────────
@@ -157,6 +161,13 @@ class ClinicServiceController extends BaseController
}
}
if (isset($data['insurance_covered'])) {
$item->setInsuranceCovered((bool) $data['insurance_covered']);
}
if (array_key_exists('insurance_price_rials', $data)) {
$item->setInsurancePriceRials($data['insurance_price_rials'] !== null ? (int) $data['insurance_price_rials'] : null);
}
$this->itemRepo->save($item);
return $this->success($item->toArray(), 201);
@@ -181,6 +192,12 @@ class ClinicServiceController extends BaseController
$staff = $data['staff_uuid'] ? $this->staffRepo->findByUuid($data['staff_uuid']) : null;
$item->setStaff($staff);
}
if (isset($data['insurance_covered'])) {
$item->setInsuranceCovered((bool) $data['insurance_covered']);
}
if (array_key_exists('insurance_price_rials', $data)) {
$item->setInsurancePriceRials($data['insurance_price_rials'] !== null ? (int) $data['insurance_price_rials'] : null);
}
$this->itemRepo->save($item);
@@ -206,6 +223,51 @@ class ClinicServiceController extends BaseController
return $this->success(['message' => 'سرویس حذف شد']);
}
// ── Tariffs (تعرفه‌ی نسخه‌دار سالانه) ──────────────────────────────────────
#[Route('/api/v1/service-items/{uuid}/tariffs', methods: ['GET'])]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function listTariffs(string $uuid, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
$item = $this->itemRepo->findByUuid($uuid);
if ($item === null || !$this->ownsSection($item->getSection(), $entityType, $entityId)) {
return $this->error(ErrorCodes::ERR_SERVICE_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SERVICE_NOT_FOUND), 404);
}
$tariffs = $this->tariffRepo->findByService($item->getId());
return $this->success([
'current_year' => $this->tariffService->currentJalaliYear(),
'default_price_rials' => $item->getPriceRials(),
'data' => array_map(fn($t) => $t->toArray(), $tariffs),
]);
}
#[Route('/api/v1/service-items/{uuid}/tariffs/{year}', methods: ['PUT'], requirements: ['year' => '\d+'])]
#[IsGranted('IS_AUTHENTICATED_FULLY')]
public function setTariff(string $uuid, int $year, Request $request, #[CurrentUser] User $user): JsonResponse
{
[$entityType, $entityId] = $this->resolveEntity($user);
$item = $this->itemRepo->findByUuid($uuid);
if ($item === null || !$this->ownsSection($item->getSection(), $entityType, $entityId)) {
return $this->error(ErrorCodes::ERR_SERVICE_NOT_FOUND, ErrorCodes::message(ErrorCodes::ERR_SERVICE_NOT_FOUND), 404);
}
if ($year < 1390 || $year > 1500) {
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'سال نامعتبر است', 422);
}
$data = json_decode($request->getContent(), true) ?? [];
$price = (int) ($data['price_rials'] ?? 0);
$tariff = $this->tariffService->upsert($item->getId(), $year, $price);
return $this->success(['data' => $tariff->toArray()]);
}
// ── Helpers ──────────────────────────────────────────────────────────────
private function resolveEntity(User $user): array