feat(pricing): tooth-based selection, lab-owned pricing, clinic-specific overrides

What changed
  - jobs.teeth (FDI string[]). memberCount becomes a derived field (teeth.length).
    A new TeethChart component renders the full permanent dentition as a
    16-column grid for each arch with click-toggle selection.
  - /jobs/new: removed the price + currency inputs and the manual memberCount
    field. Clinics now pick teeth via the chart; the form blocks submission
    until at least one tooth is selected.
  - createJobAction calls a new calculateJobPrice() helper that walks the
    pricing cascade and writes price + currency on the job server-side. A
    clinic-supplied price hidden field would now be ignored — the field
    isn't even in the schema.

Pricing cascade (calculateJobPrice, lib/appwrite/pricing.ts)
  1. clinic_pricing row matching (lab, clinic, type) with customUnitPrice
     → use that flat unit price.
  2. clinic_pricing row with discountPercent → catalog unitPrice × (1-d).
  3. lab's prosthetics catalog row matching type (not archived).
  4. nothing → price stays null; lab can still set it manually later.

Clinic-specific overrides (clinic_pricing table)
  - Unique on (labTenantId, clinicTenantId, prostheticType) so each
    combination has at most one rule.
  - Row permissions: read by both teams (transparency for clinic), write
    only by lab — clinic can see the discount they're getting but cannot
    edit it.
  - setClinicPricingAction validates an approved connection exists before
    creating/updating, and rejects requests where neither customUnitPrice
    nor discountPercent is set.
  - clearClinicPricingAction wipes a rule (catalog price re-applies).

UI
  - /connections 'Bağlantılarım' table gets a new column showing the active
    pricing rules per counterpart. Lab side has a 'Fiyatlandırma' button
    that opens a dialog (PROSTHETIC_TYPE × customPrice|discountPercent form
    + list of active rules with delete). Clinic side is read-only.
  - Job detail: 'Fiyat' field now shows 'Lab tarafından belirlenecek' when
    null, instead of a literal —. Adds a 'Dişler' info block listing the
    selected FDI numbers.
This commit is contained in:
kovakmedya
2026-05-21 22:04:26 +03:00
parent ee9c0015a5
commit 95f2d065b4
14 changed files with 971 additions and 76 deletions
+11 -1
View File
@@ -133,11 +133,21 @@ export default async function JobDetailPage({
<Info label="Fiyat">
{typeof job.price === "number"
? formatMoney(job.price, job.currency || "TRY")
: "—"}
: <span className="text-muted-foreground">Lab tarafından belirlenecek</span>}
</Info>
<Info label="Mevcut Aşama">
{job.currentStep ? JOB_STEP_LABELS[job.currentStep] : "—"}
</Info>
<div className="md:col-span-2">
<p className="text-muted-foreground mb-1 text-xs font-medium uppercase tracking-wide">
Dişler ({job.teeth?.length ?? job.memberCount})
</p>
<p className="font-mono text-sm">
{job.teeth && job.teeth.length > 0
? job.teeth.join(", ")
: `${job.memberCount} üye (diş listesi yok)`}
</p>
</div>
<div className="md:col-span-2">
<p className="text-muted-foreground mb-1 text-xs font-medium uppercase tracking-wide">
Açıklama
@@ -16,6 +16,7 @@ import {
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { TeethChart } from "@/components/teeth-chart";
import { Textarea } from "@/components/ui/textarea";
import { createJobAction } from "@/lib/appwrite/job-actions";
import {
@@ -41,7 +42,7 @@ type PatientOption = { id: string; code: string; label: string };
export function NewJobForm({
labs,
patients,
defaultCurrency,
defaultCurrency: _defaultCurrency,
}: {
labs: JobCounterpart[];
patients: PatientOption[];
@@ -52,6 +53,7 @@ export function NewJobForm({
const [patientId, setPatientId] = useState<string>(
patients.length > 0 ? patients[0].id : NONE_PATIENT,
);
const [teeth, setTeeth] = useState<string[]>([]);
const patientById = useMemo(
() => new Map(patients.map((p) => [p.id, p])),
@@ -152,22 +154,6 @@ export function NewJobForm({
)}
</div>
<div className="grid gap-2">
<Label htmlFor="memberCount">Üye Sayısı *</Label>
<Input
id="memberCount"
name="memberCount"
type="number"
min="1"
max="32"
required
defaultValue={1}
/>
{state.fieldErrors?.memberCount && (
<p className="text-destructive text-xs">{state.fieldErrors.memberCount}</p>
)}
</div>
<div className="grid gap-2">
<Label htmlFor="prostheticType">Protez Türü *</Label>
<Select name="prostheticType" required>
@@ -197,28 +183,15 @@ export function NewJobForm({
<Input id="dueDate" name="dueDate" type="date" />
</div>
<div className="grid grid-cols-[1fr_100px] gap-2">
<div className="grid gap-2">
<Label htmlFor="price">Fiyat</Label>
<Input
id="price"
name="price"
type="number"
step="0.01"
min="0"
placeholder="0.00"
/>
</div>
<div className="grid gap-2">
<Label htmlFor="currency">Para</Label>
<Input
id="currency"
name="currency"
defaultValue={defaultCurrency}
maxLength={8}
style={{ textTransform: "uppercase" }}
/>
</div>
<div className="grid gap-2 md:col-span-2">
<Label>Diş Seçimi * <span className="text-muted-foreground text-xs">(en az 1 diş)</span></Label>
<TeethChart value={teeth} onChange={setTeeth} />
{state.fieldErrors?.teeth && (
<p className="text-destructive text-xs">{state.fieldErrors.teeth}</p>
)}
<p className="text-muted-foreground text-xs">
Fiyat laboratuvarın katalog ve klinik indirimine göre otomatik hesaplanır siz girmiyorsunuz.
</p>
</div>
<div className="grid gap-2 md:col-span-2">
@@ -234,7 +207,7 @@ export function NewJobForm({
</div>
<div className="flex justify-end">
<Button type="submit" disabled={pending || labs.length === 0}>
<Button type="submit" disabled={pending || labs.length === 0 || teeth.length === 0}>
{pending ? (
<>
<Loader2 className="size-4 animate-spin" />