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>
53 lines
2.0 KiB
PHP
53 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Billing\Service;
|
|
|
|
use App\Billing\ValueObject\Money;
|
|
use App\Billing\ValueObject\ShareBreakdown;
|
|
use App\Insurance\ValueObject\CoverageRule;
|
|
|
|
class BillingCalculator
|
|
{
|
|
/**
|
|
* محاسبهی سهم برای یک آیتم.
|
|
* ترتیب: کل → پوشش پایه (با سقف) → باقیمانده → پوشش مکمل روی باقیمانده (با سقف) → فرانشیز تکمیلی.
|
|
*/
|
|
public function calculateItem(
|
|
Money $total,
|
|
?CoverageRule $base,
|
|
?CoverageRule $supplementary,
|
|
): ShareBreakdown {
|
|
$baseShare = Money::zero();
|
|
$remaining = $total;
|
|
|
|
if ($base !== null && $base->covered) {
|
|
$baseShare = $total->percent($base->coveragePercent);
|
|
if ($base->ceilingRials !== null) {
|
|
$baseShare = $baseShare->min(new Money($base->ceilingRials));
|
|
}
|
|
$remaining = $total->sub($baseShare);
|
|
}
|
|
|
|
$suppShare = Money::zero();
|
|
if ($supplementary !== null && $supplementary->covered) {
|
|
$suppShare = $remaining->percent($supplementary->coveragePercent);
|
|
if ($supplementary->ceilingRials !== null) {
|
|
$suppShare = $suppShare->min(new Money($supplementary->ceilingRials));
|
|
}
|
|
$remaining = $remaining->sub($suppShare);
|
|
}
|
|
|
|
// بیمهٔ پایه صرفاً درصدی است: سهم بیمار = کل − سهم پایه. فرانشیز فقط در بیمهٔ
|
|
// تکمیلی معنا دارد و سهم بیمار را از کل بیشتر نمیکند.
|
|
$franchise = new Money($supplementary?->franchiseRials ?? 0);
|
|
$patient = $remaining->add($franchise)->min($total);
|
|
|
|
return new ShareBreakdown(
|
|
totalRials: $total->rials,
|
|
baseInsuranceRials: $baseShare->rials,
|
|
supplementaryRials: $suppShare->rials,
|
|
patientRials: $patient->rials,
|
|
);
|
|
}
|
|
}
|