feat: criado tabela no db (UnitOfMeasure, ProductComponent, StockMovementType, StockMovement) add UnitOfMeasure na tabela produtos

This commit is contained in:
2025-07-22 15:39:37 -03:00
parent b74736f391
commit a9289cb28d
9 changed files with 184 additions and 1 deletions

View File

@@ -1,6 +1,18 @@
from django.db import models
from django.contrib.auth.models import User
from categories.models import Categories
# from comandas.models import Comanda
class UnitOfMeasure(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=50, unique=True, help_text="Ex: 'Unidade', 'Kg', 'Litro'")
abbreviation = models.CharField(max_length=10, unique=True, help_text="Ex: 'un', 'kg', 'L'")
def __str__(self):
return self.abbreviation
# Create your models here.
@@ -14,6 +26,23 @@ class Product(models.Model):
category = models.ForeignKey(Categories, on_delete=models.CASCADE)
cuisine = models.BooleanField(default=False)
active = models.BooleanField(default=True)
unit_of_measure = models.ForeignKey(
UnitOfMeasure,
on_delete=models.SET_NULL, # Define como NULL se a unidade de medida for excluída
null=True,
blank=True,
help_text="Unidade de medida para este produto."
)
# Campo de composição (mantido da resposta anterior)
components = models.ManyToManyField(
'self',
through='ProductComponent',
through_fields=('composite_product', 'component_product'),
symmetrical=False,
related_name='is_component_of'
)
def __str__(self) -> str:
return f"{self.name}"
@@ -25,3 +54,36 @@ class Product(models.Model):
def addStock(self, qtd):
self.quantity += qtd
self.save()
class ProductComponent(models.Model):
composite_product = models.ForeignKey(
Product,
on_delete=models.CASCADE,
related_name='composition_entries',
help_text="O produto que é composto por outros produtos."
)
component_product = models.ForeignKey(
Product,
on_delete=models.CASCADE,
related_name='used_as_component_in',
help_text="Um produto que é componente de outro produto."
)
quantity_required = models.PositiveIntegerField(
default=1,
help_text="Quantidade deste componente necessária para o produto composto."
)
class Meta:
unique_together = ('composite_product', 'component_product')
def __str__(self):
return (
f"{self.composite_product.name} requer "
f"{self.quantity_required} de {self.component_product.name}"
)