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
@@ -0,0 +1,222 @@
"use client";
import { useActionState, useEffect, useState } from "react";
import { DollarSign, Loader2, Trash2 } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
clearClinicPricingAction,
setClinicPricingAction,
} from "@/lib/appwrite/clinic-pricing-actions";
import {
initialClinicPricingActionState,
initialClinicPricingFormState,
} from "@/lib/appwrite/clinic-pricing-types";
import { PROSTHETIC_TYPE_OPTIONS } from "@/lib/appwrite/prosthetic-types";
import type { ClinicPricing, ProstheticType } from "@/lib/appwrite/schema";
const TYPE_LABELS = Object.fromEntries(
PROSTHETIC_TYPE_OPTIONS.map((o) => [o.value, o.label]),
) as Record<string, string>;
export function ClinicPricingDialog({
clinicTenantId,
clinicName,
rows,
defaultCurrency,
}: {
clinicTenantId: string;
clinicName: string;
rows: ClinicPricing[];
defaultCurrency: string;
}) {
const [open, setOpen] = useState(false);
return (
<Dialog open={open} onOpenChange={setOpen}>
<Button size="sm" variant="outline" onClick={() => setOpen(true)}>
<DollarSign className="size-4" />
Fiyatlandırma
</Button>
<DialogContent className="max-w-xl">
<DialogHeader>
<DialogTitle>{clinicName} Fiyatlandırma</DialogTitle>
<DialogDescription>
Protez türü bazında özel fiyat veya yüzdelik indirim tanımlayın.
Boş bırakırsanız katalog fiyatı uygulanır.
</DialogDescription>
</DialogHeader>
<PricingForm
clinicTenantId={clinicTenantId}
defaultCurrency={defaultCurrency}
/>
{rows.length > 0 && (
<div className="space-y-2">
<p className="text-muted-foreground text-xs font-medium uppercase tracking-wide">
Aktif kurallar
</p>
<ul className="divide-y rounded-md border">
{rows.map((r) => (
<PricingRow key={r.$id} row={r} />
))}
</ul>
</div>
)}
<DialogFooter>
<DialogClose asChild>
<Button type="button" variant="outline">
Kapat
</Button>
</DialogClose>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
function PricingForm({
clinicTenantId,
defaultCurrency,
}: {
clinicTenantId: string;
defaultCurrency: string;
}) {
const [state, action, pending] = useActionState(
setClinicPricingAction,
initialClinicPricingFormState,
);
useEffect(() => {
if (state.ok) toast.success("Fiyatlandırma kaydedildi.");
else if (state.error) toast.error(state.error);
}, [state]);
return (
<form action={action} className="grid gap-3 rounded-md border bg-muted/30 p-3">
<input type="hidden" name="clinicTenantId" value={clinicTenantId} />
<div className="grid gap-2">
<Label htmlFor={`type-${clinicTenantId}`}>Protez Türü</Label>
<Select name="prostheticType" required defaultValue={PROSTHETIC_TYPE_OPTIONS[0].value}>
<SelectTrigger id={`type-${clinicTenantId}`}>
<SelectValue />
</SelectTrigger>
<SelectContent>
{PROSTHETIC_TYPE_OPTIONS.map((o) => (
<SelectItem key={o.value} value={o.value}>
{o.label}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
<div className="grid grid-cols-3 gap-2">
<div className="grid gap-2">
<Label htmlFor={`custom-${clinicTenantId}`}>Özel Birim Fiyat</Label>
<Input
id={`custom-${clinicTenantId}`}
name="customUnitPrice"
type="number"
min="0"
step="0.01"
placeholder="—"
/>
{state.fieldErrors?.customUnitPrice && (
<p className="text-destructive text-xs">{state.fieldErrors.customUnitPrice}</p>
)}
</div>
<div className="grid gap-2">
<Label htmlFor={`disc-${clinicTenantId}`}>İndirim %</Label>
<Input
id={`disc-${clinicTenantId}`}
name="discountPercent"
type="number"
min="0"
max="100"
step="1"
placeholder="—"
/>
{state.fieldErrors?.discountPercent && (
<p className="text-destructive text-xs">{state.fieldErrors.discountPercent}</p>
)}
</div>
<div className="grid gap-2">
<Label htmlFor={`cur-${clinicTenantId}`}>Para</Label>
<Input
id={`cur-${clinicTenantId}`}
name="currency"
defaultValue={defaultCurrency}
maxLength={8}
style={{ textTransform: "uppercase" }}
/>
</div>
</div>
<p className="text-muted-foreground text-xs">
Özel fiyat girilirse katalogdan bağımsız uygulanır. Sadece indirim verirseniz katalog fiyatından oran düşülür.
</p>
<Button type="submit" size="sm" disabled={pending}>
{pending ? <Loader2 className="size-4 animate-spin" /> : <DollarSign className="size-4" />}
Kaydet / Güncelle
</Button>
</form>
);
}
function PricingRow({ row }: { row: ClinicPricing }) {
const [state, action, pending] = useActionState(
clearClinicPricingAction,
initialClinicPricingActionState,
);
useEffect(() => {
if (state.ok) toast.success("Kural silindi.");
else if (state.error) toast.error(state.error);
}, [state]);
return (
<li className="flex items-center gap-3 px-3 py-2 text-sm">
<span className="flex-1 font-medium">
{TYPE_LABELS[row.prostheticType] ?? row.prostheticType}
</span>
<span className="text-muted-foreground text-xs">
{row.customUnitPrice !== undefined && row.customUnitPrice !== null ? (
<>Özel fiyat: <span className="tabular-nums">{row.customUnitPrice}</span> {row.currency || "TRY"}</>
) : row.discountPercent ? (
<>İndirim: %{row.discountPercent}</>
) : (
"—"
)}
</span>
<form action={action}>
<input type="hidden" name="id" value={row.$id} />
<Button type="submit" size="sm" variant="ghost" disabled={pending}>
{pending ? <Loader2 className="size-4 animate-spin" /> : <Trash2 className="size-4" />}
</Button>
</form>
</li>
);
}
type ProstheticTypeLabel = ProstheticType;
export type { ProstheticTypeLabel };
@@ -27,6 +27,13 @@ import {
import { deleteConnectionAction } from "@/lib/appwrite/connection-actions";
import { initialConnectionActionState } from "@/lib/appwrite/connection-types";
import type { ConnectionWithCounterpart } from "@/lib/appwrite/connection-queries";
import type { ClinicPricing, TenantKind } from "@/lib/appwrite/schema";
import { PROSTHETIC_TYPE_OPTIONS } from "@/lib/appwrite/prosthetic-types";
import { ClinicPricingDialog } from "./clinic-pricing-dialog";
const TYPE_LABELS = Object.fromEntries(
PROSTHETIC_TYPE_OPTIONS.map((o) => [o.value, o.label]),
) as Record<string, string>;
const dateFormatter = new Intl.DateTimeFormat("tr-TR", {
day: "2-digit",
@@ -34,7 +41,17 @@ const dateFormatter = new Intl.DateTimeFormat("tr-TR", {
year: "numeric",
});
export function ConnectionsTable({ rows }: { rows: ConnectionWithCounterpart[] }) {
export function ConnectionsTable({
rows,
selfKind,
pricingByCounterpart = {},
defaultCurrency = "TRY",
}: {
rows: ConnectionWithCounterpart[];
selfKind: TenantKind | null;
pricingByCounterpart?: Record<string, ClinicPricing[]>;
defaultCurrency?: string;
}) {
if (rows.length === 0) {
return (
<p className="text-muted-foreground py-6 text-center text-sm">
@@ -43,6 +60,8 @@ export function ConnectionsTable({ rows }: { rows: ConnectionWithCounterpart[] }
);
}
const isLab = selfKind === "lab";
return (
<Table>
<TableHeader>
@@ -50,19 +69,40 @@ export function ConnectionsTable({ rows }: { rows: ConnectionWithCounterpart[] }
<TableHead>Karşı taraf</TableHead>
<TableHead>Tür</TableHead>
<TableHead>Onay tarihi</TableHead>
<TableHead>{isLab ? "Fiyat kuralları" : "Sizin için kurallar"}</TableHead>
<TableHead className="text-right">İşlem</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{rows.map((r) => (
<ApprovedRow key={r.$id} row={r} />
))}
{rows.map((r) => {
const counterpartId = isLab ? r.clinicTenantId : r.labTenantId;
const pricing = pricingByCounterpart[counterpartId] ?? [];
return (
<ApprovedRow
key={r.$id}
row={r}
isLab={isLab}
pricing={pricing}
defaultCurrency={defaultCurrency}
/>
);
})}
</TableBody>
</Table>
);
}
function ApprovedRow({ row }: { row: ConnectionWithCounterpart }) {
function ApprovedRow({
row,
isLab,
pricing,
defaultCurrency,
}: {
row: ConnectionWithCounterpart;
isLab: boolean;
pricing: ClinicPricing[];
defaultCurrency: string;
}) {
const [state, action, pending] = useActionState(
deleteConnectionAction,
initialConnectionActionState,
@@ -86,6 +126,8 @@ function ApprovedRow({ row }: { row: ConnectionWithCounterpart }) {
? "Klinik"
: "—";
const counterpartId = isLab ? row.clinicTenantId : row.labTenantId;
return (
<TableRow>
<TableCell className="font-medium">{row.counterpart?.companyName ?? "—"}</TableCell>
@@ -95,7 +137,38 @@ function ApprovedRow({ row }: { row: ConnectionWithCounterpart }) {
<TableCell className="text-muted-foreground">
{row.approvedAt ? dateFormatter.format(new Date(row.approvedAt)) : "—"}
</TableCell>
<TableCell className="text-muted-foreground text-xs">
{pricing.length === 0 ? (
<span>Katalog fiyatı</span>
) : (
<ul className="space-y-0.5">
{pricing.slice(0, 3).map((p) => (
<li key={p.$id}>
<span className="text-foreground">
{TYPE_LABELS[p.prostheticType] ?? p.prostheticType}
</span>
{": "}
{p.customUnitPrice !== undefined && p.customUnitPrice !== null
? `${p.customUnitPrice} ${p.currency || defaultCurrency}`
: p.discountPercent
? `%${p.discountPercent} indirim`
: "—"}
</li>
))}
{pricing.length > 3 && <li>+ {pricing.length - 3} kural</li>}
</ul>
)}
</TableCell>
<TableCell className="text-right">
<div className="flex justify-end gap-2">
{isLab && (
<ClinicPricingDialog
clinicTenantId={counterpartId}
clinicName={row.counterpart?.companyName ?? "Klinik"}
rows={pricing}
defaultCurrency={defaultCurrency}
/>
)}
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button size="sm" variant="outline">
@@ -129,6 +202,7 @@ function ApprovedRow({ row }: { row: ConnectionWithCounterpart }) {
</DialogFooter>
</DialogContent>
</Dialog>
</div>
</TableCell>
</TableRow>
);
+41 -2
View File
@@ -1,11 +1,13 @@
import { redirect } from "next/navigation";
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
import { listClinicPricing } from "@/lib/appwrite/clinic-pricing-queries";
import {
listApprovedConnections,
listPendingInbound,
listPendingOutbound,
} from "@/lib/appwrite/connection-queries";
import type { ClinicPricing } from "@/lib/appwrite/schema";
import { requireTenant } from "@/lib/appwrite/tenant-guard";
import { ConnectionCodeCard } from "./components/connection-code-card";
import { ConnectionRequestForm } from "./components/connection-request-form";
@@ -35,6 +37,35 @@ export default async function ConnectionsPage() {
listPendingOutbound(ctx.tenantId, ctx.user.id),
]);
// Lab side: load pricing rules per approved clinic so the dialog can
// surface existing rules. Clinic side leaves this empty — it's display-only
// for them (see ConnectionsTable).
const pricingByCounterpart = new Map<string, ClinicPricing[]>();
if (isLab) {
const pricingLists = await Promise.all(
approved.map((c) =>
listClinicPricing(ctx.tenantId, c.clinicTenantId).then(
(rows) => [c.clinicTenantId, rows] as const,
),
),
);
for (const [id, rows] of pricingLists) pricingByCounterpart.set(id, rows);
} else {
// Clinic side: only its own pricing rules from each lab, read-only
const pricingLists = await Promise.all(
approved.map((c) =>
listClinicPricing(c.labTenantId, ctx.tenantId).then(
(rows) => [c.labTenantId, rows] as const,
),
),
);
for (const [id, rows] of pricingLists) pricingByCounterpart.set(id, rows);
}
const pricingObject: Record<string, ClinicPricing[]> = Object.fromEntries(
pricingByCounterpart,
);
const defaultCurrency = ctx.settings?.defaultCurrency ?? "TRY";
return (
<div className="flex-1 space-y-6 px-6">
<div className="flex flex-col gap-1">
@@ -90,10 +121,18 @@ export default async function ConnectionsPage() {
<Card>
<CardHeader>
<CardTitle>Bağlantılarım</CardTitle>
<CardDescription>Onaylanmış aktif bağlantılar.</CardDescription>
<CardDescription>
Onaylanmış aktif bağlantılar.
{isLab && " Lab tarafı her klinik için özel fiyat/indirim tanımlayabilir."}
</CardDescription>
</CardHeader>
<CardContent>
<ConnectionsTable rows={approved} />
<ConnectionsTable
rows={approved}
selfKind={ctx.kind}
pricingByCounterpart={pricingObject}
defaultCurrency={defaultCurrency}
/>
</CardContent>
</Card>
</div>
+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" />