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:
@@ -3,12 +3,19 @@
|
||||
namespace App\Insurance\Controller;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Clinic\Repository\ClinicRepository;
|
||||
use App\Doctor\Repository\DoctorRepository;
|
||||
use App\Insurance\Entity\DoctorInsurance;
|
||||
use App\Insurance\Entity\EntityInsurancePricing;
|
||||
use App\Insurance\Entity\Insurance;
|
||||
use App\Insurance\Enum\InsuranceType;
|
||||
use App\Insurance\Entity\TenantInsurance;
|
||||
use App\Insurance\Repository\DoctorInsuranceRepository;
|
||||
use App\Insurance\Repository\EntityInsurancePricingRepository;
|
||||
use App\Insurance\Repository\InsuranceRepository;
|
||||
use App\Insurance\Repository\TenantInsuranceRepository;
|
||||
use App\Insurance\Repository\TenantServiceCoverageRepository;
|
||||
use App\Insurance\Service\TenantInsuranceService;
|
||||
use App\Shared\Constant\ErrorCodes;
|
||||
use App\Shared\Controller\BaseController;
|
||||
use App\Shared\Service\FileValidatorService;
|
||||
@@ -27,10 +34,28 @@ class InsuranceController extends BaseController
|
||||
private readonly InsuranceRepository $insuranceRepo,
|
||||
private readonly DoctorInsuranceRepository $doctorInsuranceRepo,
|
||||
private readonly DoctorRepository $doctorRepo,
|
||||
private readonly ClinicRepository $clinicRepo,
|
||||
private readonly EntityInsurancePricingRepository $pricingRepo,
|
||||
private readonly TenantInsuranceRepository $tenantInsuranceRepo,
|
||||
private readonly TenantServiceCoverageRepository $serviceCoverageRepo,
|
||||
private readonly TenantInsuranceService $tenantInsuranceService,
|
||||
private readonly FileValidatorService $fileValidator,
|
||||
private readonly string $projectDir,
|
||||
) {}
|
||||
|
||||
private function resolveEntity(User $user): array
|
||||
{
|
||||
if ($user->hasRole('ROLE_DOCTOR')) {
|
||||
$doctor = $this->doctorRepo->findByUser($user);
|
||||
return $doctor !== null ? [EntityInsurancePricing::TYPE_DOCTOR, $doctor->getId()] : [EntityInsurancePricing::TYPE_DOCTOR, null];
|
||||
}
|
||||
if ($user->hasRole('ROLE_CLINIC')) {
|
||||
$clinic = $this->clinicRepo->findByUser($user);
|
||||
return $clinic !== null ? [EntityInsurancePricing::TYPE_CLINIC, $clinic->getId()] : [EntityInsurancePricing::TYPE_CLINIC, null];
|
||||
}
|
||||
return ['unknown', null];
|
||||
}
|
||||
|
||||
// ── Public list ───────────────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/insurances', methods: ['GET'])]
|
||||
@@ -183,6 +208,232 @@ class InsuranceController extends BaseController
|
||||
}
|
||||
}
|
||||
|
||||
// ── Entity insurance pricing (visit price by insurance) ───────────────────
|
||||
|
||||
#[Route('/api/v1/insurance-pricing', methods: ['GET'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function getInsurancePricing(#[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
|
||||
}
|
||||
|
||||
$rows = $this->pricingRepo->findByEntity($entityType, $entityId);
|
||||
|
||||
$freeVisitPriceRials = 0;
|
||||
$perInsurance = [];
|
||||
foreach ($rows as $row) {
|
||||
if ($row->isFreeVisit()) {
|
||||
$freeVisitPriceRials = $row->getPatientShareRials();
|
||||
} else {
|
||||
$perInsurance[$row->getInsuranceId()] = $row->getPatientShareRials();
|
||||
}
|
||||
}
|
||||
|
||||
$insurances = array_map(function (Insurance $i) use ($perInsurance) {
|
||||
return [
|
||||
'insurance_id' => $i->getId(),
|
||||
'insurance_name' => $i->getName(),
|
||||
'type' => $i->getType()->value,
|
||||
'patient_share_rials' => $perInsurance[$i->getId()] ?? null,
|
||||
];
|
||||
}, $this->insuranceRepo->findActive(null));
|
||||
|
||||
return $this->success([
|
||||
'entity_type' => $entityType,
|
||||
'entity_id' => $entityId,
|
||||
'free_visit_price_rials' => $freeVisitPriceRials,
|
||||
'insurances' => $insurances,
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/insurance-pricing', methods: ['PUT'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function saveInsurancePricing(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
|
||||
if (array_key_exists('free_visit_price_rials', $data)) {
|
||||
$this->upsertPricing($entityType, $entityId, null, (int) $data['free_visit_price_rials']);
|
||||
}
|
||||
|
||||
foreach (($data['insurances'] ?? []) as $row) {
|
||||
$insuranceId = isset($row['insurance_id']) ? (int) $row['insurance_id'] : null;
|
||||
if ($insuranceId === null) {
|
||||
continue;
|
||||
}
|
||||
if (!array_key_exists('patient_share_rials', $row) || $row['patient_share_rials'] === null) {
|
||||
$existing = $this->pricingRepo->findOneForInsurance($entityType, $entityId, $insuranceId);
|
||||
if ($existing !== null) {
|
||||
$this->pricingRepo->remove($existing, false);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
$this->upsertPricing($entityType, $entityId, $insuranceId, (int) $row['patient_share_rials']);
|
||||
}
|
||||
|
||||
$this->pricingRepo->getEntityManager()->flush();
|
||||
|
||||
return $this->getInsurancePricing($user);
|
||||
}
|
||||
|
||||
private function upsertPricing(string $entityType, int $entityId, ?int $insuranceId, int $shareRials): void
|
||||
{
|
||||
$row = $this->pricingRepo->findOneForInsurance($entityType, $entityId, $insuranceId);
|
||||
if ($row === null) {
|
||||
$row = new EntityInsurancePricing($entityType, $entityId, $insuranceId, $shareRials);
|
||||
} else {
|
||||
$row->setPatientShareRials($shareRials);
|
||||
}
|
||||
$this->pricingRepo->save($row, false);
|
||||
}
|
||||
|
||||
// ── TenantInsurance — قراردادهای بیمهی tenant ─────────────────────────────
|
||||
|
||||
#[Route('/api/v1/billing/tenant-insurances', methods: ['GET'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function listTenantInsurances(#[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
|
||||
}
|
||||
|
||||
$contracts = $this->tenantInsuranceRepo->findActiveByTenant($entityType, $entityId);
|
||||
|
||||
$byId = [];
|
||||
foreach ($this->insuranceRepo->findActive(null) as $ins) {
|
||||
$byId[$ins->getId()] = ['name' => $ins->getName(), 'type' => $ins->getType()->value];
|
||||
}
|
||||
|
||||
$data = array_map(function (TenantInsurance $c) use ($byId) {
|
||||
$row = $c->toArray();
|
||||
$row['insurance_name'] = $byId[$c->getInsuranceId()]['name'] ?? null;
|
||||
$row['insurance_kind'] = $byId[$c->getInsuranceId()]['type'] ?? null;
|
||||
return $row;
|
||||
}, $contracts);
|
||||
|
||||
return $this->success(['data' => $data]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/billing/tenant-insurances', methods: ['POST'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function activateTenantInsurance(Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
if ($entityId === null) {
|
||||
return $this->error(ErrorCodes::ERR_FORBIDDEN_001, 'پروفایل یافت نشد', 403);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$insuranceId = isset($data['insurance_id']) ? (int) $data['insurance_id'] : 0;
|
||||
if ($insuranceId <= 0) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'insurance_id الزامی است', 422);
|
||||
}
|
||||
|
||||
$contract = $this->tenantInsuranceService->activate(
|
||||
$entityType,
|
||||
$entityId,
|
||||
$insuranceId,
|
||||
(float) ($data['coverage_percent'] ?? 0),
|
||||
(int) ($data['franchise_rials'] ?? 0),
|
||||
isset($data['annual_ceiling_rials']) && $data['annual_ceiling_rials'] !== null
|
||||
? (int) $data['annual_ceiling_rials'] : null,
|
||||
);
|
||||
|
||||
return $this->success(['data' => $contract->toArray()], 201);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/billing/tenant-insurances/{uuid}', methods: ['PATCH'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function updateTenantInsurance(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$contract = $this->tenantInsuranceRepo->findByUuid($uuid);
|
||||
if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
if (array_key_exists('coverage_percent', $data)) {
|
||||
$contract->setCoveragePercent((float) $data['coverage_percent']);
|
||||
}
|
||||
if (array_key_exists('franchise_rials', $data)) {
|
||||
$contract->setFranchiseRials((int) $data['franchise_rials']);
|
||||
}
|
||||
if (array_key_exists('annual_ceiling_rials', $data)) {
|
||||
$contract->setAnnualCeilingRials($data['annual_ceiling_rials'] !== null ? (int) $data['annual_ceiling_rials'] : null);
|
||||
}
|
||||
|
||||
$this->tenantInsuranceRepo->save($contract);
|
||||
|
||||
return $this->success(['data' => $contract->toArray()]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/billing/tenant-insurances/{uuid}', methods: ['DELETE'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function deactivateTenantInsurance(string $uuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$contract = $this->tenantInsuranceRepo->findByUuid($uuid);
|
||||
if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
|
||||
}
|
||||
|
||||
$this->tenantInsuranceService->deactivate($contract);
|
||||
|
||||
return $this->success(['message' => 'قرارداد بیمه غیرفعال شد']);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/billing/tenant-insurances/{uuid}/service-coverage', methods: ['GET'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function listServiceCoverage(string $uuid, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$contract = $this->tenantInsuranceRepo->findByUuid($uuid);
|
||||
if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
|
||||
}
|
||||
|
||||
$rows = $this->serviceCoverageRepo->findByContract($contract->getId());
|
||||
|
||||
return $this->success(['data' => array_map(fn($r) => $r->toArray(), $rows)]);
|
||||
}
|
||||
|
||||
#[Route('/api/v1/billing/tenant-insurances/{uuid}/service-coverage', methods: ['PUT'])]
|
||||
#[IsGranted('IS_AUTHENTICATED_FULLY')]
|
||||
public function setServiceCoverage(string $uuid, Request $request, #[CurrentUser] User $user): JsonResponse
|
||||
{
|
||||
[$entityType, $entityId] = $this->resolveEntity($user);
|
||||
$contract = $this->tenantInsuranceRepo->findByUuid($uuid);
|
||||
if ($contract === null || $contract->getEntityType() !== $entityType || $contract->getEntityId() !== $entityId) {
|
||||
return $this->error(ErrorCodes::ERR_NOT_FOUND_001, 'قرارداد یافت نشد', 404);
|
||||
}
|
||||
|
||||
$data = json_decode($request->getContent(), true) ?? [];
|
||||
$serviceItemId = isset($data['service_item_id']) ? (int) $data['service_item_id'] : 0;
|
||||
if ($serviceItemId <= 0) {
|
||||
return $this->error(ErrorCodes::ERR_VALIDATION_001, 'service_item_id الزامی است', 422);
|
||||
}
|
||||
|
||||
$this->tenantInsuranceService->setServiceCoverage(
|
||||
$contract,
|
||||
$serviceItemId,
|
||||
(bool) ($data['covered'] ?? true),
|
||||
isset($data['coverage_percent']) && $data['coverage_percent'] !== null ? (float) $data['coverage_percent'] : null,
|
||||
isset($data['franchise_rials']) && $data['franchise_rials'] !== null ? (int) $data['franchise_rials'] : null,
|
||||
isset($data['ceiling_rials']) && $data['ceiling_rials'] !== null ? (int) $data['ceiling_rials'] : null,
|
||||
);
|
||||
|
||||
return $this->success(['message' => 'پوشش خدمت ذخیره شد']);
|
||||
}
|
||||
|
||||
// ── DoctorInsurance CRUD ──────────────────────────────────────────────────
|
||||
|
||||
#[Route('/api/v1/insurance/', methods: ['POST'])]
|
||||
|
||||
Reference in New Issue
Block a user