Skip to content

Add Expiry Date on Receive Line Item #8867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/backend/InvenTree/order/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ class PurchaseOrderReceive(PurchaseOrderContextMixin, CreateAPI):
- supplier_part: pk value of the supplier part
- quantity: quantity to receive
- status: stock item status
- expiry_date: stock item expiry date (optional)
- location: destination for stock item (optional)
- batch_code: the batch code for this stock item
- serial_numbers: serial numbers for this stock item
Expand Down
4 changes: 4 additions & 0 deletions src/backend/InvenTree/order/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,9 @@ def receive_line_item(
# Extract optional batch code for the new stock item
batch_code = kwargs.get('batch_code', '')

# Extract optional expiry data for the new stock item
expiry_date = kwargs.get('expiry_date', '')

# Extract optional list of serial numbers
serials = kwargs.get('serials')

Expand Down Expand Up @@ -863,6 +866,7 @@ def receive_line_item(
purchase_order=self,
status=status,
batch=batch_code,
expiry_date=expiry_date,
packaging=packaging,
serial=sn,
purchase_price=unit_purchase_price,
Expand Down
9 changes: 9 additions & 0 deletions src/backend/InvenTree/order/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ class Meta:
'quantity',
'status',
'batch_code',
'expiry_date',
'serial_numbers',
'packaging',
'note',
Expand Down Expand Up @@ -765,6 +766,13 @@ def validate_quantity(self, quantity):
allow_blank=True,
)

expiry_date = serializers.DateField(
label=_('Expiry Date'),
help_text=_('Enter expiry date for incoming stock items'),
required=False,
default=None,
)

serial_numbers = serializers.CharField(
label=_('Serial Numbers'),
help_text=_('Enter serial numbers for incoming stock items'),
Expand Down Expand Up @@ -967,6 +975,7 @@ def save(self):
status=item['status'],
barcode=item.get('barcode', ''),
batch_code=item.get('batch_code', ''),
expiry_date=item.get('expiry_date', ''),
packaging=item.get('packaging', ''),
serials=item.get('serials', None),
notes=item.get('note', None),
Expand Down
16 changes: 14 additions & 2 deletions src/backend/InvenTree/order/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import base64
import io
import json
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta

from django.core.exceptions import ValidationError
from django.db import connection
Expand Down Expand Up @@ -1061,12 +1061,20 @@ def test_valid(self):
self.assertEqual(line_1.received, 0)
self.assertEqual(line_2.received, 50)

one_week_from_today = date.today() + timedelta(days=7)

valid_data = {
'items': [
{'line_item': 1, 'quantity': 50, 'barcode': 'MY-UNIQUE-BARCODE-123'},
{
'line_item': 1,
'quantity': 50,
'expiry_date': one_week_from_today.strftime(r'%Y-%m-%d'),
'barcode': 'MY-UNIQUE-BARCODE-123',
},
{
'line_item': 2,
'quantity': 200,
'expiry_date': one_week_from_today.strftime(r'%Y-%m-%d'),
'location': 2, # Explicit location
'barcode': 'MY-UNIQUE-BARCODE-456',
},
Expand Down Expand Up @@ -1111,6 +1119,10 @@ def test_valid(self):
self.assertEqual(stock_1.last().location.pk, 1)
self.assertEqual(stock_2.last().location.pk, 2)

# Expiry dates should be set
self.assertEqual(stock_1.last().expiry_date, one_week_from_today)
self.assertEqual(stock_2.last().expiry_date, one_week_from_today)

# Barcodes should have been assigned to the stock items
self.assertTrue(
StockItem.objects.filter(barcode_data='MY-UNIQUE-BARCODE-123').exists()
Expand Down
30 changes: 29 additions & 1 deletion src/frontend/src/forms/PurchaseOrderForms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { useQuery } from '@tanstack/react-query';
import { useEffect, useMemo, useState } from 'react';

import { IconCalendarExclamation } from '@tabler/icons-react';
import { api } from '../App';
import { ActionButton } from '../components/buttons/ActionButton';
import RemoveRowButton from '../components/buttons/RemoveRowButton';
Expand All @@ -49,7 +50,6 @@ import {
useSerialNumberGenerator
} from '../hooks/UseGenerator';
import { apiUrl } from '../states/ApiState';

/*
* Construct a set of fields for creating / editing a PurchaseOrderLineItem instance
*/
Expand Down Expand Up @@ -298,6 +298,12 @@ function LineItemFormRow({
}
});

const [expiryDateOpen, expiryDateHandlers] = useDisclosure(false, {
onClose: () => {
props.changeFn(props.idx, 'expiry_date', undefined);
}
});

// Status value
const [statusOpen, statusHandlers] = useDisclosure(false, {
onClose: () => props.changeFn(props.idx, 'status', undefined)
Expand Down Expand Up @@ -440,6 +446,14 @@ function LineItemFormRow({
tooltipAlignment='top'
variant={batchOpen ? 'filled' : 'transparent'}
/>
<ActionButton
size='sm'
onClick={() => expiryDateHandlers.toggle()}
icon={<IconCalendarExclamation />}
tooltip={t`Set Expiry Date`}
tooltipAlignment='top'
variant={expiryDateOpen ? 'filled' : 'transparent'}
/>
<ActionButton
size='sm'
icon={<InvenTreeIcon icon='packaging' />}
Expand Down Expand Up @@ -586,6 +600,19 @@ function LineItemFormRow({
}}
error={props.rowErrors?.serial_numbers?.message}
/>
<TableFieldExtraRow
visible={expiryDateOpen}
onValueChange={(value) =>
props.changeFn(props.idx, 'expiry_date', value)
}
fieldDefinition={{
field_type: 'date',
label: t`Expiry Date`,
description: t`Enter an expiry date for received items`,
value: props.item.expiry_date
}}
error={props.rowErrors?.expiry_date?.message}
/>
<TableFieldExtraRow
visible={packagingOpen}
onValueChange={(value) => props.changeFn(props.idx, 'packaging', value)}
Expand Down Expand Up @@ -672,6 +699,7 @@ export function useReceiveLineItems(props: LineItemsForm) {
line_item: elem.pk,
location: elem.destination ?? elem.destination_detail?.pk ?? null,
quantity: elem.quantity - elem.received,
expiry_date: null,
batch_code: '',
serial_numbers: '',
status: 10,
Expand Down
Loading