import { type FC, type ChangeEvent } from "react"; import Input from "@/components/Input"; import SwitchComponent from "@/components/Switch"; import ProductsSelect from "@/pages/order/components/ProductsSelect"; import type { CreatePreInvoiceItemType } from "../types/Types"; import { clampDiscountPercent, clampDiscountValue, getItemLineTotal, getItemSubTotal, } from "../utils/invoiceItem"; import { Trash } from "iconsax-react"; import { Link } from "react-router-dom"; import { Paths } from "@/config/Paths"; type Props = { item: CreatePreInvoiceItemType; index: number; onChange: ( index: number, field: keyof CreatePreInvoiceItemType, value: string | number | null, ) => void; onRemove: () => void; canRemove: boolean; onConvertToOrder?: () => void; error?: Partial>; }; const InvoiceItemRow: FC = ({ item, index, onChange, onRemove, canRemove, onConvertToOrder, error, }) => { const total = getItemLineTotal(item); const subTotal = getItemSubTotal(item); const isPercentDiscount = item.discountPercent != null; const convertToOrderUrl = item.id ? `${Paths.convertToOrder}?${new URLSearchParams({ invoiceItemId: item.id, productId: item.productId, }).toString()}` : ""; const handleProductChange = (e: ChangeEvent) => { onChange(index, "productId", e.target.value); }; const handleDiscountTypeChange = (isPercent: boolean) => { if (isPercent) { onChange(index, "discount", 0); onChange(index, "discountPercent", 0); return; } onChange(index, "discountPercent", null); onChange(index, "discount", 0); }; return (
onChange(index, "description", e.target.value)} error_text={error?.description} />
{ const nextQuantity = e.target.value ? Number(e.target.value) : 0; const nextSubTotal = nextQuantity * (item.unitPrice || 0); onChange(index, "quantity", nextQuantity); if (!isPercentDiscount) { onChange(index, "discount", clampDiscountValue(item.discount, nextSubTotal)); } }} error_text={error?.quantity} />
{ const raw = String(e.target.value).replace(/,/g, ""); const nextUnitPrice = raw ? Number(raw) : 0; const nextSubTotal = (item.quantity || 0) * nextUnitPrice; onChange(index, "unitPrice", nextUnitPrice); if (!isPercentDiscount) { onChange(index, "discount", clampDiscountValue(item.discount, nextSubTotal)); } }} error_text={error?.unitPrice} />
تخفیف
درصد مبلغ
{ const raw = String(e.target.value).replace(/,/g, ""); const value = raw ? Number(raw) : 0; if (isPercentDiscount) { onChange(index, "discountPercent", clampDiscountPercent(value)); onChange(index, "discount", 0); return; } onChange(index, "discount", clampDiscountValue(value, subTotal)); onChange(index, "discountPercent", null); }} unit={isPercentDiscount ? "٪" : undefined} />
= 0 ? total.toLocaleString("fa-IR") : "۰"} unit="ریال" />
{canRemove && ( )} {item.id && ( )}
); }; export default InvoiceItemRow;