diff --git a/fournée/core/admin.py b/fournée/core/admin.py index 6578cdb..cbceb01 100644 --- a/fournée/core/admin.py +++ b/fournée/core/admin.py @@ -33,8 +33,8 @@ admin_site.register(models.Pain, PainAdmin) class FournitureAdmin(admin.ModelAdmin): - list_display = ["nom", "ordre", "couleur"] - list_editable = ["ordre", "couleur"] + list_display = ["nom", "ordre", "couleur", "conditionnement", "emballage"] + list_editable = ["ordre", "couleur", "conditionnement", "emballage"] actions = None save_as = True @@ -229,16 +229,16 @@ class FournéeAdmin(nested_admin.NestedModelAdmin): """ on créé les tableaux coulée / ingrédient """ extra_context["coulées"] = [] for c in obj.coulée_set.all(): - quantités = [c, ] - ingrédients = c.pâte.ingrédient_set.values_list( - 'fourniture__nom', - 'fourniture__couleur' - ) - for i, _ in ingrédients: - quantités.append((c.quantité / c.pâte.poids_de_base * sum( - c.pâte.ingrédient_set.filter(fourniture__nom=i) - .values_list("quantité", flat=True) - )).quantize(Decimal('1.000')).normalize()) + quantités = [[None, c]] + ingrédients = c.pâte.ingrédient_set.all() + for i in ingrédients: + quantités.append([ + i, + (c.quantité / c.pâte.poids_de_base * sum( + c.pâte.ingrédient_set.filter(pk=i.pk) + .values_list("quantité", flat=True) + )).quantize(Decimal('1.000')).normalize(), + ]) masse_coulée = sum( obj.coulée_set.filter(pâte=c.pâte).values_list( @@ -276,7 +276,9 @@ class FournéeAdmin(nested_admin.NestedModelAdmin): pk__in=obj.commande_set.exclude(réservation__isnull=True) .values_list("pains", flat=True) ).values_list("nom", flat=True) - totaux = [sum(obj.coulée_set.values_list('quantité', flat=True))] + totaux = [ + [None, sum(obj.coulée_set.values_list('quantité', flat=True))] + ] for i in ingrédients: total = sum([ qté * qté_unit / unit @@ -290,7 +292,7 @@ class FournéeAdmin(nested_admin.NestedModelAdmin): 'pâte__poids_de_base', ) ]).quantize(Decimal('1.000')).normalize() - totaux.append(total) + totaux.append([i, total]) extra_context["totaux_coulées"] = totaux """ liste des pains, vérifiant s'ils sont coulés """ diff --git a/fournée/core/migrations/0014_fourniture_emballage.py b/fournée/core/migrations/0014_fourniture_emballage.py new file mode 100644 index 0000000..07715ea --- /dev/null +++ b/fournée/core/migrations/0014_fourniture_emballage.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.13 on 2024-05-14 16:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0013_alter_fourniture_nom"), + ] + + operations = [ + migrations.AddField( + model_name="fourniture", + name="emballage", + field=models.CharField(blank=True, max_length=64), + ), + ] diff --git a/fournée/core/models.py b/fournée/core/models.py index 45fce1d..29eb16b 100644 --- a/fournée/core/models.py +++ b/fournée/core/models.py @@ -33,6 +33,8 @@ class Fourniture(models.Model): 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) + emballage = models.CharField(max_length=64, blank=True) def __str__(self): return f"{self.nom}" diff --git a/fournée/core/templatetags/fournée_modify.py b/fournée/core/templatetags/fournée_modify.py index f1088bb..ec4983d 100644 --- a/fournée/core/templatetags/fournée_modify.py +++ b/fournée/core/templatetags/fournée_modify.py @@ -1,6 +1,7 @@ from django import template from django.contrib.admin.templatetags.admin_modify import submit_row from django.contrib.admin.templatetags.base import InclusionAdminNode +from django.utils.safestring import mark_safe register = template.Library() @@ -12,4 +13,29 @@ def submit_row_tag(parser, token): ) +@register.simple_tag +def emballe(quantité, fourniture): + quotient = quantité // fourniture.conditionnement + reste = quantité % fourniture.conditionnement + if not quotient and reste < fourniture.conditionnement/2: + return None + if quotient == 1 and reste < fourniture.conditionnement/2: + return f"{quotient} {fourniture.emballage} et {reste} kg" + if quotient > 1 and reste < fourniture.conditionnement/2: + return f"{quotient} {fourniture.emballage}s et {reste} kg" + retrait = fourniture.conditionnement - reste + if not quotient and reste >= fourniture.conditionnement/2: + return f"{quotient + 1} {fourniture.emballage} moins {retrait} kg" + if quotient == 1 and reste >= fourniture.conditionnement/2: + return mark_safe( + f"{quotient} {fourniture.emballage} et {reste} kg" + "
ou
" + f"{quotient + 1} {fourniture.emballage}s moins {retrait} kg" + ) + if quotient > 1 and reste >= fourniture.conditionnement/2: + return mark_safe( + f"{quotient} {fourniture.emballage}s et {reste} kg" + "
ou
" + f"{quotient + 1} {fourniture.emballage}s moins {retrait} kg" + ) diff --git a/fournée/templates/admin/fournée_change_form.html b/fournée/templates/admin/fournée_change_form.html index 4c8b657..97d4d4e 100644 --- a/fournée/templates/admin/fournée_change_form.html +++ b/fournée/templates/admin/fournée_change_form.html @@ -5,6 +5,14 @@ {% block submit_buttons_bottom %}{% fournée_submit_row %}{% endblock %} + +{% block extrahead %}{{ block.super }} + +{% endblock %} + {% block after_field_sets %} {% if commandes %}

Résumé des commandes

@@ -30,11 +38,13 @@ - {% for p in totaux_coulées %} + {% for i, p in totaux_coulées %} {% if forloop.first %} Total pain : {{p}} kg {% else %} - {{ p }} + + {{ p }} kg + {% endif %} {% endfor %} @@ -42,23 +52,36 @@
{% for problems, ingrédients, quantités in coulées %} -

{{ quantités.0 }}{% if problems %} ({{ problems }}){% endif %}

+

{{ quantités.0.1 }}{% if problems %} ({{ problems }}){% endif %}

- {% for i, c in ingrédients %} - + {% for i in ingrédients %} + {% endfor %} - {% for q in quantités %} + {% for i, q in quantités %} {% if forloop.first %} {% else %} - + {% if i.fourniture.conditionnement %} + {% emballe q i.fourniture as alt %} + {% if alt %} + + {% else %} + + {% endif %} + {% endif %} {% endif %} {% endfor %} @@ -103,4 +126,14 @@
{{ i }}{{ i.fourniture.nom }}
{{ q.pâte.nom }}{{ q }} + {{ q }} kg + symbole œil +
({{ alt }})
+
+ {{ q }} kg +

{% endif %} + + {% endblock %}