diff --git a/src/pages/accounting/AccountingReconcileDetailPage.tsx b/src/pages/accounting/AccountingReconcileDetailPage.tsx index 408e00c..ed907b1 100644 --- a/src/pages/accounting/AccountingReconcileDetailPage.tsx +++ b/src/pages/accounting/AccountingReconcileDetailPage.tsx @@ -90,8 +90,8 @@ export default function AccountingReconcileDetailPage() { // Add a deposit/withdrawal directly from the reconciliation screen. const [addOpen, setAddOpen] = useState(false); const [addSaving, setAddSaving] = useState(false); - const [addTx, setAddTx] = useState<{ type: "credit" | "debit"; date: string; amount: string; description: string; coa_account_id: string; reference: string }>( - { type: "credit", date: "", amount: "", description: "", coa_account_id: "", reference: "" }, + const [addTx, setAddTx] = useState<{ type: "credit" | "debit"; date: string; amount: string; description: string; coa_account_id: string; reference: string; vendor_id: string }>( + { type: "credit", date: "", amount: "", description: "", coa_account_id: "", reference: "", vendor_id: "" }, ); const { data: account } = useQuery({ @@ -128,6 +128,13 @@ export default function AccountingReconcileDetailPage() { (await accounting.from("accounts").select("id,name,type,is_bank").eq("company_id", cid).eq("is_archived", false).order("name")).data ?? [], }); + const { data: vendors = [] } = useQuery({ + queryKey: ["vendors-lookup", cid], + enabled: !!cid, + queryFn: async () => + (await accounting.from("vendors").select("id,name").eq("company_id", cid).order("name")).data ?? [], + }); + const { data: txs = [] } = useQuery({ queryKey: ["recon-txs", accountId, active?.statement_end_date, priorReconDate], enabled: !!accountId && !!active, @@ -328,7 +335,7 @@ export default function AccountingReconcileDetailPage() { setAddTx({ type, date: active?.statement_end_date ?? new Date().toLocaleDateString("en-CA", { timeZone: "America/New_York" }), - amount: "", description: "", coa_account_id: "", reference: "", + amount: "", description: "", coa_account_id: "", reference: "", vendor_id: "", }); setAddOpen(true); }; @@ -339,6 +346,7 @@ export default function AccountingReconcileDetailPage() { if (!amt || amt <= 0) return toast.error("Enter an amount"); if (!addTx.description.trim()) return toast.error("Enter a description"); if (!addTx.coa_account_id) return toast.error("Pick a category account"); + if (addTx.type === "debit" && !addTx.vendor_id) return toast.error("Vendor is required for withdrawals"); setAddSaving(true); try { const coaName = (allAccounts as any[]).find((a) => a.id === addTx.coa_account_id)?.name ?? ""; @@ -353,6 +361,7 @@ export default function AccountingReconcileDetailPage() { type: addTx.type, // credit = deposit (money in), debit = withdrawal (money out) category: coaName, coa_account_id: addTx.coa_account_id, + vendor_id: addTx.type === "debit" ? (addTx.vendor_id || null) : null, reference: addTx.reference.trim() || null, cleared: true, }) @@ -687,6 +696,20 @@ export default function AccountingReconcileDetailPage() { + {addTx.type === "debit" && ( +
+ + + {(vendors as any[]).length === 0 && ( +

No vendors yet — add one on the Vendors page first.

+ )} +
+ )}
setAddTx({ ...addTx, reference: e.target.value })} placeholder="Check # / memo" />