diff --git a/src/app/(dashboard)/finance/components/balances-card.tsx b/src/app/(dashboard)/finance/components/balances-card.tsx new file mode 100644 index 0000000..7c7779b --- /dev/null +++ b/src/app/(dashboard)/finance/components/balances-card.tsx @@ -0,0 +1,100 @@ +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; +import type { CounterpartBalance } from "@/lib/appwrite/payment-queries"; +import { RecordPaymentDialog } from "./record-payment-dialog"; + +function formatMoney(amount: number, currency: string): string { + try { + return new Intl.NumberFormat("tr-TR", { style: "currency", currency }).format(amount); + } catch { + return `${amount.toFixed(2)} ${currency}`; + } +} + +const dateFormatter = new Intl.DateTimeFormat("tr-TR", { + day: "2-digit", + month: "2-digit", + year: "numeric", +}); + +export function BalancesCard({ + balances, + counterpartNames, + selfKind, + defaultCurrency, +}: { + balances: CounterpartBalance[]; + counterpartNames: Record; + selfKind: "lab" | "clinic"; + defaultCurrency: string; +}) { + const isLab = selfKind === "lab"; + return ( + + + {isLab ? "Klinik Bakiyeleri" : "Laboratuvar Bakiyeleri"} + + {isLab + ? "Her klinik için açık bakiye ve son tahsilatlar. Toplu ödemeleri buradan girebilirsiniz." + : "Çalıştığınız her laboratuvar için açık bakiye ve son ödemeleriniz."} + + + + {balances.length === 0 ? ( +

+ Bağlantılarınız için henüz finansal hareket yok. +

+ ) : ( +
    + {balances.map((b) => { + const name = counterpartNames[b.counterpartTenantId] ?? "—"; + const settled = b.open <= 0.01; + return ( +
  • +
    +

    {name}

    +

    + Fatura: {formatMoney(b.invoiced, b.currency)} · Ödenen:{" "} + {formatMoney(b.paid, b.currency)} + {b.lastPaymentAt && ( + <> + {" "}· Son ödeme:{" "} + {dateFormatter.format(new Date(b.lastPaymentAt))} + + )} +

    +
    +
    +

    + {formatMoney(b.open, b.currency)} +

    +

    + {settled ? "Kapalı" : isLab ? "Açık alacak" : "Açık borç"} +

    +
    + 0 ? b.open : undefined} + /> +
  • + ); + })} +
+ )} +
+
+ ); +} diff --git a/src/app/(dashboard)/finance/components/record-payment-dialog.tsx b/src/app/(dashboard)/finance/components/record-payment-dialog.tsx new file mode 100644 index 0000000..e0576a5 --- /dev/null +++ b/src/app/(dashboard)/finance/components/record-payment-dialog.tsx @@ -0,0 +1,173 @@ +"use client"; + +import { useActionState, useEffect, useState } from "react"; +import { useRouter } from "next/navigation"; +import { Banknote, Loader2 } 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 { Textarea } from "@/components/ui/textarea"; +import { recordPaymentAction } from "@/lib/appwrite/payment-actions"; +import { + PAYMENT_METHOD_OPTIONS, + initialPaymentFormState, +} from "@/lib/appwrite/payment-types"; + +export function RecordPaymentDialog({ + counterpartTenantId, + counterpartName, + selfKind, + defaultCurrency, + openAmount, + triggerLabel, +}: { + counterpartTenantId: string; + counterpartName: string; + selfKind: "lab" | "clinic"; + defaultCurrency: string; + openAmount?: number; + triggerLabel?: string; +}) { + const router = useRouter(); + const [open, setOpen] = useState(false); + const [state, action, pending] = useActionState( + recordPaymentAction, + initialPaymentFormState, + ); + + useEffect(() => { + if (state.ok) { + toast.success("Ödeme kaydedildi."); + setOpen(false); + router.refresh(); + } else if (state.error) { + toast.error(state.error); + } + }, [state, router]); + + const label = triggerLabel ?? (selfKind === "lab" ? "Ödeme Al" : "Ödeme Yap"); + const title = selfKind === "lab" ? "Tahsilat Kaydı" : "Ödeme Kaydı"; + const today = new Date().toISOString().slice(0, 10); + + return ( + + + + + {title} — {counterpartName} + + {selfKind === "lab" + ? "Bu kliniğin yaptığı toplu ödemeyi kaydedin. Açık bakiyeden otomatik düşülür." + : "Bu laboratuvara yaptığınız ödemeyi kaydedin."} + {typeof openAmount === "number" && ( + <> + {" "}Açık bakiye:{" "} + + {openAmount.toLocaleString("tr-TR")} {defaultCurrency} + + + )} + + +
+ +
+
+ + + {state.fieldErrors?.amount && ( +

{state.fieldErrors.amount}

+ )} +
+
+ + +
+
+
+
+ + +
+
+ + +
+
+
+ +