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
+85 -2
View File
@@ -1,5 +1,5 @@
import uuid
from decimal import Decimal
from decimal import Decimal, ROUND_HALF_UP
from django.conf import settings
from django.core.exceptions import ValidationError
@@ -192,6 +192,14 @@ class KitchenInventoryItem(models.Model):
remaining_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=NON_NEGATIVE)
original_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
inventory_unit = models.CharField(max_length=8, blank=True)
pieces_per_package = models.PositiveIntegerField(null=True, blank=True)
remaining_pieces = models.PositiveIntegerField(null=True, blank=True)
piece_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
inner_packs_per_package = models.PositiveIntegerField(null=True, blank=True)
pieces_per_inner_pack = models.PositiveIntegerField(null=True, blank=True)
inner_pack_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
inner_pack_label = models.CharField(max_length=40, blank=True)
piece_label = models.CharField(max_length=40, blank=True)
opened_at = models.DateTimeField(null=True, blank=True)
purchase_batch = models.CharField(max_length=100, blank=True)
best_before = models.DateField(null=True, blank=True)
@@ -211,6 +219,56 @@ class KitchenInventoryItem(models.Model):
if (self.original_amount is not None and self.remaining_amount is not None and
0 < self.remaining_amount < self.original_amount and not self.opened):
raise ValidationError({'opened': 'A partly used package must be opened.'})
if self.pieces_per_package is not None:
if not self.pieces_per_package or (self.pk and self.quantity != 1) or self.original_amount is None:
raise ValidationError({'pieces_per_package': 'Track one package with a known contents amount.'})
if self.remaining_pieces is None or self.remaining_pieces > self.pieces_per_package:
raise ValidationError({'remaining_pieces': 'Enter a piece count within the package capacity.'})
if self.remaining_pieces < self.pieces_per_package and not self.opened and self.remaining_pieces > 0:
raise ValidationError({'opened': 'A partly used package must be opened.'})
if self.inventory_unit == 'pieces':
if self.original_amount != self.pieces_per_package or self.remaining_amount != self.remaining_pieces:
raise ValidationError({'original_amount': 'Piece stock must match the package piece counts.'})
if self.piece_amount is not None:
raise ValidationError({'piece_amount': 'A separate piece amount is only used for weighed packages.'})
elif self.inventory_unit in ('mg', 'g', 'kg', 'ml', 'L'):
if self.piece_amount is not None and self.piece_amount <= 0:
raise ValidationError({'piece_amount': 'Enter a positive amount per piece.'})
if self.remaining_pieces == 0 and self.remaining_amount != 0:
raise ValidationError({'remaining_amount': 'A package with no pieces must have no remaining amount.'})
else:
raise ValidationError({'inventory_unit': 'Piece contents require pieces, mass, or volume.'})
elif self.remaining_pieces is not None or self.piece_amount is not None:
raise ValidationError({'pieces_per_package': 'Enter pieces per package first.'})
nested_values = (self.inner_packs_per_package, self.pieces_per_inner_pack)
if any(value is not None for value in nested_values):
if any(value is None or value < 1 for value in nested_values):
raise ValidationError({'inner_packs_per_package': 'Enter both inner packs per package and pieces per inner pack.'})
if self.pieces_per_package != self.inner_packs_per_package * self.pieces_per_inner_pack:
raise ValidationError({'pieces_per_package': 'Total pieces must equal inner packs times pieces per inner pack.'})
if self.piece_amount is not None:
raise ValidationError({'piece_amount': 'Nested packages derive piece weight from the inner-pack amount.'})
if self.inventory_unit == 'pieces':
if self.inner_pack_amount is not None:
raise ValidationError({'inner_pack_amount': 'Piece-only packages do not use a measured inner-pack amount.'})
elif self.inventory_unit in ('mg', 'g', 'kg', 'ml', 'L'):
if self.inner_pack_amount is None:
raise ValidationError({'inner_pack_amount': 'Enter the measured amount in one inner pack.'})
if self.original_amount != self.inner_pack_amount * self.inner_packs_per_package:
raise ValidationError({'original_amount': 'Outer package amount must equal inner-pack amount times inner packs.'})
full_packs, loose_pieces = divmod(self.remaining_pieces, self.pieces_per_inner_pack)
expected_remaining = self.inner_pack_amount * full_packs
if loose_pieces:
expected_remaining += (self.inner_pack_amount * loose_pieces / self.pieces_per_inner_pack).quantize(
Decimal('.001'), rounding=ROUND_HALF_UP)
if self.remaining_amount != expected_remaining:
raise ValidationError({'remaining_amount': 'Remaining amount must match the remaining whole pieces and inner packs.'})
if self.inner_packs_per_package > 1 and not self.inner_pack_label:
raise ValidationError({'inner_pack_label': 'Name the inner package unit (for example, packet).'})
if not self.piece_label:
raise ValidationError({'piece_label': 'Name the individual item (for example, biscuit).'})
elif self.inner_pack_amount is not None or self.inner_pack_label or self.piece_label:
raise ValidationError({'inner_packs_per_package': 'Enter nested package counts before adding inner-pack details.'})
if self.user_id and self.product_id and self.product.owner_id != self.user_id:
raise ValidationError({'product': 'Choose one of your own products.'})
if self.user_id and self.location_id and self.location.user_id != self.user_id:
@@ -235,13 +293,36 @@ class KitchenInventoryItem(models.Model):
return 'depleted'
return 'opened' if self.opened else 'unopened'
@property
def remaining_full_inner_packs(self):
if not self.pieces_per_inner_pack or self.remaining_pieces is None:
return 0
return self.remaining_pieces // self.pieces_per_inner_pack
@property
def opened_inner_pack_pieces(self):
if not self.pieces_per_inner_pack or self.remaining_pieces is None:
return 0
return self.remaining_pieces % self.pieces_per_inner_pack
@property
def remaining_inner_pack_balance(self):
if not self.inner_packs_per_package:
return ''
parts = []
if self.remaining_full_inner_packs:
parts.append(f'{self.remaining_full_inner_packs} unopened {self.inner_pack_label or "inner pack"}{"s" if self.remaining_full_inner_packs != 1 else ""}')
if self.opened_inner_pack_pieces:
parts.append(f'1 opened {self.inner_pack_label or "inner pack"} with {self.opened_inner_pack_pieces} {self.piece_label or "piece"}{"s" if self.opened_inner_pack_pieces != 1 else ""} left')
return ' + '.join(parts) if parts else 'no inner packs remaining'
class InventoryConsumption(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
product = models.ForeignKey(FoodProduct, on_delete=models.PROTECT)
amount = models.DecimalField(max_digits=10, decimal_places=3, validators=POSITIVE)
unit = models.CharField(max_length=8)
unit = models.CharField(max_length=16)
source = models.CharField(max_length=16, choices=[('manual', 'Manual'), ('mcp', 'MCP'), ('recipe', 'Recipe')])
source_reference = models.CharField(max_length=255, blank=True)
description = models.CharField(max_length=255, blank=True)
@@ -263,6 +344,8 @@ class InventoryConsumptionLine(models.Model):
after_amount = models.DecimalField(max_digits=10, decimal_places=3)
before_opened = models.BooleanField(default=False)
before_opened_at = models.DateTimeField(null=True, blank=True)
before_pieces = models.PositiveIntegerField(null=True, blank=True)
after_pieces = models.PositiveIntegerField(null=True, blank=True)
class PersonalAccessToken(models.Model):