/** * 付款對話框組件 */ import { useState, useEffect } from "react"; import { DollarSign } from "lucide-react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "../ui/dialog"; import { Button } from "../ui/button"; import { Label } from "../ui/label"; import { Input } from "../ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "../ui/select"; import { Switch } from "../ui/switch"; import { PAYMENT_METHODS, INVOICE_TYPES } from "../../constants/purchase-order"; import type { PurchaseOrder, PaymentMethod, InvoiceType, PaymentInfo } from "../../types/purchase-order"; interface PaymentDialogProps { order: PurchaseOrder; open: boolean; onOpenChange: (open: boolean) => void; onConfirm: (orderId: string, paymentInfo: PaymentInfo) => void; } export function PaymentDialog({ order, open, onOpenChange, onConfirm, }: PaymentDialogProps) { // 付款資訊 const [paymentMethod, setPaymentMethod] = useState("bank_transfer"); const [paymentDate, setPaymentDate] = useState( new Date().toISOString().split("T")[0] ); const [actualAmount, setActualAmount] = useState(order.totalAmount.toString()); // 發票資訊 const [hasInvoice, setHasInvoice] = useState(false); const [invoiceNumber, setInvoiceNumber] = useState(""); const [invoiceAmount, setInvoiceAmount] = useState(order.totalAmount.toString()); const [invoiceDate, setInvoiceDate] = useState( new Date().toISOString().split("T")[0] ); const [invoiceType, setInvoiceType] = useState("duplicate"); const [companyName, setCompanyName] = useState(""); const [taxId, setTaxId] = useState(""); // 當訂單變更時重置表單 useEffect(() => { if (open) { setActualAmount(order.totalAmount.toString()); setInvoiceAmount(order.totalAmount.toString()); } }, [order.totalAmount, open]); const handleConfirm = () => { const paymentInfo: PaymentInfo = { paymentMethod, paymentDate, actualAmount: parseFloat(actualAmount), paidBy: "付款人名稱", // 實際應從登入使用者取得 paidAt: new Date().toLocaleString("zh-TW"), hasInvoice, }; if (hasInvoice) { paymentInfo.invoice = { invoiceNumber, invoiceAmount: parseFloat(invoiceAmount), invoiceDate, invoiceType, }; if (invoiceType === "triplicate") { paymentInfo.invoice.companyName = companyName; paymentInfo.invoice.taxId = taxId; } } onConfirm(order.id, paymentInfo); handleClose(); }; const handleClose = () => { // 重置表單 setPaymentMethod("bank_transfer"); setPaymentDate(new Date().toISOString().split("T")[0]); setActualAmount(order.totalAmount.toString()); setHasInvoice(false); setInvoiceNumber(""); setInvoiceAmount(order.totalAmount.toString()); setInvoiceDate(new Date().toISOString().split("T")[0]); setInvoiceType("duplicate"); setCompanyName(""); setTaxId(""); onOpenChange(false); }; const isValid = () => { // 驗證付款資訊 if (!paymentMethod || !paymentDate || !actualAmount) { return false; } const amount = parseFloat(actualAmount); if (isNaN(amount) || amount <= 0) { return false; } // 如果有發票,驗證發票資訊 if (hasInvoice) { if (!invoiceNumber || !invoiceDate || !invoiceType) { return false; } const invAmount = parseFloat(invoiceAmount); if (isNaN(invAmount) || invAmount <= 0) { return false; } // 如果是三聯式,必須填寫公司抬頭和統編 if (invoiceType === "triplicate") { if (!companyName || !taxId) { return false; } } } return true; }; return ( 標記為已付款 採購單編號:{order.poNumber}
{/* 採購單資訊摘要 */}
廠商: {order.supplierName}
採購總金額: ${order.totalAmount.toLocaleString()}
{/* 付款資訊區塊 */}

付款資訊

{/* 付款方式 */}
{/* 付款日期 */}
setPaymentDate(e.target.value)} className="h-11 border-2 border-input" />
{/* 實際付款金額 */}
setActualAmount(e.target.value)} className="h-11 border-2 border-input" />
{/* 發票資訊區塊 */}

是否有發票

請選擇此筆採購是否有開立發票

{/* 發票欄位(條件顯示) */} {hasInvoice && (
{/* 發票號碼 */}
setInvoiceNumber(e.target.value)} placeholder="例:AA-12345678" className="h-11 border-2 border-input bg-background" />
{/* 發票金額 */}
setInvoiceAmount(e.target.value)} className="h-11 border-2 border-input bg-background" />
{/* 發票日期 */}
setInvoiceDate(e.target.value)} className="h-11 border-2 border-input bg-background" />
{/* 發票類型 */}
{/* 三聯式專用欄位 */} {invoiceType === "triplicate" && ( <>
setCompanyName(e.target.value)} placeholder="請輸入公司抬頭" className="h-11 border-2 border-input bg-background" />
setTaxId(e.target.value)} placeholder="請輸入統一編號" maxLength={8} className="h-11 border-2 border-input bg-background" />
)}
)}
); }