diff --git a/src/components/pages/inventory/movement/form/MovementForm.tsx b/src/components/pages/inventory/movement/form/MovementForm.tsx index 5c5138bf..0483b4c1 100644 --- a/src/components/pages/inventory/movement/form/MovementForm.tsx +++ b/src/components/pages/inventory/movement/form/MovementForm.tsx @@ -462,7 +462,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { } } catch (error) { console.error(`Failed to fetch product ${productId}:`, error); - newFetchedIds.add(productId); // Mark as fetched to avoid retry loops + newFetchedIds.add(productId); } } @@ -535,16 +535,18 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { const getAvailableStock = useCallback( (productId: number) => { + if (type === 'detail') return 0; const productWarehouse = productWarehouseOptions.find( (pw) => pw.product_id === productId ); return productWarehouse?.quantity ?? 0; }, - [productWarehouseOptions] + [productWarehouseOptions, type] ); const getProductQtyError = useCallback( (productIdx: number) => { + if (type === 'detail') return null; const product = formik.values.products?.[productIdx]; if (!product || !product.product_id) return null; @@ -557,11 +559,12 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { return null; }, - [formik.values.products, getAvailableStock] + [formik.values.products, getAvailableStock, type] ); const validateDeliveryQty = useCallback( (deliveryIdx: number, deliveryProductIdx: number, qty: number) => { + if (type === 'detail') return true; const delivery = formik.values.deliveries?.[deliveryIdx]; if (!delivery) return true; @@ -592,11 +595,12 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { return totalQtyUsed + qty <= Number(relatedProduct.product_qty); }, - [formik.values.deliveries, formik.values.products] + [formik.values.deliveries, formik.values.products, type] ); const getDeliveryQtyError = useCallback( (deliveryIdx: number, deliveryProductIdx: number) => { + if (type === 'detail') return null; const delivery = formik.values.deliveries?.[deliveryIdx]; if (!delivery) return null; @@ -633,32 +637,40 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { return null; }, - [formik.values.deliveries, formik.values.products] + [formik.values.deliveries, formik.values.products, type] ); const invalidQtyRows = useMemo( () => - formik.values.deliveries?.flatMap((delivery, deliveryIdx) => - delivery.products.map((product, productIdx) => { - const qty = Number(product.product_qty) || 0; - return !validateDeliveryQty(deliveryIdx, productIdx, qty); - }) - ) ?? [], - [formik.values.deliveries, formik.values.products, validateDeliveryQty] + type === 'detail' + ? [] + : (formik.values.deliveries?.flatMap((delivery, deliveryIdx) => + delivery.products.map((product, productIdx) => { + const qty = Number(product.product_qty) || 0; + return !validateDeliveryQty(deliveryIdx, productIdx, qty); + }) + ) ?? []), + [ + formik.values.deliveries, + formik.values.products, + validateDeliveryQty, + type, + ] ); const hasInvalidQty = useMemo( - () => invalidQtyRows.some(Boolean), - [invalidQtyRows] + () => (type === 'detail' ? false : invalidQtyRows.some(Boolean)), + [invalidQtyRows, type] ); const hasExceededStock = useMemo(() => { + if (type === 'detail') return false; return ( formik.values.products?.some((product, idx) => { return getProductQtyError(idx) !== null; }) ?? false ); - }, [formik.values.products, getProductQtyError]); + }, [formik.values.products, getProductQtyError, type]); return ( <> @@ -960,7 +972,7 @@ const MovementForm = ({ type = 'add', initialValues }: MovementFormProps) => { wrapper: 'w-full min-w-24', }} /> - {product.product_id && ( + {type !== 'detail' && product.product_id && (