Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add migration for fixing order undiscounted total price (#11503)
Iga Karbowiakb39d10d60bcc
diff --git a/saleor/order/migrations/0140_fix_order_undiscounted_total.py b/saleor/order/migrations/0140_fix_order_undiscounted_total.pynew file mode 100644index 0000000000..b3ec089607--- /dev/null+++ b/saleor/order/migrations/0140_fix_order_undiscounted_total.py@@ -0,0 +1,126 @@+# Generated by Django 3.2.16 on 2022-12-15 09:02+from functools import partial+from collections import defaultdict++from django.apps import apps as registry+from django.db import migrations+from django.db.models import Exists, F, OuterRef+from django.db.models.signals import post_migrate+from saleor.order.tasks import send_order_updated++BATCH_SIZE = 500+++def queryset_in_batches(queryset):+ """Slice a queryset into batches.++ Input queryset should be sorted be pk.+ """+ start_pk = 0++ while True:+ qs = queryset.filter(pk__gt=start_pk)[:BATCH_SIZE]+ pks = list(qs.values_list("pk", flat=True))++ if not pks:+ break++ yield pks++ start_pk = pks[-1]+++def update_order_undiscounted_price(apps, schema_editor):+ """Fix orders with discount applied on order lines.++ When the order has a voucher discount applied on lines, it is not visible+ on the order's undiscounted total price. This method is fixing such orders.+ """++ def on_migrations_complete(sender=None, **kwargs):+ order_ids = list(kwargs.get("updated_orders_pks"))+ send_order_updated.delay(order_ids)++ Order = apps.get_model("order", "Order")+ OrderLine = apps.get_model("order", "OrderLine")++ # Take orders that has applied lines voucher discounts, but the discount is+ # not visible in undiscounted price.+ orders_to_update = Order.objects.filter(+ Exists(+ OrderLine.objects.filter(+ order_id=OuterRef("id"), voucher_code__isnull=False+ )+ ),+ total_gross_amount=F("undiscounted_total_gross_amount"),+ ).order_by("id")++ updated_orders_pks = []+ for batch_pks in queryset_in_batches(orders_to_update):+ orders = Order.objects.filter(pk__in=batch_pks)+ lines = OrderLine.objects.filter(order_id__in=orders.values("id")).values(+ "order_id",+ "undiscounted_total_price_gross_amount",+ "total_price_gross_amount",+ "undiscounted_total_price_net_amount",+ "total_price_net_amount",+ )+ lines_discount_data = defaultdict(lambda: (0, 0))+ for data in lines:+ discount_amount_gross = (+ data["undiscounted_total_price_gross_amount"]+ - data["total_price_gross_amount"]+ )+ discount_amount_net = (+ data["undiscounted_total_price_net_amount"]+ - data["total_price_net_amount"]+ )+ current_discount_gross, current_discount_net = lines_discount_data[+ data["order_id"]+ ]+ lines_discount_data[data["order_id"]] = (+ current_discount_gross + discount_amount_gross,+ current_discount_net + discount_amount_net,+ )++ for order in orders:+ discount_amount_gross, discount_amount_net = lines_discount_data.get(+ order.id+ )+ if discount_amount_gross > 0 or discount_amount_net > 0:+ order.undiscounted_total_gross_amount += discount_amount_gross+ order.undiscounted_total_net_amount += discount_amount_net++ updated_orders_pks.append(order.id)++ Order.objects.bulk_update(+ orders,+ [+ "undiscounted_total_gross_amount",+ "undiscounted_total_net_amount",+ ],+ )++ # If we updated any order we should trigger `order_updated` after migrations+ if updated_orders_pks:+ updated_orders_pks = set(updated_orders_pks)+ sender = registry.get_app_config("order")+ post_migrate.connect(+ partial(on_migrations_complete, updated_orders_pks=updated_orders_pks),+ weak=False,+ dispatch_uid="send_order_updated",+ sender=sender,+ )+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0139_fix_undiscounted_total_on_lines"),+ ]++ operations = [+ migrations.RunPython(+ update_order_undiscounted_price, reverse_code=migrations.RunPython.noop+ ),+ ]diff --git a/saleor/order/migrations/0152_merge_20221215_1217.py b/saleor/order/migrations/0152_merge_20221215_1217.pynew file mode 100644index 0000000000..009c7dd7cb--- /dev/null+++ b/saleor/order/migrations/0152_merge_20221215_1217.py@@ -0,0 +1,13 @@+# Generated by Django 3.2.16 on 2022-12-15 12:17++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0140_fix_order_undiscounted_total"),+ ("order", "0151_merge_20220615_1224"),+ ]++ operations = []diff --git a/saleor/order/migrations/0155_merge_20221215_1223.py b/saleor/order/migrations/0155_merge_20221215_1223.pynew file mode 100644index 0000000000..80afc36f0b--- /dev/null+++ b/saleor/order/migrations/0155_merge_20221215_1223.py@@ -0,0 +1,13 @@+# Generated by Django 3.2.16 on 2022-12-15 12:23++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0152_merge_20221215_1217"),+ ("order", "0154_auto_20220621_0850"),+ ]++ operations = []diff --git a/saleor/order/migrations/0156_merge_20221215_1235.py b/saleor/order/migrations/0156_merge_20221215_1235.pynew file mode 100644index 0000000000..e8f6939107--- /dev/null+++ b/saleor/order/migrations/0156_merge_20221215_1235.py@@ -0,0 +1,13 @@+# Generated by Django 3.2.16 on 2022-12-15 12:35++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0155_merge_20221215_1223"),+ ("order", "0155_order_should_refresh_prices"),+ ]++ operations = []diff --git a/saleor/order/migrations/0159_merge_20221215_1244.py b/saleor/order/migrations/0159_merge_20221215_1244.pynew file mode 100644index 0000000000..4142be7d37--- /dev/null+++ b/saleor/order/migrations/0159_merge_20221215_1244.py@@ -0,0 +1,13 @@+# Generated by Django 3.2.16 on 2022-12-15 12:44++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0156_merge_20221215_1235"),+ ("order", "0158_migrate_base_shipping_price_amount"),+ ]++ operations = []diff --git a/saleor/order/migrations/0160_merge_20221215_1253.py b/saleor/order/migrations/0160_merge_20221215_1253.pynew file mode 100644index 0000000000..9e7bb18593--- /dev/null+++ b/saleor/order/migrations/0160_merge_20221215_1253.py@@ -0,0 +1,13 @@+# Generated by Django 3.2.16 on 2022-12-15 12:53++from django.db import migrations+++class Migration(migrations.Migration):++ dependencies = [+ ("order", "0159_merge_20221215_1244"),+ ("order", "0159_order_shipping_tax_class_and_more"),+ ]++ operations = []