Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Extend logic for updating order status (#6618)

Maciej Korycinski644f33094857
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index c3b050006d..935d80c631 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -564,6 +564,7 @@ class FulfillmentRefundProducts(FulfillmentRefundAndReturnProductBase):
cleaned_input["payment"],
cleaned_input.get("order_lines", []),
cleaned_input.get("fulfillment_lines", []),
+ info.context.plugins,
cleaned_input["amount_to_refund"],
cleaned_input["include_shipping_costs"],
)
@@ -703,6 +704,7 @@ class FulfillmentReturnProducts(FulfillmentRefundAndReturnProductBase):
cleaned_input.get("payment"),
cleaned_input.get("order_lines", []),
cleaned_input.get("fulfillment_lines", []),
+ info.context.plugins,
cleaned_input["refund"],
cleaned_input.get("amount_to_refund"),
cleaned_input["include_shipping_costs"],
diff --git a/saleor/graphql/order/mutations/orders.py b/saleor/graphql/order/mutations/orders.py
index 037968bc01..7cd95f4448 100644
--- a/saleor/graphql/order/mutations/orders.py
+++ b/saleor/graphql/order/mutations/orders.py
@@ -489,7 +489,9 @@ class OrderRefund(BaseMutation):
# Confirm that we changed the status to refund. Some payment can receive
# asynchronous webhook with update status
if transaction.kind == TransactionKind.REFUND:
- order_refunded(order, info.context.user, amount, payment)
+ order_refunded(
+ order, info.context.user, amount, payment, info.context.plugins
+ )
return OrderRefund(order=order)
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 6a5dc5a23f..eab10e8901 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -19,7 +19,7 @@ from ..payment import (
)
from ..payment.models import Payment, Transaction
from ..payment.utils import create_payment
-from ..plugins.manager import get_plugins_manager
+from ..plugins.manager import PluginsManager, get_plugins_manager
from ..warehouse.management import (
deallocate_stock,
deallocate_stock_for_order,
@@ -44,8 +44,8 @@ from .events import (
draft_order_created_from_replace_event,
fulfillment_refunded_event,
fulfillment_replaced_event,
- fulfillment_returned_event,
order_replacement_created,
+ order_returned_event,
)
from .models import Fulfillment, FulfillmentLine, Order, OrderLine
from .utils import (
@@ -140,12 +140,18 @@ def cancel_order(order: "Order", user: Optional["User"]):
def order_refunded(
- order: "Order", user: Optional["User"], amount: "Decimal", payment: "Payment"
+ order: "Order",
+ user: Optional["User"],
+ amount: "Decimal",
+ payment: "Payment",
+ manager: Optional["PluginsManager"] = None,
):
events.payment_refunded_event(
order=order, user=user, amount=amount, payment=payment
)
- get_plugins_manager().order_updated(order)
+ if not manager:
+ manager = get_plugins_manager()
+ manager.order_updated(order)
send_order_refunded_confirmation(order, user, amount, payment.currency)
@@ -155,6 +161,17 @@ def order_voided(order: "Order", user: "User", payment: "Payment"):
get_plugins_manager().order_updated(order)
+def order_returned(
+ order: "Order",
+ user: Optional["User"],
+ returned_lines: List[Tuple[QuantityType, OrderLine]],
+ manager: "PluginsManager",
+):
+ order_returned_event(order=order, user=user, returned_lines=returned_lines)
+ update_order_status(order)
+ manager.order_updated(order)
+
+
def order_fulfilled(
fulfillments: List["Fulfillment"],
user: "User",
@@ -581,6 +598,7 @@ def create_refund_fulfillment(
payment,
order_lines_to_refund: List[OrderLineData],
fulfillment_lines_to_refund: List[FulfillmentLineData],
+ plugin_manager: "PluginsManager",
amount=None,
refund_shipping_costs=False,
):
@@ -603,6 +621,7 @@ def create_refund_fulfillment(
fulfillment_lines_to_refund=fulfillment_lines_to_refund,
amount=amount,
refund_shipping_costs=refund_shipping_costs,
+ manager=plugin_manager,
)
with transaction.atomic():
@@ -788,6 +807,7 @@ def create_return_fulfillment(
order: "Order",
order_lines: List[OrderLineData],
fulfillment_lines: List[FulfillmentLineData],
+ manager: "PluginsManager",
refund: bool = False,
) -> Fulfillment:
status = FulfillmentStatus.RETURNED
@@ -818,9 +838,11 @@ def create_return_fulfillment(
line_data.quantity,
order_line,
)
-
- fulfillment_returned_event(
- order=order, user=requester, returned_lines=list(returned_lines.values()),
+ order_returned(
+ order,
+ user=requester,
+ returned_lines=list(returned_lines.values()),
+ manager=manager,
)
return return_fulfillment
@@ -856,7 +878,6 @@ def process_replace(
order_replacement_created(
original_order=order, replace_order=new_order, user=requester,
)
- # TODO call order_returned
return replace_fulfillment, new_order
@@ -867,6 +888,7 @@ def create_fulfillments_for_returned_products(
payment: Optional[Payment],
order_lines: List[OrderLineData],
fulfillment_lines: List[FulfillmentLineData],
+ plugin_manager: PluginsManager,
refund: bool = False,
amount: Optional[Decimal] = None,
refund_shipping_costs=False,
@@ -903,6 +925,7 @@ def create_fulfillments_for_returned_products(
fulfillment_lines_to_refund=return_fulfillment_lines,
amount=amount,
refund_shipping_costs=refund_shipping_costs,
+ manager=plugin_manager,
)
with transaction.atomic():
@@ -922,6 +945,7 @@ def create_fulfillments_for_returned_products(
order=order,
order_lines=return_order_lines,
fulfillment_lines=return_fulfillment_lines,
+ manager=plugin_manager,
refund=refund,
)
Fulfillment.objects.filter(order=order, lines=None).delete()
@@ -969,6 +993,7 @@ def _process_refund(
fulfillment_lines_to_refund: List[FulfillmentLineData],
amount: Optional[Decimal],
refund_shipping_costs: bool,
+ manager: "PluginsManager",
):
lines_to_refund: Dict[OrderLineIDType, Tuple[QuantityType, OrderLine]] = dict()
refund_amount = _calculate_refund_amount(
@@ -983,7 +1008,7 @@ def _process_refund(
if amount:
amount = min(payment.captured_amount, amount)
gateway.refund(payment, amount)
- order_refunded(order, requester, amount, payment)
+ order_refunded(order, requester, amount, payment, manager=manager)
fulfillment_refunded_event(
order=order,
diff --git a/saleor/order/events.py b/saleor/order/events.py
index 0314de8360..232154f851 100644
--- a/saleor/order/events.py
+++ b/saleor/order/events.py
@@ -373,7 +373,7 @@ def fulfillment_fulfilled_items_event(
)
-def fulfillment_returned_event(
+def order_returned_event(
*, order: Order, user: UserType, returned_lines: List[Tuple[int, OrderLine]],
):
if not _user_is_valid(user):
diff --git a/saleor/order/models.py b/saleor/order/models.py
index f2eb23e112..8544a33d1d 100644
--- a/saleor/order/models.py
+++ b/saleor/order/models.py
@@ -270,10 +270,6 @@ class Order(ModelWithMetadata):
.exists()
)
- @property
- def quantity_fulfilled(self):
- return sum([line.quantity_fulfilled for line in self])
-
def is_shipping_required(self):
return any(line.is_shipping_required for line in self)
diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.py
index fbd43f7a09..1eabf6483b 100644
--- a/saleor/order/tests/test_order.py
+++ b/saleor/order/tests/test_order.py
@@ -18,7 +18,7 @@ from ...payment.models import Payment
from ...product.models import Collection
from ...warehouse.models import Stock
from ...warehouse.tests.utils import get_quantity_allocated_for_stock
-from .. import OrderEvents, OrderStatus, models
+from .. import FulfillmentStatus, OrderEvents, OrderStatus, models
from ..emails import send_fulfillment_confirmation_to_customer
from ..events import OrderEvent, OrderEventsEmails, email_sent_event
from ..models import Order
@@ -255,7 +255,7 @@ def test_restock_fulfillment_lines(fulfilled_order, warehouse):
)
-def test_update_order_status(fulfilled_order):
+def test_update_order_status_partially_fulfilled(fulfilled_order):
fulfillment = fulfilled_order.fulfillments.first()
line = fulfillment.lines.first()
order_line = line.order_line
@@ -267,15 +267,79 @@ def test_update_order_status(fulfilled_order):
assert fulfilled_order.status == OrderStatus.PARTIALLY_FULFILLED
- line = fulfillment.lines.first()
- order_line = line.order_line
- order_line.quantity_fulfilled -= line.quantity
- order_line.save()
- line.delete()
+def test_update_order_status_unfulfilled(order_with_lines):
+ order_with_lines.status = OrderStatus.FULFILLED
+ order_with_lines.save()
+
+ update_order_status(order_with_lines)
+
+ order_with_lines.refresh_from_db()
+ assert order_with_lines.status == OrderStatus.UNFULFILLED
+
+
+def test_update_order_status_fulfilled(fulfilled_order):
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment_line = fulfillment.lines.first()
+ fulfillment_line.quantity -= 3
+ fulfillment_line.save()
+ fulfilled_order.status = OrderStatus.UNFULFILLED
+ fulfilled_order.save()
+ replaced_fulfillment = fulfilled_order.fulfillments.create(
+ status=FulfillmentStatus.REPLACED
+ )
+ replaced_fulfillment.lines.create(
+ quantity=3, order_line=fulfillment_line.order_line
+ )
+
+ update_order_status(fulfilled_order)
+
+ fulfilled_order.refresh_from_db()
+ assert fulfilled_order.status == OrderStatus.FULFILLED
+
+
+def test_update_order_status_returned(fulfilled_order):
+ fulfilled_order.fulfillments.all().update(status=FulfillmentStatus.RETURNED)
+ fulfilled_order.status = OrderStatus.UNFULFILLED
+ fulfilled_order.save()
+
+ update_order_status(fulfilled_order)
+
+ fulfilled_order.refresh_from_db()
+ assert fulfilled_order.status == OrderStatus.RETURNED
+
+
+def test_update_order_status_partially_returned(fulfilled_order):
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment_line = fulfillment.lines.first()
+ fulfillment_line.quantity -= 3
+ fulfillment_line.save()
+ returned_fulfillment = fulfilled_order.fulfillments.create(
+ status=FulfillmentStatus.RETURNED
+ )
+ replaced_fulfillment = fulfilled_order.fulfillments.create(
+ status=FulfillmentStatus.REPLACED
+ )
+ refunded_and_returned_fulfillment = fulfilled_order.fulfillments.create(
+ status=FulfillmentStatus.REFUNDED_AND_RETURNED
+ )
+ returned_fulfillment.lines.create(
+ quantity=1, order_line=fulfillment_line.order_line
+ )
+ replaced_fulfillment.lines.create(
+ quantity=1, order_line=fulfillment_line.order_line
+ )
+ refunded_and_returned_fulfillment.lines.create(
+ quantity=1, order_line=fulfillment_line.order_line
+ )
+
+ fulfilled_order.status = OrderStatus.UNFULFILLED
+ fulfilled_order.save()
+
update_order_status(fulfilled_order)
- assert fulfilled_order.status == OrderStatus.UNFULFILLED
+ fulfilled_order.refresh_from_db()
+ assert fulfilled_order.status == OrderStatus.PARTIALLY_RETURNED
def test_validate_fulfillment_tracking_number_as_url(fulfilled_order):
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index e94125baca..5d34f4e5c3 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -6,6 +6,7 @@ from prices import Money, TaxedMoney
from ...payment import ChargeStatus, PaymentError, TransactionKind
from ...payment.models import Payment
+from ...plugins.manager import get_plugins_manager
from ...product.models import DigitalContent
from ...product.tests.utils import create_image
from ...warehouse.models import Allocation, Stock
@@ -235,7 +236,7 @@ def test_order_refunded(
amount = order.total.gross.amount
# when
- order_refunded(order, order.user, amount, payment)
+ order_refunded(order, order.user, amount, payment, get_plugins_manager())
# then
order_event = order.events.last()
diff --git a/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py b/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
index 8b80afc28e..b06fca48ab 100644
--- a/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
+++ b/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
@@ -3,6 +3,7 @@ from unittest.mock import patch
from prices import Money, TaxedMoney
+from ...plugins.manager import get_plugins_manager
from ...warehouse.models import Allocation, Stock
from .. import FulfillmentLineData, FulfillmentStatus, OrderEvents, OrderLineData
from ..actions import create_fulfillments_for_returned_products
@@ -35,6 +36,7 @@ def test_create_return_fulfillment_only_order_lines(
for line in order_lines_to_return
],
fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillment, replaced_fulfillment, replace_order = response
@@ -99,6 +101,7 @@ def test_create_return_fulfillment_only_order_lines_with_refund(
for line in order_lines_to_return
],
fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
refund=True,
)
returned_fulfillment, replaced_fulfillment, replace_order = response
@@ -153,6 +156,7 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
for line in order_lines_to_return
],
fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
refund=True,
refund_shipping_costs=True,
)
@@ -215,6 +219,7 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(
payment=payment,
order_lines=order_lines_data,
fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillment, replaced_fulfillment, replace_order = response
@@ -317,6 +322,7 @@ def test_create_return_fulfillment_multiple_order_line_returns(
OrderLineData(line=line, quantity=1) for line in order_lines_to_return
],
fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
refund=True,
)
@@ -353,6 +359,7 @@ def test_create_return_fulfillment_only_fulfillment_lines(
FulfillmentLineData(line=line, quantity=2, replace=False)
for line in fulfillment_lines
],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillment, replaced_fulfillment, replace_order = response
@@ -398,6 +405,7 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
payment=payment,
order_lines=[],
fulfillment_lines=fulfillment_lines_to_return,
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillment, replaced_fulfillment, replace_order = response
@@ -489,6 +497,7 @@ def test_create_return_fulfillment_multiple_fulfillment_lines_returns(
FulfillmentLineData(line=line, quantity=1)
for line in fulfillment_lines_to_return
],
+ plugin_manager=get_plugins_manager(),
refund=True,
)
@@ -555,6 +564,7 @@ def test_create_return_fulfillment_multiple_lines_returns(
FulfillmentLineData(line=line, quantity=1)
for line in fulfillment_lines_to_return
],
+ plugin_manager=get_plugins_manager(),
refund=True,
)
@@ -631,6 +641,7 @@ def test_create_return_fulfillment_multiple_lines_without_refund(
payment=payment,
order_lines=[OrderLineData(line=order_line, quantity=1)],
fulfillment_lines=fulfillment_lines_to_process,
+ plugin_manager=get_plugins_manager(),
refund=False,
)
@@ -714,6 +725,7 @@ def test_create_return_fulfillment_with_lines_already_refunded(
payment=payment,
order_lines=[],
fulfillment_lines=fulfillment_lines_to_process,
+ plugin_manager=get_plugins_manager(),
refund=True,
)
diff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.py
index 0f535ddb55..c02b1633c4 100644
--- a/saleor/order/tests/test_order_actions_refund_products.py
+++ b/saleor/order/tests/test_order_actions_refund_products.py
@@ -4,6 +4,7 @@ from unittest.mock import patch
from prices import Money, TaxedMoney
from ...payment import ChargeStatus
+from ...plugins.manager import get_plugins_manager
from ...warehouse.models import Allocation, Stock
from .. import FulfillmentLineData, FulfillmentStatus, OrderLineData
from ..actions import create_refund_fulfillment
@@ -38,6 +39,7 @@ def test_create_refund_fulfillment_only_order_lines(
OrderLineData(line=line, quantity=2) for line in order_lines_to_refund
],
fulfillment_lines_to_refund=[],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
@@ -88,6 +90,7 @@ def test_create_refund_fulfillment_multiple_order_line_refunds(
OrderLineData(line=line, quantity=1) for line in order_lines_to_refund
],
fulfillment_lines_to_refund=[],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillemnt = Fulfillment.objects.get(
@@ -129,6 +132,7 @@ def test_create_refund_fulfillment_included_shipping_costs(
OrderLineData(line=line, quantity=2) for line in order_lines_to_refund
],
fulfillment_lines_to_refund=[],
+ plugin_manager=get_plugins_manager(),
refund_shipping_costs=True,
)
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
@@ -167,6 +171,7 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
FulfillmentLineData(line=line, quantity=2)
for line in fulfillment_lines_to_refund
],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
@@ -207,6 +212,7 @@ def test_create_refund_fulfillment_multiple_fulfillment_lines_refunds(
FulfillmentLineData(line=line, quantity=1)
for line in fulfillment_lines_to_refund
],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillemnt = Fulfillment.objects.get(
@@ -249,6 +255,7 @@ def test_create_refund_fulfillment_custom_amount(
FulfillmentLineData(line=line, quantity=2)
for line in fulfillment_lines_to_refund
],
+ plugin_manager=get_plugins_manager(),
amount=amount,
)
@@ -313,6 +320,7 @@ def test_create_refund_fulfillment_multiple_refunds(
FulfillmentLineData(line=line, quantity=1)
for line in fulfillment_lines_to_refund
],
+ plugin_manager=get_plugins_manager(),
)
returned_fulfillemnt = Fulfillment.objects.get(
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 260195fdc3..8812ece6c3 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -15,7 +15,7 @@ from ..discount.utils import (
get_products_voucher_discount,
validate_voucher_in_order,
)
-from ..order import OrderStatus
+from ..order import FulfillmentStatus, OrderStatus
from ..order.models import Order, OrderLine
from ..plugins.manager import get_plugins_manager
from ..product.utils.digital_products import get_default_digital_content_settings
@@ -153,11 +153,37 @@ def update_order_prices(order, discounts):
def update_order_status(order):
"""Update order status depending on fulfillments."""
- quantity_fulfilled = order.quantity_fulfilled
- total_quantity = order.get_total_quantity()
-
- if quantity_fulfilled <= 0:
+ lines = list(order.lines.all())
+ total_quantity = sum([line.quantity for line in lines])
+ quantity_fulfilled = sum([line.quantity_fulfilled for line in lines])
+ quantity_returned = 0
+ quantity_replaced = 0
+ for fulfillment in order.fulfillments.all():
+ # count returned quantity for order
+ if fulfillment.status in [
+ FulfillmentStatus.RETURNED,
+ FulfillmentStatus.REFUNDED_AND_RETURNED,
+ ]:
+ quantity_returned += fulfillment.get_total_quantity()
+ # count replaced quantity for order
+ elif fulfillment.status == FulfillmentStatus.REPLACED:
+ quantity_replaced += fulfillment.get_total_quantity()
+
+ # Subtract the replace quantity as it shouldn't be taken into consideration for
+ # calculating the order status
+ total_quantity -= quantity_replaced
+ quantity_fulfilled -= quantity_replaced
+
+ # 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:
status = OrderStatus.UNFULFILLED
+ elif 0 < quantity_returned < total_quantity:
+ status = OrderStatus.PARTIALLY_RETURNED
+ elif quantity_returned == total_quantity:
+ status = OrderStatus.RETURNED
elif quantity_fulfilled < total_quantity:
status = OrderStatus.PARTIALLY_FULFILLED
else: