Skip to content

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

Merge branch 'master' into feature/possibility-to-return-products

Maciej Korycinski47499ff294e3
diff --cc saleor/graphql/order/tests/test_fulfillment_refund_products.py
index c540adca06,0000000000..4c439627ad
mode 100644,000000..100644
--- a/saleor/graphql/order/tests/test_fulfillment_refund_products.py
+++ b/saleor/graphql/order/tests/test_fulfillment_refund_products.py
@@@ -1,548 -1,0 +1,549 @@@
+from decimal import Decimal
+from unittest.mock import patch
+
+import graphene
+from prices import Money, TaxedMoney
+
+from ....core.prices import quantize_price
+from ....order.error_codes import OrderErrorCode
+from ....order.models import FulfillmentStatus
+from ....payment import ChargeStatus
+from ....warehouse.models import Stock
+from ...tests.utils import get_graphql_content
+
+ORDER_FULFILL_REFUND_MUTATION = """
+mutation OrderFulfillmentRefundProducts(
+ $order: ID!, $input: OrderRefundProductsInput!
+) {
+ orderFulfillmentRefundProducts(
+ order: $order,
+ input: $input
+ ) {
+ fulfillment{
+ id
+ status
+ lines{
+ id
+ quantity
+ orderLine{
+ id
+ }
+ }
+ }
+ orderErrors {
+ field
+ code
+ message
+ warehouse
+ orderLine
+ }
+ }
+}
+"""
+
+
+def test_fulfillment_refund_products_order_without_payment(
+ staff_api_client, permission_manage_orders, fulfilled_order
+):
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ variables = {"order": order_id, "input": {"amountToRefund": "11.00"}}
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "order"
+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name
+ assert fulfillment is None
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_amount_and_shipping_costs(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ 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)
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ amount_to_refund = Decimal("11.00")
+ variables = {
+ "order": order_id,
+ "input": {"amountToRefund": amount_to_refund, "includeShippingCosts": True},
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+
+ mocked_refund.assert_called_with(
+ payment_dummy, quantize_price(amount_to_refund, fulfilled_order.currency)
+ )
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_order_lines(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ payment_dummy,
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_refund = order_with_lines.lines.first()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_refund.pk)
+ variables = {
+ "order": order_id,
+ "input": {"orderLines": [{"orderLineId": line_id, "quantity": 2}]},
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 1
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] == line_id
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+ mocked_refund.assert_called_with(
+ payment_dummy, line_to_refund.unit_price_gross_amount * 2
+ )
+
+
+def test_fulfillment_refund_products_order_lines_quantity_bigger_than_total(
+ staff_api_client, permission_manage_orders, order_with_lines, payment_dummy
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_refund = order_with_lines.lines.first()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_refund.pk)
+ variables = {
+ "order": order_id,
+ "input": {"orderLines": [{"orderLineId": line_id, "quantity": 200}]},
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+
+ assert len(errors) == 1
+ assert errors[0]["field"] == "orderLineId"
+ assert errors[0]["code"] == OrderErrorCode.INVALID_QUANTITY.name
+ assert refund_fulfillment is None
+
+
+def test_fulfillment_refund_products_order_lines_quantity_bigger_than_unfulfilled(
+ staff_api_client, permission_manage_orders, order_with_lines, payment_dummy
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_refund = order_with_lines.lines.first()
+ line_to_refund.quantity = 3
+ line_to_refund.quantity_fulfilled = 3
+ line_to_refund.save()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_refund.pk)
+ variables = {
+ "order": order_id,
+ "input": {"orderLines": [{"orderLineId": line_id, "quantity": 1}]},
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+
+ assert len(errors) == 1
+ assert errors[0]["field"] == "orderLineId"
+ assert errors[0]["code"] == OrderErrorCode.INVALID_QUANTITY.name
+ assert refund_fulfillment is None
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_fulfillment_lines(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_refund = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_refund.pk
+ )
+ order_line_id = graphene.Node.to_global_id(
+ "OrderLine", fulfillment_line_to_refund.order_line.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ]
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 1
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] == order_line_id
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+ mocked_refund.assert_called_with(
+ payment_dummy, fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2
+ )
+
+
+def test_fulfillment_refund_products_fulfillment_lines_quantity_bigger_than_total(
+ staff_api_client, permission_manage_orders, fulfilled_order, payment_dummy
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_refund = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_refund.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 200}
+ ]
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "fulfillmentLineId"
+ assert errors[0]["code"] == OrderErrorCode.INVALID_QUANTITY.name
+ assert refund_fulfillment is None
+
+
+def test_fulfillment_refund_products_amount_bigger_than_captured_amount(
+ staff_api_client, permission_manage_orders, fulfilled_order, payment_dummy
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_refund = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_refund.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "amountToRefund": "1000.00",
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "amountToRefund"
+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name
+ assert refund_fulfillment is None
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_fulfillment_lines_include_shipping_costs(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_refund = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_refund.pk
+ )
+ order_line_id = graphene.Node.to_global_id(
+ "OrderLine", fulfillment_line_to_refund.order_line.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "includeShippingCosts": True,
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 1
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] == order_line_id
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+ amount = fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2
+ amount += fulfilled_order.shipping_price_gross_amount
+ mocked_refund.assert_called_with(payment_dummy, amount)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_order_lines_include_shipping_costs(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ payment_dummy,
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_refund = order_with_lines.lines.first()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_refund.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "includeShippingCosts": True,
+ "orderLines": [{"orderLineId": line_id, "quantity": 2}],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 1
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] == line_id
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+ amount = line_to_refund.unit_price_gross_amount * 2
+ amount += order_with_lines.shipping_price_gross_amount
+ mocked_refund.assert_called_with(payment_dummy, amount)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_fulfillment_lines_custom_amount(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ amount_to_refund = Decimal("10.99")
+ fulfillment_line_to_refund = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_refund.pk
+ )
+ order_line_id = graphene.Node.to_global_id(
+ "OrderLine", fulfillment_line_to_refund.order_line.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "amountToRefund": amount_to_refund,
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 1
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] == order_line_id
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+ mocked_refund.assert_called_with(payment_dummy, amount_to_refund)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_order_lines_custom_amount(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ payment_dummy,
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ amount_to_refund = Decimal("10.99")
+ line_to_refund = order_with_lines.lines.first()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_refund.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "amountToRefund": amount_to_refund,
+ "orderLines": [{"orderLineId": line_id, "quantity": 2}],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 1
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] == line_id
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+ mocked_refund.assert_called_with(payment_dummy, amount_to_refund)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_refund_products_fulfillment_lines_and_order_lines(
+ mocked_refund,
+ warehouse,
+ variant,
+ channel_USD,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ stock = Stock.objects.create(
+ warehouse=warehouse, product_variant=variant, quantity=5
+ )
- net = variant.get_price(channel_USD.slug)
++ 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)
+ variant.track_inventory = False
+ variant.save()
+ 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=5,
+ quantity_fulfilled=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+ fulfillment = fulfilled_order.fulfillments.get()
+ fulfillment.lines.create(order_line=order_line, quantity=2, stock=stock)
+ fulfillment_line_to_refund = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_refund.pk
+ )
+ order_line_from_fulfillment_line = graphene.Node.to_global_id(
+ "OrderLine", fulfillment_line_to_refund.order_line.pk
+ )
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "orderLines": [{"orderLineId": order_line_id, "quantity": 2}],
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ response = staff_api_client.post_graphql(ORDER_FULFILL_REFUND_MUTATION, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentRefundProducts"]
+ refund_fulfillment = data["fulfillment"]
+ errors = data["orderErrors"]
+
+ assert not errors
+ assert refund_fulfillment["status"] == FulfillmentStatus.REFUNDED.upper()
+ assert len(refund_fulfillment["lines"]) == 2
+ assert refund_fulfillment["lines"][0]["orderLine"]["id"] in [
+ order_line_id,
+ order_line_from_fulfillment_line,
+ ]
+ assert refund_fulfillment["lines"][0]["quantity"] == 2
+
+ assert refund_fulfillment["lines"][1]["orderLine"]["id"] in [
+ order_line_id,
+ order_line_from_fulfillment_line,
+ ]
+ assert refund_fulfillment["lines"][1]["quantity"] == 2
+ amount = fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2
+ amount += order_line.unit_price_gross_amount * 2
+ amount = quantize_price(amount, fulfilled_order.currency)
+ mocked_refund.assert_called_with(payment_dummy, amount)
diff --cc saleor/graphql/order/tests/test_fulfillment_return_products.py
index 0676cdc024,0000000000..dfe3a66acc
mode 100644,000000..100644
--- a/saleor/graphql/order/tests/test_fulfillment_return_products.py
+++ b/saleor/graphql/order/tests/test_fulfillment_return_products.py
@@@ -1,694 -1,0 +1,695 @@@
+from decimal import Decimal
+from unittest.mock import patch
+
+import graphene
+from prices import Money, TaxedMoney
+
+from ....core.prices import quantize_price
+from ....order import OrderStatus
+from ....order.error_codes import OrderErrorCode
+from ....order.models import FulfillmentStatus, Order
+from ....payment import ChargeStatus
+from ....warehouse.models import Stock
+from ...tests.utils import get_graphql_content
+
+ORDER_FULFILL_RETURN_MUTATION = """
+mutation OrderFulfillmentReturnProducts(
+ $order: ID!, $input: OrderReturnProductsInput!
+) {
+ orderFulfillmentReturnProducts(
+ order: $order,
+ input: $input
+ ) {
+ returnFulfillment{
+ id
+ status
+ lines{
+ id
+ quantity
+ orderLine{
+ id
+ }
+ }
+ }
+ replaceFulfillment{
+ id
+ status
+ lines{
+ id
+ quantity
+ orderLine{
+ id
+ }
+ }
+ }
+ order{
+ id
+ status
+ }
+ replaceOrder{
+ id
+ status
+ }
+ orderErrors {
+ field
+ code
+ message
+ warehouse
+ orderLine
+ }
+ }
+}
+"""
+
+
+def test_fulfillment_return_products_order_without_payment(
+ staff_api_client, permission_manage_orders, fulfilled_order
+):
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ variables = {
+ "order": order_id,
+ "input": {"refund": True, "amountToRefund": "11.00"},
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ fulfillment = data["returnFulfillment"]
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "order"
+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name
+ assert fulfillment is None
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_return_products_amount_and_shipping_costs(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ 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)
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ amount_to_refund = Decimal("11.00")
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "amountToRefund": amount_to_refund,
+ "includeShippingCosts": True,
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+
+ mocked_refund.assert_called_with(
+ payment_dummy, quantize_price(amount_to_refund, fulfilled_order.currency)
+ )
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_return_products_order_lines(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ payment_dummy,
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_return = order_with_lines.lines.first()
+ line_unfulfilled_quantity = line_to_return.quantity_unfulfilled
+ line_quantity_to_return = 2
+
+ line_to_replace = order_with_lines.lines.last()
+ line_quantity_to_replace = 1
+
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_return.pk)
+ replace_line_id = graphene.Node.to_global_id("OrderLine", line_to_replace.pk)
+
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "includeShippingCosts": True,
+ "orderLines": [
+ {
+ "orderLineId": line_id,
+ "quantity": line_quantity_to_return,
+ "replace": False,
+ },
+ {
+ "orderLineId": replace_line_id,
+ "quantity": line_quantity_to_replace,
+ "replace": True,
+ },
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+
+ order_with_lines.refresh_from_db()
+ order_return_fulfillment = order_with_lines.fulfillments.get(
+ status=FulfillmentStatus.REFUNDED_AND_RETURNED
+ )
+ return_fulfillment_line = order_return_fulfillment.lines.filter(
+ order_line_id=line_to_return.pk
+ ).first()
+ assert return_fulfillment_line
+ assert return_fulfillment_line.quantity == line_quantity_to_return
+
+ order_replace_fulfillment = order_with_lines.fulfillments.get(
+ status=FulfillmentStatus.REPLACED
+ )
+ replace_fulfillment_line = order_replace_fulfillment.lines.filter(
+ order_line_id=line_to_replace.pk
+ ).first()
+ assert replace_fulfillment_line
+ assert replace_fulfillment_line.quantity == line_quantity_to_replace
+
+ line_to_return.refresh_from_db()
+ assert (
+ line_to_return.quantity_unfulfilled
+ == line_unfulfilled_quantity - line_quantity_to_return
+ )
+
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+ replace_fulfillment = data["replaceFulfillment"]
+ replace_order = data["replaceOrder"]
+ errors = data["orderErrors"]
+
+ assert not errors
+ assert (
+ return_fulfillment["status"] == FulfillmentStatus.REFUNDED_AND_RETURNED.upper()
+ )
+ assert len(return_fulfillment["lines"]) == 1
+ assert return_fulfillment["lines"][0]["orderLine"]["id"] == line_id
+ assert return_fulfillment["lines"][0]["quantity"] == line_quantity_to_return
+
+ assert replace_fulfillment["status"] == FulfillmentStatus.REPLACED.upper()
+ assert len(replace_fulfillment["lines"]) == 1
+ assert replace_fulfillment["lines"][0]["orderLine"]["id"] == replace_line_id
+ assert replace_fulfillment["lines"][0]["quantity"] == line_quantity_to_replace
+
+ assert replace_order["status"] == OrderStatus.DRAFT.upper()
+ replace_order = Order.objects.get(status=OrderStatus.DRAFT)
+ assert replace_order.lines.count() == 1
+ replaced_line = replace_order.lines.get()
+ assert replaced_line.variant_id == line_to_replace.variant_id
+ assert (
+ replaced_line.unit_price_gross_amount == line_to_replace.unit_price_gross_amount
+ )
+ assert replaced_line.quantity == line_quantity_to_replace
+
+ amount = line_to_return.unit_price_gross_amount * line_quantity_to_return
+ amount += order_with_lines.shipping_price_gross_amount
+ mocked_refund.assert_called_with(payment_dummy, amount)
+
+
+def test_fulfillment_return_products_order_lines_quantity_bigger_than_total(
+ staff_api_client, permission_manage_orders, order_with_lines, payment_dummy
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_refund = order_with_lines.lines.first()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_refund.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "orderLines": [{"orderLineId": line_id, "quantity": 200, "replace": False}]
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+ errors = data["orderErrors"]
+
+ assert len(errors) == 1
+ assert errors[0]["field"] == "orderLineId"
+ assert errors[0]["code"] == OrderErrorCode.INVALID_QUANTITY.name
+ assert return_fulfillment is None
+
+
- def test_fulfillment_refund_products_order_lines_quantity_bigger_than_unfulfilled(
++def test_fulfillment_return_products_order_lines_quantity_bigger_than_unfulfilled(
+ staff_api_client, permission_manage_orders, order_with_lines, payment_dummy
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ line_to_return = order_with_lines.lines.first()
+ line_to_return.quantity = 3
+ line_to_return.quantity_fulfilled = 3
+ line_to_return.save()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_return.pk)
+ variables = {
+ "order": order_id,
+ "input": {"orderLines": [{"orderLineId": line_id, "quantity": 1}]},
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+ errors = data["orderErrors"]
+
+ assert len(errors) == 1
+ assert errors[0]["field"] == "orderLineId"
+ assert errors[0]["code"] == OrderErrorCode.INVALID_QUANTITY.name
+ assert return_fulfillment is None
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_return_products_order_lines_custom_amount(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ order_with_lines,
+ payment_dummy,
+):
+ payment_dummy.total = order_with_lines.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ order_with_lines.payments.add(payment_dummy)
+ amount_to_refund = Decimal("10.99")
+ line_to_return = order_with_lines.lines.first()
+ order_id = graphene.Node.to_global_id("Order", order_with_lines.pk)
+ line_id = graphene.Node.to_global_id("OrderLine", line_to_return.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "amountToRefund": amount_to_refund,
+ "orderLines": [{"orderLineId": line_id, "quantity": 2, "replace": False}],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+ errors = data["orderErrors"]
+ assert not errors
+ assert (
+ return_fulfillment["status"] == FulfillmentStatus.REFUNDED_AND_RETURNED.upper()
+ )
+ assert len(return_fulfillment["lines"]) == 1
+ assert return_fulfillment["lines"][0]["orderLine"]["id"] == line_id
+ assert return_fulfillment["lines"][0]["quantity"] == 2
+ mocked_refund.assert_called_with(payment_dummy, amount_to_refund)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_return_products_fulfillment_lines(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ order_fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment_line_to_return = order_fulfillment.lines.first()
+ original_fulfillment_line_quantity = fulfillment_line_to_return.quantity
+ quantity_to_return = 2
+
+ fulfillment_line_to_replace = order_fulfillment.lines.last()
+ quantity_to_replace = 1
+
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_return.pk
+ )
+ fulfillment_line_to_replace_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_replace.pk
+ )
+ order_line_id = graphene.Node.to_global_id(
+ "OrderLine", fulfillment_line_to_return.order_line.pk
+ )
+
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "includeShippingCosts": True,
+ "fulfillmentLines": [
+ {
+ "fulfillmentLineId": fulfillment_line_id,
+ "quantity": quantity_to_return,
+ "replace": False,
+ },
+ {
+ "fulfillmentLineId": fulfillment_line_to_replace_id,
+ "quantity": quantity_to_replace,
+ "replace": True,
+ },
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+
+ fulfilled_order.refresh_from_db()
+ order_return_fulfillment = fulfilled_order.fulfillments.get(
+ status=FulfillmentStatus.REFUNDED_AND_RETURNED
+ )
+ return_fulfillment_line = order_return_fulfillment.lines.filter(
+ order_line_id=fulfillment_line_to_return.order_line_id
+ ).first()
+ assert return_fulfillment_line
+ assert return_fulfillment_line.quantity == quantity_to_return
+
+ order_replace_fulfillment = fulfilled_order.fulfillments.get(
+ status=FulfillmentStatus.REPLACED
+ )
+ replace_fulfillment_line = order_replace_fulfillment.lines.filter(
+ order_line_id=fulfillment_line_to_replace.order_line_id
+ ).first()
+ assert replace_fulfillment_line
+ assert replace_fulfillment_line.quantity == quantity_to_replace
+
+ fulfillment_line_to_return.refresh_from_db()
+ assert (
+ fulfillment_line_to_return.quantity
+ == original_fulfillment_line_quantity - quantity_to_return
+ )
+
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ errors = data["orderErrors"]
+ return_fulfillment = data["returnFulfillment"]
+ replace_fulfillment = data["replaceFulfillment"]
+ replace_order = data["replaceOrder"]
+
+ assert not errors
+ assert (
+ return_fulfillment["status"] == FulfillmentStatus.REFUNDED_AND_RETURNED.upper()
+ )
+ assert len(return_fulfillment["lines"]) == 1
+ assert return_fulfillment["lines"][0]["orderLine"]["id"] == order_line_id
+ assert return_fulfillment["lines"][0]["quantity"] == 2
+
+ assert replace_fulfillment["status"] == FulfillmentStatus.REPLACED.upper()
+ assert len(replace_fulfillment["lines"]) == 1
+ assert replace_fulfillment["lines"][0]["quantity"] == quantity_to_replace
+
+ assert replace_order["status"] == OrderStatus.DRAFT.upper()
+ replace_order = Order.objects.get(status=OrderStatus.DRAFT)
+ assert replace_order.lines.count() == 1
+ replaced_line = replace_order.lines.get()
+ assert replaced_line.variant_id == fulfillment_line_to_replace.order_line.variant_id
+ assert (
+ replaced_line.unit_price_gross_amount
+ == fulfillment_line_to_replace.order_line.unit_price_gross_amount
+ )
+ assert replaced_line.quantity == quantity_to_replace
+
+ amount = (
+ fulfillment_line_to_return.order_line.unit_price_gross_amount
+ * quantity_to_return
+ )
+ amount += fulfilled_order.shipping_price_gross_amount
+ mocked_refund.assert_called_with(payment_dummy, amount)
+
+
+def test_fulfillment_return_products_fulfillment_lines_quantity_bigger_than_total(
+ staff_api_client, permission_manage_orders, fulfilled_order, payment_dummy
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_return = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_return.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 200}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "fulfillmentLineId"
+ assert errors[0]["code"] == OrderErrorCode.INVALID_QUANTITY.name
+ assert return_fulfillment is None
+
+
+def test_fulfillment_return_products_amount_bigger_than_captured_amount(
+ staff_api_client, permission_manage_orders, fulfilled_order, payment_dummy
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_return = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_return.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "amountToRefund": "1000.00",
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "amountToRefund"
+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name
+ assert return_fulfillment is None
+
+
+def test_fulfillment_return_products_lines_with_incorrect_status(
+ staff_api_client, permission_manage_orders, fulfilled_order, payment_dummy
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment = fulfilled_order.fulfillments.first()
+ fulfillment.status = FulfillmentStatus.RETURNED
+ fulfillment.save()
+ fulfillment_line_to_return = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_return.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "amountToRefund": "1000.00",
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+
+ errors = data["orderErrors"]
+ assert len(errors) == 1
+ assert errors[0]["field"] == "amountToRefund"
+ assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name
+ assert return_fulfillment is None
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_return_products_fulfillment_lines_include_shipping_costs(
+ mocked_refund,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ fulfillment_line_to_return = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_return.pk
+ )
+ order_line_id = graphene.Node.to_global_id(
+ "OrderLine", fulfillment_line_to_return.order_line.pk
+ )
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "includeShippingCosts": True,
+ "fulfillmentLines": [
+ {"fulfillmentLineId": fulfillment_line_id, "quantity": 2}
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ return_fulfillment = data["returnFulfillment"]
+ errors = data["orderErrors"]
+
+ assert not errors
+ assert (
+ return_fulfillment["status"] == FulfillmentStatus.REFUNDED_AND_RETURNED.upper()
+ )
+ assert len(return_fulfillment["lines"]) == 1
+ assert return_fulfillment["lines"][0]["orderLine"]["id"] == order_line_id
+ assert return_fulfillment["lines"][0]["quantity"] == 2
+ amount = fulfillment_line_to_return.order_line.unit_price_gross_amount * 2
+ amount += fulfilled_order.shipping_price_gross_amount
+ mocked_refund.assert_called_with(payment_dummy, amount)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_fulfillment_return_products_fulfillment_lines_and_order_lines(
+ mocked_refund,
+ warehouse,
+ variant,
+ channel_USD,
+ staff_api_client,
+ permission_manage_orders,
+ fulfilled_order,
+ payment_dummy,
+):
+ payment_dummy.total = fulfilled_order.total_gross_amount
+ payment_dummy.captured_amount = payment_dummy.total
+ payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
+ payment_dummy.save()
+ fulfilled_order.payments.add(payment_dummy)
+ stock = Stock.objects.create(
+ warehouse=warehouse, product_variant=variant, quantity=5
+ )
- net = variant.get_price(channel_USD.slug)
++ 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)
+ variant.track_inventory = False
+ variant.save()
+ 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=5,
+ quantity_fulfilled=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+ fulfillment = fulfilled_order.fulfillments.get()
+ fulfillment.lines.create(order_line=order_line, quantity=2, stock=stock)
+ fulfillment_line_to_replace = fulfilled_order.fulfillments.first().lines.first()
+ order_id = graphene.Node.to_global_id("Order", fulfilled_order.pk)
+ fulfillment_line_id = graphene.Node.to_global_id(
+ "FulfillmentLine", fulfillment_line_to_replace.pk
+ )
+ order_line_id = graphene.Node.to_global_id("OrderLine", order_line.pk)
+ variables = {
+ "order": order_id,
+ "input": {
+ "refund": True,
+ "orderLines": [
+ {"orderLineId": order_line_id, "quantity": 2, "replace": False}
+ ],
+ "fulfillmentLines": [
+ {
+ "fulfillmentLineId": fulfillment_line_id,
+ "quantity": 1,
+ "replace": True,
+ }
+ ],
+ },
+ }
+ staff_api_client.user.user_permissions.add(permission_manage_orders)
+
+ response = staff_api_client.post_graphql(ORDER_FULFILL_RETURN_MUTATION, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["orderFulfillmentReturnProducts"]
+ errors = data["orderErrors"]
+ return_fulfillment = data["returnFulfillment"]
+ replace_fulfillment = data["replaceFulfillment"]
+ replace_order = data["replaceOrder"]
+
+ assert not errors
+ assert (
+ return_fulfillment["status"] == FulfillmentStatus.REFUNDED_AND_RETURNED.upper()
+ )
+ assert len(return_fulfillment["lines"]) == 1
+ assert return_fulfillment["lines"][0]["orderLine"]["id"] == order_line_id
+ assert return_fulfillment["lines"][0]["quantity"] == 2
+
+ assert replace_fulfillment["status"] == FulfillmentStatus.REPLACED.upper()
+ assert len(replace_fulfillment["lines"]) == 1
+ assert replace_fulfillment["lines"][0]["quantity"] == 1
+
+ assert replace_order["status"] == OrderStatus.DRAFT.upper()
+ replace_order = Order.objects.get(status=OrderStatus.DRAFT)
+ assert replace_order.lines.count() == 1
+ replaced_line = replace_order.lines.get()
+ assert replaced_line.variant_id == fulfillment_line_to_replace.order_line.variant_id
+ assert (
+ replaced_line.unit_price_gross_amount
+ == fulfillment_line_to_replace.order_line.unit_price_gross_amount
+ )
+ assert replaced_line.quantity == 1
+
+ amount = order_line.unit_price_gross_amount * 2
+ amount = quantize_price(amount, fulfilled_order.currency)
+ mocked_refund.assert_called_with(payment_dummy, amount)
diff --cc saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py
index b06fca48ab,0000000000..f133728dd5
mode 100644,000000..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
@@@ -1,752 -1,0 +1,759 @@@
+from decimal import Decimal
+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
+from ..models import Fulfillment, FulfillmentLine
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_return_fulfillment_only_order_lines(
+ 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
+ }
+ order_line_ids = order_lines_to_return.values_list("id", flat=True)
+ original_allocations = list(
+ Allocation.objects.filter(order_line_id__in=order_line_ids)
+ )
+ lines_count = order_with_lines.lines.count()
+
+ response = create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=[
+ OrderLineData(line=line, quantity=2, replace=False)
+ for line in order_lines_to_return
+ ],
+ fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
+ )
+ returned_fulfillment, replaced_fulfillment, replace_order = response
+
+ returned_fulfillment_lines = returned_fulfillment.lines.all()
+ assert returned_fulfillment.status == FulfillmentStatus.RETURNED
+ 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
+
+ current_allocations = Allocation.objects.in_bulk(
+ [allocation.pk for allocation in original_allocations]
+ )
+ for original_allocation in original_allocations:
+ current_allocation = current_allocations.get(original_allocation.pk)
+ assert (
+ original_allocation.quantity_allocated - 2
+ == current_allocation.quantity_allocated
+ )
+ assert not mocked_refund.called
+ assert not replace_order
+
+ # check if we have correct events
+ events = order_with_lines.events.all()
+ assert events.count() == 1
+ returned_event = events[0]
+ assert returned_event.type == OrderEvents.FULFILLMENT_RETURNED
+ assert len(returned_event.parameters["lines"]) == 2
+ event_lines = returned_event.parameters["lines"]
+ assert order_lines_to_return.filter(id=event_lines[0]["line_pk"]).exists()
+ assert event_lines[0]["quantity"] == 2
+
+ assert order_lines_to_return.filter(id=event_lines[1]["line_pk"]).exists()
+ assert event_lines[1]["quantity"] == 2
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_return_fulfillment_only_order_lines_with_refund(
+ 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
+ }
+ order_line_ids = order_lines_to_return.values_list("id", flat=True)
+ original_allocations = list(
+ Allocation.objects.filter(order_line_id__in=order_line_ids)
+ )
+ lines_count = order_with_lines.lines.count()
+
+ response = create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=[
+ OrderLineData(line=line, quantity=2, replace=False)
+ for line in order_lines_to_return
+ ],
+ fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
+ refund=True,
+ )
+ returned_fulfillment, replaced_fulfillment, replace_order = response
+
+ returned_fulfillment_lines = returned_fulfillment.lines.all()
+ assert returned_fulfillment.status == FulfillmentStatus.REFUNDED_AND_RETURNED
+ 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
+
+ current_allocations = Allocation.objects.in_bulk(
+ [allocation.pk for allocation in original_allocations]
+ )
+ for original_allocation in original_allocations:
+ current_allocation = current_allocations.get(original_allocation.pk)
+ assert (
+ original_allocation.quantity_allocated - 2
+ == current_allocation.quantity_allocated
+ )
+
+ amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_return])
+ mocked_refund.assert_called_once_with(payment_dummy_fully_charged, amount)
+ assert not replace_order
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
+ 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
+ }
+ order_line_ids = order_lines_to_return.values_list("id", flat=True)
+ original_allocations = list(
+ Allocation.objects.filter(order_line_id__in=order_line_ids)
+ )
+ lines_count = order_with_lines.lines.count()
+
+ response = create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=[
+ OrderLineData(line=line, quantity=2, replace=False)
+ for line in order_lines_to_return
+ ],
+ fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
+ refund=True,
+ refund_shipping_costs=True,
+ )
+ returned_fulfillment, replaced_fulfillment, replace_order = response
+
+ returned_fulfillment_lines = returned_fulfillment.lines.all()
+ assert returned_fulfillment.status == FulfillmentStatus.REFUNDED_AND_RETURNED
+ 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
+
+ current_allocations = Allocation.objects.in_bulk(
+ [allocation.pk for allocation in original_allocations]
+ )
+ for original_allocation in original_allocations:
+ current_allocation = current_allocations.get(original_allocation.pk)
+ assert (
+ original_allocation.quantity_allocated - 2
+ == current_allocation.quantity_allocated
+ )
+
+ amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_return])
+ amount += order_with_lines.shipping_price_gross_amount
+ mocked_refund.assert_called_once_with(payment_dummy_fully_charged, amount)
+ assert not replace_order
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_return_fulfillment_only_order_lines_with_replace_request(
+ 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
+ }
+ order_line_ids = order_lines_to_return.values_list("id", flat=True)
+ original_allocations = list(
+ Allocation.objects.filter(order_line_id__in=order_line_ids)
+ )
+ lines_count = order_with_lines.lines.count()
+ quantity_to_replace = 2
+ order_lines_data = [
+ OrderLineData(line=line, quantity=2, replace=False)
+ for line in order_lines_to_return
+ ]
+
+ # set replace request for the first line
+ order_lines_data[0].replace = True
+ order_lines_data[0].quantity = quantity_to_replace
+
+ response = create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=order_with_lines,
+ payment=payment,
+ order_lines=order_lines_data,
+ fulfillment_lines=[],
+ plugin_manager=get_plugins_manager(),
+ )
+ returned_fulfillment, replaced_fulfillment, replace_order = response
+
+ returned_fulfillment_lines = returned_fulfillment.lines.all()
+ assert returned_fulfillment.status == FulfillmentStatus.RETURNED
+ # we replaced one line
+ assert len(returned_fulfillment_lines) == lines_count - 1
+
+ for fulfillment_line in returned_fulfillment_lines:
+ assert fulfillment_line.quantity == 2
+ assert fulfillment_line.order_line_id in order_line_ids
+
+ order_lines_to_return = order_with_lines.lines.all()
+ for line in order_lines_to_return:
+ assert line.quantity_unfulfilled == original_quantity.get(line.pk) - 2
+
+ replaced_fulfillment_lines = replaced_fulfillment.lines.all()
+ assert replaced_fulfillment_lines.count() == 1
+ assert replaced_fulfillment_lines[0].quantity == quantity_to_replace
+ assert replaced_fulfillment_lines[0].order_line_id == order_lines_data[0].line.id
+
+ current_allocations = Allocation.objects.in_bulk(
+ [allocation.pk for allocation in original_allocations]
+ )
+ for original_allocation in original_allocations:
+ current_allocation = current_allocations.get(original_allocation.pk)
+ assert (
+ original_allocation.quantity_allocated - 2
+ == current_allocation.quantity_allocated
+ )
+
+ order_with_lines.refresh_from_db()
+
+ # refund should not be called
+ assert not mocked_refund.called
+
+ # new order should have own id
+ assert replace_order.id != order_with_lines.id
+
+ # make sure that we have new instances of addresses
+ assert replace_order.shipping_address.id != order_with_lines.shipping_address.id
+ assert replace_order.billing_address.id != order_with_lines.billing_address.id
+
+ # the rest of address data should be the same
+ replace_order.shipping_address.id = None
+ order_with_lines.shipping_address.id = None
+ assert replace_order.shipping_address == order_with_lines.shipping_address
+
+ replace_order.billing_address.id = None
+ order_with_lines.billing_address.id = None
+ assert replace_order.billing_address == order_with_lines.billing_address
+
+ expected_replaced_line = order_lines_to_return[0]
+
+ assert replace_order.lines.count() == 1
+ replaced_line = replace_order.lines.first()
+ # make sure that all data from original line is in replaced line
+ assert replaced_line.variant_id == expected_replaced_line.variant_id
+ assert replaced_line.product_name == expected_replaced_line.product_name
+ assert replaced_line.variant_name == expected_replaced_line.variant_name
+ assert replaced_line.product_sku == expected_replaced_line.product_sku
+ assert (
+ replaced_line.is_shipping_required
+ == expected_replaced_line.is_shipping_required
+ )
+ assert replaced_line.quantity == quantity_to_replace
+ assert replaced_line.quantity_fulfilled == 0
+ assert replaced_line.currency == expected_replaced_line.currency
+ assert (
+ replaced_line.unit_price_net_amount
+ == expected_replaced_line.unit_price_net_amount
+ )
+ assert (
+ replaced_line.unit_price_gross_amount
+ == expected_replaced_line.unit_price_gross_amount
+ )
+ 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
+ }
+ 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=[],
+ plugin_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 == 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
+):
+ 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}
+
+ response = create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines=[],
+ 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
+
+ returned_fulfillment_lines = returned_fulfillment.lines.all()
+ assert returned_fulfillment.status == FulfillmentStatus.RETURNED
+ 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 not mocked_refund.called
+ assert not replace_order
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
+ 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}
+
+ # Prepare the structure for return method
+ fulfillment_lines_to_return = [
+ FulfillmentLineData(line=line, quantity=2, replace=False)
+ for line in fulfillment_lines
+ ]
+ # The line which should be replaced
+ replace_quantity = 2
+ fulfillment_lines_to_return[0].replace = True
+ fulfillment_lines_to_return[0].quantity = replace_quantity
+
+ response = create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines=[],
+ fulfillment_lines=fulfillment_lines_to_return,
+ plugin_manager=get_plugins_manager(),
+ )
+
+ returned_fulfillment, replaced_fulfillment, replace_order = response
+
+ returned_fulfillment_lines = returned_fulfillment.lines.all()
+ assert returned_fulfillment.status == FulfillmentStatus.RETURNED
+ # make sure that all order lines from refund are in expected fulfillment
+ # minus one as we replaced the one item
+ assert returned_fulfillment_lines.count() == len(order_line_ids) - 1
+
+ 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
+
+ replaced_fulfillment_lines = replaced_fulfillment.lines.all()
+ assert replaced_fulfillment_lines.count() == 1
+ assert replaced_fulfillment_lines[0].quantity == replace_quantity
+ assert (
+ replaced_fulfillment_lines[0].order_line_id
+ == fulfillment_lines_to_return[0].line.order_line_id
+ )
+
+ assert not mocked_refund.called
+
+ # new order should have own id
+ assert replace_order.id != fulfilled_order.id
+
+ # make sure that we have new instances of addresses
+ assert replace_order.shipping_address.id != fulfilled_order.shipping_address.id
+ assert replace_order.billing_address.id != fulfilled_order.billing_address.id
+
+ # the rest of address data should be the same
+ replace_order.shipping_address.id = None
+ fulfilled_order.shipping_address.id = None
+ assert replace_order.shipping_address == fulfilled_order.shipping_address
+
+ replace_order.billing_address.id = None
+ fulfilled_order.billing_address.id = None
+ assert replace_order.billing_address == fulfilled_order.billing_address
+
+ expected_replaced_line = fulfillment_lines[0].order_line
+
+ assert replace_order.lines.count() == 1
+ replaced_line = replace_order.lines.first()
+ # make sure that all data from original line is in replaced line
+ assert replaced_line.variant_id == expected_replaced_line.variant_id
+ assert replaced_line.product_name == expected_replaced_line.product_name
+ assert replaced_line.variant_name == expected_replaced_line.variant_name
+ assert replaced_line.product_sku == expected_replaced_line.product_sku
+ assert (
+ replaced_line.is_shipping_required
+ == expected_replaced_line.is_shipping_required
+ )
+ assert replaced_line.quantity == replace_quantity
+ assert replaced_line.quantity_fulfilled == 0
+ assert replaced_line.currency == expected_replaced_line.currency
+ assert (
+ replaced_line.unit_price_net_amount
+ == expected_replaced_line.unit_price_net_amount
+ )
+ assert (
+ replaced_line.unit_price_gross_amount
+ == expected_replaced_line.unit_price_gross_amount
+ )
+ 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
+ ],
+ plugin_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 == 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
+ )
- net = variant.get_price(channel_USD.slug)
++
++ 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)
+ 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=5,
+ quantity_fulfilled=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+ 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
+ ],
+ plugin_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 == 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
+ )
- net = variant.get_price(channel_USD.slug)
++
++ 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)
+ 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=5,
+ quantity_fulfilled=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+ 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,
+ plugin_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,
+ 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
+ )
- net = variant.get_price(channel_USD.slug)
++
++ 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)
+ 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=5,
+ quantity_fulfilled=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+ 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=2)
+ for line in fulfillment_lines_to_return
+ ]
+ fulfillment_lines_to_process.append(
+ FulfillmentLineData(line=refunded_fulfillment_line, quantity=2)
+ )
+ create_fulfillments_for_returned_products(
+ requester=staff_user,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines=[],
+ fulfillment_lines=fulfillment_lines_to_process,
+ plugin_manager=get_plugins_manager(),
+ refund=True,
+ )
+
+ 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_and_refunded_lines.count() == len(order_line_ids)
+ for fulfillment_line in returned_and_refunded_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
+
+ # the already refunded line is not included in amount
+ amount = sum(
+ [
+ line.order_line.unit_price_gross_amount * 2
+ for line in fulfillment_lines_to_return
+ ]
+ )
+ mocked_refund.assert_called_once_with(payment_dummy_fully_charged, amount)
diff --cc saleor/order/tests/test_order_actions_refund_products.py
index c02b1633c4,0000000000..63aee29690
mode 100644,000000..100644
--- a/saleor/order/tests/test_order_actions_refund_products.py
+++ b/saleor/order/tests/test_order_actions_refund_products.py
@@@ -1,342 -1,0 +1,343 @@@
+from decimal import Decimal
+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
+from ..models import Fulfillment, FulfillmentLine
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_refund_fulfillment_only_order_lines(
+ 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
+ }
+ order_line_ids = order_lines_to_refund.values_list("id", flat=True)
+ original_allocations = list(
+ Allocation.objects.filter(order_line_id__in=order_line_ids)
+ )
+ lines_count = order_with_lines.lines.count()
+
+ returned_fulfillemnt = create_refund_fulfillment(
+ requester=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines_to_refund=[
+ 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()
+ 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
+
+ current_allocations = Allocation.objects.in_bulk(
+ [allocation.pk for allocation in original_allocations]
+ )
+ for original_allocation in original_allocations:
+ current_allocation = current_allocations.get(original_allocation.pk)
+ assert (
+ original_allocation.quantity_allocated - 2
+ == current_allocation.quantity_allocated
+ )
+ amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_refund])
+ mocked_refund.assert_called_once_with(payment_dummy, 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
+ }
+ 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=[],
+ plugin_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 == 2
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_refund_fulfillment_included_shipping_costs(
+ 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
+ }
+ order_line_ids = order_lines_to_refund.values_list("id", flat=True)
+ lines_count = order_with_lines.lines.count()
+
+ returned_fulfillemnt = create_refund_fulfillment(
+ requester=None,
+ order=order_with_lines,
+ payment=payment,
+ order_lines_to_refund=[
+ 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()
+ 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
+ amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_refund])
+ amount += order_with_lines.shipping_price_gross_amount
+ mocked_refund.assert_called_once_with(payment_dummy, amount)
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_refund_fulfillment_only_fulfillment_lines(
+ 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
+
+ returned_fulfillemnt = create_refund_fulfillment(
+ requester=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines_to_refund=[],
+ fulfillment_lines_to_refund=[
+ 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
+ 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
+ amount = sum(
+ [line.order_line.unit_price_gross_amount * 2 for line in fulfillment_lines]
+ )
+ mocked_refund.assert_called_once_with(payment_dummy, 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
+ ],
+ plugin_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 == 2
+
+
+@patch("saleor.order.actions.gateway.refund")
+def test_create_refund_fulfillment_custom_amount(
+ 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
+ amount = Decimal("10.00")
+
+ returned_fulfillemnt = create_refund_fulfillment(
+ requester=None,
+ order=fulfilled_order,
+ payment=payment,
+ order_lines_to_refund=[],
+ fulfillment_lines_to_refund=[
+ FulfillmentLineData(line=line, quantity=2)
+ for line in fulfillment_lines_to_refund
+ ],
+ plugin_manager=get_plugins_manager(),
+ amount=amount,
+ )
+
+ 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
+ mocked_refund.assert_called_once_with(payment_dummy, 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
+ )
- net = variant.get_price(channel_USD.slug)
++ 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)
+ 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=5,
+ quantity_fulfilled=2,
+ variant=variant,
+ unit_price=TaxedMoney(net=net, gross=gross),
+ tax_rate=23,
+ )
+ 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
+ ],
+ plugin_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 == 2
diff --cc saleor/order/utils.py
index 8812ece6c3,31456862f2..c79b20f126
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@@ -10,12 -10,8 +10,8 @@@ from ..account.models import Use
from ..core.taxes import zero_money
from ..core.weight import zero_weight
from ..discount.models import NotApplicable, Voucher, VoucherType
- from ..discount.utils import (
- get_discounted_lines,
- get_products_voucher_discount,
- validate_voucher_in_order,
- )
+ 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