Skip to content

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

WIP: fix create_refunded_lines product refunds with manual amount & billed amount

Mateusz Grzyb484ce8815cf4
diff --git a/saleor/payment/gateways/np_atobarai/__init__.py b/saleor/payment/gateways/np_atobarai/__init__.py
index 466481010b..e25d2aea25 100644
--- a/saleor/payment/gateways/np_atobarai/__init__.py
+++ b/saleor/payment/gateways/np_atobarai/__init__.py
@@ -118,7 +118,7 @@ def refund(payment_information: PaymentData, config: ApiConfig) -> GatewayRespon
# in case of refunding less than already captured
# and not all lines are being refunded,
# the transaction needs to be re-registered in NP Atobarai
- if payment_information.amount < payment.captured_amount and goods:
+ if payment_information.amount < payment.captured_amount:
order = payment.order
if not order:
diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.py
index 988bdfba59..18cf5b9910 100644
--- a/saleor/payment/gateways/np_atobarai/api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py
@@ -25,7 +25,6 @@ from .errors import (
SHIPPING_COMPANY_CODE_INVALID,
)
from .utils import (
- calculate_manual_refund_amount,
create_refunded_lines,
notify_dashboard,
np_atobarai_opentracing_trace,
@@ -189,11 +188,8 @@ def get_goods_with_refunds(
raise PaymentError("Cannot refund payment without order.")
refunded_lines = create_refunded_lines(order, refund_data)
- refunded_manual_amount = calculate_manual_refund_amount(
- order, payment_information, refund_data
- )
- billed_amount = Decimal("0.00")
+ subtotal = Decimal("0.00")
for line in payment_information.lines_data.lines:
quantity = line.quantity
refunded_quantity = refunded_lines.get(line.variant_id)
@@ -210,25 +206,28 @@ def get_goods_with_refunds(
"quantity": quantity,
}
)
- billed_amount += line.amount * quantity
+ subtotal += line.amount * quantity
goods_lines.extend(_get_voucher_and_shipping_goods(config, payment_information))
- billed_amount += payment_information.lines_data.shipping_amount
- billed_amount += payment_information.lines_data.voucher_amount
+ subtotal += payment_information.lines_data.shipping_amount
+ subtotal += payment_information.lines_data.voucher_amount
+
+ total = payment.captured_amount - payment_information.amount
+
+ refunded_manual_amount = total - subtotal
if refunded_manual_amount:
goods_lines.append(
{
"goods_name": "Discount",
"goods_price": format_price(
- -refunded_manual_amount, payment_information.currency
+ refunded_manual_amount, payment_information.currency
),
"quantity": 1,
}
)
- billed_amount -= refunded_manual_amount
- return goods_lines, billed_amount
+ return goods_lines, total
def get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dict]:
diff --git a/saleor/payment/gateways/np_atobarai/plugin.py b/saleor/payment/gateways/np_atobarai/plugin.py
index 62bd5a05cb..f898cecc02 100644
--- a/saleor/payment/gateways/np_atobarai/plugin.py
+++ b/saleor/payment/gateways/np_atobarai/plugin.py
@@ -36,7 +36,7 @@ class NPAtobaraiGatewayPlugin(BasePlugin):
PLUGIN_ID = NP_PLUGIN_ID
PLUGIN_NAME = GATEWAY_NAME
CONFIGURATION_PER_CHANNEL = True
- SUPPORTED_CURRENCIES = "JPY"
+ SUPPORTED_CURRENCIES = "PLN"
DEFAULT_CONFIGURATION = [
{"name": MERCHANT_CODE, "value": None},
diff --git a/saleor/payment/gateways/np_atobarai/tests/conftest.py b/saleor/payment/gateways/np_atobarai/tests/conftest.py
index aba02930c0..6919a2cb7e 100644
--- a/saleor/payment/gateways/np_atobarai/tests/conftest.py
+++ b/saleor/payment/gateways/np_atobarai/tests/conftest.py
@@ -1,8 +1,10 @@
import dataclasses
from decimal import Decimal
+from unittest.mock import patch
import pytest
+from .....order.actions import create_refund_fulfillment
from .....plugins.manager import get_plugins_manager
from .....plugins.models import PluginConfiguration
from ....interface import AddressData, PaymentLineData, PaymentLinesData
@@ -11,6 +13,7 @@ from ..const import (
FILL_MISSING_ADDRESS,
MERCHANT_CODE,
SHIPPING_COMPANY,
+ SKU_AS_NAME,
SP_CODE,
TERMINAL_ID,
USE_SANDBOX,
@@ -27,6 +30,7 @@ def np_atobarai_plugin(settings, monkeypatch, channel_USD):
use_sandbox=True,
fill_missing_address=True,
shipping_company="50000",
+ sku_as_name=False,
active=True,
):
settings.PLUGINS = [
@@ -40,6 +44,7 @@ def np_atobarai_plugin(settings, monkeypatch, channel_USD):
{"name": USE_SANDBOX, "value": use_sandbox},
{"name": FILL_MISSING_ADDRESS, "value": fill_missing_address},
{"name": SHIPPING_COMPANY, "value": shipping_company},
+ {"name": SKU_AS_NAME, "value": sku_as_name},
]
PluginConfiguration.objects.create(
identifier=NPAtobaraiGatewayPlugin.PLUGIN_ID,
@@ -56,20 +61,24 @@ def np_atobarai_plugin(settings, monkeypatch, channel_USD):
return fun
-def _resolve_lines():
+def payment_lines_data(order_with_lines):
return PaymentLinesData(
lines=[
PaymentLineData(
- amount=Decimal("100.00"),
- product_name=f"Product Name {i}",
- product_sku=f"PRODUCT_SKU_{i}",
- variant_id=i,
- quantity=5,
+ amount=line.unit_price_gross_amount,
+ product_name=f"{line.product_name} {line.variant_name}",
+ product_sku=line.product_sku,
+ variant_id=line.variant_id,
+ quantity=line.quantity,
)
- for i in range(3)
+ for line in order_with_lines.lines.all()
],
- shipping_amount=Decimal("120.00"),
- voucher_amount=Decimal("-10.00"),
+ shipping_amount=order_with_lines.shipping_price_gross_amount,
+ voucher_amount=(
+ order_with_lines.total_gross_amount
+ - order_with_lines.undiscounted_total_gross_amount
+ ),
)
@@ -91,15 +100,51 @@ def np_address_data():
@pytest.fixture
-def np_payment_data(np_address_data, dummy_payment_data):
+def np_payment_data(np_address_data, dummy_payment_data, payment_lines_data):
return dataclasses.replace(
dummy_payment_data,
billing=np_address_data,
shipping=np_address_data,
- _resolve_lines_data=_resolve_lines,
+ amount=Decimal("0.00"),
+ _resolve_lines_data=lambda: payment_lines_data,
)
@pytest.fixture
def config(np_atobarai_plugin):
return get_api_config(np_atobarai_plugin().config.connection_params)
+
+
+def payment_dummy(payment_dummy, order_with_lines):
+ payment_dummy.captured_amount = order_with_lines.total_gross_amount
+ payment_dummy.save(update_fields=["captured_amount"])
+ return payment_dummy
+
+
+def create_refund(payment_dummy):
+ def factory(
+ order,
+ order_lines=None,
+ fulfillment_lines=None,
+ manual_refund_amount=None,
+ refund_shipping_costs=False,
+ ):
+ # payment_dummy.captured_amount = order.total_gross_amount
+ # payment_dummy.save(update_fields=["captured_amount"])
+
+ with patch("saleor.order.actions.gateway.refund"):
+ return create_refund_fulfillment(
+ user=None,
+ app=None,
+ order=order,
+ payment=payment_dummy,
+ order_lines_to_refund=order_lines or [],
+ fulfillment_lines_to_refund=fulfillment_lines or [],
+ manager=get_plugins_manager(),
+ amount=manual_refund_amount,
+ refund_shipping_costs=refund_shipping_costs,
+ )
+
+ return factory
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api.py b/saleor/payment/gateways/np_atobarai/tests/test_api.py
index ee8525b57f..f6f140145f 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_api.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_api.py
@@ -26,6 +26,7 @@ def test_refund_payment(
# given
plugin = np_atobarai_plugin()
payment_data = np_payment_data
+ payment_data.amount = payment_dummy.captured_amount
psp_reference = "18121200001"
payment_data.psp_reference = psp_reference
response = Mock(
@@ -63,6 +64,7 @@ def test_refund_payment_payment_not_created(
# given
plugin = np_atobarai_plugin()
payment_data = np_payment_data
+ payment_data.amount = payment_dummy.captured_amount
response = Mock(
spec=requests.Response,
status_code=200,
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py b/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
index feb776f1a1..06a6f136a1 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
@@ -5,6 +5,8 @@ from unittest.mock import DEFAULT, Mock, patch, sentinel
import pytest
from posuto import Posuto
+from saleor.order.fetch import OrderLineInfo
+
from ....interface import AddressData, RefundData
from ....utils import price_to_minor_unit
from .. import api_helpers, errors
@@ -172,15 +174,6 @@ def test_get_goods(
}
for line in np_payment_data.lines_data.lines
] + [
- {
- "goods_name": "Voucher",
- "goods_price": int(
- price_to_minor_unit(
- np_payment_data.lines_data.voucher_amount, np_payment_data.currency
- )
- ),
- "quantity": 1,
- },
{
"goods_name": "Shipping",
"goods_price": int(
@@ -196,7 +189,7 @@ def test_get_goods(
@pytest.mark.parametrize(
"refund_amount, discount_goods",
[
- (None, []),
+ (Decimal("0.00"), []),
(
Decimal("5.00"),
[{"goods_name": "Discount", "goods_price": -500, "quantity": 1}],
@@ -238,16 +231,6 @@ def test_get_goods_with_refunds(
for line in np_payment_data.lines_data.lines
]
+ [
- {
- "goods_name": "Voucher",
- "goods_price": int(
- price_to_minor_unit(
- np_payment_data.lines_data.voucher_amount,
- np_payment_data.currency,
- )
- ),
- "quantity": 1,
- },
{
"goods_name": "Shipping",
"goods_price": int(
@@ -269,3 +252,45 @@ def test_get_goods_with_refunds(
+ np_payment_data.lines_data.shipping_amount
- manual_refund_amount
)
+
+
+def test_get_goods_with_refunds_previous_manual_product_refund(
+ create_refund, order_with_lines, config, np_payment_data, payment_dummy
+):
+ # given
+ lines = list(order_with_lines.lines.all())
+ line_to_refund = lines[0]
+ payment_dummy.captured_amount = order_with_lines.total_gross_amount
+
+ create_refund(
+ order_with_lines,
+ order_lines=[
+ OrderLineInfo(
+ line=line_to_refund, quantity=1, variant=line_to_refund.variant
+ )
+ ],
+ manual_refund_amount=Decimal("3.00"),
+ )
+ payment_dummy.captured_amount -= Decimal("3.00")
+
+ # when
+ np_payment_data.refund_data = RefundData(
+ order_lines_to_refund=[
+ OrderLineInfo(
+ line=line_to_refund, quantity=1, variant=line_to_refund.variant
+ ),
+ ]
+ )
+ payment_dummy.captured_amount -= line_to_refund.unit_price_gross_amount
+ goods, billed_amount = get_goods_with_refunds(
+ config, payment_dummy, np_payment_data
+ )
+
+ # then
+ expected_billed_amount = order_with_lines.total_gross_amount - (
+ Decimal("3.00") + line_to_refund.unit_price_gross_amount
+ )
+ assert billed_amount == expected_billed_amount
+ assert goods[0]["quantity"] == line_to_refund.quantity - 1
+ for goods_line, order_line in zip(goods[1:], lines[1:]):
+ assert goods_line["quantity"] == order_line.quantity
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_utils.py b/saleor/payment/gateways/np_atobarai/tests/test_utils.py
index eec4736263..97bf6f0f07 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_utils.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_utils.py
@@ -1,18 +1,15 @@
-from decimal import Decimal
from itertools import chain, zip_longest
from unittest.mock import Mock, patch
import pytest
from .....order import FulfillmentLineData, FulfillmentStatus, OrderEvents
-from .....order.actions import create_refund_fulfillment
from .....order.fetch import OrderLineInfo
-from .....plugins.manager import get_plugins_manager
from .... import PaymentError
from ....interface import RefundData
from .. import get_fulfillment_for_order, get_shipping_company_code, notify_dashboard
from ..const import SHIPPING_COMPANY_CODE_METADATA_KEY, SHIPPING_COMPANY_CODES
-from ..utils import calculate_manual_refund_amount, create_refunded_lines
+from ..utils import create_refunded_lines
def test_notify_dashboard(order):
@@ -149,38 +146,6 @@ def test_get_shipping_company_code_invalid_metadata(
assert shipping_company_code == config_shipping_company_code
-def create_refund_fulfillment_helper(payment_dummy):
- def factory(
- order,
- order_lines=None,
- fulfillment_lines=None,
- manual_refund_amount=None,
- refund_shipping_costs=False,
- ):
- if manual_refund_amount:
- payment_dummy.captured_amount = manual_refund_amount
- payment_dummy.save(update_fields=["captured_amount"])
- if refund_shipping_costs:
- payment_dummy.captured_amount = order.shipping_price_gross_amount
- payment_dummy.save(update_fields=["captured_amount"])
-
- with patch("saleor.order.actions.gateway.refund"):
- return create_refund_fulfillment(
- user=None,
- app=None,
- order=order,
- payment=payment_dummy,
- order_lines_to_refund=order_lines or [],
- fulfillment_lines_to_refund=fulfillment_lines or [],
- manager=get_plugins_manager(),
- amount=manual_refund_amount,
- refund_shipping_costs=refund_shipping_costs,
- )
-
- return factory
-
-
def test_create_refund_lines_order_lines(order_with_lines):
# given
order_lines = order_with_lines.lines.all()
@@ -241,7 +206,7 @@ def test_create_refund_lines_fulfillment_lines(fulfilled_order):
def test_create_refund_data_previously_refunded_order_lines(
_mocked_refund,
order_with_lines,
- create_refund_fulfillment_helper,
+ create_refund,
previous_refund_shipping_costs,
):
# given
@@ -249,7 +214,7 @@ def test_create_refund_data_previously_refunded_order_lines(
previous_order_refund_lines = [
OrderLineInfo(line=(line := order_lines[0]), quantity=1, variant=line.variant)
]
- create_refund_fulfillment_helper(
+ create_refund(
order_with_lines,
order_lines=previous_order_refund_lines,
refund_shipping_costs=previous_refund_shipping_costs,
@@ -287,7 +252,7 @@ def test_create_refund_data_previously_refunded_order_lines(
def test_create_refund_data_previously_refunded_fulfillment_lines(
_mocked_refund,
fulfilled_order,
- create_refund_fulfillment_helper,
+ create_refund,
previous_refund_shipping_costs,
):
# given
@@ -297,7 +262,7 @@ def test_create_refund_data_previously_refunded_fulfillment_lines(
previous_fulfillment_refund_lines = [
FulfillmentLineData(line=fulfillment_lines[0], quantity=1)
]
- create_refund_fulfillment_helper(
+ create_refund(
fulfilled_order,
fulfillment_lines=previous_fulfillment_refund_lines,
refund_shipping_costs=previous_refund_shipping_costs,
@@ -339,119 +304,117 @@ def test_create_refund_data_previously_refunded_fulfillment_lines(
}
- [
- "current_refund_amount",
- "shipping_amount",
- "refund_shipping",
- "is_manual",
- "order_lines",
- "fulfillment_lines",
- "expected_manual_amount",
- ],
- [
- (None, Decimal("40.00"), False, False, [], [], Decimal("0.00")),
- (Decimal("12.34"), Decimal("40.00"), False, False, [], [], Decimal("12.34")),
- (None, Decimal("40.00"), True, True, [], [], Decimal("40.00")),
- (
- Decimal("12.34"),
- Decimal("40.00"),
- False,
- False,
- [Mock()],
- [],
- Decimal("0.00"),
- ),
- (
- Decimal("12.34"),
- Decimal("40.00"),
- False,
- False,
- [],
- [Mock()],
- Decimal("0.00"),
- ),
- ],
-)
-def test_calculate_refund_amount(
- order,
- current_refund_amount,
- shipping_amount,
- refund_shipping,
- is_manual,
- order_lines,
- fulfillment_lines,
- expected_manual_amount,
-):
- # given
- payment_information = Mock(
- amount=current_refund_amount,
- lines_data=Mock(
- shipping_amount=shipping_amount,
- ),
- )
- refund_data = Mock(
- refund_shipping_costs=refund_shipping,
- refund_is_automatically_calculated=not is_manual,
- order_lines_to_refund=order_lines,
- fulfillment_lines_to_refund=fulfillment_lines,
- )
-
- # when
- amount = calculate_manual_refund_amount(order, payment_information, refund_data)
-
- # then
- assert amount == expected_manual_amount
-
-
- [
- "previous_manual_refund_amount",
- "current_refund_amount",
- "refund_shipping",
- "expected_refund_amount",
- ],
- [
- (None, None, False, Decimal("0.00")),
- (None, None, True, Decimal("12.30")),
- (None, Decimal("8.00"), False, Decimal("8.00")),
- (None, Decimal("8.00"), True, Decimal("20.30")),
- (Decimal("5.00"), None, False, Decimal("5.00")),
- (Decimal("5.00"), None, True, Decimal("17.30")),
- (Decimal("5.00"), Decimal("8.00"), False, Decimal("13.00")),
- (Decimal("5.00"), Decimal("8.00"), True, Decimal("25.30")),
- ],
-)
-def test_calculate_manual_refund_amount_previously_refunded(
- order,
- create_refund_fulfillment_helper,
- previous_manual_refund_amount,
- current_refund_amount,
- refund_shipping,
- expected_refund_amount,
-):
- # given
- if previous_manual_refund_amount:
- create_refund_fulfillment_helper(
- order, manual_refund_amount=previous_manual_refund_amount
- )
- if refund_shipping:
- create_refund_fulfillment_helper(
- order,
- refund_shipping_costs=refund_shipping,
- )
-
- refund_data = Mock(
- refund_shipping_costs=False,
- refund_is_automatically_calculated=False,
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=[],
- )
- payment_information = Mock(amount=current_refund_amount)
-
- # when
- refund_amount = calculate_manual_refund_amount(
- order, payment_information, refund_data
- )
- # then
- assert refund_amount == expected_refund_amount
+# @pytest.mark.parametrize(
+# [
+# "current_refund_amount",
+# "shipping_amount",
+# "refund_shipping",
+# "is_manual",
+# "order_lines",
+# "fulfillment_lines",
+# "expected_manual_amount",
+# ],
+# [
+# (None, Decimal("40.00"), False, False, [], [], Decimal("0.00")),
+# (Decimal("12.34"), Decimal("40.00"), False, False, [], [], Decimal("12.34")),
+# (None, Decimal("40.00"), True, True, [], [], Decimal("40.00")),
+# (
+# Decimal("12.34"),
+# Decimal("40.00"),
+# False,
+# False,
+# [Mock()],
+# [],
+# Decimal("0.00"),
+# ),
+# (
+# Decimal("12.34"),
+# Decimal("40.00"),
+# False,
+# False,
+# [],
+# [Mock()],
+# Decimal("0.00"),
+# ),
+# ],
+# )
+# def test_calculate_refund_amount(
+# order,
+# current_refund_amount,
+# shipping_amount,
+# refund_shipping,
+# is_manual,
+# order_lines,
+# fulfillment_lines,
+# expected_manual_amount,
+# ):
+# # given
+# payment_information = Mock(
+# amount=current_refund_amount,
+# lines_data=Mock(
+# shipping_amount=shipping_amount,
+# ),
+# )
+# refund_data = Mock(
+# refund_shipping_costs=refund_shipping,
+# refund_is_automatically_calculated=not is_manual,
+# order_lines_to_refund=order_lines,
+# fulfillment_lines_to_refund=fulfillment_lines,
+# )
+#
+# # when
+# amount = calculate_manual_refund_amount(order, payment_information, refund_data)
+#
+# # then
+# assert amount == expected_manual_amount
+
+
+# @pytest.mark.parametrize(
+# [
+# "previous_manual_refund_amount",
+# "current_refund_amount",
+# "refund_shipping",
+# "expected_refund_amount",
+# ],
+# [
+# (None, None, False, Decimal("0.00")),
+# (None, None, True, Decimal("12.30")),
+# (None, Decimal("8.00"), False, Decimal("8.00")),
+# (None, Decimal("8.00"), True, Decimal("20.30")),
+# (Decimal("5.00"), None, False, Decimal("5.00")),
+# (Decimal("5.00"), None, True, Decimal("17.30")),
+# (Decimal("5.00"), Decimal("8.00"), False, Decimal("13.00")),
+# (Decimal("5.00"), Decimal("8.00"), True, Decimal("25.30")),
+# ],
+# )
+# def test_calculate_manual_refund_amount_previously_refunded(
+# order,
+# create_refund,
+# previous_manual_refund_amount,
+# current_refund_amount,
+# refund_shipping,
+# expected_refund_amount,
+# ):
+# # given
+# if previous_manual_refund_amount:
+# create_refund(order, manual_refund_amount=previous_manual_refund_amount)
+# if refund_shipping:
+# create_refund(
+# order,
+# refund_shipping_costs=refund_shipping,
+# )
+#
+# refund_data = Mock(
+# refund_shipping_costs=False,
+# refund_is_automatically_calculated=False,
+# order_lines_to_refund=[],
+# fulfillment_lines_to_refund=[],
+# )
+# payment_information = Mock(amount=current_refund_amount)
+#
+# # when
+# refund_amount = calculate_manual_refund_amount(
+# order, payment_information, refund_data
+# )
+# # then
+# assert refund_amount == expected_refund_amount
diff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.py
index 4ba5fd3060..968b33ecb8 100644
--- a/saleor/payment/gateways/np_atobarai/utils.py
+++ b/saleor/payment/gateways/np_atobarai/utils.py
@@ -1,17 +1,17 @@
from collections import defaultdict
from contextlib import contextmanager
-from decimal import Decimal
from itertools import chain
from typing import Dict, Optional
-from django.db.models import Q, Sum
+from django.db.models import BooleanField, F, Q, Value
+from django.db.models.expressions import CombinedExpression
from ....core.tracing import opentracing_trace
from ....order import FulfillmentStatus
from ....order.events import external_notification_event
from ....order.models import Fulfillment, FulfillmentLine, Order
from ... import PaymentError
-from ...interface import PaymentData, RefundData
+from ...interface import RefundData
from .api_types import ApiConfig
from .const import SHIPPING_COMPANY_CODE_METADATA_KEY, SHIPPING_COMPANY_CODES
@@ -84,15 +84,29 @@ def create_refunded_lines(
order_lines_to_refund = refund_data.order_lines_to_refund
fulfillment_lines_to_refund = refund_data.fulfillment_lines_to_refund
- previous_fulfillment_lines = FulfillmentLine.objects.prefetch_related(
- "order_line"
- ).filter(
- fulfillment__order_id=order.pk,
- fulfillment__status__in=[
- FulfillmentStatus.REFUNDED,
- FulfillmentStatus.REFUNDED_AND_RETURNED,
- ],
- order_line__variant_id__isnull=False,
+ # Refund fulfillments for product refunds with automatic amount
+ previous_fulfillment_lines = (
+ FulfillmentLine.objects.prefetch_related("order_line")
+ .annotate(
+ has_manual_amount=CombinedExpression(
+ F("fulfillment__total_refund_amount")
+ % F("order_line__unit_price_gross_amount"),
+ "!=",
+ Value(0),
+ output_field=BooleanField(),
+ )
+ )
+ .filter(
+ fulfillment__order_id=order.pk,
+ fulfillment__status__in=[
+ FulfillmentStatus.REFUNDED,
+ FulfillmentStatus.REFUNDED_AND_RETURNED,
+ ],
+ # No misc refunds
+ order_line__variant_id__isnull=False,
+ # No product refunds with manual amount
+ has_manual_amount=False,
+ )
)
previous_refund_lines = (
@@ -124,39 +138,57 @@ def create_refunded_lines(
return dict(summed_refund_lines)
-def calculate_manual_refund_amount(
- order: Order,
- payment_information: PaymentData,
- refund_data: RefundData,
-) -> Decimal:
- """Return sum of all manual refunds for specified order.
-
- Takes into account previous refunds and current refund mutation amount.
- """
- if (
- refund_data.order_lines_to_refund or refund_data.fulfillment_lines_to_refund
- ) and refund_data.refund_amount_is_automatically_calculated:
- # automatic line refund
- manual_amount_to_refund = Decimal("0.00")
- elif (
- refund_data.refund_shipping_costs
- and refund_data.refund_amount_is_automatically_calculated
- ):
- # automatic shipping refund
- manual_amount_to_refund = payment_information.lines_data.shipping_amount
- else:
- # manual refund
- manual_amount_to_refund = payment_information.amount or Decimal("0.00")
-
- previous_manual_amount_to_refund = (
- order.fulfillments.filter(
- status__in=[
- FulfillmentStatus.REFUNDED,
- FulfillmentStatus.REFUNDED_AND_RETURNED,
- ],
- lines__isnull=True,
- ).aggregate(manual_amount=Sum("total_refund_amount"))["manual_amount"]
- or Decimal("0.00")
- )
-
- return previous_manual_amount_to_refund + manual_amount_to_refund
+# def calculate_manual_refund_amount(
+# order: Order,
+# payment_information: PaymentData,
+# refund_data: RefundData,
+# ) -> Decimal:
+# """Return sum of all manual refunds for specified order.
+#
+# Takes into account previous refunds and current refund mutation amount.
+# """
+# if (
+# refund_data.order_lines_to_refund or refund_data.fulfillment_lines_to_refund
+# ) and refund_data.refund_amount_is_automatically_calculated:
+# # automatic line refund
+# manual_amount_to_refund = Decimal("0.00")
+# elif (
+# refund_data.refund_shipping_costs
+# and refund_data.refund_amount_is_automatically_calculated
+# ):
+# # automatic shipping refund
+# manual_amount_to_refund = payment_information.lines_data.shipping_amount
+# else:
+# # manual refund
+# manual_amount_to_refund = payment_information.amount or Decimal("0.00")
+#
+# # Sum of all previous manual refunds
+# previous_manual_amount_to_refund = (
+# order.fulfillments.annotate(
+# has_manual_amount=CombinedExpression(
+# lhs=F("total_refund_amount")
+# % F("lines__order_line__unit_price_gross_amount"),
+# connector="!=",
+# rhs=Value(0),
+# output_field=BooleanField(),
+# )
+# )
+# .filter(
+# Q(
+# # Misc refunds
+# lines__isnull=True,
+# )
+# | Q(
+# # Product refunds with manual amount
+# lines__isnull=False,
+# has_manual_amount=True,
+# ),
+# status__in=[
+# FulfillmentStatus.REFUNDED,
+# FulfillmentStatus.REFUNDED_AND_RETURNED,
+# ],
+# )
+# .aggregate(manual_amount=Sum("total_refund_amount"))["manual_amount"]
+# or Decimal("0.00")
+# )
+# return previous_manual_amount_to_refund + manual_amount_to_refund