Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Improve order status logic for fulfillments waiting for approval (#17471)
delemeator6e0a2cfc9287
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4764606935..3961e0b936 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -91,3 +91,4 @@ All notable, unreleased changes to this project will be documented in this file.- Use denormalized base prices during order update - #17160 by @zedzior- `UNCONFIRMED` orders will never refresh its base prices- `DRAFT` orders will refresh its base prices after default 24 hours+- Improve status calculation for orders with waiting-for-approval fulfillments - #17471 by @delemeatordiff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex c957ce38c2..087842e5a6 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -833,7 +833,6 @@ def cancel_waiting_fulfillment(OrderLine.objects.bulk_update(order_lines, ["quantity_fulfilled"])fulfillment.delete()- update_order_status(fulfillment.order)call_event(manager.fulfillment_canceled, fulfillment)call_order_event(manager,diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex e02331d17e..1667c4d9f8 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -472,7 +472,25 @@ def test_update_order_status_partially_returned(fulfilled_order):def test_update_order_status_waiting_for_approval(fulfilled_order):- fulfilled_order.fulfillments.create(status=FulfillmentStatus.WAITING_FOR_APPROVAL)+ for fulfillment in fulfilled_order.fulfillments.all():+ fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL+ fulfillment.save()+ fulfilled_order.status = OrderStatus.FULFILLED+ fulfilled_order.save()++ update_order_status(fulfilled_order)++ assert fulfilled_order.status == OrderStatus.UNFULFILLED+++def test_update_order_status_partially_waiting_for_approval(fulfilled_order):+ first_fulfillment = fulfilled_order.fulfillments.first()+ second_fulfillment = fulfilled_order.fulfillments.create(+ status=FulfillmentStatus.WAITING_FOR_APPROVAL+ )+ first_line = first_fulfillment.lines.first()+ first_line.fulfillment = second_fulfillment+ first_line.save()fulfilled_order.status = OrderStatus.FULFILLEDfulfilled_order.save()diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.pyindex 71ef3575f5..acb705d2d7 100644--- a/saleor/order/tests/test_order_actions.py+++ b/saleor/order/tests/test_order_actions.py@@ -37,6 +37,7 @@ from ..actions import (call_order_events,cancel_fulfillment,cancel_order,+ cancel_waiting_fulfillment,clean_mark_order_as_paid,fulfill_order_lines,handle_fully_paid_order,@@ -570,7 +571,24 @@ def test_cancel_fulfillment(fulfilled_order, warehouse):assert line_2.order_line.quantity_fulfilled == 0-def test_cancel_fulfillment_variant_witout_inventory_tracking(+def test_cancel_waiting_fulfillment_for_unconfirmed_order(fulfilled_order):+ for fulfillment in fulfilled_order.fulfillments.all():+ fulfillment.status = FulfillmentStatus.WAITING_FOR_APPROVAL+ fulfillment.save()+ fulfilled_order.status = OrderStatus.UNCONFIRMED+ fulfilled_order.save()++ cancel_waiting_fulfillment(+ fulfilled_order.fulfillments.first(),+ None,+ None,+ get_plugins_manager(allow_replica=False),+ )++ assert fulfilled_order.status == OrderStatus.UNCONFIRMED+++def test_cancel_fulfillment_variant_without_inventory_tracking(fulfilled_order_without_inventory_tracking, warehouse):fulfillment = fulfilled_order_without_inventory_tracking.fulfillments.first()diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex a5e7b85b8d..45f2937dfe 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -166,6 +166,7 @@ def _calculate_quantity_including_returns(order):quantity_fulfilled = sum([line.quantity_fulfilled for line in lines])quantity_returned = 0quantity_replaced = 0+ quantity_awaiting_approval = 0for fulfillment in order.fulfillments.all():# count returned quantity for orderif fulfillment.status in [@@ -176,12 +177,19 @@ def _calculate_quantity_including_returns(order):# count replaced quantity for orderelif fulfillment.status == FulfillmentStatus.REPLACED:quantity_replaced += fulfillment.get_total_quantity()+ elif fulfillment.status == FulfillmentStatus.WAITING_FOR_APPROVAL:+ quantity_awaiting_approval += fulfillment.get_total_quantity()# Subtract the replace quantity as it shouldn't be taken into consideration for# calculating the order statustotal_quantity -= quantity_replacedquantity_fulfilled -= quantity_replaced- return total_quantity, quantity_fulfilled, quantity_returned+ return (+ total_quantity,+ quantity_fulfilled,+ quantity_returned,+ quantity_awaiting_approval,+ )def update_order_status(order: Order):@@ -195,19 +203,18 @@ def update_order_status(order: Order):total_quantity,quantity_fulfilled,quantity_returned,+ quantity_awaiting_approval,) = _calculate_quantity_including_returns(locked_order)- # check if order contains any fulfillments that awaiting approval- awaiting_approval = locked_order.fulfillments.filter(- status=FulfillmentStatus.WAITING_FOR_APPROVAL- ).exists()+ all_products_replaced = total_quantity == 0+ if all_products_replaced:+ returnstatus = determine_order_status(- order,total_quantity,quantity_fulfilled,quantity_returned,- awaiting_approval,+ quantity_awaiting_approval,)# we would like to update the status for the order provided as the argument@@ -218,23 +225,18 @@ def update_order_status(order: Order):def determine_order_status(- order: Order,total_quantity: int,quantity_fulfilled: int,quantity_returned: int,- awaiting_approval: bool,+ quantity_awaiting_approval: int,):- # total_quantity == 0 means that all products have been replaced, we don't change- # the order status in that case- if total_quantity == 0:- status = order.status- elif quantity_fulfilled <= 0:+ if quantity_fulfilled - quantity_awaiting_approval <= 0:status = OrderStatus.UNFULFILLEDelif 0 < quantity_returned < total_quantity:status = OrderStatus.PARTIALLY_RETURNEDelif quantity_returned == total_quantity:status = OrderStatus.RETURNED- elif quantity_fulfilled < total_quantity or awaiting_approval:+ elif quantity_fulfilled - quantity_awaiting_approval < total_quantity:status = OrderStatus.PARTIALLY_FULFILLEDelse:status = OrderStatus.FULFILLED