Files
clinicpro/src/Insurance/Service/InsuranceScopeResolver.php
T
hamedandClaude Opus 5 b24f45cc83 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>
2026-08-18 17:18:35 +03:30

73 lines
2.9 KiB
PHP

<?php
namespace App\Insurance\Service;
use App\Insurance\Repository\EntityInsurancePricingRepository;
use App\Insurance\Repository\TenantInsuranceRepository;
use App\Insurance\Repository\TenantServiceCategorySettingRepository;
/**
* «این تنظیمِ بیمه از کدام محیط خوانده شود؟»
*
* پزشکِ عضو کلینیک دو محیط دارد و تنظیمات بیمه‌اش می‌تواند در هر کدام ثبت شده باشد:
* پنل کلینیک آن را روی خودِ پزشک می‌نویسد (`doctor_uuid`)، ولی نوبتِ ثبت‌شده در
* کلینیک محیطش «کلینیک» است. تا امروز این دو هرگز به هم نمی‌رسیدند: قرارداد روی
* پزشک ذخیره می‌شد و هنگام قطعی‌کردنِ نوبت، لیست از محیط کلینیک خوانده می‌شد و
* خالی بود — کاربر می‌دید «بیمه‌ای برای انتخاب نیست».
*
* قاعده: اول پزشک، بعد کلینیک. تنظیمِ خودِ پزشک همیشه برنده است؛ نبودنش یعنی
* «کلینیک برایم تصمیم می‌گیرد». هر نوع تنظیم جدا سنجیده می‌شود، چون پزشک ممکن است
* قرارداد بیمهٔ خودش را داشته باشد ولی قیمت ویزیت را به کلینیک سپرده باشد.
*/
final class InsuranceScopeResolver
{
public function __construct(
private readonly TenantInsuranceRepository $contracts,
private readonly EntityInsurancePricingRepository $pricing,
private readonly TenantServiceCategorySettingRepository $categorySettings,
) {}
/** @return array{0: string, 1: int} */
public function forContracts(int $doctorId, ?int $clinicId): array
{
return $this->pick(
$doctorId,
$clinicId,
fn (int $id): bool => $this->contracts->findActiveByTenant('doctor', $id) !== [],
);
}
/** @return array{0: string, 1: int} */
public function forVisitPrice(int $doctorId, ?int $clinicId): array
{
return $this->pick(
$doctorId,
$clinicId,
fn (int $id): bool => $this->pricing->findByEntity('doctor', $id) !== [],
);
}
/** @return array{0: string, 1: int} */
public function forServiceCategories(int $doctorId, ?int $clinicId): array
{
return $this->pick(
$doctorId,
$clinicId,
fn (int $id): bool => $this->categorySettings->enabledMapFor('doctor', $id) !== [],
);
}
/**
* @param callable(int):bool $doctorHasOwn
* @return array{0: string, 1: int}
*/
private function pick(int $doctorId, ?int $clinicId, callable $doctorHasOwn): array
{
if ($clinicId === null || $doctorHasOwn($doctorId)) {
return ['doctor', $doctorId];
}
return ['clinic', $clinicId];
}
}