"use client"; import { useState } from "react"; import { useQuery, useMutation } from "@tanstack/react-query"; import { toast } from "sonner"; import { ArrowRight } from "lucide-react"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/Card"; import { Button } from "@/components/ui/Button"; import { Input } from "@/components/ui/Input"; import { Table, TableHead, TableBody, TableRow, Th, Td, EmptyState } from "@/components/ui/Table"; import { Badge } from "@/components/ui/Badge"; import { Modal } from "@/components/ui/Modal"; import { AccountingNav } from "@/components/accounting/AccountingNav"; import { formatCurrency } from "@/lib/utils"; import api from "@/lib/api"; import type { CompanyAccount, CompanyAccountType, Transfer } from "@/types/index"; function formatDate(iso: string) { return new Date(iso).toLocaleDateString("en-PH", { year: "numeric", month: "short", day: "numeric" }); } const TYPE_COLORS: Record = { CASH: "success", BANK: "default", EWALLET: "warning", }; const ACCOUNT_TYPES: CompanyAccountType[] = ["CASH", "BANK", "EWALLET"]; export default function CompanyAccountsPage() { const today = new Date().toISOString().split("T")[0]; const [showAdd, setShowAdd] = useState(false); const [addForm, setAddForm] = useState({ name: "", type: "BANK" as CompanyAccountType, bankName: "", accountNumber: "", initialBalance: "0", }); const [transferAccount, setTransferAccount] = useState(null); const [transferForm, setTransferForm] = useState({ toAccountId: "", amount: "", date: today, note: "" }); const { data: accounts = [], isLoading, refetch: refetchAccounts } = useQuery({ queryKey: ["company-accounts"], queryFn: async () => { const res = await api.get("/api/v1/company-accounts"); const d = res.data; return Array.isArray(d) ? d : (d as { data: CompanyAccount[] }).data ?? []; }, }); const { data: transfers = [], refetch: refetchTransfers } = useQuery({ queryKey: ["transfers"], queryFn: async () => { const res = await api.get("/api/v1/transfers"); const d = res.data; return Array.isArray(d) ? d : (d as { data: Transfer[] }).data ?? []; }, }); const addMutation = useMutation({ mutationFn: async () => { await api.post("/api/v1/company-accounts", { name: addForm.name, type: addForm.type, bankName: addForm.bankName || undefined, accountNumber: addForm.accountNumber || undefined, initialBalance: parseFloat(addForm.initialBalance) || 0, }); }, onSuccess: () => { toast.success("Account added"); setShowAdd(false); setAddForm({ name: "", type: "BANK", bankName: "", accountNumber: "", initialBalance: "0" }); refetchAccounts(); }, onError: () => toast.error("Failed to add account"), }); const transferMutation = useMutation({ mutationFn: async () => { if (!transferAccount) return; await api.post("/api/v1/transfers", { fromAccountId: transferAccount.id, toAccountId: transferForm.toAccountId, amount: parseFloat(transferForm.amount), date: transferForm.date, note: transferForm.note || undefined, }); }, onSuccess: () => { toast.success("Transfer completed"); setTransferAccount(null); setTransferForm({ toAccountId: "", amount: "", date: today, note: "" }); refetchAccounts(); refetchTransfers(); }, onError: () => toast.error("Failed to process transfer"), }); const toAccounts = accounts.filter((a) => a.id !== transferAccount?.id); return (

Accounting

Company cash, bank, and e-wallet accounts

{/* Account Cards */} {isLoading ? (
{Array.from({ length: 3 }).map((_, i) => (
))}
) : accounts.length === 0 ? ( No company accounts yet. Add your first account to get started. ) : (
{accounts.map((acct) => (

{acct.name}

{acct.bankName && (

{acct.bankName}

)} {acct.accountNumber && (

{acct.accountNumber}

)}
{acct.type}

{formatCurrency(Number(acct.balance))}

))}
)} {/* Recent Transfers */} Recent Transfers {transfers.length === 0 ? ( ) : ( transfers.slice(0, 20).map((t) => ( )) )}
Date From To Amount Note {formatDate(t.date)} {t.fromAccount?.name ?? "—"} {t.toAccount?.name ?? "—"} {formatCurrency(Number(t.amount))} {t.note ?? "—"}
{/* Add Account Modal */} setShowAdd(false)} title="Add Company Account">
{ e.preventDefault(); addMutation.mutate(); }} className="space-y-4"> setAddForm((f) => ({ ...f, name: e.target.value }))} placeholder="e.g. BDO Savings" />
setAddForm((f) => ({ ...f, bankName: e.target.value }))} placeholder="e.g. BDO" /> setAddForm((f) => ({ ...f, accountNumber: e.target.value }))} placeholder="e.g. 0012-3456-7890" />
setAddForm((f) => ({ ...f, initialBalance: e.target.value }))} />
{/* Transfer Modal */} setTransferAccount(null)} title={`Transfer from ${transferAccount?.name ?? ""}`} >
{ e.preventDefault(); transferMutation.mutate(); }} className="space-y-4">
Available balance: {formatCurrency(Number(transferAccount?.balance ?? 0))}
setTransferForm((f) => ({ ...f, amount: e.target.value }))} placeholder="0.00" /> setTransferForm((f) => ({ ...f, date: e.target.value }))} />
setTransferForm((f) => ({ ...f, note: e.target.value }))} placeholder="Optional note" />
); }