An appointment can now carry the insurance it is billed with: the service kind (outpatient/inpatient) and the basic insurance. Confirming it no longer hands the whole amount to the patient — the visit is split through BillingCalculator with the coverage percent of that service kind, and the choice travels to the encounter and the invoice built from it. The enabled service kinds are a tenant-wide setting (all of that tenant's insurances share it), so a tenant covering only one kind is never asked which one: the panel resolves it the same way the server does. - add tenant_service_category_settings + TenantServiceCategoryService, exposed on the existing insurance-pricing endpoint (service_categories, default_service_category); at least one kind must stay enabled - add appointments.insurance_service_category / insurance_base_id with AppointmentInsuranceService validating them against the tenant's own settings and active contracts (basic only), accepted by PATCH and by confirm - snapshot the kind on patient_sessions and invoices; the visit's coverage rule is resolved per kind (services keep using their own ServiceItem.service_category) - lib/insuranceShares becomes the single client-side mirror of BillingCalculator, shared by the confirm modal, the appointment edit page and the session form - surface the selection: confirm modal (with live shares), turns timeline chip, appointment edit page, patient record service card and invoice summary - the session form shows the insurance block whenever the tenant has an active contract and prefills the patient's own insurance, so it can be changed - fix: the confirm modal showed a zero visit price when the appointment had none — it now falls back to the tenant's free-visit price like the server - fix: useServiceCategories read one level too shallow, so Persian labels never arrived and raw enum keys leaked into the contract summary - fix: BlogsPage test asserted the public blogs endpoint after the page moved to the admin one Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
89 lines
3.9 KiB
TypeScript
89 lines
3.9 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) {
|
|
const pricingQuery = useQuery<ApiResponse<PricingPayload>>({
|
|
queryKey: ['insurance-pricing'],
|
|
queryFn: () => api.get('/api/v1/insurance-pricing'),
|
|
enabled,
|
|
});
|
|
|
|
const contractsQuery = useQuery<ApiResponse<{ data: TenantContract[] }>>({
|
|
queryKey: ['tenant-insurances'],
|
|
queryFn: () => api.get('/api/v1/billing/tenant-insurances'),
|
|
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 basicContracts = useMemo(
|
|
() => contracts.filter((c) => c.is_active !== false && c.insurance_kind !== 'supplementary'),
|
|
[contracts],
|
|
);
|
|
|
|
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: basicContracts.map((c) => ({
|
|
value: String(c.insurance_id),
|
|
label: c.insurance_name ?? `#${c.insurance_id}`,
|
|
})),
|
|
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): ShareBreakdown =>
|
|
breakdownOf(lines, basicContracts.find((c) => String(c.insurance_id) === String(insuranceId)) ?? null),
|
|
|
|
isLoading: pricingQuery.isLoading || contractsQuery.isLoading,
|
|
};
|
|
}
|