From 11d3b740aa2ed3b0b21caad3d5897f125666bb18 Mon Sep 17 00:00:00 2001 From: Nathan Chapman Date: Wed, 26 Oct 2022 09:29:51 -0600 Subject: [PATCH] Add basic stock handling --- src/core/models.py | 18 +++++++++++++++ .../templates/dashboard/variant_form.html | 2 +- src/dashboard/views.py | 5 ++++ .../templates/storefront/category_detail.html | 1 - src/storefront/views.py | 23 +++++++++++++++---- 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/core/models.py b/src/core/models.py index d4de40b..2962905 100644 --- a/src/core/models.py +++ b/src/core/models.py @@ -366,6 +366,14 @@ class Order(models.Model): objects = OrderManager() + def minus_stock(self): + for line in self.lines.all(): + line.minus_stock() + + def add_stock(self): + for line in self.lines.all(): + line.add_stock() + def get_total_quantity(self): return sum([line.quantity for line in self]) @@ -443,6 +451,16 @@ class OrderLine(models.Model): def quantity_unfulfilled(self): return self.quantity - self.quantity_fulfilled + def minus_stock(self): + if self.variant.track_inventory: + self.variant.stock -= self.quantity + self.variant.save() + + def add_stock(self): + if self.variant.track_inventory: + self.variant.stock += self.quantity + self.variant.save() + class TrackingNumber(models.Model): order = models.ForeignKey( diff --git a/src/dashboard/templates/dashboard/variant_form.html b/src/dashboard/templates/dashboard/variant_form.html index 03f496e..44107d8 100644 --- a/src/dashboard/templates/dashboard/variant_form.html +++ b/src/dashboard/templates/dashboard/variant_form.html @@ -11,7 +11,7 @@ {% csrf_token %} {{form.as_p}}

- or cancel + or cancel

diff --git a/src/dashboard/views.py b/src/dashboard/views.py index 1da356a..de64a44 100644 --- a/src/dashboard/views.py +++ b/src/dashboard/views.py @@ -234,6 +234,11 @@ class OrderCancelView(LoginRequiredMixin, SuccessMessageMixin, UpdateView): 'status': OrderStatus.CANCELED } + def form_valid(self, form): + form.instance.add_stock() + form.instance.save() + return super().form_valid(form) + def get_success_url(self): return reverse('dashboard:order-detail', kwargs={'pk': self.object.pk}) diff --git a/src/storefront/templates/storefront/category_detail.html b/src/storefront/templates/storefront/category_detail.html index 1ab698e..c19c4fb 100644 --- a/src/storefront/templates/storefront/category_detail.html +++ b/src/storefront/templates/storefront/category_detail.html @@ -10,7 +10,6 @@

Welcome to our new website!

NEW COOL LOOK, SAME GREAT COFFEE

-{# Home > Category > "Coffee/Merchandise" #}