diff --git a/src/app/(dashboard)/finance/loans/components/loan-form-sheet.tsx b/src/app/(dashboard)/finance/loans/components/loan-form-sheet.tsx new file mode 100644 index 0000000..6d87335 --- /dev/null +++ b/src/app/(dashboard)/finance/loans/components/loan-form-sheet.tsx @@ -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 ( + + + + Yeni kredi + + Kaydedince {term || 0} adet taksit otomatik hesaplanır ve eklenir. + + + +
{ + if (fd.get("bankAccountId") === NONE) fd.set("bankAccountId", ""); + formAction(fd); + }} + className="flex flex-1 flex-col" + > +
+
+
+ + + {state.fieldErrors?.bankName && ( +

{state.fieldErrors.bankName}

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

+ Taksit ödemeleri seçilen hesaba expense olarak yazılır. +

+
+ +
+
+ + setPrincipal(Number(e.target.value) || 0)} + /> + {state.fieldErrors?.principal && ( +

{state.fieldErrors.principal}

+ )} +
+
+ + setRate(Number(e.target.value) || 0)} + /> +
+
+ + setTerm(Number(e.target.value) || 0)} + /> +
+
+ +
+
+ + +
+
+ + +
+
+ +
+
+ Aylık taksit + {formatTRY(monthly)} +
+
+ Toplam ödeme + {formatTRY(total)} +
+
+ Toplam faiz + {formatTRY(Math.max(0, total - principal))} +
+
+ +
+ +