fix(insurance): read a doctor's own settings first, then their clinic's

A clinic owner configures insurance on the doctor (`doctor_uuid`), but an
appointment booked at the clinic belongs to the clinic — so at confirm time
the engine looked for contracts under the clinic, found none, and the operator
had no insurance to pick and no way to save one ("this insurance has no active
contract"). The two sides were writing and reading different tenants.

Contracts, service kinds and the visit price now resolve doctor-first with the
appointment's clinic as fallback, each judged separately: a doctor who holds
their own contracts but leaves the visit price to the clinic gets each from the
right place. The confirm modal asks the same question the engine answers, via
`inherit=1` on the two read endpoints; the settings pages deliberately do not
send it, since editing must target the doctor's own row.

Two further things came out of the same sweep. The service-kind settings
repository had the tenant-filter blindness already fixed for contracts and
pricing — reads pinned to the caller's environment while the target is another
tenant — so it is now exempted the same way. And a coverage percentage of zero
is accepted as a real choice meaning "this contract does not cover that service
kind"; what is still rejected is leaving an enabled kind with no percentage at
all, inheriting a central default of zero included.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hamed
2026-08-18 17:18:35 +03:30
co-authored by Claude Opus 5
parent 1f64b516d2
commit b24f45cc83
14 changed files with 437 additions and 29 deletions
@@ -7,6 +7,7 @@ use App\Insurance\Enum\InsuranceType;
use App\Insurance\Enum\ServiceCategory;
use App\Insurance\Repository\InsuranceRepository;
use App\Insurance\Repository\TenantInsuranceRepository;
use App\Insurance\Service\InsuranceScopeResolver;
use App\Insurance\Service\TenantServiceCategoryService;
use App\Shared\Constant\ErrorCodes;
use App\Shared\Exception\AppException;
@@ -22,6 +23,7 @@ class AppointmentInsuranceService
private readonly TenantServiceCategoryService $serviceCategories,
private readonly TenantInsuranceRepository $tenantInsuranceRepo,
private readonly InsuranceRepository $insuranceRepo,
private readonly InsuranceScopeResolver $scope,
) {}
/**
@@ -39,6 +41,32 @@ class AppointmentInsuranceService
: ['doctor', (int) $appointment->getDoctor()->getId()];
}
/**
* محیطی که تنظیمات بیمهٔ این نوبت از آن خوانده می‌شود: اول خودِ پزشک، در نبودِ
* تنظیمِ او کلینیکِ همان نوبت. {@see InsuranceScopeResolver}
*
* جدا از {@see tenantOf()} است: مالکیتِ نوبت — و پرونده و پرداختش — همچنان با
* کلینیک است؛ این فقط می‌گوید قرارداد و درصدها را کجا پیدا کنیم.
*
* @return array{0: string, 1: int}
*/
public function contractScopeOf(Appointment $appointment): array
{
return $this->scope->forContracts(
(int) $appointment->getDoctor()->getId(),
$appointment->getClinic()?->getId() !== null ? (int) $appointment->getClinic()->getId() : null,
);
}
/** @return array{0: string, 1: int} */
public function categoryScopeOf(Appointment $appointment): array
{
return $this->scope->forServiceCategories(
(int) $appointment->getDoctor()->getId(),
$appointment->getClinic()?->getId() !== null ? (int) $appointment->getClinic()->getId() : null,
);
}
/**
* نوع خدمتِ مؤثر برای محاسبه: انتخابِ نوبت، وگرنه تنها نوع فعالِ tenant،
* وگرنه سرپایی (رفتار تاریخیِ ویزیت).
@@ -49,7 +77,7 @@ class AppointmentInsuranceService
return $appointment->getInsuranceServiceCategory();
}
[$entityType, $entityId] = $this->tenantOf($appointment);
[$entityType, $entityId] = $this->categoryScopeOf($appointment);
return $this->serviceCategories->defaultCategory($entityType, $entityId) ?? ServiceCategory::Outpatient;
}
@@ -63,23 +91,26 @@ class AppointmentInsuranceService
*/
public function apply(Appointment $appointment, array $data): void
{
[$entityType, $entityId] = $this->tenantOf($appointment);
// هر کدام محیط خودش را دارد: پزشکی که قرارداد بیمهٔ خودش را دارد ولی نوع
// خدمات را به کلینیک سپرده، هر دو را از جای درست می‌گیرد.
[$categoryType, $categoryId] = $this->categoryScopeOf($appointment);
[$contractType, $contractId] = $this->contractScopeOf($appointment);
if (array_key_exists('insurance_service_category', $data)) {
$appointment->setInsuranceServiceCategory(
$this->resolveCategory($data['insurance_service_category'], $entityType, $entityId)
$this->resolveCategory($data['insurance_service_category'], $categoryType, $categoryId)
);
}
if (array_key_exists('insurance_base_id', $data)) {
$appointment->setInsuranceBaseId(
$this->resolveInsuranceId($data['insurance_base_id'], $entityType, $entityId, false)
$this->resolveInsuranceId($data['insurance_base_id'], $contractType, $contractId, false)
);
}
if (array_key_exists('insurance_supplementary_id', $data)) {
$appointment->setInsuranceSupplementaryId(
$this->resolveInsuranceId($data['insurance_supplementary_id'], $entityType, $entityId, true)
$this->resolveInsuranceId($data['insurance_supplementary_id'], $contractType, $contractId, true)
);
}
}