Add nested inventory package tracking

This commit is contained in:
Joren de Graaf
2026-10-26 00:25:00 +01:00
parent 4d76a2bc9c
commit 5217c7f30c
19 changed files with 838 additions and 44 deletions
+128 -5
View File
@@ -207,9 +207,18 @@ class InventoryItemForm(forms.ModelForm):
class Meta:
model = KitchenInventoryItem
fields = ('quantity', 'original_amount', 'inventory_unit', 'opened', 'best_before', 'notes')
fields = ('quantity', 'original_amount', 'inventory_unit', 'pieces_per_package',
'remaining_pieces', 'piece_amount', 'inner_packs_per_package',
'pieces_per_inner_pack', 'inner_pack_amount', 'inner_pack_label',
'piece_label', 'opened', 'best_before', 'notes')
widgets = {'quantity': forms.NumberInput(attrs={'min': 1, 'step': 1}),
'original_amount': forms.NumberInput(attrs={'min': '.001', 'step': '.001'}),
'pieces_per_package': forms.NumberInput(attrs={'min': 1, 'step': 1}),
'remaining_pieces': forms.NumberInput(attrs={'min': 0, 'step': 1}),
'piece_amount': forms.NumberInput(attrs={'min': '.001', 'step': '.001'}),
'inner_packs_per_package': forms.NumberInput(attrs={'min': 1, 'step': 1}),
'pieces_per_inner_pack': forms.NumberInput(attrs={'min': 1, 'step': 1}),
'inner_pack_amount': forms.NumberInput(attrs={'min': '.001', 'step': '.001'}),
'best_before': forms.DateInput(attrs={'type': 'date'}),
'notes': forms.Textarea(attrs={'rows': 2})}
@@ -218,7 +227,26 @@ class InventoryItemForm(forms.ModelForm):
self.fields['inventory_unit'] = forms.ChoiceField(required=False, label='Package unit',
choices=[('', 'Count only (unknown size)')] + [(unit, unit) for unit in
('mg', 'g', 'kg', 'ml', 'L', 'pieces', 'packs', 'portions')])
self.fields['original_amount'].label = 'Amount per package'
self.fields['original_amount'].label = 'Amount in outer package'
self.fields['quantity'].label = 'Number of packages'
self.fields['pieces_per_package'].label = 'Total pieces per package (flat package)'
self.fields['remaining_pieces'].label = 'Individual pieces remaining'
self.fields['piece_amount'].label = 'Amount per piece in package unit (optional)'
self.fields['inner_packs_per_package'].label = 'Inner packs per package (optional)'
self.fields['pieces_per_inner_pack'].label = 'Pieces per inner pack'
self.fields['inner_pack_amount'].label = 'Amount per inner pack'
self.fields['inner_pack_label'].label = 'Inner pack name'
self.fields['piece_label'].label = 'Individual item name'
self.fields['quantity'].help_text = 'Count packages or cans here, not the pieces inside them.'
self.fields['original_amount'].help_text = 'For nested packs, leave blank to calculate it from inner-pack amount × inner packs.'
self.fields['pieces_per_package'].help_text = 'For flat packages, such as 12 rolls or 10 sausages. Leave blank when entering nested pack counts below.'
self.fields['remaining_pieces'].help_text = 'Total individual pieces left across all inner packs. For a full package, leave blank.'
self.fields['piece_amount'].help_text = 'Enter a measured amount per piece for g or ml conversion; never inferred from the pack label.'
self.fields['inner_packs_per_package'].help_text = 'For a nested pack, such as 6 individually wrapped inner packs.'
self.fields['pieces_per_inner_pack'].help_text = 'How many individual items are in each inner pack.'
self.fields['inner_pack_amount'].help_text = 'Measured amount in one inner pack, for example 44 g. This derives the approximate amount per piece.'
self.fields['inner_pack_label'].help_text = 'For example, packet or wrapper.'
self.fields['piece_label'].help_text = 'For example, biscuit or cracker.'
if self.instance.pk:
self.fields['location_name'].initial = self.instance.location.name
self.fields['category_name'].initial = self.instance.category.name if self.instance.category else ''
@@ -226,16 +254,111 @@ class InventoryItemForm(forms.ModelForm):
def clean(self):
data = super().clean()
pieces = data.get('pieces_per_package')
remaining_pieces = data.get('remaining_pieces')
piece_amount = data.get('piece_amount')
original = data.get('original_amount')
unit = data.get('inventory_unit')
inner_packs = data.get('inner_packs_per_package')
pieces_per_inner = data.get('pieces_per_inner_pack')
inner_amount = data.get('inner_pack_amount')
was_count_only = self.instance.pk and KitchenInventoryItem.objects.filter(
pk=self.instance.pk, original_amount__isnull=True).exists()
nested = inner_packs is not None or pieces_per_inner is not None
if nested:
if inner_packs is None or pieces_per_inner is None:
raise forms.ValidationError('Enter both inner packs per package and pieces per inner pack.')
if pieces is not None and pieces != inner_packs * pieces_per_inner:
raise forms.ValidationError('Total pieces must equal inner packs times pieces per inner pack.')
pieces = inner_packs * pieces_per_inner
data['pieces_per_package'] = pieces
self.instance.pieces_per_package = pieces
if not data.get('piece_label'):
raise forms.ValidationError('Name the individual item, for example biscuit.')
if inner_packs > 1 and not data.get('inner_pack_label'):
raise forms.ValidationError('Name the inner package, for example packet.')
if not original and not unit:
if inner_amount is not None:
raise forms.ValidationError('Choose a package unit for the measured inner-pack amount.')
original, unit = Decimal(pieces), 'pieces'
data['original_amount'], data['inventory_unit'] = original, unit
self.instance.original_amount, self.instance.inventory_unit = original, unit
if unit == 'pieces':
if inner_amount is not None:
raise forms.ValidationError('Piece-only packages do not use a measured inner-pack amount.')
original = Decimal(pieces)
data['original_amount'] = original
self.instance.original_amount = original
elif unit in ('mg', 'g', 'kg', 'ml', 'L'):
if inner_amount is None:
raise forms.ValidationError('Enter the measured amount in one inner pack.')
expected_original = inner_amount * inner_packs
if original is not None and original != expected_original:
raise forms.ValidationError('Outer package amount must equal inner-pack amount times inner packs.')
original = expected_original
data['original_amount'] = original
self.instance.original_amount = original
else:
raise forms.ValidationError('Nested contents require a pieces, mass, or volume unit.')
if piece_amount is not None:
raise forms.ValidationError('Nested packages derive piece amounts from the inner-pack amount.')
if pieces is not None:
if remaining_pieces is None:
remaining_pieces = pieces
data['remaining_pieces'] = pieces
self.instance.remaining_pieces = pieces
if remaining_pieces > pieces:
raise forms.ValidationError('Pieces remaining cannot exceed pieces per package.')
if not original and not unit:
original, unit = Decimal(pieces), 'pieces'
data['original_amount'], data['inventory_unit'] = original, unit
self.instance.original_amount, self.instance.inventory_unit = original, unit
if unit == 'pieces':
if original != Decimal(pieces) or piece_amount is not None:
raise forms.ValidationError('For piece stock, package amount must equal pieces per package; leave amount per piece blank.')
if not self.instance.pk or was_count_only:
self.instance.remaining_amount = Decimal(remaining_pieces)
elif unit in ('mg', 'g', 'kg', 'ml', 'L'):
if remaining_pieces < pieces and (not self.instance.pk or was_count_only):
if nested:
full_packs, loose_pieces = divmod(remaining_pieces, pieces_per_inner)
remaining_value = inner_amount * full_packs
if loose_pieces:
remaining_value += (inner_amount * loose_pieces / pieces_per_inner).quantize(Decimal('.001'))
self.instance.remaining_amount = remaining_value
elif piece_amount is None:
raise forms.ValidationError('Enter an explicit amount per piece to record a partly used weighed package.')
else:
self.instance.remaining_amount = piece_amount * remaining_pieces
if self.instance.remaining_amount > original:
raise forms.ValidationError('Piece amounts exceed the package amount.')
else:
raise forms.ValidationError('Piece contents require a piece, mass, or volume package unit.')
if remaining_pieces < pieces:
self.instance.opened = True
data['opened'] = True
elif remaining_pieces is not None or piece_amount is not None:
raise forms.ValidationError('Enter pieces per package first.')
if bool(data.get('original_amount')) != bool(data.get('inventory_unit')):
raise forms.ValidationError('Enter both package amount and unit, or leave both blank.')
if data.get('original_amount') and self.instance.remaining_amount is None:
self.instance.remaining_amount = data['original_amount']
if self.instance.pk and self.instance.original_amount and data.get('original_amount') != self.instance.original_amount:
if self.instance.pk and not was_count_only and self.instance.original_amount and data.get('original_amount') != self.instance.original_amount:
raise forms.ValidationError('Existing package sizes cannot be changed after tracking starts.')
if self.instance.pk and self.instance.original_amount and data.get('inventory_unit') != self.instance.inventory_unit:
if self.instance.pk and not was_count_only and self.instance.original_amount and data.get('inventory_unit') != self.instance.inventory_unit:
raise forms.ValidationError('Existing package units cannot be changed after tracking starts.')
if self.instance.pk and self.instance.original_amount and data.get('quantity') != 1:
if self.instance.pk and not was_count_only and self.instance.original_amount and data.get('quantity') != 1:
raise forms.ValidationError('A tracked package has a quantity of one.')
if self.instance.pk and not was_count_only:
old = KitchenInventoryItem.objects.get(pk=self.instance.pk)
nested_state = (inner_packs, pieces_per_inner, inner_amount,
data.get('inner_pack_label'), data.get('piece_label'))
old_nested_state = (old.inner_packs_per_package, old.pieces_per_inner_pack,
old.inner_pack_amount, old.inner_pack_label, old.piece_label)
if old.pieces_per_package is not None and (pieces != old.pieces_per_package or piece_amount != old.piece_amount or remaining_pieces != old.remaining_pieces or nested_state != old_nested_state):
raise forms.ValidationError('Use inventory consumption to change tracked pieces.')
if old.pieces_per_package is None and pieces is not None and not nested and piece_amount is not None and old.remaining_amount != piece_amount * remaining_pieces:
raise forms.ValidationError('Piece counts must match current measured stock; use inventory consumption for deductions.')
return data
def clean_location_name(self):