feat: API調整訂單與販賣機訂單同步強制使用warehouse_code,更新API對接文件,及優化生產與配方模組UI顯示
All checks were successful
ERP-Deploy-Demo / deploy-demo (push) Successful in 55s

This commit is contained in:
2026-03-03 14:28:15 +08:00
parent 58bd995cd8
commit 183583c739
19 changed files with 486 additions and 89 deletions

View File

@@ -25,6 +25,16 @@ interface RecipeDetailModalProps {
export function RecipeDetailModal({ isOpen, onClose, recipe, isLoading }: RecipeDetailModalProps) {
if (!isOpen) return null;
const getUnitCost = (product: any, unitId: string | number) => {
if (!product || !product.cost_price) return 0;
let cost = Number(product.cost_price);
if (String(unitId) === String(product.large_unit_id) || String(unitId) === String(product.purchase_unit_id)) {
cost = cost * Number(product.conversion_rate || 1);
}
return cost;
};
return (
<Dialog open={isOpen} onOpenChange={onClose}>
<DialogContent className="max-w-3xl max-h-[90vh] overflow-y-auto p-0 gap-0">
@@ -92,7 +102,7 @@ export function RecipeDetailModal({ isOpen, onClose, recipe, isLoading }: Recipe
<TableRow>
<TableCell className="font-medium text-gray-700"></TableCell>
<TableCell className="text-gray-900 font-medium">
{Number(recipe.yield_quantity).toLocaleString()} {recipe.product?.base_unit?.name || '份'}
{Number(recipe.yield_quantity).toLocaleString(undefined, { maximumFractionDigits: 4 })} {recipe.product?.base_unit?.name || '份'}
</TableCell>
</TableRow>
{recipe.description && (
@@ -120,30 +130,42 @@ export function RecipeDetailModal({ isOpen, onClose, recipe, isLoading }: Recipe
<TableHead> / </TableHead>
<TableHead className="text-right"></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{recipe.items?.length > 0 ? (
recipe.items.map((item: any, index: number) => (
<TableRow key={index} className="hover:bg-gray-50/50">
<TableCell className="font-medium">
<div className="flex flex-col">
<span className="text-gray-900">{item.product?.name || 'Unknown'}</span>
<span className="text-xs text-gray-400">{item.product?.code}</span>
</div>
</TableCell>
<TableCell className="text-right font-medium text-gray-900">
{Number(item.quantity).toLocaleString()}
</TableCell>
<TableCell className="text-gray-600">
{item.unit?.name || '-'}
</TableCell>
<TableCell className="text-gray-500 text-sm">
{item.remark || '-'}
</TableCell>
</TableRow>
))
recipe.items.map((item: any, index: number) => {
const unitCost = item.product ? getUnitCost(item.product, item.unit_id) : 0;
const subtotal = unitCost * Number(item.quantity);
return (
<TableRow key={index} className="hover:bg-gray-50/50">
<TableCell className="font-medium">
<div className="flex flex-col">
<span className="text-gray-900">{item.product?.name || 'Unknown'}</span>
<span className="text-xs text-gray-400">{item.product?.code}</span>
</div>
</TableCell>
<TableCell className="text-right font-medium text-gray-900">
{Number(item.quantity).toLocaleString(undefined, { maximumFractionDigits: 4 })}
</TableCell>
<TableCell className="text-gray-600">
{item.unit?.name || '-'}
</TableCell>
<TableCell className="text-right text-gray-600">
{unitCost.toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="text-right font-medium text-gray-900">
{subtotal.toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="text-gray-500 text-sm">
{item.remark || '-'}
</TableCell>
</TableRow>
)
})
) : (
<TableRow>
<TableCell colSpan={4} className="h-24 text-center text-gray-500">
@@ -154,6 +176,20 @@ export function RecipeDetailModal({ isOpen, onClose, recipe, isLoading }: Recipe
</TableBody>
</Table>
</div>
<div className="mt-4 flex justify-end">
<div className="bg-gray-50 p-4 rounded-lg border border-gray-200 min-w-[300px]">
<div className="flex justify-between items-center mb-2">
<span className="text-sm text-gray-600"></span>
<span className="text-lg font-bold text-gray-900">{(recipe.items || []).reduce((sum: number, item: any) => sum + (getUnitCost(item.product, item.unit_id) * Number(item.quantity)), 0).toLocaleString(undefined, { maximumFractionDigits: 2 })} </span>
</div>
<div className="flex justify-between items-center pt-2 border-t border-gray-200">
<span className="text-sm font-medium text-gray-700">
<span className="text-xs text-gray-500 ml-1">( {recipe.yield_quantity} )</span>
</span>
<span className="text-md font-bold text-primary-main">{(Number(recipe.yield_quantity) > 0 ? (recipe.items || []).reduce((sum: number, item: any) => sum + (getUnitCost(item.product, item.unit_id) * Number(item.quantity)), 0) / Number(recipe.yield_quantity) : 0).toLocaleString(undefined, { maximumFractionDigits: 2 })} </span>
</div>
</div>
</div>
</div>
</div>
) : (

View File

@@ -20,6 +20,9 @@ interface Product {
code: string;
base_unit_id?: number;
large_unit_id?: number;
purchase_unit_id?: number;
cost_price?: number;
conversion_rate?: number;
}
interface Unit {
@@ -108,6 +111,25 @@ export default function RecipeCreate({ products, units }: Props) {
post(route('recipes.store'));
};
const getUnitCost = (productId: string, unitId: string) => {
const product = products.find(p => String(p.id) === productId);
if (!product || !product.cost_price) return 0;
let cost = Number(product.cost_price);
// Check if selected unit is large_unit or purchase_unit
if (unitId && (String(unitId) === String(product.large_unit_id) || String(unitId) === String(product.purchase_unit_id))) {
cost = cost * Number(product.conversion_rate || 1);
}
return cost;
};
const totalCost = data.items.reduce((sum, item) => {
const unitCost = getUnitCost(item.product_id, item.unit_id);
return sum + (unitCost * Number(item.quantity || 0));
}, 0);
const unitCost = Number(data.yield_quantity) > 0 ? totalCost / Number(data.yield_quantity) : 0;
return (
<AuthenticatedLayout breadcrumbs={getBreadcrumbs("recipes", [{ label: "新增", isPage: true }])}>
<Head title="新增配方" />
@@ -233,10 +255,12 @@ export default function RecipeCreate({ products, units }: Props) {
<Table>
<TableHeader className="bg-gray-50">
<TableRow>
<TableHead className="w-[35%]"></TableHead>
<TableHead className="w-[20%]"></TableHead>
<TableHead className="w-[20%]"></TableHead>
<TableHead className="w-[20%]"></TableHead>
<TableHead className="w-[30%]"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[10%] text-right"></TableHead>
<TableHead className="w-[10%] text-right"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[5%]"></TableHead>
</TableRow>
</TableHeader>
@@ -272,10 +296,23 @@ export default function RecipeCreate({ products, units }: Props) {
className="text-right"
/>
</TableCell>
<TableCell className="align-middle">
<div className="text-sm text-gray-700 bg-gray-50 px-3 py-2 rounded-md border border-gray-100 min-h-[38px] flex items-center">
{item.ui_unit_name || (units.find(u => String(u.id) === item.unit_id)?.name) || '-'}
</div>
<TableCell className="align-top">
<SearchableSelect
value={item.unit_id}
onValueChange={(v) => updateItem(index, 'unit_id', v)}
options={units.map(u => ({
label: u.name,
value: String(u.id)
}))}
placeholder="單位"
className="w-full"
/>
</TableCell>
<TableCell className="align-middle text-right text-sm text-gray-600">
{getUnitCost(item.product_id, item.unit_id).toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="align-middle text-right font-medium text-gray-900">
{(getUnitCost(item.product_id, item.unit_id) * Number(item.quantity || 0)).toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="align-top">
<Input
@@ -300,6 +337,23 @@ export default function RecipeCreate({ products, units }: Props) {
</TableBody>
</Table>
</div>
{/* 配方成本總計區塊 */}
<div className="mt-6 flex justify-end">
<div className="bg-gray-50 p-4 rounded-lg border border-gray-200 min-w-[300px]">
<div className="flex justify-between items-center mb-2">
<span className="text-sm text-gray-600"></span>
<span className="text-lg font-bold text-gray-900">{totalCost.toLocaleString(undefined, { maximumFractionDigits: 2 })} </span>
</div>
<div className="flex justify-between items-center pt-2 border-t border-gray-200">
<span className="text-sm font-medium text-gray-700">
<span className="text-xs text-gray-500 ml-1">( {Number(data.yield_quantity || 0).toLocaleString(undefined, { maximumFractionDigits: 4 })} )</span>
</span>
<span className="text-md font-bold text-primary-main">{unitCost.toLocaleString(undefined, { maximumFractionDigits: 2 })} </span>
</div>
</div>
</div>
{errors.items && <p className="text-red-500 text-sm mt-2">{errors.items}</p>}
</div>
</div>

View File

@@ -20,6 +20,9 @@ interface Product {
code: string;
base_unit_id?: number;
large_unit_id?: number;
purchase_unit_id?: number;
cost_price?: number;
conversion_rate?: number;
}
interface Unit {
@@ -73,10 +76,10 @@ export default function RecipeEdit({ recipe, products, units }: Props) {
code: recipe.code,
name: recipe.name,
description: recipe.description || "",
yield_quantity: String(recipe.yield_quantity),
yield_quantity: String(Number(recipe.yield_quantity || 0)),
items: recipe.items.map(item => ({
product_id: String(item.product_id),
quantity: String(item.quantity),
quantity: String(Number(item.quantity || 0)),
unit_id: String(item.unit_id),
remark: item.remark || "",
ui_product_name: item.product?.name,
@@ -133,6 +136,25 @@ export default function RecipeEdit({ recipe, products, units }: Props) {
put(route('recipes.update', recipe.id));
};
const getUnitCost = (productId: string, unitId: string) => {
const product = products.find(p => String(p.id) === productId);
if (!product || !product.cost_price) return 0;
let cost = Number(product.cost_price);
// Check if selected unit is large_unit or purchase_unit
if (unitId && (String(unitId) === String(product.large_unit_id) || String(unitId) === String(product.purchase_unit_id))) {
cost = cost * Number(product.conversion_rate || 1);
}
return cost;
};
const totalCost = data.items.reduce((sum, item) => {
const unitCost = getUnitCost(item.product_id, item.unit_id);
return sum + (unitCost * Number(item.quantity || 0));
}, 0);
const unitCost = Number(data.yield_quantity) > 0 ? totalCost / Number(data.yield_quantity) : 0;
return (
<AuthenticatedLayout breadcrumbs={getBreadcrumbs("recipes", [{ label: "編輯", isPage: true }])}>
<Head title="編輯配方" />
@@ -258,10 +280,12 @@ export default function RecipeEdit({ recipe, products, units }: Props) {
<Table>
<TableHeader className="bg-gray-50">
<TableRow>
<TableHead className="w-[35%]"></TableHead>
<TableHead className="w-[20%]"></TableHead>
<TableHead className="w-[20%]"></TableHead>
<TableHead className="w-[20%]"></TableHead>
<TableHead className="w-[30%]"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[10%] text-right"></TableHead>
<TableHead className="w-[10%] text-right"></TableHead>
<TableHead className="w-[15%]"></TableHead>
<TableHead className="w-[5%]"></TableHead>
</TableRow>
</TableHeader>
@@ -297,10 +321,23 @@ export default function RecipeEdit({ recipe, products, units }: Props) {
className="text-right"
/>
</TableCell>
<TableCell className="align-middle">
<div className="text-sm text-gray-700 bg-gray-50 px-3 py-2 rounded-md border border-gray-100 min-h-[38px] flex items-center">
{item.ui_unit_name || (units.find(u => String(u.id) === item.unit_id)?.name) || '-'}
</div>
<TableCell className="align-top">
<SearchableSelect
value={item.unit_id}
onValueChange={(v) => updateItem(index, 'unit_id', v)}
options={units.map(u => ({
label: u.name,
value: String(u.id)
}))}
placeholder="單位"
className="w-full"
/>
</TableCell>
<TableCell className="align-middle text-right text-sm text-gray-600">
{getUnitCost(item.product_id, item.unit_id).toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="align-middle text-right font-medium text-gray-900">
{(getUnitCost(item.product_id, item.unit_id) * Number(item.quantity || 0)).toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="align-top">
<Input
@@ -325,6 +362,23 @@ export default function RecipeEdit({ recipe, products, units }: Props) {
</TableBody>
</Table>
</div>
{/* 配方成本總計區塊 */}
<div className="mt-6 flex justify-end">
<div className="bg-gray-50 p-4 rounded-lg border border-gray-200 min-w-[300px]">
<div className="flex justify-between items-center mb-2">
<span className="text-sm text-gray-600"></span>
<span className="text-lg font-bold text-gray-900">{totalCost.toLocaleString(undefined, { maximumFractionDigits: 2 })} </span>
</div>
<div className="flex justify-between items-center pt-2 border-t border-gray-200">
<span className="text-sm font-medium text-gray-700">
<span className="text-xs text-gray-500 ml-1">( {Number(data.yield_quantity || 0).toLocaleString(undefined, { maximumFractionDigits: 4 })} )</span>
</span>
<span className="text-md font-bold text-primary-main">{unitCost.toLocaleString(undefined, { maximumFractionDigits: 2 })} </span>
</div>
</div>
</div>
{errors.items && <p className="text-red-500 text-sm mt-2">{errors.items}</p>}
</div>
</div>

View File

@@ -39,6 +39,8 @@ interface Recipe {
is_active: boolean;
description: string;
updated_at: string;
estimated_total_cost?: number;
estimated_unit_cost?: number;
}
interface Props {
@@ -187,6 +189,8 @@ export default function RecipeIndex({ recipes, filters }: Props) {
<TableHead></TableHead>
<TableHead></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-right"></TableHead>
<TableHead className="text-center w-[100px]"></TableHead>
<TableHead className="w-[150px]"></TableHead>
<TableHead className="text-center w-[150px]"></TableHead>
@@ -195,7 +199,7 @@ export default function RecipeIndex({ recipes, filters }: Props) {
<TableBody>
{recipes.data.length === 0 ? (
<TableRow>
<TableCell colSpan={7} className="h-32 text-center text-gray-500">
<TableCell colSpan={9} className="h-32 text-center text-gray-500">
<div className="flex flex-col items-center justify-center gap-2">
<BookOpen className="h-10 w-10 text-gray-300" />
<p></p>
@@ -227,7 +231,13 @@ export default function RecipeIndex({ recipes, filters }: Props) {
) : '-'}
</TableCell>
<TableCell className="text-right font-medium">
{recipe.yield_quantity}
{Number(recipe.yield_quantity).toLocaleString(undefined, { maximumFractionDigits: 4 })}
</TableCell>
<TableCell className="text-right font-medium text-primary-main">
{Number(recipe.estimated_unit_cost || 0).toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="text-right font-medium text-gray-700">
{Number(recipe.estimated_total_cost || 0).toLocaleString(undefined, { maximumFractionDigits: 2 })}
</TableCell>
<TableCell className="text-center">
{recipe.is_active ? (
@@ -338,6 +348,6 @@ export default function RecipeIndex({ recipes, filters }: Props) {
isLoading={isViewLoading}
/>
</div>
</AuthenticatedLayout>
</AuthenticatedLayout >
);
}