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>
112 lines
5.4 KiB
TypeScript
112 lines
5.4 KiB
TypeScript
import { useMemo } from 'react';
|
|
import { useQuery } from '@tanstack/react-query';
|
|
import { api } from '../lib/api';
|
|
import type { ApiResponse } from '../lib/api';
|
|
import { breakdownOf, type BillableLine, type ShareBreakdown, type TenantContract } from '../lib/insuranceShares';
|
|
|
|
interface ServiceCategoryRow {
|
|
key: string;
|
|
label: string;
|
|
enabled: boolean;
|
|
}
|
|
|
|
interface PricingPayload {
|
|
service_categories?: ServiceCategoryRow[];
|
|
default_service_category?: string | null;
|
|
/** «قیمت ویزیت آزاد» tenant — سرور وقتی نوبت قیمت ندارد همین را میگذارد. */
|
|
free_visit_price_rials?: number;
|
|
}
|
|
|
|
/**
|
|
* انتخاب بیمهٔ یک نوبت: نوع خدماتِ فعالِ همین پزشک/کلینیک و قراردادهای بیمهٔ پایهٔ فعال،
|
|
* بههمراه محاسبهٔ سهم — مشترک بین مودال «قطعی کردن نوبت» و صفحهٔ ویرایش نوبت تا
|
|
* هر دو یک قاعده را نشان دهند.
|
|
*/
|
|
export function useAppointmentInsurance(enabled: boolean, doctorUuid?: string | null) {
|
|
// بدون پزشک، محیطِ خودِ کاربر پرسیده میشود — همان رفتار قبلی برای مطب شخصی.
|
|
//
|
|
// با پزشک، `inherit=1` هم میرود: نوبتِ ثبتشده در کلینیک محیطش «کلینیک» است ولی
|
|
// قرارداد بیمه معمولاً روی خودِ پزشک ذخیره شده. سرور اول تنظیم پزشک را میدهد و
|
|
// در نبودش تنظیم کلینیک را — دقیقاً همان چیزی که سرِ قطعیکردن اعمال میشود.
|
|
const scopeQuery = doctorUuid ? `?doctor_uuid=${encodeURIComponent(doctorUuid)}&inherit=1` : '';
|
|
|
|
const pricingQuery = useQuery<ApiResponse<PricingPayload>>({
|
|
queryKey: ['insurance-pricing', doctorUuid ?? null],
|
|
queryFn: () => api.get(`/api/v1/insurance-pricing${scopeQuery}`),
|
|
enabled,
|
|
});
|
|
|
|
const contractsQuery = useQuery<ApiResponse<{ data: TenantContract[] }>>({
|
|
queryKey: ['tenant-insurances', doctorUuid ?? null],
|
|
queryFn: () => api.get(`/api/v1/billing/tenant-insurances${scopeQuery}`),
|
|
enabled,
|
|
});
|
|
|
|
const pricing = (pricingQuery.data?.data ?? {}) as PricingPayload;
|
|
|
|
const enabledCategories = useMemo(
|
|
() => (pricing.service_categories ?? []).filter((c) => c.enabled),
|
|
[pricing.service_categories],
|
|
);
|
|
|
|
const contracts: TenantContract[] = useMemo(
|
|
() => ((contractsQuery.data?.data as any)?.data ?? []) as TenantContract[],
|
|
[contractsQuery.data],
|
|
);
|
|
|
|
const activeContracts = useMemo(() => contracts.filter((c) => c.is_active !== false), [contracts]);
|
|
const basicContracts = useMemo(
|
|
() => activeContracts.filter((c) => c.insurance_kind !== 'supplementary'),
|
|
[activeContracts],
|
|
);
|
|
const supplementaryContracts = useMemo(
|
|
() => activeContracts.filter((c) => c.insurance_kind === 'supplementary'),
|
|
[activeContracts],
|
|
);
|
|
|
|
const optionsOf = (list: TenantContract[]) =>
|
|
list.map((c) => ({ value: String(c.insurance_id), label: c.insurance_name ?? `#${c.insurance_id}` }));
|
|
|
|
return {
|
|
/**
|
|
* هزینهٔ ویزیتِ مؤثر — آینهٔ همان fallback سرور: قیمت خودِ نوبت، در نبودش
|
|
* «قیمت ویزیت آزاد» تنظیمات همین tenant.
|
|
*/
|
|
visitPriceOf: (appointmentVisitPrice: number | null | undefined): number =>
|
|
Number(appointmentVisitPrice ?? 0) > 0
|
|
? Number(appointmentVisitPrice)
|
|
: Number(pricing.free_visit_price_rials ?? 0),
|
|
|
|
/** فقط وقتی بیش از یک نوع فعال است، انتخاب از کاربر پرسیده میشود. */
|
|
needsCategoryChoice: enabledCategories.length > 1,
|
|
categoryOptions: enabledCategories.map((c) => ({ value: c.key, label: c.label })),
|
|
/** تنها نوع فعال (یا null اگر چند نوع فعال باشد) — همان قاعدهٔ سرور. */
|
|
defaultCategory: pricing.default_service_category ?? enabledCategories[0]?.key ?? null,
|
|
categoryLabelOf: (key: string | null | undefined) =>
|
|
(pricing.service_categories ?? []).find((c) => c.key === key)?.label ?? null,
|
|
|
|
insuranceOptions: optionsOf(basicContracts),
|
|
/** قراردادهای تکمیلیِ فعال — روی باقیماندهٔ بعد از بیمهٔ پایه اعمال میشوند. */
|
|
supplementaryOptions: optionsOf(supplementaryContracts),
|
|
hasSupplementary: supplementaryContracts.length > 0,
|
|
contractOf: (insuranceId: string | number | null | undefined): TenantContract | null =>
|
|
basicContracts.find((c) => String(c.insurance_id) === String(insuranceId)) ?? null,
|
|
insuranceNameOf: (insuranceId: string | number | null | undefined): string | null =>
|
|
contracts.find((c) => String(c.insurance_id) === String(insuranceId))?.insurance_name ?? null,
|
|
|
|
/** تفکیک زنجیرهای سهمها: پایه روی کل، تکمیلی روی باقیمانده. */
|
|
breakdown: (
|
|
lines: BillableLine[],
|
|
insuranceId: string | number | null | undefined,
|
|
supplementaryId: string | number | null | undefined = null,
|
|
): ShareBreakdown =>
|
|
breakdownOf(
|
|
lines,
|
|
basicContracts.find((c) => String(c.insurance_id) === String(insuranceId)) ?? null,
|
|
supplementaryContracts.find((c) => String(c.insurance_id) === String(supplementaryId)) ?? null,
|
|
),
|
|
|
|
isLoading: pricingQuery.isLoading || contractsQuery.isLoading,
|
|
};
|
|
}
|