大更新
This commit is contained in:
@@ -52,10 +52,10 @@ export function usePurchaseOrderForm({ order, suppliers }: UsePurchaseOrderFormP
|
||||
{
|
||||
productId: "",
|
||||
productName: "",
|
||||
quantity: 0,
|
||||
unit: "",
|
||||
quantity: 1,
|
||||
unitPrice: 0,
|
||||
subtotal: 0,
|
||||
selectedUnit: "base",
|
||||
},
|
||||
]);
|
||||
};
|
||||
@@ -66,32 +66,60 @@ export function usePurchaseOrderForm({ order, suppliers }: UsePurchaseOrderFormP
|
||||
};
|
||||
|
||||
// 更新商品項目
|
||||
const updateItem = (index: number, field: keyof PurchaseOrderItem, value: string | number) => {
|
||||
const updateItem = (index: number, field: keyof PurchaseOrderItem, value: any) => {
|
||||
const newItems = [...items];
|
||||
newItems[index] = { ...newItems[index], [field]: value };
|
||||
const item = { ...newItems[index] };
|
||||
|
||||
// 當選擇商品時,自動填入商品資訊
|
||||
if (field === "productId" && selectedSupplier) {
|
||||
// value is productId string
|
||||
const product = selectedSupplier.commonProducts.find((p) => p.productId === value);
|
||||
if (product) {
|
||||
newItems[index].productName = product.productName;
|
||||
newItems[index].unit = product.unit;
|
||||
newItems[index].base_unit = product.base_unit;
|
||||
newItems[index].purchase_unit = product.purchase_unit;
|
||||
newItems[index].conversion_rate = product.conversion_rate;
|
||||
newItems[index].unitPrice = product.lastPrice;
|
||||
newItems[index].previousPrice = product.lastPrice;
|
||||
// @ts-ignore
|
||||
item.productId = value;
|
||||
item.productName = product.productName;
|
||||
item.base_unit_id = product.base_unit_id;
|
||||
item.base_unit_name = product.base_unit_name;
|
||||
item.large_unit_id = product.large_unit_id;
|
||||
item.large_unit_name = product.large_unit_name;
|
||||
item.purchase_unit_id = product.purchase_unit_id;
|
||||
item.conversion_rate = product.conversion_rate;
|
||||
item.unitPrice = product.lastPrice;
|
||||
item.previousPrice = product.lastPrice;
|
||||
|
||||
// 決定預設單位
|
||||
// 若有採購單位且等於大單位,預設為大單位
|
||||
const isPurchaseUnitLarge = product.purchase_unit_id && product.large_unit_id && product.purchase_unit_id === product.large_unit_id;
|
||||
|
||||
if (isPurchaseUnitLarge) {
|
||||
item.selectedUnit = 'large';
|
||||
item.unitId = product.large_unit_id;
|
||||
} else {
|
||||
item.selectedUnit = 'base';
|
||||
item.unitId = product.base_unit_id;
|
||||
}
|
||||
}
|
||||
} else if (field === "selectedUnit") {
|
||||
// @ts-ignore
|
||||
item.selectedUnit = value;
|
||||
if (value === 'large') {
|
||||
item.unitId = item.large_unit_id;
|
||||
} else {
|
||||
item.unitId = item.base_unit_id;
|
||||
}
|
||||
} else {
|
||||
// @ts-ignore
|
||||
item[field] = value;
|
||||
}
|
||||
|
||||
// 計算小計
|
||||
if (field === "quantity" || field === "unitPrice") {
|
||||
newItems[index].subtotal = calculateSubtotal(
|
||||
Number(newItems[index].quantity),
|
||||
Number(newItems[index].unitPrice)
|
||||
if (field === "quantity" || field === "unitPrice" || field === "productId") {
|
||||
item.subtotal = calculateSubtotal(
|
||||
Number(item.quantity),
|
||||
Number(item.unitPrice)
|
||||
);
|
||||
}
|
||||
|
||||
newItems[index] = item;
|
||||
setItems(newItems);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user