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
+66
View File
@@ -0,0 +1,66 @@
import { z } from "zod";
const PROSTHETIC_TYPES = [
"metal_porselen",
"zirkonyum",
"implant_ustu_zirkonyum",
"gecici",
"e_max",
"diger",
] as const;
function toOptionalNumber(v: unknown): number | undefined {
if (v === undefined || v === null || v === "") return undefined;
const n = typeof v === "number" ? v : Number(String(v).replace(",", "."));
return Number.isFinite(n) ? n : undefined;
}
export const clinicPricingSchema = z
.object({
clinicTenantId: z.string().min(1, "Klinik seçin."),
prostheticType: z.enum(PROSTHETIC_TYPES, { message: "Protez türü seçin." }),
customUnitPrice: z
.union([z.string(), z.number(), z.literal("")])
.optional()
.transform(toOptionalNumber),
discountPercent: z
.union([z.string(), z.number(), z.literal("")])
.optional()
.transform(toOptionalNumber),
currency: z
.string()
.trim()
.max(8)
.optional()
.transform((v) => (v ? v.toUpperCase() : undefined)),
})
.superRefine((data, ctx) => {
if (
data.customUnitPrice === undefined &&
data.discountPercent === undefined
) {
ctx.addIssue({
code: "custom",
path: ["customUnitPrice"],
message: "Özel fiyat veya indirim oranı girin.",
});
}
if (data.discountPercent !== undefined) {
if (data.discountPercent < 0 || data.discountPercent > 100) {
ctx.addIssue({
code: "custom",
path: ["discountPercent"],
message: "İndirim 0-100 arası olmalı.",
});
}
}
if (data.customUnitPrice !== undefined && data.customUnitPrice < 0) {
ctx.addIssue({
code: "custom",
path: ["customUnitPrice"],
message: "Negatif fiyat olamaz.",
});
}
});
export type ClinicPricingInput = z.infer<typeof clinicPricingSchema>;
+6 -22
View File
@@ -9,6 +9,8 @@ const PROSTHETIC_TYPES = [
"diger",
] as const;
const FDI_TOOTH = /^(1[1-8]|2[1-8]|3[1-8]|4[1-8])$/;
export const createJobSchema = z.object({
labTenantId: z.string().min(1, "Laboratuvar seçin."),
patientId: z
@@ -18,14 +20,10 @@ export const createJobSchema = z.object({
.transform((v) => (v ? v : undefined)),
patientCode: z.string().trim().min(1, "Hasta kodu zorunlu.").max(50),
prostheticType: z.enum(PROSTHETIC_TYPES, { message: "Protez türü seçin." }),
memberCount: z
.union([z.string(), z.number()])
.transform((v) => {
if (typeof v === "number") return v;
const n = parseInt(v, 10);
return Number.isFinite(n) ? n : NaN;
})
.pipe(z.number().int().min(1, "En az 1 üye.").max(32, "En fazla 32 üye.")),
teeth: z
.array(z.string().regex(FDI_TOOTH, "Geçersiz diş numarası"))
.min(1, "En az 1 diş seçin.")
.max(32, "En fazla 32 diş seçilebilir."),
color: z
.string()
.trim()
@@ -38,20 +36,6 @@ export const createJobSchema = z.object({
.max(2000)
.optional()
.transform((v) => (v ? v : undefined)),
price: z
.union([z.string(), z.number()])
.optional()
.transform((v) => {
if (v === undefined || v === "") return undefined;
const n = typeof v === "number" ? v : Number(String(v).replace(",", "."));
return Number.isFinite(n) ? n : undefined;
}),
currency: z
.string()
.trim()
.max(8)
.optional()
.transform((v) => (v ? v.toUpperCase() : "TRY")),
dueDate: z
.string()
.trim()