Skip to content

Commit 9d7ce87

Browse files
authored
Merge pull request #532 from bruecksen/528-imp-customer-order-updated-if-nothing-has-changed
In create/update order view detect if order has been changed and only…
2 parents 73860c9 + 0dcce6b commit 9d7ce87

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

bakeup/shop/models.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -829,20 +829,25 @@ def create_or_update_customer_order(
829829
cls, request, production_day, customer, products, point_of_sale=None
830830
):
831831
with transaction.atomic():
832+
changes_detected = False
832833
point_of_sale = (
833834
point_of_sale
834835
and PointOfSale.objects.get(pk=point_of_sale)
835836
or customer.point_of_sale
836837
)
838+
old_customer_order = CustomerOrder.objects.filter(
839+
production_day=production_day, customer=customer
840+
).first()
837841
customer_order, created = CustomerOrder.objects.update_or_create(
838842
production_day=production_day,
839843
customer=customer,
840844
defaults={
841845
"point_of_sale": point_of_sale,
842846
},
843847
)
848+
if old_customer_order and old_customer_order.point_of_sale != point_of_sale:
849+
changes_detected = True
844850
for product, quantity in products.items():
845-
# print(product, quantity)
846851
production_day_product = ProductionDayProduct.objects.get(
847852
production_day=production_day, product=product
848853
)
@@ -883,6 +888,10 @@ def create_or_update_customer_order(
883888
if product.sale_price:
884889
price = product.sale_price.price.amount
885890
price_total = price * quantity
891+
old_position = CustomerOrderPosition.objects.filter(
892+
Q(product=product)
893+
| Q(product__product_template=product, order=customer_order)
894+
).first()
886895
position, created = CustomerOrderPosition.objects.filter(
887896
Q(product=product) | Q(product__product_template=product)
888897
).update_or_create(
@@ -894,12 +903,19 @@ def create_or_update_customer_order(
894903
"price_total": price_total,
895904
},
896905
)
906+
if created:
907+
changes_detected = True
908+
elif old_position and old_position.quantity != quantity:
909+
changes_detected = True
897910
elif quantity == 0:
898-
CustomerOrderPosition.objects.filter(
911+
deleted, object_types = CustomerOrderPosition.objects.filter(
899912
Q(product=product) | Q(product__product_template=product),
900913
order=customer_order,
901914
).delete()
902-
return customer_order, created
915+
if deleted:
916+
changes_detected = True
917+
918+
return customer_order, created, changes_detected
903919

904920
def get_production_day_products_ordered_list(self):
905921
production_day_products = (

bakeup/shop/views.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ def customer_order_add_or_update(request, production_day):
130130
for k, v in request.POST.items()
131131
if k.startswith("product-")
132132
}
133-
order, created = CustomerOrder.create_or_update_customer_order(
134-
request,
135-
production_day,
136-
request.user.customer,
137-
products,
138-
request.POST.get("point_of_sale", None),
133+
order, created, changes_detected = (
134+
CustomerOrder.create_or_update_customer_order(
135+
request,
136+
production_day,
137+
request.user.customer,
138+
products,
139+
request.POST.get("point_of_sale", None),
140+
)
139141
)
140142
products_recurring = {
141143
Product.objects.get(pk=k.replace("productabo-", "")): int(
@@ -162,12 +164,12 @@ def customer_order_add_or_update(request, production_day):
162164
messages.INFO,
163165
"Vielen Dank, die Bestellung wurde geändert.",
164166
)
165-
if order:
167+
if order and changes_detected:
166168
if EmailSettings.load(request_or_site=request).send_email_order_confirm:
167169
order.send_order_confirm_email(request)
168-
return HttpResponseRedirect(
169-
"{}#bestellung-{}".format(reverse("shop:order-list"), order.pk)
170-
)
170+
return HttpResponseRedirect(
171+
"{}#bestellung-{}".format(reverse("shop:order-list"), order.pk)
172+
)
171173
elif "cancel" in request.POST:
172174
# cancellation of order
173175
customer_order = get_object_or_404(

0 commit comments

Comments
 (0)