Skip to content

Commit

Permalink
Adjust StockItem form (#8869)
Browse files Browse the repository at this point in the history
- Auto-set expiry date
  • Loading branch information
SchrodingersGat authored Jan 10, 2025
1 parent c99aae5 commit 0004192
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/frontend/src/forms/StockForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export function useStockFields({
const [nextBatchCode, setNextBatchCode] = useState<string>('');
const [nextSerialNumber, setNextSerialNumber] = useState<string>('');

const [expiryDate, setExpiryDate] = useState<string | null>(null);

const batchGenerator = useBatchCodeGenerator((value: any) => {
if (value) {
setNextBatchCode(`${t`Next batch code`}: ${value}`);
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -171,7 +185,11 @@ export function useStockFields({
},
expiry_date: {
icon: <IconCalendarExclamation />,
hidden: !globalSettings.isSet('STOCK_ENABLE_EXPIRY')
hidden: !globalSettings.isSet('STOCK_ENABLE_EXPIRY'),
value: expiryDate,
onValueChange: (value) => {
setExpiryDate(value);
}
},
purchase_price: {
icon: <IconCurrencyDollar />
Expand All @@ -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,
Expand Down

0 comments on commit 0004192

Please sign in to comment.