@@ -661,6 +722,8 @@ export function BendingLotForm() {
resolvedItem,
resolveError,
isSmokeBarrier,
+ isEditMode,
+ initialData,
handleProdChange,
handleSpecChange,
handleLengthChange,
@@ -670,8 +733,8 @@ export function BendingLotForm() {
return (
<>
(() => {
- if (initialData) {
- return {
- productionReason: initialData.productionReason || '',
- targetStockQty: initialData.targetStockQty ? String(initialData.targetStockQty) : '',
- memo: initialData.memo || '',
- remarks: initialData.remarks || '',
- items: initialData.items.map((item) => ({
- id: item.id,
- itemId: item.itemId,
- itemCode: item.itemCode,
- itemName: item.itemName,
- specification: item.specification,
- quantity: item.quantity,
- unit: item.unit,
- unitPrice: item.unitPrice,
- amount: item.totalAmount,
- })),
- };
- }
- return INITIAL_FORM;
- });
-
- const [fieldErrors, setFieldErrors] = useState>({});
-
- // 필드 에러 초기화
- const clearFieldError = useCallback((field: string) => {
- setFieldErrors((prev) => {
- if (prev[field]) {
- const { [field]: _, ...rest } = prev;
- return rest;
- }
- return prev;
- });
- }, []);
-
- // 품목 삭제
- const handleRemoveItem = useCallback((itemId: string) => {
- setForm((prev) => ({
- ...prev,
- items: prev.items.filter((item) => item.id !== itemId),
- }));
- }, []);
-
- // 품목 수량 변경
- const handleQuantityChange = useCallback((itemId: string, quantity: number) => {
- setForm((prev) => ({
- ...prev,
- items: prev.items.map((item) =>
- item.id === itemId
- ? { ...item, quantity, amount: item.unitPrice * quantity }
- : item
- ),
- }));
- }, []);
-
- // 유효성 검사
- const validate = useCallback((): boolean => {
- const errors: Record = {};
-
- if (form.items.length === 0) {
- errors.items = '품목을 1개 이상 추가해주세요';
- }
-
- setFieldErrors(errors);
- return Object.keys(errors).length === 0;
- }, [form.items]);
-
- // 저장
- const handleSave = useCallback(async () => {
- if (!validate()) {
- toast.error('입력 정보를 확인해주세요.');
- return;
- }
-
- const formData: StockOrderFormData = {
- orderTypeCode: 'STOCK',
- memo: form.memo,
- remarks: form.remarks,
- productionReason: form.productionReason,
- targetStockQty: Number(form.targetStockQty) || 0,
- items: form.items.map((item): StockOrderItemFormData => ({
- itemId: item.itemId,
- itemCode: item.itemCode,
- itemName: item.itemName,
- specification: item.specification,
- quantity: item.quantity,
- unit: item.unit || 'EA',
- unitPrice: item.unitPrice,
- })),
- };
-
- const result = isEditMode && initialData
- ? await updateStockOrder(initialData.id, formData)
- : await createStockOrder(formData);
-
- if (result.__authError) {
- toast.error('인증이 만료되었습니다. 다시 로그인해주세요.');
- return;
- }
-
- if (!result.success) {
- toast.error(result.error || '저장에 실패했습니다.');
- return;
- }
-
- toast.success(isEditMode ? '재고생산이 수정되었습니다.' : '재고생산이 등록되었습니다.');
-
- if (result.data?.id) {
- router.push(`${basePath}/${result.data.id}`);
- } else {
- router.push(basePath);
- }
- }, [form, isEditMode, initialData, validate, router, basePath]);
-
- // 취소
- const handleCancel = useCallback(() => {
- if (isEditMode && initialData) {
- router.push(`${basePath}/${initialData.id}`);
- } else {
- router.push(basePath);
- }
- }, [isEditMode, initialData, router, basePath]);
-
- // renderForm
- const renderFormContent = useMemo(
- () =>
- () => (
-
- {/* 기본 정보 */}
-
-
-
-
- setForm((prev) => ({ ...prev, productionReason: e.target.value }))}
- />
-
-
-
- setForm((prev) => ({ ...prev, targetStockQty: String(value ?? 0) }))}
- min={0}
- />
-
-
-
-
- {/* 비고 */}
-
-
-
-
- {/* 품목 내역 */}
-
- {fieldErrors.items && (
- {fieldErrors.items}
- )}
-
- {form.items.length === 0 ? (
-
-
-
품목이 없습니다. 재고생산 저장 시 자동으로 추가됩니다.
-
- ) : (
-
-
-
-
- No
- 품목명
- 규격
- 수량
- 단위
- 단가
- 금액
- 삭제
-
-
-
- {form.items.map((item, index) => (
-
- {index + 1}
- {item.itemName}
-
- {item.specification || item.spec || '-'}
-
-
- handleQuantityChange(item.id, value ?? 1)}
- min={1}
- className="w-20"
- />
-
-
- {item.unit || 'EA'}
-
-
- {formatAmount(item.unitPrice)}
-
-
- {formatAmount((item.amount ?? item.unitPrice * item.quantity))}
-
-
-
-
-
- ))}
-
-
-
- )}
-
-
- ),
- [form, fieldErrors, handleQuantityChange, handleRemoveItem]
- );
-
- return (
- handleSave()}
- renderForm={renderFormContent}
- />
- );
-}
diff --git a/src/components/stocks/actions.ts b/src/components/stocks/actions.ts
index b5bbec38..85d1df50 100644
--- a/src/components/stocks/actions.ts
+++ b/src/components/stocks/actions.ts
@@ -724,6 +724,81 @@ export async function createBendingStockOrder(params: {
return { success: result.success, data: result.data, error: result.error };
}
+/**
+ * 절곡품 재고생산 수정 (기존 orders API + bending_lot 확장)
+ */
+export async function updateBendingStockOrder(id: string, params: {
+ memo?: string;
+ targetStockQty: number;
+ bendingLot: BendingLotFormData;
+ item: {
+ itemId?: number;
+ itemCode?: string;
+ itemName: string;
+ specification?: string;
+ quantity: number;
+ unit?: string;
+ };
+}): Promise<{
+ success: boolean;
+ data?: StockOrder;
+ error?: string;
+ __authError?: boolean;
+}> {
+ const apiData = {
+ order_type_code: 'STOCK',
+ memo: params.memo || null,
+ remarks: null,
+ options: {
+ production_reason: '절곡품 재고생산',
+ target_stock_qty: params.targetStockQty || null,
+ bending_lot: {
+ lot_number: params.bendingLot.lot_number,
+ prod_code: params.bendingLot.prod_code,
+ spec_code: params.bendingLot.spec_code,
+ length_code: params.bendingLot.length_code,
+ raw_lot_no: params.bendingLot.raw_lot_no || null,
+ fabric_lot_no: params.bendingLot.fabric_lot_no || null,
+ material: params.bendingLot.material || null,
+ },
+ },
+ client_id: null,
+ client_name: null,
+ site_name: null,
+ delivery_date: null,
+ delivery_method_code: null,
+ discount_rate: 0,
+ discount_amount: 0,
+ supply_amount: 0,
+ tax_amount: 0,
+ total_amount: 0,
+ items: [
+ {
+ item_id: params.item.itemId || null,
+ item_code: params.item.itemCode || null,
+ item_name: params.item.itemName,
+ specification: params.item.specification || null,
+ quantity: params.item.quantity,
+ unit: params.item.unit || 'EA',
+ unit_price: 0,
+ supply_amount: 0,
+ tax_amount: 0,
+ total_amount: 0,
+ },
+ ],
+ };
+
+ const result = await executeServerAction({
+ url: buildApiUrl(`/api/v1/orders/${id}`),
+ method: 'PUT',
+ body: apiData,
+ transform: (d: ApiStockOrder) => transformApiToFrontend(d),
+ errorMessage: '절곡품 재고생산 수정에 실패했습니다.',
+ });
+ if (result.__authError) return { success: false, __authError: true };
+ return { success: result.success, data: result.data, error: result.error };
+}
+
/**
* 원자재 LOT 목록 조회 (수입검사 완료 입고 건)
*/