fournee/fournée/core/models.py
2024-05-16 08:54:05 +02:00

144 lines
4.0 KiB
Python

from uuid import uuid4
from django.db import models
from colorfield.fields import ColorField
from .utils import clean
def upload_to(instance, filename):
return "{}/{}".format(uuid4(), filename)
class Fournée(models.Model):
class Meta:
ordering = ["-date"]
date = models.DateField()
pour = models.CharField(max_length=64)
def __str__(self):
return f"Fournée du {self.date: %d-%m-%Y} pour {self.pour}"
def masse_commandée(self, pâte_id):
"Renvoie la masse commandée d'une pâte donnée"
poids_qté = (
self.commande_set.exclude(réservation=None)
.filter(réservation__pain__pâte_id=pâte_id)
.values('réservation__pain__poids', 'réservation__quantité')
)
return sum([
c['réservation__quantité'] * c['réservation__pain__poids']
for c in poids_qté
])
class Document(models.Model):
class Meta:
ordering = ["nom"]
fourniture = models.ForeignKey("Fourniture", on_delete=models.CASCADE)
nom = models.CharField(max_length=64)
fichier = models.FileField(upload_to=upload_to)
def __str__(self):
return f"{self.nom}"
class Fourniture(models.Model):
class Meta:
ordering = ["ordre"]
nom = models.CharField(max_length=64, unique=True)
ordre = models.SmallIntegerField(default=0)
couleur = ColorField(default='#000000')
conditionnement = models.DecimalField(
max_digits=6, decimal_places=3, default=0
)
emballage = models.CharField(max_length=64, blank=True)
def __str__(self):
return f"{self.nom}"
class Ingrédient(models.Model):
class Meta:
ordering = ["fourniture__ordre", "-quantité"]
recette = models.ForeignKey("Recette", on_delete=models.CASCADE)
fourniture = models.ForeignKey("Fourniture", on_delete=models.CASCADE)
quantité = models.DecimalField(max_digits=6, decimal_places=3)
def __str__(self):
return f"{self.fourniture.nom} ({clean(self.quantité)} kg)"
class Recette(models.Model):
class Meta:
ordering = ["ordre", "nom"]
nom = models.CharField(max_length=64)
poids_de_base = models.DecimalField(max_digits=6, decimal_places=3)
ordre = models.SmallIntegerField(default=0)
def __str__(self):
return self.nom
class Pain(models.Model):
class Meta:
ordering = ["pâte", "-poids", "moulé"]
nom = models.CharField(max_length=64)
pâte = models.ForeignKey("Recette", on_delete=models.CASCADE)
poids = models.DecimalField(max_digits=6, decimal_places=3)
moulé = models.BooleanField()
def __str__(self):
return self.nom
class Réservation(models.Model):
class Meta:
ordering = ["pain", "commande__pour", "commande__fournée__date"]
quantité = models.DecimalField(max_digits=6, decimal_places=3)
pain = models.ForeignKey("Pain", on_delete=models.CASCADE)
commande = models.ForeignKey("Commande", on_delete=models.CASCADE)
def __str__(self):
return f"{clean(self.quantité)} {self.pain}"
class Commande(models.Model):
class Meta:
ordering = ["pour"]
pour = models.CharField(max_length=64)
pains = models.ManyToManyField("Pain", through="Réservation")
fournée = models.ForeignKey("Fournée", on_delete=models.CASCADE)
def __str__(self):
details = ""
if self.id and self.réservation_set.exists():
details = " (" + ", ".join(
[str(r) for r in self.réservation_set.all()]
) + ")"
return f"{self.pour}" + details
class Coulée(models.Model):
class Meta:
ordering = ["fournée__date", "ordre", "quantité", "pâte"]
ordre = models.SmallIntegerField(default=0)
quantité = models.DecimalField(max_digits=6, decimal_places=3)
pâte = models.ForeignKey("Recette", on_delete=models.CASCADE)
fournée = models.ForeignKey("Fournée", on_delete=models.CASCADE)
def __str__(self):
return f"{self.pâte} ({clean(self.quantité)} kg)"