fournee/fournée/core/models.py

142 lines
4.0 KiB
Python
Raw Normal View History

from uuid import uuid4
2024-04-30 12:27:17 +02:00
from django.db import models
2024-05-13 22:02:49 +02:00
from colorfield.fields import ColorField
2024-04-30 12:27:17 +02:00
def upload_to(instance, filename):
return "{}/{}".format(uuid4(), filename)
2024-04-30 12:27:17 +02:00
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"
2024-05-13 22:09:10 +02:00
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é
])
2024-04-30 12:27:17 +02:00
class Document(models.Model):
class Meta:
ordering = ["nom"]
fourniture = models.ForeignKey("Fourniture", on_delete=models.CASCADE)
nom = models.CharField(max_length=64, unique=True)
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}"
2024-04-30 12:27:17 +02:00
class Ingrédient(models.Model):
class Meta:
ordering = ["fourniture__ordre", "-quantité"]
2024-04-30 12:27:17 +02:00
recette = models.ForeignKey("Recette", on_delete=models.CASCADE)
fourniture = models.ForeignKey("Fourniture", on_delete=models.CASCADE)
2024-04-30 12:27:17 +02:00
quantité = models.DecimalField(max_digits=6, decimal_places=3)
def __str__(self):
return f"{self.fourniture.nom} ({self.quantité} kg)"
2024-04-30 12:27:17 +02:00
class Recette(models.Model):
class Meta:
ordering = ["ordre", "nom"]
2024-04-30 12:27:17 +02:00
nom = models.CharField(max_length=64)
poids_de_base = models.DecimalField(max_digits=6, decimal_places=3)
ordre = models.SmallIntegerField(default=0)
2024-04-30 12:27:17 +02:00
def __str__(self):
return self.nom
class Pain(models.Model):
class Meta:
ordering = ["pâte", "-poids", "moulé"]
2024-04-30 12:27:17 +02:00
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"{self.quantité.normalize()} {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} ({self.quantité.normalize()} kg)"