diff --git a/src/frontend/src/forms/StockForms.tsx b/src/frontend/src/forms/StockForms.tsx index 70692f0a92..7ea4444537 100644 --- a/src/frontend/src/forms/StockForms.tsx +++ b/src/frontend/src/forms/StockForms.tsx @@ -68,6 +68,8 @@ export function useStockFields({ const [nextBatchCode, setNextBatchCode] = useState(''); const [nextSerialNumber, setNextSerialNumber] = useState(''); + const [expiryDate, setExpiryDate] = useState(null); + const batchGenerator = useBatchCodeGenerator((value: any) => { if (value) { setNextBatchCode(`${t`Next batch code`}: ${value}`); @@ -106,6 +108,18 @@ export function useStockFields({ // Clear the 'supplier_part' field if the part is changed setSupplierPart(null); + + // Adjust the 'expiry date' for the stock item + const expiry_days = record?.default_expiry ?? 0; + + if (expiry_days && expiry_days > 0) { + // Adjust the expiry date based on the part default expiry + setExpiryDate( + new Date( + new Date().getTime() + expiry_days * 24 * 60 * 60 * 1000 + ).toISOString() + ); + } } }, supplier_part: { @@ -171,7 +185,11 @@ export function useStockFields({ }, expiry_date: { icon: , - hidden: !globalSettings.isSet('STOCK_ENABLE_EXPIRY') + hidden: !globalSettings.isSet('STOCK_ENABLE_EXPIRY'), + value: expiryDate, + onValueChange: (value) => { + setExpiryDate(value); + } }, purchase_price: { icon: @@ -194,9 +212,15 @@ export function useStockFields({ // TODO: Handle custom field management based on provided options // TODO: refer to stock.py in original codebase + // Remove the expiry date field if it is not enabled + if (!globalSettings.isSet('STOCK_ENABLE_EXPIRY')) { + delete fields.expiry_date; + } + return fields; }, [ stockItem, + expiryDate, partInstance, partId, globalSettings,