Files
clinicpro/assets/admin/components/InsuranceServiceCategoriesCard.tsx
T
hamedandClaude Opus 5 1f58b1b9b3 feat(insurance): bill an appointment with a chosen service kind and insurance
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>
2026-07-25 17:50:14 +03:30

94 lines
3.6 KiB
TypeScript

import { useEffect, useState } from 'react';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';
import { api } from '../lib/api';
import type { ApiResponse } from '../lib/api';
import { usePermissions } from '../hooks/usePermissions';
interface ServiceCategoryRow {
key: string;
label: string;
enabled: boolean;
}
/**
* «نوع خدمات بیمه» — تنظیمی سراسری برای همهٔ بیمه‌های این پزشک/کلینیک: بیمه‌ها کدام
* نوع خدمات را پوشش می‌دهند. اگر فقط یک نوع فعال بماند، همان به‌صورت خودکار مبنای
* محاسبه است و سرِ پذیرش چیزی پرسیده نمی‌شود.
*/
export default function InsuranceServiceCategoriesCard() {
const qc = useQueryClient();
const { can } = usePermissions();
const canUpdate = can('insurances', 'update');
const [rows, setRows] = useState<ServiceCategoryRow[]>([]);
const { data } = useQuery<ApiResponse<{ service_categories?: ServiceCategoryRow[] }>>({
queryKey: ['insurance-pricing'],
queryFn: () => api.get('/api/v1/insurance-pricing'),
});
const serverRows = data?.data?.service_categories ?? [];
useEffect(() => {
if (serverRows.length > 0) setRows(serverRows);
}, [data]);
const save = useMutation({
mutationFn: (next: ServiceCategoryRow[]) => api.put('/api/v1/insurance-pricing', {
service_categories: next.map((r) => ({ key: r.key, enabled: r.enabled })),
}),
onSuccess: () => {
toast.success('نوع خدمات بیمه ذخیره شد');
qc.invalidateQueries({ queryKey: ['insurance-pricing'] });
},
onError: (e: Error) => {
toast.error(e.message);
setRows(serverRows);
},
});
const toggle = (key: string) => {
const next = rows.map((r) => (r.key === key ? { ...r, enabled: !r.enabled } : r));
if (next.every((r) => !r.enabled)) {
toast.error('حداقل یک نوع خدمت باید فعال باشد');
return;
}
setRows(next);
save.mutate(next);
};
if (rows.length === 0) return null;
return (
<div className="card" style={{ padding: 20, marginBottom: 16 }}>
<h2 style={{ fontSize: 15, fontWeight: 700, margin: '0 0 6px' }}>نوع خدمات بیمه</h2>
<p style={{ margin: '0 0 14px', fontSize: 12, lineHeight: 1.9, color: 'var(--text-2)' }}>
بیمه‌های شما کدام نوع خدمات را پوشش می‌دهند؟ این تنظیم برای همهٔ بیمه‌ها یکسان است.
اگر فقط یک نوع فعال باشد، همان به‌صورت پیش‌فرض برای محاسبهٔ بیمه استفاده می‌شود؛
با فعال بودن هر دو، هنگام قطعی‌کردن نوبت نوع خدمت پرسیده می‌شود.
</p>
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 18 }}>
{rows.map((row) => (
<label
key={row.key}
style={{ display: 'inline-flex', alignItems: 'center', gap: 10, cursor: canUpdate ? 'pointer' : 'default' }}
>
<span className="switch">
<input
type="checkbox"
aria-label={row.label}
checked={row.enabled}
disabled={!canUpdate || save.isPending}
onChange={() => toggle(row.key)}
/>
<span className="switch-track"><span className="switch-thumb" /></span>
</span>
<span style={{ fontSize: 13 }}>{row.label}</span>
</label>
))}
</div>
</div>
);
}