feat(insurance): resolve coverage percent per service category

Base insurance is a percentage-only rule: patient share is now total minus the
base share, and the contract franchise no longer inflates it (franchise stays
meaningful for supplementary contracts only).

Coverage percentages are managed centrally by admin per service category
(outpatient/inpatient, extensible via the ServiceCategory enum). A tenant
contract may override a category, otherwise it follows the admin default live —
changing the central value immediately applies to every contract that did not
override it.

- add ServiceCategory enum + GET /api/v1/service-categories as the single source
  of the category list for every client
- add insurance_coverage_defaults (+ GET/PUT admin coverage-defaults endpoints)
  and expose coverage_defaults on the insurance list and insurance-pricing
- add tenant_insurance_category_coverage; tenant-insurances accepts optional
  category_coverages (needs insurances.update) and returns the effective
  percentages with their source
- add service_items.service_category; visits always resolve as outpatient
- drop the reverse-engineered percent from patient_share_rials in MyPatientsPage
  and align the client-side BillingCalculator mirror in CreateStep

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-07-25 16:21:19 +03:30
co-authored by Claude Opus 5
parent 1a9eda3576
commit 58c6d9ac18
41 changed files with 2558 additions and 143 deletions
@@ -7,6 +7,7 @@ use App\ClinicService\Entity\ServiceItem;
use App\ClinicService\Entity\ServiceItemAuditLog;
use App\ClinicService\Entity\ServiceSection;
use App\Insurance\Entity\TenantServiceCoverage;
use App\Insurance\Enum\ServiceCategory;
use App\ClinicService\Entity\Tariff;
use App\Clinic\Security\ClinicDoctorAccessChecker;
use App\Secretary\Security\SecretaryAccessChecker;
@@ -111,6 +112,30 @@ class ClinicServiceController extends BaseController
return null;
}
/** نوع خدمت (سرپایی/بستری) را ست می‌کند؛ مقدار نامعتبر ۴۲۲ می‌دهد. */
private function applyServiceCategory(ServiceItem $item, array $data): ?JsonResponse
{
if (!array_key_exists('service_category', $data)) {
return null;
}
$category = ServiceCategory::tryFromValue(
$data['service_category'] !== null ? (string) $data['service_category'] : null
);
if ($category === null) {
return $this->error(
ErrorCodes::ERR_VALIDATION_001,
'نوع خدمت نامعتبر است: ' . implode('، ', ServiceCategory::values()),
422,
'service_category',
);
}
$item->setServiceCategory($category);
return null;
}
/**
* `consumables: [{item_uuid, amount}]` را روی خدمت می‌نشاند. آرایه‌ی خالی یعنی حذف
* همه‌ی اقلام. هر قلم باید متعلق به همان مطب/کلینیک باشد.
@@ -144,6 +169,23 @@ class ClinicServiceController extends BaseController
return null;
}
// ── Service categories (سرپایی/بستری) ────────────────────────────────────
/**
* تنها منبع لیست نوع خدمت برای فرانت (فرم خدمت و تنظیمات پوشش بیمه)؛
* افزودن نوع تازه فقط یک case در ServiceCategory است و اینجا خودکار ظاهر می‌شود.
*/
#[Route('/api/v1/service-categories', methods: ['GET'])]
public function listServiceCategories(): JsonResponse
{
return $this->success([
'data' => array_map(
static fn(ServiceCategory $c) => ['key' => $c->value, 'label' => $c->label()],
ServiceCategory::cases(),
),
]);
}
// ── Service Sections ─────────────────────────────────────────────────────
#[Route('/api/v1/service-sections', methods: ['GET'])]
@@ -327,6 +369,9 @@ class ClinicServiceController extends BaseController
if (array_key_exists('bookable', $data)) {
$item->setBookable((bool) $data['bookable']);
}
if (($categoryError = $this->applyServiceCategory($item, $data)) !== null) {
return $categoryError;
}
$packageError = $this->applyInventoryPackage($item, $data, $entityType, $entityId);
if ($packageError !== null) {
return $packageError;
@@ -379,6 +424,9 @@ class ClinicServiceController extends BaseController
if (array_key_exists('bookable', $data)) {
$item->setBookable((bool) $data['bookable']);
}
if (($categoryError = $this->applyServiceCategory($item, $data)) !== null) {
return $categoryError;
}
$packageError = $this->applyInventoryPackage($item, $data, $entityType, $entityId);
if ($packageError !== null) {
return $packageError;