Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix tests & refactor refund fulfillment lines
Mateusz Grzybf3369f145ed7
diff --git a/saleor/payment/gateways/np_atobarai/tests/conftest.py b/saleor/payment/gateways/np_atobarai/tests/conftest.pyindex bfe2339493..53daf4ac16 100644--- a/saleor/payment/gateways/np_atobarai/tests/conftest.py+++ b/saleor/payment/gateways/np_atobarai/tests/conftest.py@@ -5,6 +5,7 @@ from unittest.mock import patchimport pytestfrom .....order.actions import create_refund_fulfillment+from .....order.utils import recalculate_orderfrom .....plugins.manager import get_plugins_managerfrom .....plugins.models import PluginConfigurationfrom ....interface import AddressData, PaymentLineData, PaymentLinesData@@ -122,6 +123,24 @@ def payment_dummy(payment_dummy, order_with_lines):return payment_dummy+def order_with_lines(order_with_lines):+ order_with_lines.shipping_price_net_amount = Decimal("5.55")+ order_with_lines.shipping_price_gross_amount = Decimal("5.55") * Decimal("1.34")+ order_with_lines.shipping_tax_rate = Decimal("1.34")++ order_with_lines.save()++ recalculate_order(order_with_lines)++ return order_with_lines+++def order_lines(order_with_lines):+ return list(order_with_lines.lines.all())++@pytest.fixturedef create_refund(payment_dummy):def factory(diff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.pyindex 9e8e89ab31..3b5eb138a8 100644--- a/saleor/payment/gateways/np_atobarai/utils.py+++ b/saleor/payment/gateways/np_atobarai/utils.py@@ -1,10 +1,10 @@from collections import defaultdictfrom contextlib import contextmanager+from decimal import Decimalfrom itertools import chain-from typing import Dict, Optional+from typing import Dict, Iterable, Optional-from django.db.models import BooleanField, F, Q, Value-from django.db.models.expressions import CombinedExpression+from django.db.models import Qfrom ....core.tracing import opentracing_tracefrom ....order import FulfillmentStatus@@ -68,12 +68,42 @@ def np_atobarai_opentracing_trace(span_name: str):yield-_FULFILLMENT_LINE_HAS_MANUAL_AMOUNT_EXPRESSION = CombinedExpression(- F("fulfillment__total_refund_amount") % F("order_line__unit_price_gross_amount"),- "!=",- Value(0),- output_field=BooleanField(),-)+def _is_refunded_with_automatic_amount(line: FulfillmentLine) -> bool:+ fulfillment = line.fulfillment+ order_line = line.order_line++ shipping_amount = fulfillment.shipping_refund_amount or Decimal("0.00")+ if not fulfillment.total_refund_amount:+ return False++ refund_amount_is_a_multiply_of_unit_price = (+ (fulfillment.total_refund_amount - shipping_amount)+ % order_line.unit_price_gross_amount+ ) == 0++ return (+ order_line.variant_id is not None+ and refund_amount_is_a_multiply_of_unit_price+ and fulfillment.total_refund_amount <= order_line.total_price_gross_amount+ )+++def _get_refunded_fulfillment_lines_with_automatic_amount(+ order: Order,+) -> Iterable[FulfillmentLine]:+ return (+ line+ for line in FulfillmentLine.objects.select_related(+ "order_line", "fulfillment"+ ).filter(+ fulfillment__order_id=order.pk,+ fulfillment__status__in=[+ FulfillmentStatus.REFUNDED,+ FulfillmentStatus.REFUNDED_AND_RETURNED,+ ],+ )+ if _is_refunded_with_automatic_amount(line)+ )def create_refunded_lines(@@ -93,20 +123,8 @@ def create_refunded_lines(fulfillment_lines_to_refund = refund_data.fulfillment_lines_to_refund# Refund fulfillments for product refunds with automatic amount- previous_fulfillment_lines = (- FulfillmentLine.objects.prefetch_related("order_line")- .annotate(has_manual_amount=_FULFILLMENT_LINE_HAS_MANUAL_AMOUNT_EXPRESSION)- .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_fulfillment_lines = _get_refunded_fulfillment_lines_with_automatic_amount(+ order)previous_refund_lines = (