Added inventory amount per food package management
This commit is contained in:
+61
-4
@@ -5,6 +5,7 @@ from django.core.exceptions import ValidationError
|
||||
from django.core.validators import MinValueValidator
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
from django.utils import timezone
|
||||
|
||||
|
||||
NON_NEGATIVE = [MinValueValidator(0)]
|
||||
@@ -172,6 +173,10 @@ class KitchenInventoryItem(models.Model):
|
||||
notes = models.TextField(blank=True)
|
||||
opened = models.BooleanField(default=False)
|
||||
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)
|
||||
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)
|
||||
archived_at = models.DateTimeField(null=True, blank=True)
|
||||
added_at = models.DateTimeField(auto_now_add=True)
|
||||
@@ -179,10 +184,16 @@ class KitchenInventoryItem(models.Model):
|
||||
|
||||
def clean(self):
|
||||
super().clean()
|
||||
if self.remaining_amount is not None and (not self.product_id or self.quantity != 1 or not self.opened or
|
||||
not self.product.package_amount or
|
||||
self.remaining_amount > self.product.package_amount):
|
||||
raise ValidationError({'remaining_amount': 'Remaining contents require one opened package with a known package size.'})
|
||||
if (self.original_amount is None) != (self.remaining_amount is None):
|
||||
raise ValidationError('Original and remaining quantities must be supplied together.')
|
||||
was_count_only = self.pk and KitchenInventoryItem.objects.filter(pk=self.pk, original_amount__isnull=True).exists()
|
||||
if self.original_amount is not None and ((self.pk is not None and self.quantity != 1 and not was_count_only) or
|
||||
self.inventory_unit not in ('mg', 'g', 'kg', 'ml', 'L', 'pieces', 'packs', 'portions') or
|
||||
self.remaining_amount > self.original_amount):
|
||||
raise ValidationError('A package needs one valid unit and remaining quantity within its original quantity.')
|
||||
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.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:
|
||||
@@ -190,6 +201,52 @@ class KitchenInventoryItem(models.Model):
|
||||
if self.user_id and self.category_id and self.category.user_id != self.user_id:
|
||||
raise ValidationError({'category': 'Choose one of your own categories.'})
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.opened and self.opened_at is None:
|
||||
self.opened_at = timezone.now()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def percentage_remaining(self):
|
||||
if self.original_amount:
|
||||
return int(self.remaining_amount * 100 / self.original_amount)
|
||||
return 100
|
||||
|
||||
@property
|
||||
def package_status(self):
|
||||
if self.remaining_amount == 0:
|
||||
return 'depleted'
|
||||
return 'opened' if self.opened else 'unopened'
|
||||
|
||||
|
||||
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)
|
||||
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)
|
||||
idempotency_key = models.CharField(max_length=128, blank=True)
|
||||
request_signature = models.CharField(max_length=64, blank=True)
|
||||
reverses = models.OneToOneField('self', on_delete=models.PROTECT, null=True, blank=True, related_name='reversal')
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [models.UniqueConstraint(fields=('user', 'idempotency_key'), condition=~Q(idempotency_key=''), name='food_unique_consumption_key')]
|
||||
|
||||
|
||||
class InventoryConsumptionLine(models.Model):
|
||||
transaction = models.ForeignKey(InventoryConsumption, on_delete=models.CASCADE, related_name='lines')
|
||||
package = models.ForeignKey(KitchenInventoryItem, on_delete=models.PROTECT, related_name='consumption_lines')
|
||||
amount = models.DecimalField(max_digits=10, decimal_places=3, validators=POSITIVE)
|
||||
unit = models.CharField(max_length=8)
|
||||
before_amount = models.DecimalField(max_digits=10, decimal_places=3)
|
||||
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)
|
||||
|
||||
|
||||
class PersonalAccessToken(models.Model):
|
||||
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='personal_access_tokens')
|
||||
|
||||
Reference in New Issue
Block a user