Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix incorrect stock allocation (#8963)
Iga Karbowiakc052f59016d8
diff --git a/saleor/order/tests/test_fulfillments_actions.py b/saleor/order/tests/test_fulfillments_actions.pyindex c6dfca61ec..a41f51e753 100644--- a/saleor/order/tests/test_fulfillments_actions.py+++ b/saleor/order/tests/test_fulfillments_actions.py@@ -662,3 +662,87 @@ def test_create_fullfilment_with_out_of_stock_webhook_not_triggered(flush_post_commit_hooks()product_variant_out_of_stock_webhook.assert_not_called()+++@patch("saleor.order.actions.send_fulfillment_confirmation_to_customer", autospec=True)+def test_create_fulfillments_quantity_allocated_lower_than_line_quantity(+ mock_email_fulfillment,+ staff_user,+ order_with_lines,+ warehouse,+ site_settings,+):+ """Ensure that when for some lines quantity allocated is lower than line quantity+ and an error is raised during the deallocation uantity allocated value for+ all allocation will be updated."""++ # given+ order = order_with_lines+ order_line1, order_line2 = order.lines.all()+ line_1_qty = 3+ line_2_qty = 2++ allocation_1 = order_line1.allocations.first()+ allocation_2 = order_line2.allocations.first()++ stock_quantity = 100+ allocation_1_qty_allocated = line_1_qty - 1+ allocation_2_qty_allocated = line_2_qty++ stock_1 = allocation_1.stock+ stock_2 = allocation_2.stock+ stock_1.quantity = stock_quantity+ stock_2.quantity = stock_quantity+ Stock.objects.bulk_update([stock_1, stock_2], ["quantity"])++ allocation_1.quantity_allocated = allocation_1_qty_allocated+ allocation_2.quantity_allocated = allocation_2_qty_allocated+ Allocation.objects.bulk_update([allocation_1, allocation_2], ["quantity_allocated"])++ fulfillment_lines_for_warehouses = {+ str(warehouse.pk): [+ {"order_line": order_line1, "quantity": line_1_qty},+ {"order_line": order_line2, "quantity": line_2_qty},+ ]+ }+ manager = get_plugins_manager()++ # when+ [fulfillment] = create_fulfillments(+ staff_user,+ None,+ order,+ fulfillment_lines_for_warehouses,+ manager,+ site_settings,+ True,+ )+ flush_post_commit_hooks()++ # then+ order.refresh_from_db()+ fulfillment_lines = FulfillmentLine.objects.filter(+ fulfillment__order=order+ ).order_by("pk")+ assert fulfillment_lines[0].stock == order_line1.variant.stocks.get()+ assert fulfillment_lines[0].quantity == line_1_qty+ assert fulfillment_lines[1].stock == order_line2.variant.stocks.get()+ assert fulfillment_lines[1].quantity == line_2_qty++ assert order.status == OrderStatus.FULFILLED+ assert order.fulfillments.get() == fulfillment++ order_line1, order_line2 = order.lines.all()+ assert order_line1.quantity_fulfilled == line_1_qty+ assert order_line2.quantity_fulfilled == line_2_qty++ assert (+ Allocation.objects.filter(+ order_line__order=order, quantity_allocated__gt=0+ ).count()+ == 0+ )++ mock_email_fulfillment.assert_called_once_with(+ order, order.fulfillments.get(), staff_user, None, manager+ )diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex 80935b9eeb..2018916894 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -46,7 +46,6 @@ from ..utils import (get_voucher_discount_for_order,recalculate_order,restock_fulfillment_lines,- restock_order_lines,update_order_prices,update_order_status,)@@ -302,77 +301,6 @@ def test_add_variant_to_order_not_allocates_stock_for_existing_variant(assert existing_line.quantity_unfulfilled == quantity_unfulfilled_before + 1[email protected]("track_inventory", (True, False))-def test_restock_order_lines(order_with_lines, track_inventory):-- order = order_with_lines- line_1 = order.lines.first()- line_2 = order.lines.last()-- line_1.variant.track_inventory = track_inventory- line_2.variant.track_inventory = 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 = get_quantity_allocated_for_stock(stock_1)- stock_2_quantity_allocated_before = get_quantity_allocated_for_stock(stock_2)-- stock_1_quantity_before = stock_1.quantity- stock_2_quantity_before = stock_2.quantity-- restock_order_lines(order, get_plugins_manager())-- stock_1.refresh_from_db()- stock_2.refresh_from_db()-- if track_inventory:- assert get_quantity_allocated_for_stock(stock_1) == (- stock_1_quantity_allocated_before - line_1.quantity- )- assert get_quantity_allocated_for_stock(stock_2) == (- stock_2_quantity_allocated_before - line_2.quantity- )- else:- assert get_quantity_allocated_for_stock(stock_1) == (- stock_1_quantity_allocated_before- )- assert get_quantity_allocated_for_stock(stock_2) == (- stock_2_quantity_allocated_before- )-- assert stock_1.quantity == stock_1_quantity_before- assert stock_2.quantity == stock_2_quantity_before- assert line_1.quantity_fulfilled == 0- assert line_2.quantity_fulfilled == 0---def test_restock_fulfilled_order_lines(fulfilled_order):- line_1 = fulfilled_order.lines.first()- line_2 = fulfilled_order.lines.last()- 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 = get_quantity_allocated_for_stock(stock_1)- stock_2_quantity_allocated_before = get_quantity_allocated_for_stock(stock_2)- stock_1_quantity_before = stock_1.quantity- stock_2_quantity_before = stock_2.quantity-- restock_order_lines(fulfilled_order, get_plugins_manager())-- stock_1.refresh_from_db()- stock_2.refresh_from_db()- assert (- get_quantity_allocated_for_stock(stock_1) == stock_1_quantity_allocated_before- )- assert (- get_quantity_allocated_for_stock(stock_2) == 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.quantity--def test_restock_fulfillment_lines(fulfilled_order, warehouse):fulfillment = fulfilled_order.fulfillments.first()line_1 = fulfillment.lines.first()diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 7290944947..127011cc9b 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -27,7 +27,6 @@ from ..order.models import Order, OrderLinefrom ..product.utils.digital_products import get_default_digital_content_settingsfrom ..shipping.models import ShippingMethodfrom ..warehouse.management import (- deallocate_stock,decrease_allocations,get_order_lines_with_track_inventory,increase_allocations,@@ -630,35 +629,6 @@ def delete_order_line(line_info, manager):line_info.line.delete()-def restock_order_lines(order, manager):- """Return ordered products to corresponding stocks."""- country = get_order_country(order)- default_warehouse = Warehouse.objects.filter(- shipping_zones__countries__contains=country- ).first()-- dellocating_stock_lines: List[OrderLineInfo] = []- for line in order.lines.all():- if line.variant and line.variant.track_inventory:- if line.quantity_unfulfilled > 0:- dellocating_stock_lines.append(- OrderLineInfo(line=line, quantity=line.quantity_unfulfilled)- )- if line.quantity_fulfilled > 0:- allocation = line.allocations.first()- warehouse = (- allocation.stock.warehouse if allocation else default_warehouse- )- increase_stock(line, warehouse, line.quantity_fulfilled)-- if line.quantity_fulfilled > 0:- line.quantity_fulfilled = 0- line.save(update_fields=["quantity_fulfilled"])-- if dellocating_stock_lines:- deallocate_stock(dellocating_stock_lines, manager)--def restock_fulfillment_lines(fulfillment, warehouse):"""Return fulfilled products to corresponding stocks.diff --git a/saleor/warehouse/management.py b/saleor/warehouse/management.pyindex 9fb286f717..17f2d7a231 100644--- a/saleor/warehouse/management.py+++ b/saleor/warehouse/management.py@@ -183,7 +183,6 @@ def _create_allocations(return insufficient_stock, []-@traced_atomic_transaction()def deallocate_stock(order_lines_data: Iterable["OrderLineInfo"], manager: PluginsManager):@@ -234,9 +233,6 @@ def deallocate_stock(if not quantity_dealocated == quantity:not_dellocated_lines.append(order_line)- if not_dellocated_lines:- raise AllocationError(not_dellocated_lines)-allocations_before_update = list(Allocation.objects.filter(id__in=[a.id for a in allocations_to_update]@@ -259,6 +255,9 @@ def deallocate_stock())+ if not_dellocated_lines:+ raise AllocationError(not_dellocated_lines)+@traced_atomic_transaction()def increase_stock(diff --git a/saleor/warehouse/tests/test_stock_management.py b/saleor/warehouse/tests/test_stock_management.pyindex fff706a626..735a60707e 100644--- a/saleor/warehouse/tests/test_stock_management.py+++ b/saleor/warehouse/tests/test_stock_management.py@@ -560,6 +560,75 @@ def test_decrease_stock_multiple_lines(allocations):assert allocation_1.quantity_allocated == 10+def test_decrease_stock_multiple_lines_deallocate_stock_raises_error(order_with_lines):+ """Ensure that when some of the lines raise an error during the deallocation+ quantity allocated value for all allocation will be updated."""++ # given+ order_line_1 = order_with_lines.lines.first()+ order_line_2 = order_with_lines.lines.last()++ allocation_1 = order_line_1.allocations.first()+ allocation_2 = order_line_2.allocations.first()++ stock_quantity = 100+ allocation_1_qty_allocated = 10+ allocation_2_qty_allocated = 80++ stock_1 = allocation_1.stock+ stock_2 = allocation_2.stock+ stock_1.quantity = stock_quantity+ stock_2.quantity = stock_quantity+ Stock.objects.bulk_update([stock_1, stock_2], ["quantity"])++ allocation_1.quantity_allocated = allocation_1_qty_allocated+ allocation_1.order_line = order_line_1+ warehouse_pk_1 = stock_1.warehouse.pk++ allocation_2.quantity_allocated = allocation_2_qty_allocated+ allocation_2.order_line = order_line_2+ warehouse_pk_2 = stock_2.warehouse.pk++ Allocation.objects.bulk_update(+ [allocation_1, allocation_2], ["quantity_allocated", "order_line"]+ )++ line_1_qty = 50+ line_2_qty = 20++ # when+ decrease_stock(+ [+ OrderLineInfo(+ line=order_line_1,+ quantity=line_1_qty,+ variant=order_line_1.variant,+ warehouse_pk=warehouse_pk_1,+ ),+ OrderLineInfo(+ line=order_line_2,+ quantity=line_2_qty,+ variant=order_line_2.variant,+ warehouse_pk=warehouse_pk_2,+ ),+ ],+ manager=get_plugins_manager(),+ )++ # then+ stock_1.refresh_from_db()+ assert stock_1.quantity == stock_quantity - line_1_qty++ stock_2.refresh_from_db()+ assert stock_2.quantity == stock_quantity - line_2_qty++ allocation_1.refresh_from_db()+ assert allocation_1.quantity_allocated == 0++ allocation_2.refresh_from_db()+ assert allocation_2.quantity_allocated == allocation_2_qty_allocated - line_2_qty++def test_decrease_stock_partially(allocation):stock = allocation.stockstock.quantity = 100