Skip to content

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

Change the way of creating refund/return fulfillments (#7320)

Maciej Korycinski1448e2a5d9fe
diff --git a/saleor/graphql/order/mutations/fulfillments.py b/saleor/graphql/order/mutations/fulfillments.py
index cbfb83327f..c7c892e311 100644
--- a/saleor/graphql/order/mutations/fulfillments.py
+++ b/saleor/graphql/order/mutations/fulfillments.py
@@ -343,7 +343,7 @@ class OrderRefundProductsInput(graphene.InputObjectType):
)
amount_to_refund = PositiveDecimal(
required=False,
- description=("The total amount of refund when the value is provided manually."),
+ description="The total amount of refund when the value is provided manually.",
)
include_shipping_costs = graphene.Boolean(
description=(
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 08469e11db..290e1e32e4 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -592,42 +592,32 @@ def _get_fulfillment_line(
@transaction.atomic()
def _move_order_lines_to_target_fulfillment(
order_lines_to_move: List[OrderLineData],
- lines_in_target_fulfillment: List[FulfillmentLine],
target_fulfillment: Fulfillment,
-):
+) -> List[FulfillmentLine]:
"""Move order lines with given quantity to the target fulfillment."""
fulfillment_lines_to_create: List[FulfillmentLine] = []
- fulfillment_lines_to_update: List[FulfillmentLine] = []
order_lines_to_update: List[OrderLine] = []
lines_to_dellocate: List[OrderLineData] = []
for line_data in order_lines_to_move:
line_to_move = line_data.line
quantity_to_move = line_data.quantity
- moved_line, fulfillment_line_existed = _get_fulfillment_line(
- target_fulfillment=target_fulfillment,
- lines_in_target_fulfillment=lines_in_target_fulfillment,
- order_line_id=line_to_move.id,
- stock_id=None,
- )
# calculate the quantity fulfilled/unfulfilled to move
unfulfilled_to_move = min(line_to_move.quantity_unfulfilled, quantity_to_move)
- quantity_to_move -= unfulfilled_to_move
line_to_move.quantity_fulfilled += unfulfilled_to_move
- moved_line.quantity += unfulfilled_to_move
+
+ fulfillment_line = FulfillmentLine(
+ fulfillment=target_fulfillment,
+ order_line_id=line_to_move.id,
+ stock_id=None,
+ quantity=unfulfilled_to_move,
+ )
# update current lines with new value of quantity
order_lines_to_update.append(line_to_move)
- if moved_line.quantity > 0 and not fulfillment_line_existed:
- # If this is new type of (order_line, stock) then we create new fulfillment
- # line
- fulfillment_lines_to_create.append(moved_line)
- elif fulfillment_line_existed:
- # if target fulfillment already have the same line, we just update the
- # quantity
- fulfillment_lines_to_update.append(moved_line)
+ fulfillment_lines_to_create.append(fulfillment_line)
line_allocations_exists = line_to_move.allocations.exists()
if line_allocations_exists:
@@ -643,10 +633,11 @@ def _move_order_lines_to_target_fulfillment(
f"Unable to deallocate stock for line {', '.join(e.order_lines)}."
)
- # update the fulfillment lines with new values
- FulfillmentLine.objects.bulk_update(fulfillment_lines_to_update, ["quantity"])
- FulfillmentLine.objects.bulk_create(fulfillment_lines_to_create)
+ created_fulfillment_lines = FulfillmentLine.objects.bulk_create(
+ fulfillment_lines_to_create
+ )
OrderLine.objects.bulk_update(order_lines_to_update, ["quantity_fulfilled"])
+ return created_fulfillment_lines
@transaction.atomic()
@@ -715,13 +706,10 @@ def create_refund_fulfillment(
):
"""Proceed with all steps required for refunding products.
- Calculate refunds for products based on the order's order lines and fulfillment
+ Calculate refunds for products based on the order's lines and fulfillment
lines. The logic takes the list of order lines, fulfillment lines, and their
quantities which is used to create the refund fulfillment. The stock for
- unfulfilled lines will be deallocated. It creates only single refund fulfillment
- for each order. Calling the method N-time will increase the quantity of the already
- refunded line. The refund fulfillment can have assigned lines with the same
- products but with the different stocks.
+ unfulfilled lines will be deallocated.
"""
_process_refund(
@@ -736,18 +724,17 @@ def create_refund_fulfillment(
)
with transaction.atomic():
- refunded_fulfillment, _ = Fulfillment.objects.get_or_create(
+ refunded_fulfillment = Fulfillment.objects.create(
status=FulfillmentStatus.REFUNDED, order=order
)
- already_refunded_lines = list(refunded_fulfillment.lines.all())
- _move_order_lines_to_target_fulfillment(
+ created_fulfillment_lines = _move_order_lines_to_target_fulfillment(
order_lines_to_move=order_lines_to_refund,
- lines_in_target_fulfillment=already_refunded_lines,
target_fulfillment=refunded_fulfillment,
)
+
_move_fulfillment_lines_to_target_fulfillment(
fulfillment_lines_to_move=fulfillment_lines_to_refund,
- lines_in_target_fulfillment=already_refunded_lines,
+ lines_in_target_fulfillment=created_fulfillment_lines,
target_fulfillment=refunded_fulfillment,
)
@@ -849,13 +836,11 @@ def _move_lines_to_return_fulfillment(
fulfillment_status: str,
order: "Order",
) -> Fulfillment:
- target_fulfillment, _ = Fulfillment.objects.get_or_create(
+ target_fulfillment = Fulfillment.objects.create(
status=fulfillment_status, order=order
)
- lines_in_target_fulfillment = list(target_fulfillment.lines.all())
- _move_order_lines_to_target_fulfillment(
+ lines_in_target_fulfillment = _move_order_lines_to_target_fulfillment(
order_lines_to_move=order_lines,
- lines_in_target_fulfillment=lines_in_target_fulfillment,
target_fulfillment=target_fulfillment,
)
@@ -882,13 +867,15 @@ def _move_lines_to_return_fulfillment(
)
if refunded_fulfillment_lines_to_return:
- refund_and_return_fulfillment, _ = Fulfillment.objects.get_or_create(
- status=FulfillmentStatus.REFUNDED_AND_RETURNED, order=order
- )
- lines_in_target_fulfillment = list(refund_and_return_fulfillment.lines.all())
+ if fulfillment_status == FulfillmentStatus.REFUNDED_AND_RETURNED:
+ refund_and_return_fulfillment = target_fulfillment
+ else:
+ refund_and_return_fulfillment = Fulfillment.objects.create(
+ status=FulfillmentStatus.REFUNDED_AND_RETURNED, order=order
+ )
_move_fulfillment_lines_to_target_fulfillment(
fulfillment_lines_to_move=refunded_fulfillment_lines_to_return,
- lines_in_target_fulfillment=lines_in_target_fulfillment,
+ lines_in_target_fulfillment=[],
target_fulfillment=refund_and_return_fulfillment,
)
@@ -900,13 +887,11 @@ def _move_lines_to_replace_fulfillment(
fulfillment_lines_to_replace: List[FulfillmentLineData],
order: "Order",
) -> Fulfillment:
- target_fulfillment, _ = Fulfillment.objects.get_or_create(
+ target_fulfillment = Fulfillment.objects.create(
status=FulfillmentStatus.REPLACED, order=order
)
- lines_in_target_fulfillment = list(target_fulfillment.lines.all())
- _move_order_lines_to_target_fulfillment(
+ lines_in_target_fulfillment = _move_order_lines_to_target_fulfillment(
order_lines_to_move=order_lines_to_replace,
- lines_in_target_fulfillment=lines_in_target_fulfillment,
target_fulfillment=target_fulfillment,
)
_move_fulfillment_lines_to_target_fulfillment(
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 c1419febf6..c07233eebb 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
@@ -301,47 +301,6 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(
assert replaced_line.tax_rate == expected_replaced_line.tax_rate
-@patch("saleor.order.actions.gateway.refund")
-def test_create_return_fulfillment_multiple_order_line_returns(
- mocked_refund, order_with_lines, payment_dummy_fully_charged, staff_user
-):
- order_with_lines.payments.add(payment_dummy_fully_charged)
- payment = order_with_lines.get_last_payment()
- order_lines_to_return = order_with_lines.lines.all()
- original_quantity = {
- line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()
- }
- order_line_ids = order_lines_to_return.values_list("id", flat=True)
- lines_count = order_lines_to_return.count()
-
- for _ in range(2):
- # call refund two times
- create_fulfillments_for_returned_products(
- requester=staff_user,
- order=order_with_lines,
- payment=payment,
- order_lines=[
- OrderLineData(line=line, quantity=1) for line in order_lines_to_return
- ],
- fulfillment_lines=[],
- manager=get_plugins_manager(),
- refund=True,
- )
-
- returned_fulfillemnt = Fulfillment.objects.get(
- order=order_with_lines, status=FulfillmentStatus.REFUNDED_AND_RETURNED
- )
- returned_fulfillment_lines = returned_fulfillemnt.lines.all()
- assert len(returned_fulfillment_lines) == lines_count
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
- for line in order_lines_to_return:
- assert line.quantity_unfulfilled == original_quantity.get(line.pk) - 2
-
- assert mocked_refund.call_count == ANY, 2
-
-
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_fulfillment_lines(
mocked_refund, fulfilled_order, payment_dummy_fully_charged, staff_user
@@ -478,210 +437,6 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
assert replaced_line.tax_rate == expected_replaced_line.tax_rate
-@patch("saleor.order.actions.gateway.refund")
-def test_create_return_fulfillment_multiple_fulfillment_lines_returns(
- mocked_refund, fulfilled_order, payment_dummy_fully_charged, staff_user
-):
- fulfilled_order.payments.add(payment_dummy_fully_charged)
- payment = fulfilled_order.get_last_payment()
- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)
- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)
- original_quantity = {line.id: line.quantity for line in fulfillment_lines}
- fulfillment_lines_to_return = fulfillment_lines
-
- for _ in range(2):
- create_fulfillments_for_returned_products(
- requester=staff_user,
- order=fulfilled_order,
- payment=payment,
- order_lines=[],
- fulfillment_lines=[
- FulfillmentLineData(line=line, quantity=1)
- for line in fulfillment_lines_to_return
- ],
- manager=get_plugins_manager(),
- refund=True,
- )
-
- returned_fulfillment = Fulfillment.objects.get(
- order=fulfilled_order, status=FulfillmentStatus.REFUNDED_AND_RETURNED
- )
- returned_fulfillment_lines = returned_fulfillment.lines.all()
-
- assert returned_fulfillment_lines.count() == len(order_line_ids)
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
-
- for line in fulfillment_lines:
- assert line.quantity == original_quantity.get(line.pk) - 2
-
- assert mocked_refund.call_count == ANY, 2
-
-
-@patch("saleor.order.actions.gateway.refund")
-def test_create_return_fulfillment_multiple_lines_returns(
- mocked_refund,
- fulfilled_order,
- payment_dummy_fully_charged,
- staff_user,
- channel_USD,
- variant,
- warehouse,
-):
- fulfilled_order.payments.add(payment_dummy_fully_charged)
- payment = fulfilled_order.get_last_payment()
- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)
- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)
- original_quantity = {line.id: line.quantity for line in fulfillment_lines}
- fulfillment_lines_to_return = fulfillment_lines
-
- stock = Stock.objects.create(
- warehouse=warehouse, product_variant=variant, quantity=5
- )
-
- channel_listing = variant.channel_listings.get()
- net = variant.get_price(variant.product, [], channel_USD, channel_listing)
-
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- unit_price = TaxedMoney(net=net, gross=gross)
- quantity = 5
- order_line = fulfilled_order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- quantity=quantity,
- quantity_fulfilled=2,
- variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
- tax_rate=Decimal("0.23"),
- total_price=unit_price * quantity,
- )
- Allocation.objects.create(
- order_line=order_line, stock=stock, quantity_allocated=order_line.quantity
- )
-
- for _ in range(2):
- create_fulfillments_for_returned_products(
- requester=staff_user,
- order=fulfilled_order,
- payment=payment,
- order_lines=[OrderLineData(line=order_line, quantity=1)],
- fulfillment_lines=[
- FulfillmentLineData(line=line, quantity=1)
- for line in fulfillment_lines_to_return
- ],
- manager=get_plugins_manager(),
- refund=True,
- )
-
- returned_fulfillment = Fulfillment.objects.get(
- order=fulfilled_order, status=FulfillmentStatus.REFUNDED_AND_RETURNED
- )
- returned_fulfillment_lines = returned_fulfillment.lines.all()
-
- assert returned_fulfillment_lines.count() == len(order_line_ids)
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
-
- for line in fulfillment_lines:
- assert line.quantity == original_quantity.get(line.pk) - 2
-
- assert mocked_refund.call_count == ANY, 2
-
-
-@patch("saleor.order.actions.gateway.refund")
-def test_create_return_fulfillment_multiple_lines_without_refund(
- mocked_refund,
- fulfilled_order,
- payment_dummy_fully_charged,
- staff_user,
- channel_USD,
- variant,
- warehouse,
-):
- fulfilled_order.payments.add(payment_dummy_fully_charged)
- payment = fulfilled_order.get_last_payment()
- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)
- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)
- original_quantity = {line.id: line.quantity for line in fulfillment_lines}
- fulfillment_lines_to_return = fulfillment_lines
-
- stock = Stock.objects.create(
- warehouse=warehouse, product_variant=variant, quantity=5
- )
-
- channel_listing = variant.channel_listings.get()
- net = variant.get_price(variant.product, [], channel_USD, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- unit_price = TaxedMoney(net=net, gross=gross)
- quantity = 5
- order_line = fulfilled_order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- quantity=quantity,
- quantity_fulfilled=2,
- variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
- tax_rate=Decimal("0.23"),
- total_price=unit_price * quantity,
- )
- Allocation.objects.create(
- order_line=order_line, stock=stock, quantity_allocated=order_line.quantity
- )
- refunded_fulfillment = Fulfillment.objects.create(
- order=fulfilled_order, status=FulfillmentStatus.REFUNDED
- )
- refunded_fulfillment_line = refunded_fulfillment.lines.create(
- order_line=order_line, quantity=2
- )
-
- fulfillment_lines_to_process = [
- FulfillmentLineData(line=line, quantity=1)
- for line in fulfillment_lines_to_return
- ]
- fulfillment_lines_to_process.append(
- FulfillmentLineData(line=refunded_fulfillment_line, quantity=1)
- )
- for _ in range(2):
- create_fulfillments_for_returned_products(
- requester=staff_user,
- order=fulfilled_order,
- payment=payment,
- order_lines=[OrderLineData(line=order_line, quantity=1)],
- fulfillment_lines=fulfillment_lines_to_process,
- manager=get_plugins_manager(),
- refund=False,
- )
-
- returned_fulfillment = Fulfillment.objects.get(
- order=fulfilled_order, status=FulfillmentStatus.RETURNED
- )
- returned_fulfillment_lines = returned_fulfillment.lines.all()
-
- returned_and_refunded_fulfillment = Fulfillment.objects.get(
- order=fulfilled_order, status=FulfillmentStatus.REFUNDED_AND_RETURNED
- )
- returned_and_refunded_lines = returned_and_refunded_fulfillment.lines.all()
-
- assert returned_fulfillment_lines.count() == len(order_line_ids)
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
-
- for line in fulfillment_lines:
- assert line.quantity == original_quantity.get(line.pk) - 2
-
- assert returned_and_refunded_lines.count() == 1
- assert returned_and_refunded_lines[0].order_line_id == order_line.id
-
- assert not mocked_refund.called
-
-
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_with_lines_already_refunded(
mocked_refund,
diff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.py
index 168cd429eb..a6eb47aa5a 100644
--- a/saleor/order/tests/test_order_actions_refund_products.py
+++ b/saleor/order/tests/test_order_actions_refund_products.py
@@ -1,14 +1,12 @@
from decimal import Decimal
from unittest.mock import ANY, patch
-from prices import Money, TaxedMoney
-
from ...payment import ChargeStatus
from ...plugins.manager import get_plugins_manager
-from ...warehouse.models import Allocation, Stock
+from ...warehouse.models import Allocation
from .. import FulfillmentLineData, FulfillmentStatus, OrderLineData
from ..actions import create_refund_fulfillment
-from ..models import Fulfillment, FulfillmentLine
+from ..models import FulfillmentLine
@patch("saleor.order.actions.gateway.refund")
@@ -64,50 +62,6 @@ def test_create_refund_fulfillment_only_order_lines(
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
-@patch("saleor.order.actions.gateway.refund")
-def test_create_refund_fulfillment_multiple_order_line_refunds(
- mocked_refund, order_with_lines, payment_dummy
-):
- payment_dummy.captured_amount = payment_dummy.total
- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
- payment_dummy.save()
- order_with_lines.payments.add(payment_dummy)
- payment = order_with_lines.get_last_payment()
- order_lines_to_refund = order_with_lines.lines.all()
- original_quantity = {
- line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()
- }
- order_line_ids = order_lines_to_refund.values_list("id", flat=True)
- lines_count = order_lines_to_refund.count()
-
- for _ in range(2):
- # call refund two times
- create_refund_fulfillment(
- requester=None,
- order=order_with_lines,
- payment=payment,
- order_lines_to_refund=[
- OrderLineData(line=line, quantity=1) for line in order_lines_to_refund
- ],
- fulfillment_lines_to_refund=[],
- manager=get_plugins_manager(),
- )
-
- returned_fulfillemnt = Fulfillment.objects.get(
- order=order_with_lines, status=FulfillmentStatus.REFUNDED
- )
- returned_fulfillment_lines = returned_fulfillemnt.lines.all()
- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
- assert len(returned_fulfillment_lines) == lines_count
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
- for line in order_lines_to_refund:
- assert line.quantity_unfulfilled == original_quantity.get(line.pk) - 2
-
- assert mocked_refund.call_count == ANY, 2
-
-
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_included_shipping_costs(
mocked_refund, order_with_lines, payment_dummy
@@ -188,49 +142,6 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
-@patch("saleor.order.actions.gateway.refund")
-def test_create_refund_fulfillment_multiple_fulfillment_lines_refunds(
- mocked_refund, fulfilled_order, payment_dummy
-):
- payment_dummy.captured_amount = payment_dummy.total
- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
- payment_dummy.save()
- fulfilled_order.payments.add(payment_dummy)
- payment = fulfilled_order.get_last_payment()
- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)
- fulfillment_lines = FulfillmentLine.objects.filter(order_line_id__in=order_line_ids)
- original_quantity = {line.id: line.quantity for line in fulfillment_lines}
- fulfillment_lines_to_refund = fulfillment_lines
-
- for _ in range(2):
- create_refund_fulfillment(
- requester=None,
- order=fulfilled_order,
- payment=payment,
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=[
- FulfillmentLineData(line=line, quantity=1)
- for line in fulfillment_lines_to_refund
- ],
- manager=get_plugins_manager(),
- )
-
- returned_fulfillemnt = Fulfillment.objects.get(
- order=fulfilled_order, status=FulfillmentStatus.REFUNDED
- )
- returned_fulfillment_lines = returned_fulfillemnt.lines.all()
- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
- assert len(returned_fulfillment_lines) == len(order_line_ids)
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
-
- for line in fulfillment_lines:
- assert line.quantity == original_quantity.get(line.pk) - 2
-
- assert mocked_refund.call_count == ANY, 2
-
-
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_custom_amount(
mocked_refund, fulfilled_order, payment_dummy
@@ -269,78 +180,3 @@ def test_create_refund_fulfillment_custom_amount(
for line in fulfillment_lines:
assert line.quantity == original_quantity.get(line.pk) - 2
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
-
-
-@patch("saleor.order.actions.gateway.refund")
-def test_create_refund_fulfillment_multiple_refunds(
- mocked_refund, fulfilled_order, variant, payment_dummy, warehouse, channel_USD
-):
- payment_dummy.captured_amount = payment_dummy.total
- payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
- payment_dummy.save()
- fulfilled_order.payments.add(payment_dummy)
- payment = fulfilled_order.get_last_payment()
-
- stock = Stock.objects.create(
- warehouse=warehouse, product_variant=variant, quantity=5
- )
- channel_listing = variant.channel_listings.get()
- net = variant.get_price(variant.product, [], channel_USD, channel_listing)
- gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency)
- quantity = 5
- unit_price = TaxedMoney(net=net, gross=gross)
- order_line = fulfilled_order.lines.create(
- product_name=str(variant.product),
- variant_name=str(variant),
- product_sku=variant.sku,
- is_shipping_required=variant.is_shipping_required(),
- quantity=quantity,
- quantity_fulfilled=2,
- variant=variant,
- unit_price=TaxedMoney(net=net, gross=gross),
- tax_rate=Decimal("0.23"),
- total_price=unit_price * quantity,
- )
- Allocation.objects.create(
- order_line=order_line, stock=stock, quantity_allocated=order_line.quantity
- )
- fulfillment = fulfilled_order.fulfillments.get()
- fulfillment.lines.create(order_line=order_line, quantity=2, stock=stock)
-
- order_line_ids = fulfilled_order.lines.all().values_list("id", flat=True)
- fulfillment_lines = fulfillment.lines.all()
- original_fulfillment_quantity = {
- line.id: line.quantity for line in fulfillment_lines
- }
- fulfillment_lines_to_refund = fulfillment_lines
-
- for _ in range(2):
- create_refund_fulfillment(
- requester=None,
- order=fulfilled_order,
- payment=payment,
- order_lines_to_refund=[OrderLineData(line=order_line, quantity=1)],
- fulfillment_lines_to_refund=[
- FulfillmentLineData(line=line, quantity=1)
- for line in fulfillment_lines_to_refund
- ],
- manager=get_plugins_manager(),
- )
-
- returned_fulfillemnt = Fulfillment.objects.get(
- order=fulfilled_order, status=FulfillmentStatus.REFUNDED
- )
- returned_fulfillment_lines = returned_fulfillemnt.lines.all()
- assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
- assert len(returned_fulfillment_lines) == len(order_line_ids) + 1 # + order line
- for fulfillment_line in returned_fulfillment_lines:
- assert fulfillment_line.quantity == 2
- assert fulfillment_line.order_line_id in order_line_ids
-
- for line in fulfillment_lines:
- assert line.quantity == original_fulfillment_quantity.get(line.pk) - 2
-
- order_line.refresh_from_db()
- assert order_line.quantity_fulfilled == 4
- assert order_line.quantity_unfulfilled == 1
- assert mocked_refund.call_count == ANY, 2