Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Update order module to work with new stock implementation
Mira Szewczyk022368dc611c
diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex 3d32310a87..12a93cd45c 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -13,6 +13,7 @@ from . import FulfillmentStatus, OrderStatus, emails, events, utilsfrom .emails import send_fulfillment_confirmation_to_customer, send_payment_confirmationfrom .models import Fulfillment, FulfillmentLinefrom .utils import (+ get_order_country,order_line_needs_automatic_fulfillment,recalculate_order,restock_fulfillment_lines,@@ -216,8 +217,9 @@ def clean_mark_order_as_paid(order: "Order"):def fulfill_order_line(order_line, quantity):"""Fulfill order line with given quantity."""+ country = get_order_country(order_line.order)if order_line.variant and order_line.variant.track_inventory:- decrease_stock(order_line.variant, quantity)+ decrease_stock(order_line.variant, country, quantity)order_line.quantity_fulfilled += quantityorder_line.save(update_fields=["quantity_fulfilled"])diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 16dc164efb..c62a43952a 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -20,6 +20,13 @@ from ..stock.management import allocate_stock, deallocate_stock, increase_stockfrom . import events+def get_order_country(order) -> str:+ address = order.billing_address+ if order.is_shipping_required():+ address = order.shipping_address+ return address.country.code++def order_line_needs_automatic_fulfillment(line: OrderLine) -> bool:"""Check if given line is digital and should be automatically fulfilled."""digital_content_settings = get_default_digital_content_settings()@@ -168,7 +175,7 @@ def add_variant_to_order(By default, raises InsufficientStock exception if quantity could not befulfilled. This can be disabled by setting `allow_overselling` to True."""- country = order.shipping_address.country+ country = get_order_country(order)if not allow_overselling:check_stock_quantity(variant, country, quantity)@@ -213,7 +220,7 @@ def add_variant_to_order()if variant.track_inventory and track_inventory:- allocate_stock(variant, quantity)+ allocate_stock(variant, country, quantity)return line@@ -263,12 +270,14 @@ def delete_order_line(line):def restock_order_lines(order):"""Return ordered products to corresponding stocks."""+ country = get_order_country(order)+for line in order:if line.variant and line.variant.track_inventory:if line.quantity_unfulfilled > 0:- deallocate_stock(line.variant, line.quantity_unfulfilled)+ deallocate_stock(line.variant, country, line.quantity_unfulfilled)if line.quantity_fulfilled > 0:- increase_stock(line.variant, line.quantity_fulfilled)+ increase_stock(line.variant, country, line.quantity_fulfilled)if line.quantity_fulfilled > 0:line.quantity_fulfilled = 0@@ -277,9 +286,12 @@ def restock_order_lines(order):def restock_fulfillment_lines(fulfillment):"""Return fulfilled products to corresponding stocks."""+ country = get_order_country(fulfillment.order)for line in fulfillment:if line.order_line.variant and line.order_line.variant.track_inventory:- increase_stock(line.order_line.variant, line.quantity, allocate=True)+ increase_stock(+ line.order_line.variant, country, line.quantity, allocate=True+ )def sum_order_totals(qs):diff --git a/tests/test_order.py b/tests/test_order.pyindex 3e25a9fbac..cb5ca3e4c7 100644--- a/tests/test_order.py+++ b/tests/test_order.py@@ -32,6 +32,7 @@ from saleor.order.utils import (from saleor.payment import ChargeStatusfrom saleor.payment.models import Paymentfrom saleor.product.models import Collection+from saleor.stock.models import Stockdef test_total_setter():@@ -81,16 +82,17 @@ def test_add_variant_to_order_allocates_stock_for_new_variant(variant = product.variants.get()variant.track_inventory = track_inventoryvariant.save()+ stock = Stock.objects.get(product_variant=variant)- stock_before = variant.quantity_allocated+ stock_before = stock.quantity_allocatedadd_variant_to_order(order_with_lines, variant, 1)- variant.refresh_from_db()+ stock.refresh_from_db()if track_inventory:- assert variant.quantity_allocated == stock_before + 1+ assert stock.quantity_allocated == stock_before + 1else:- assert variant.quantity_allocated == stock_before+ assert stock.quantity_allocated == stock_beforedef test_add_variant_to_order_edits_line_for_existing_variant(order_with_lines):@@ -110,28 +112,30 @@ def test_add_variant_to_order_edits_line_for_existing_variant(order_with_lines):def test_add_variant_to_order_allocates_stock_for_existing_variant(order_with_lines):existing_line = order_with_lines.lines.first()variant = existing_line.variant- stock_before = variant.quantity_allocated+ stock = Stock.objects.get(product_variant=variant)+ stock_before = stock.quantity_allocatedadd_variant_to_order(order_with_lines, variant, 1)- variant.refresh_from_db()- assert variant.quantity_allocated == stock_before + 1+ stock.refresh_from_db()+ assert stock.quantity_allocated == stock_before + 1def test_add_variant_to_order_allow_overselling(order_with_lines):existing_line = order_with_lines.lines.first()variant = existing_line.variant- stock_before = variant.quantity_allocated+ stock = Stock.objects.get(product_variant=variant)+ stock_before = stock.quantity_allocated- quantity = variant.quantity + 1+ quantity = stock.quantity + 1with pytest.raises(InsufficientStock):add_variant_to_order(order_with_lines, variant, quantity, allow_overselling=False)add_variant_to_order(order_with_lines, variant, quantity, allow_overselling=True)- variant.refresh_from_db()- assert variant.quantity_allocated == stock_before + quantity+ stock.refresh_from_db()+ assert stock.quantity_allocated == stock_before + quantity@pytest.mark.parametrize("track_inventory", (True, False))@@ -146,31 +150,33 @@ def test_restock_order_lines(order_with_lines, track_inventory):line_1.variant.save()line_2.variant.save()+ stock_1 = Stock.objects.get(product_variant=line_1.variant)+ stock_2 = Stock.objects.get(product_variant=line_2.variant)- stock_1_quantity_allocated_before = line_1.variant.quantity_allocated- stock_2_quantity_allocated_before = line_2.variant.quantity_allocated+ stock_1_quantity_allocated_before = stock_1.quantity_allocated+ stock_2_quantity_allocated_before = stock_2.quantity_allocated- stock_1_quantity_before = line_1.variant.quantity- stock_2_quantity_before = line_2.variant.quantity+ stock_1_quantity_before = stock_1.quantity+ stock_2_quantity_before = stock_2.quantityrestock_order_lines(order)- line_1.variant.refresh_from_db()- line_2.variant.refresh_from_db()+ stock_1.refresh_from_db()+ stock_2.refresh_from_db()if track_inventory:- assert line_1.variant.quantity_allocated == (+ assert stock_1.quantity_allocated == (stock_1_quantity_allocated_before - line_1.quantity)- assert line_2.variant.quantity_allocated == (+ assert stock_2.quantity_allocated == (stock_2_quantity_allocated_before - line_2.quantity)else:- assert line_1.variant.quantity_allocated == (stock_1_quantity_allocated_before)- assert line_2.variant.quantity_allocated == (stock_2_quantity_allocated_before)+ assert stock_1.quantity_allocated == (stock_1_quantity_allocated_before)+ assert stock_2.quantity_allocated == (stock_2_quantity_allocated_before)- assert line_1.variant.quantity == stock_1_quantity_before- assert line_2.variant.quantity == stock_2_quantity_before+ assert stock_1.quantity == stock_1_quantity_before+ assert stock_2.quantity == stock_2_quantity_beforeassert line_1.quantity_fulfilled == 0assert line_2.quantity_fulfilled == 0@@ -178,27 +184,29 @@ def test_restock_order_lines(order_with_lines, track_inventory):def test_restock_fulfilled_order_lines(fulfilled_order):line_1 = fulfilled_order.lines.first()line_2 = fulfilled_order.lines.last()- stock_1_quantity_allocated_before = line_1.variant.quantity_allocated- stock_2_quantity_allocated_before = line_2.variant.quantity_allocated- stock_1_quantity_before = line_1.variant.quantity- stock_2_quantity_before = line_2.variant.quantity+ stock_1 = Stock.objects.get(product_variant=line_1.variant)+ stock_2 = Stock.objects.get(product_variant=line_2.variant)+ stock_1_quantity_allocated_before = stock_1.quantity_allocated+ stock_2_quantity_allocated_before = stock_2.quantity_allocated+ stock_1_quantity_before = stock_1.quantity+ stock_2_quantity_before = stock_2.quantityrestock_order_lines(fulfilled_order)- line_1.variant.refresh_from_db()- line_2.variant.refresh_from_db()- assert line_1.variant.quantity_allocated == stock_1_quantity_allocated_before- assert line_2.variant.quantity_allocated == stock_2_quantity_allocated_before- assert line_1.variant.quantity == stock_1_quantity_before + line_1.quantity- assert line_2.variant.quantity == stock_2_quantity_before + line_2.quantity+ stock_1.refresh_from_db()+ stock_2.refresh_from_db()+ assert stock_1.quantity_allocated == stock_1_quantity_allocated_before+ assert stock_2.quantity_allocated == stock_2_quantity_allocated_before+ assert stock_1.quantity == stock_1_quantity_before + line_1.quantity+ assert stock_2.quantity == stock_2_quantity_before + line_2.quantitydef test_restock_fulfillment_lines(fulfilled_order):fulfillment = fulfilled_order.fulfillments.first()line_1 = fulfillment.lines.first()line_2 = fulfillment.lines.last()- stock_1 = line_1.order_line.variant- stock_2 = line_2.order_line.variant+ stock_1 = Stock.objects.get(product_variant=line_1.order_line.variant)+ stock_2 = Stock.objects.get(product_variant=line_2.order_line.variant)stock_1_quantity_allocated_before = stock_1.quantity_allocatedstock_2_quantity_allocated_before = stock_2.quantity_allocatedstock_1_quantity_before = stock_1.quantity