From d03102fe57f7592d7ae332796b9b8e616f11aefa Mon Sep 17 00:00:00 2001 From: Laetitia Getti Date: Mon, 15 May 2023 11:47:30 +0200 Subject: [PATCH] afficher et calculer le prix de la commande --- .../order/migrations/0019_order_price.py | 17 +++++++++++++++++ la_chariotte/order/models.py | 4 ++++ .../order/templates/order/order_detail.html | 5 ++--- la_chariotte/order/views.py | 10 ++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 la_chariotte/order/migrations/0019_order_price.py diff --git a/la_chariotte/order/migrations/0019_order_price.py b/la_chariotte/order/migrations/0019_order_price.py new file mode 100644 index 0000000..f502bba --- /dev/null +++ b/la_chariotte/order/migrations/0019_order_price.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2 on 2023-05-15 09:33 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("order", "0018_order_articles_nb"), + ] + + operations = [ + migrations.AddField( + model_name="order", + name="price", + field=models.DecimalField(decimal_places=2, default=0, max_digits=10), + ), + ] diff --git a/la_chariotte/order/models.py b/la_chariotte/order/models.py index c9a1f19..25fb651 100644 --- a/la_chariotte/order/models.py +++ b/la_chariotte/order/models.py @@ -79,6 +79,7 @@ class Order(models.Model): ) author = models.ForeignKey(OrderAuthor, on_delete=models.CASCADE) articles_nb = models.PositiveIntegerField(default=0) + price = models.DecimalField(max_digits=10, decimal_places=2, default=0) def __str__(self): # pragma: no cover return f"Commande de {self.author} pour la commande groupée {self.grouped_order.pk}" @@ -110,5 +111,8 @@ class OrderedItem(models.Model): ) item = models.ForeignKey(Item, on_delete=models.CASCADE, related_name="orders") + def get_price(self): + return self.nb * self.item.price + def __str__(self): # pragma: no cover return f"{self.nb} {self.item}, dans la commande {self.order.pk}" diff --git a/la_chariotte/order/templates/order/order_detail.html b/la_chariotte/order/templates/order/order_detail.html index acd73a9..dfae7c4 100644 --- a/la_chariotte/order/templates/order/order_detail.html +++ b/la_chariotte/order/templates/order/order_detail.html @@ -14,11 +14,10 @@

Votre commande

+

Prix total de la commande : {{ order.price }} €

Rendez-vous le {{ order.grouped_order.delivery_date }} à {{ order.grouped_order.place }} pour récupérer vos produits !

diff --git a/la_chariotte/order/views.py b/la_chariotte/order/views.py index eece0b9..a79f8be 100644 --- a/la_chariotte/order/views.py +++ b/la_chariotte/order/views.py @@ -141,6 +141,7 @@ def order(request, grouped_order_id): OrderedItem.objects.create(nb=v, order=order, item=item) compute_ordered_nb(item) compute_order_articles_nb(order) + compute_order_price(order) if order.articles_nb == 0: # Redisplay the order form for this grouped order. return render( @@ -178,6 +179,15 @@ def compute_order_articles_nb(order): order.save() +def compute_order_price(order): + """Computes the total price of the order""" + price = 0 + for ord_item in order.ordered_items.all(): + price += ord_item.get_price() + order.price = price + order.save() + + class OrderDetailView(generic.DetailView): """Confirmation page after a user orders"""