feat(banking B): bank loans + amortization schedule
Step 2 of banking. Loan creation auto-generates the full installment schedule using standard amortization (eşit taksitli kredi): monthlyPayment = P × r × (1+r)^n / ((1+r)^n − 1) Schema: - bank_loans: bankAccountId (optional FK), bankName, loanName, loanType enum (consumer/vehicle/housing/commercial/kmh/other), principal, interestRate (monthly nominal %), termMonths, monthlyPayment, startDate, paymentDay (1-28, clamped per month), status (active/closed/defaulted). - loan_installments: loanId, installmentNo, dueDate, amount, principalPart, interestPart, paid, paidAt, financeEntryId. - Indexes on bank_loans(tenantId, status) and loan_installments(tenantId, loanId) and (tenantId, paid, dueDate). Server (lib/appwrite/loan-actions.ts): - createLoanAction: validates with Zod, computes amortization including rounding-drift handling on the last installment, persists loan + N installments, audit-logs. Atomic rollback on failure (deletes any partially-created installments and the loan). - payInstallmentAction: atomically creates a finance_entry (expense, bankAccountId carried over from the loan), updates installment with paid=true + financeEntryId. If it was the last unpaid installment, marks loan status='closed'. - unpayInstallmentAction: deletes the linked finance_entry, clears paid fields, reopens the loan if it was closed. - deleteLoanAction: cascade-deletes all installments first, then the loan. UI (/finance/loans): - 3 stat cards: aktif kredi sayısı, toplam çekilen, kalan ödeme. - Loan card per loan with bank/name/type/status badges, anapara/aylık taksit/faiz/sonraki ödeme grid, progress bar (paid/total), expandable installment table. - Installment row: # / vade (red if overdue) / anapara / faiz / toplam / Ödendi-Geri al toggle. - LoanFormSheet: live preview of monthly payment, total payment, total interest as user changes principal/rate/term. paymentDay clamped 1-28 to avoid month-length issues.
This commit is contained in:
@@ -0,0 +1,254 @@
|
||||
"use client";
|
||||
|
||||
import { useActionState, useEffect, useState } from "react";
|
||||
import { Loader2, Save } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetDescription,
|
||||
SheetFooter,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
} from "@/components/ui/sheet";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
import { createLoanAction } from "@/lib/appwrite/loan-actions";
|
||||
import { initialLoanState } from "@/lib/appwrite/loan-types";
|
||||
import { formatTRY } from "@/lib/format";
|
||||
|
||||
import type { BankAccountOption } from "./types";
|
||||
|
||||
const NONE = "__none__";
|
||||
|
||||
function computeMonthly(principal: number, ratePct: number, n: number): number {
|
||||
if (!principal || !n) return 0;
|
||||
const r = ratePct / 100;
|
||||
if (r === 0) return Number((principal / n).toFixed(2));
|
||||
const factor = Math.pow(1 + r, n);
|
||||
return Number(((principal * r * factor) / (factor - 1)).toFixed(2));
|
||||
}
|
||||
|
||||
export function LoanFormSheet({
|
||||
open,
|
||||
onOpenChange,
|
||||
bankAccounts,
|
||||
}: {
|
||||
open: boolean;
|
||||
onOpenChange: (v: boolean) => void;
|
||||
bankAccounts: BankAccountOption[];
|
||||
}) {
|
||||
const [state, formAction, isPending] = useActionState(createLoanAction, initialLoanState);
|
||||
const [principal, setPrincipal] = useState(0);
|
||||
const [rate, setRate] = useState(2.5);
|
||||
const [term, setTerm] = useState(24);
|
||||
|
||||
useEffect(() => {
|
||||
if (state.ok) {
|
||||
toast.success("Kredi kaydedildi, taksitler oluşturuldu.");
|
||||
onOpenChange(false);
|
||||
} else if (state.error) {
|
||||
toast.error(state.error);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [state]);
|
||||
|
||||
const monthly = computeMonthly(principal, rate, term);
|
||||
const total = monthly * term;
|
||||
const today = new Date().toISOString().slice(0, 10);
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||
<SheetContent className="flex w-full flex-col gap-0 p-0 sm:max-w-xl">
|
||||
<SheetHeader className="border-b px-6 py-4">
|
||||
<SheetTitle>Yeni kredi</SheetTitle>
|
||||
<SheetDescription>
|
||||
Kaydedince {term || 0} adet taksit otomatik hesaplanır ve eklenir.
|
||||
</SheetDescription>
|
||||
</SheetHeader>
|
||||
|
||||
<form
|
||||
action={(fd) => {
|
||||
if (fd.get("bankAccountId") === NONE) fd.set("bankAccountId", "");
|
||||
formAction(fd);
|
||||
}}
|
||||
className="flex flex-1 flex-col"
|
||||
>
|
||||
<div className="flex-1 space-y-4 overflow-y-auto px-6 py-5">
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="bankName">Banka *</Label>
|
||||
<Input id="bankName" name="bankName" required placeholder="Garanti BBVA" />
|
||||
{state.fieldErrors?.bankName && (
|
||||
<p className="text-destructive text-xs">{state.fieldErrors.bankName}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="loanType">Tür</Label>
|
||||
<Select name="loanType" defaultValue="consumer">
|
||||
<SelectTrigger id="loanType">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="consumer">İhtiyaç</SelectItem>
|
||||
<SelectItem value="vehicle">Taşıt</SelectItem>
|
||||
<SelectItem value="housing">Konut</SelectItem>
|
||||
<SelectItem value="commercial">Ticari</SelectItem>
|
||||
<SelectItem value="kmh">KMH</SelectItem>
|
||||
<SelectItem value="other">Diğer</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="loanName">Kredi adı *</Label>
|
||||
<Input id="loanName" name="loanName" required placeholder="Örn. Ofis kredisi" />
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="bankAccountId">Bağlı hesap</Label>
|
||||
<Select name="bankAccountId" defaultValue={NONE} disabled={bankAccounts.length === 0}>
|
||||
<SelectTrigger id="bankAccountId">
|
||||
<SelectValue placeholder={bankAccounts.length === 0 ? "Önce hesap ekleyin" : "Yok"} />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value={NONE}>Yok</SelectItem>
|
||||
{bankAccounts.map((b) => (
|
||||
<SelectItem key={b.id} value={b.id}>
|
||||
{b.label}
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<p className="text-muted-foreground text-xs">
|
||||
Taksit ödemeleri seçilen hesaba expense olarak yazılır.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="principal">Anapara (₺) *</Label>
|
||||
<Input
|
||||
id="principal"
|
||||
name="principal"
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0.01"
|
||||
required
|
||||
value={principal || ""}
|
||||
onChange={(e) => setPrincipal(Number(e.target.value) || 0)}
|
||||
/>
|
||||
{state.fieldErrors?.principal && (
|
||||
<p className="text-destructive text-xs">{state.fieldErrors.principal}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="interestRate">Aylık faiz %</Label>
|
||||
<Input
|
||||
id="interestRate"
|
||||
name="interestRate"
|
||||
type="number"
|
||||
step="0.01"
|
||||
min="0"
|
||||
max="100"
|
||||
required
|
||||
value={rate}
|
||||
onChange={(e) => setRate(Number(e.target.value) || 0)}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="termMonths">Vade (ay) *</Label>
|
||||
<Input
|
||||
id="termMonths"
|
||||
name="termMonths"
|
||||
type="number"
|
||||
min="1"
|
||||
max="480"
|
||||
required
|
||||
value={term}
|
||||
onChange={(e) => setTerm(Number(e.target.value) || 0)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-4 md:grid-cols-2">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="startDate">Başlangıç *</Label>
|
||||
<Input
|
||||
id="startDate"
|
||||
name="startDate"
|
||||
type="date"
|
||||
defaultValue={today}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="paymentDay">Ödeme günü (1-28)</Label>
|
||||
<Input
|
||||
id="paymentDay"
|
||||
name="paymentDay"
|
||||
type="number"
|
||||
min="1"
|
||||
max="28"
|
||||
defaultValue={1}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="bg-muted/40 rounded-md border p-3 text-sm">
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-muted-foreground">Aylık taksit</span>
|
||||
<span className="font-medium tabular-nums">{formatTRY(monthly)}</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-muted-foreground">Toplam ödeme</span>
|
||||
<span className="font-medium tabular-nums">{formatTRY(total)}</span>
|
||||
</div>
|
||||
<div className="flex items-center justify-between">
|
||||
<span className="text-muted-foreground">Toplam faiz</span>
|
||||
<span className="tabular-nums">{formatTRY(Math.max(0, total - principal))}</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="notes">Notlar</Label>
|
||||
<Textarea id="notes" name="notes" rows={3} placeholder="Sözleşme no, kefiller, vb." />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<SheetFooter className="border-t bg-muted/30 px-6 py-4">
|
||||
<div className="flex w-full justify-end gap-2">
|
||||
<Button type="button" variant="outline" onClick={() => onOpenChange(false)} disabled={isPending}>
|
||||
Vazgeç
|
||||
</Button>
|
||||
<Button type="submit" disabled={isPending}>
|
||||
{isPending ? (
|
||||
<>
|
||||
<Loader2 className="size-4 animate-spin" />
|
||||
Oluşturuluyor...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Save className="size-4" />
|
||||
Krediyi kaydet
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</SheetFooter>
|
||||
</form>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,357 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import {
|
||||
Banknote,
|
||||
Check,
|
||||
ChevronDown,
|
||||
ChevronUp,
|
||||
Loader2,
|
||||
Plus,
|
||||
RotateCcw,
|
||||
Trash2,
|
||||
} from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent } from "@/components/ui/card";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import {
|
||||
deleteLoanAction,
|
||||
payInstallmentAction,
|
||||
unpayInstallmentAction,
|
||||
} from "@/lib/appwrite/loan-actions";
|
||||
import { formatDate, formatTRY } from "@/lib/format";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
import { LoanFormSheet } from "./loan-form-sheet";
|
||||
import {
|
||||
type BankAccountOption,
|
||||
type InstallmentRow,
|
||||
LOAN_STATUS_LABEL,
|
||||
LOAN_TYPE_LABEL,
|
||||
type LoanRow,
|
||||
} from "./types";
|
||||
|
||||
type Props = {
|
||||
loans: LoanRow[];
|
||||
installments: InstallmentRow[];
|
||||
bankAccounts: BankAccountOption[];
|
||||
};
|
||||
|
||||
export function LoansClient({ loans, installments, bankAccounts }: Props) {
|
||||
const [formOpen, setFormOpen] = useState(false);
|
||||
const [expanded, setExpanded] = useState<string | null>(null);
|
||||
const [deleting, setDeleting] = useState<LoanRow | null>(null);
|
||||
const [busy, startTransition] = useTransition();
|
||||
|
||||
const totalPrincipal = loans
|
||||
.filter((l) => l.status === "active")
|
||||
.reduce((s, l) => s + l.principal, 0);
|
||||
const totalRemaining = loans
|
||||
.filter((l) => l.status === "active")
|
||||
.reduce((s, l) => s + (l.totalAmount - l.paidAmount), 0);
|
||||
|
||||
const installmentsByLoan = new Map<string, InstallmentRow[]>();
|
||||
for (const i of installments) {
|
||||
const arr = installmentsByLoan.get(i.loanId) ?? [];
|
||||
arr.push(i);
|
||||
installmentsByLoan.set(i.loanId, arr);
|
||||
}
|
||||
|
||||
const togglePay = (inst: InstallmentRow) => {
|
||||
startTransition(async () => {
|
||||
const fd = new FormData();
|
||||
fd.set("id", inst.id);
|
||||
const result = inst.paid ? await unpayInstallmentAction(fd) : await payInstallmentAction(fd);
|
||||
if (result.ok) {
|
||||
toast.success(inst.paid ? "Taksit ödenmedi olarak işaretlendi." : "Taksit ödendi olarak işaretlendi.");
|
||||
} else {
|
||||
toast.error(result.error ?? "İşlem başarısız.");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const handleDelete = () => {
|
||||
if (!deleting) return;
|
||||
startTransition(async () => {
|
||||
const fd = new FormData();
|
||||
fd.set("id", deleting.id);
|
||||
const result = await deleteLoanAction(fd);
|
||||
if (result.ok) {
|
||||
toast.success("Kredi silindi.");
|
||||
setDeleting(null);
|
||||
} else {
|
||||
toast.error(result.error ?? "Silme başarısız.");
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="grid gap-3 md:grid-cols-3">
|
||||
<Card>
|
||||
<CardContent className="p-4">
|
||||
<p className="text-muted-foreground text-xs">Aktif kredi sayısı</p>
|
||||
<p className="mt-1 text-2xl font-semibold">
|
||||
{loans.filter((l) => l.status === "active").length}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardContent className="p-4">
|
||||
<p className="text-muted-foreground text-xs">Toplam çekilen</p>
|
||||
<p className="mt-1 text-2xl font-semibold tabular-nums">{formatTRY(totalPrincipal)}</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<Card>
|
||||
<CardContent className="p-4">
|
||||
<p className="text-muted-foreground text-xs">Kalan ödeme</p>
|
||||
<p className="mt-1 text-2xl font-semibold tabular-nums text-amber-600 dark:text-amber-400">
|
||||
{formatTRY(totalRemaining)}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button onClick={() => setFormOpen(true)}>
|
||||
<Plus className="size-4" />
|
||||
Yeni kredi
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{loans.length === 0 ? (
|
||||
<Card>
|
||||
<CardContent className="flex flex-col items-center gap-2 py-12 text-center">
|
||||
<Banknote className="text-muted-foreground size-8" />
|
||||
<p className="text-sm">Henüz kredi tanımlanmamış.</p>
|
||||
<Button variant="outline" size="sm" onClick={() => setFormOpen(true)}>
|
||||
<Plus className="size-3.5" />
|
||||
İlk krediyi ekle
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
) : (
|
||||
<div className="space-y-4">
|
||||
{loans.map((loan) => {
|
||||
const isOpen = expanded === loan.id;
|
||||
const items = installmentsByLoan.get(loan.id) ?? [];
|
||||
const progressPct =
|
||||
loan.totalAmount > 0 ? (loan.paidAmount / loan.totalAmount) * 100 : 0;
|
||||
return (
|
||||
<Card key={loan.id}>
|
||||
<CardContent className="space-y-3 p-4">
|
||||
<div className="flex flex-wrap items-start justify-between gap-3">
|
||||
<div className="min-w-0 flex-1">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<h3 className="font-semibold">{loan.bankName}</h3>
|
||||
<span className="text-muted-foreground">·</span>
|
||||
<span className="text-sm">{loan.loanName}</span>
|
||||
<Badge variant="outline" className="text-[10px]">
|
||||
{LOAN_TYPE_LABEL[loan.loanType]}
|
||||
</Badge>
|
||||
<Badge
|
||||
variant="outline"
|
||||
className={cn(
|
||||
"text-[10px]",
|
||||
loan.status === "active"
|
||||
? "border-blue-500/30 bg-blue-500/15 text-blue-700 dark:text-blue-300"
|
||||
: loan.status === "closed"
|
||||
? "border-emerald-500/30 bg-emerald-500/15 text-emerald-700 dark:text-emerald-300"
|
||||
: "border-red-500/30 bg-red-500/15 text-red-700 dark:text-red-300",
|
||||
)}
|
||||
>
|
||||
{LOAN_STATUS_LABEL[loan.status]}
|
||||
</Badge>
|
||||
</div>
|
||||
{loan.bankAccountLabel && (
|
||||
<p className="text-muted-foreground mt-1 text-xs">
|
||||
Hesap: {loan.bankAccountLabel}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={() => setExpanded(isOpen ? null : loan.id)}
|
||||
>
|
||||
{isOpen ? (
|
||||
<>
|
||||
<ChevronUp className="size-3.5" />
|
||||
Kapat
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<ChevronDown className="size-3.5" />
|
||||
Taksitler ({items.length})
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="text-destructive hover:text-destructive"
|
||||
onClick={() => setDeleting(loan)}
|
||||
>
|
||||
<Trash2 className="size-3.5" />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid gap-2 md:grid-cols-4">
|
||||
<Stat label="Anapara" value={formatTRY(loan.principal)} />
|
||||
<Stat label="Aylık taksit" value={formatTRY(loan.monthlyPayment)} />
|
||||
<Stat label="Aylık faiz" value={`%${loan.interestRate}`} />
|
||||
<Stat
|
||||
label="Sonraki ödeme"
|
||||
value={loan.nextDue ? formatDate(loan.nextDue) : "—"}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div className="text-muted-foreground flex items-center justify-between text-xs">
|
||||
<span>
|
||||
{loan.remainingCount === 0
|
||||
? "Tüm taksitler ödendi"
|
||||
: `${loan.remainingCount} taksit kaldı`}
|
||||
</span>
|
||||
<span>
|
||||
{formatTRY(loan.paidAmount)} / {formatTRY(loan.totalAmount)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="bg-muted mt-1 h-1.5 overflow-hidden rounded-full">
|
||||
<div
|
||||
className="bg-emerald-500 h-full"
|
||||
style={{ width: `${Math.min(100, progressPct)}%` }}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isOpen && (
|
||||
<div className="border-t pt-3">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead className="w-[40px]">#</TableHead>
|
||||
<TableHead>Vade</TableHead>
|
||||
<TableHead className="text-right">Anapara</TableHead>
|
||||
<TableHead className="text-right">Faiz</TableHead>
|
||||
<TableHead className="text-right">Toplam</TableHead>
|
||||
<TableHead className="text-right">Durum</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{items.map((it) => {
|
||||
const overdue =
|
||||
!it.paid && new Date(it.dueDate) < new Date();
|
||||
return (
|
||||
<TableRow
|
||||
key={it.id}
|
||||
className={cn(it.paid && "opacity-60")}
|
||||
>
|
||||
<TableCell className="font-mono">{it.installmentNo}</TableCell>
|
||||
<TableCell
|
||||
className={cn(
|
||||
"text-muted-foreground text-sm",
|
||||
overdue && "text-destructive font-medium",
|
||||
)}
|
||||
>
|
||||
{formatDate(it.dueDate)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">
|
||||
{formatTRY(it.principalPart)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">
|
||||
{formatTRY(it.interestPart)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right font-medium tabular-nums">
|
||||
{formatTRY(it.amount)}
|
||||
</TableCell>
|
||||
<TableCell className="text-right">
|
||||
<Button
|
||||
variant={it.paid ? "ghost" : "outline"}
|
||||
size="sm"
|
||||
disabled={busy}
|
||||
onClick={() => togglePay(it)}
|
||||
>
|
||||
{busy ? (
|
||||
<Loader2 className="size-3.5 animate-spin" />
|
||||
) : it.paid ? (
|
||||
<>
|
||||
<RotateCcw className="size-3.5" />
|
||||
Geri al
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Check className="size-3.5" />
|
||||
Ödendi
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
);
|
||||
})}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<LoanFormSheet open={formOpen} onOpenChange={setFormOpen} bankAccounts={bankAccounts} />
|
||||
|
||||
<Dialog open={Boolean(deleting)} onOpenChange={(v) => !v && setDeleting(null)}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Krediyi sil</DialogTitle>
|
||||
<DialogDescription>
|
||||
<strong>{deleting?.bankName} — {deleting?.loanName}</strong> ve tüm taksitleri silinecek.
|
||||
Bu işlem geri alınamaz.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<DialogFooter>
|
||||
<Button variant="outline" onClick={() => setDeleting(null)} disabled={busy}>
|
||||
Vazgeç
|
||||
</Button>
|
||||
<Button variant="destructive" onClick={handleDelete} disabled={busy}>
|
||||
{busy ? <Loader2 className="size-4 animate-spin" /> : <Trash2 className="size-4" />}
|
||||
Sil
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function Stat({ label, value }: { label: string; value: string }) {
|
||||
return (
|
||||
<div>
|
||||
<p className="text-muted-foreground text-[10px] uppercase tracking-wide">{label}</p>
|
||||
<p className="mt-0.5 text-sm font-medium tabular-nums">{value}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
export type LoanRow = {
|
||||
id: string;
|
||||
bankName: string;
|
||||
loanName: string;
|
||||
loanType: "consumer" | "vehicle" | "housing" | "commercial" | "kmh" | "other";
|
||||
principal: number;
|
||||
interestRate: number;
|
||||
termMonths: number;
|
||||
monthlyPayment: number;
|
||||
startDate: string;
|
||||
paymentDay: number;
|
||||
status: "active" | "closed" | "defaulted";
|
||||
bankAccountId: string;
|
||||
bankAccountLabel: string;
|
||||
notes: string;
|
||||
totalAmount: number;
|
||||
paidAmount: number;
|
||||
remainingCount: number;
|
||||
nextDue: string | null;
|
||||
};
|
||||
|
||||
export type InstallmentRow = {
|
||||
id: string;
|
||||
loanId: string;
|
||||
installmentNo: number;
|
||||
dueDate: string;
|
||||
amount: number;
|
||||
principalPart: number;
|
||||
interestPart: number;
|
||||
paid: boolean;
|
||||
paidAt: string;
|
||||
};
|
||||
|
||||
export type BankAccountOption = { id: string; label: string };
|
||||
|
||||
export const LOAN_TYPE_LABEL: Record<LoanRow["loanType"], string> = {
|
||||
consumer: "İhtiyaç",
|
||||
vehicle: "Taşıt",
|
||||
housing: "Konut",
|
||||
commercial: "Ticari",
|
||||
kmh: "KMH",
|
||||
other: "Diğer",
|
||||
};
|
||||
|
||||
export const LOAN_STATUS_LABEL: Record<LoanRow["status"], string> = {
|
||||
active: "Aktif",
|
||||
closed: "Kapalı",
|
||||
defaulted: "Temerrüt",
|
||||
};
|
||||
Reference in New Issue
Block a user