Added inventory amount per food package management
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
# Generated by Django 6.1.1 on 2026-10-11 13:22
|
||||
|
||||
import django.core.validators
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def backfill_packages(apps, schema_editor):
|
||||
Item = apps.get_model('food', 'KitchenInventoryItem')
|
||||
for item in Item.objects.select_related('product').all().iterator():
|
||||
amount = item.product.package_amount
|
||||
unit = item.product.package_unit
|
||||
if not amount or not unit:
|
||||
continue # Unknown-size legacy entries remain count based.
|
||||
count = item.quantity
|
||||
remaining = item.remaining_amount if item.remaining_amount is not None else amount
|
||||
Item.objects.filter(pk=item.pk).update(quantity=1, original_amount=amount,
|
||||
remaining_amount=remaining, inventory_unit=unit)
|
||||
for _ in range(count - 1):
|
||||
copy = Item.objects.create(user_id=item.user_id, product_id=item.product_id, quantity=1,
|
||||
location_id=item.location_id, category_id=item.category_id, notes=item.notes,
|
||||
opened=False, original_amount=amount, remaining_amount=amount,
|
||||
inventory_unit=unit, best_before=item.best_before, archived_at=item.archived_at)
|
||||
Item.objects.filter(pk=copy.pk).update(added_at=item.added_at, updated_at=item.updated_at)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('food', '0005_package_contents'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='kitcheninventoryitem',
|
||||
name='inventory_unit',
|
||||
field=models.CharField(blank=True, max_length=8),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='kitcheninventoryitem',
|
||||
name='opened_at',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='kitcheninventoryitem',
|
||||
name='original_amount',
|
||||
field=models.DecimalField(blank=True, decimal_places=3, max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(0.001)]),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='kitcheninventoryitem',
|
||||
name='purchase_batch',
|
||||
field=models.CharField(blank=True, max_length=100),
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InventoryConsumption',
|
||||
fields=[
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('amount', models.DecimalField(decimal_places=3, max_digits=10, validators=[django.core.validators.MinValueValidator(0.001)])),
|
||||
('unit', models.CharField(max_length=8)),
|
||||
('source', models.CharField(choices=[('manual', 'Manual'), ('mcp', 'MCP'), ('recipe', 'Recipe')], max_length=16)),
|
||||
('source_reference', models.CharField(blank=True, max_length=255)),
|
||||
('description', models.CharField(blank=True, max_length=255)),
|
||||
('idempotency_key', models.CharField(blank=True, max_length=128)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('product', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to='food.foodproduct')),
|
||||
('reverses', models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='reversal', to='food.inventoryconsumption')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='InventoryConsumptionLine',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('amount', models.DecimalField(decimal_places=3, max_digits=10, validators=[django.core.validators.MinValueValidator(0.001)])),
|
||||
('unit', models.CharField(max_length=8)),
|
||||
('before_amount', models.DecimalField(decimal_places=3, max_digits=10)),
|
||||
('after_amount', models.DecimalField(decimal_places=3, max_digits=10)),
|
||||
('package', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='consumption_lines', to='food.kitcheninventoryitem')),
|
||||
('transaction', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='lines', to='food.inventoryconsumption')),
|
||||
],
|
||||
),
|
||||
migrations.AddConstraint(
|
||||
model_name='inventoryconsumption',
|
||||
constraint=models.UniqueConstraint(condition=models.Q(('idempotency_key', ''), _negated=True), fields=('user', 'idempotency_key'), name='food_unique_consumption_key'),
|
||||
),
|
||||
migrations.RunPython(backfill_packages, migrations.RunPython.noop),
|
||||
]
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 6.1.1 on 2026-10-11 13:27
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('food', '0006_inventory_packages_consumption'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='inventoryconsumption',
|
||||
name='request_signature',
|
||||
field=models.CharField(blank=True, max_length=64),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='inventoryconsumptionline',
|
||||
name='before_opened',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='inventoryconsumptionline',
|
||||
name='before_opened_at',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user