Skip to content

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

Drop calculate_checkout_subtotal from plugins, left it in manager

IKarbowiak7a36d9998776
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.py
index 40e81024e9..f59f677e82 100644
--- a/saleor/checkout/base_calculations.py
+++ b/saleor/checkout/base_calculations.py
@@ -5,7 +5,7 @@ manager.
"""
from decimal import Decimal
-from typing import TYPE_CHECKING, Iterable, List, Optional
+from typing import TYPE_CHECKING, Iterable, Optional
from prices import TaxedMoney
@@ -15,16 +15,8 @@ from ..discount import DiscountInfo
from .fetch import CheckoutLineInfo
if TYPE_CHECKING:
- # flake8: noqa
from ..channel.models import Channel
from ..checkout.fetch import CheckoutInfo
- from ..product.models import (
- Collection,
- Product,
- ProductVariant,
- ProductVariantChannelListing,
- )
- from .models import Checkout, CheckoutLine
def base_checkout_shipping_price(
@@ -52,11 +44,6 @@ def base_checkout_shipping_price(
)
-def base_checkout_subtotal(line_totals: List[TaxedMoney], currency: str) -> TaxedMoney:
- """Return the total cost of all checkout lines."""
- return sum(line_totals, zero_taxed_money(currency))
-
-
def base_checkout_total(
subtotal: TaxedMoney,
shipping_price: TaxedMoney,
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 64f44c525b..f71e4e1ab1 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -176,54 +176,6 @@ class AvataxPlugin(BasePlugin):
total -= voucher_value
return max(total, zero_taxed_money(total.currency))
- def _calculate_checkout_subtotal(
- self,
- checkout_info: "CheckoutInfo",
- lines: Iterable["CheckoutLineInfo"],
- discounts: Iterable[DiscountInfo],
- base_subtotal: TaxedMoney,
- ) -> TaxedMoney:
- currency = checkout_info.checkout.currency
- response = get_checkout_tax_data(checkout_info, lines, discounts, self.config)
- if not response or "error" in response:
- return base_subtotal
-
- sub_tax = Decimal(0.0)
- sub_net = Decimal(0.0)
- for line in response.get("lines", []):
- if line["itemCode"] == "Shipping":
- continue
- sub_tax += Decimal(line["tax"])
- sub_net += Decimal(line.get("lineAmount", 0.0))
- sub_total_gross = Money(sub_net + sub_tax, currency)
- sub_total_net = Money(sub_net, currency)
- taxed_subtotal = TaxedMoney(net=sub_total_net, gross=sub_total_gross)
- return self._append_prices_of_not_taxed_lines(
- taxed_subtotal, lines, checkout_info.channel, discounts
- )
-
- def calculate_checkout_subtotal(
- self,
- checkout_info: "CheckoutInfo",
- lines: Iterable["CheckoutLineInfo"],
- address: Optional["Address"],
- discounts: Iterable[DiscountInfo],
- previous_value: TaxedMoney,
- ) -> TaxedMoney:
- if self._skip_plugin(previous_value):
- return previous_value
-
- base_subtotal = previous_value
- if not _validate_checkout(checkout_info, lines):
- return base_subtotal
- response = get_checkout_tax_data(checkout_info, lines, discounts, self.config)
- if not response or "error" in response:
- return base_subtotal
-
- return self._calculate_checkout_subtotal(
- checkout_info, lines, discounts, base_subtotal
- )
-
def _calculate_checkout_shipping(
self, currency: str, lines: List[Dict], shipping_price: TaxedMoney
) -> TaxedMoney:
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 0bc2e7874d..30febe6739 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -323,6 +323,50 @@ def test_calculate_checkout_subtotal(
)
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
+def test_calculate_checkout_subtotal_for_product_without_tax(
+ checkout,
+ stock,
+ monkeypatch,
+ site_settings,
+ ship_to_pl_address,
+ shipping_zone,
+ address,
+ plugin_configuration,
+):
+ plugin_configuration()
+ variant = stock.product_variant
+ product = variant.product
+ product.charge_taxes = False
+ product.save(update_fields=["charge_taxes"])
+
+ monkeypatch.setattr(
+ "saleor.plugins.avatax.plugin.get_cached_tax_codes_or_fetch",
+ lambda _: {"PC040156": "desc"},
+ )
+ manager = get_plugins_manager()
+ site_settings.company_address = address
+ site_settings.include_taxes_in_prices = True
+ site_settings.save()
+
+ checkout.shipping_address = ship_to_pl_address
+ checkout.shipping_method = shipping_zone.shipping_methods.get()
+ checkout.save()
+
+ quantity = 2
+ add_variant_to_checkout(checkout, variant, quantity)
+ lines = fetch_checkout_lines(checkout)
+ assert len(lines) == 1
+
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = manager.calculate_checkout_subtotal(checkout_info, lines, address, [])
+ total = quantize_price(total, total.currency)
+ expected_total = variant.channel_listings.first().price_amount * quantity
+ assert total == TaxedMoney(
+ net=Money(expected_total, "USD"), gross=Money(expected_total, "USD")
+ )
+
+
@pytest.mark.vcr
@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])
def test_calculate_order_shipping(
diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.py
index 4a435c56d3..cb4387297c 100644
--- a/saleor/plugins/base_plugin.py
+++ b/saleor/plugins/base_plugin.py
@@ -163,21 +163,6 @@ class BasePlugin:
"""
return NotImplemented
- def calculate_checkout_subtotal(
- self,
- checkout_info: "CheckoutInfo",
- lines: List["CheckoutLineInfo"],
- address: Optional["Address"],
- discounts: List["DiscountInfo"],
- previous_value: TaxedMoney,
- ) -> TaxedMoney:
- """Calculate the subtotal for checkout.
-
- Overwrite this method if you need to apply specific logic for the calculation
- of a checkout subtotal. Return TaxedMoney.
- """
- return NotImplemented
-
def calculate_checkout_shipping(
self,
checkout_info: "CheckoutInfo",
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 81c45ae043..e113f3071a 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -157,18 +157,10 @@ class PluginsManager(PaymentInterface):
)
for line_info in lines
]
- default_value = base_calculations.base_checkout_subtotal(
- line_totals, checkout_info.checkout.currency
- )
+ currency = checkout_info.checkout.currency
+ total = sum(line_totals, zero_taxed_money(currency))
return quantize_price(
- self.__run_method_on_plugins(
- "calculate_checkout_subtotal",
- default_value,
- checkout_info,
- lines,
- address,
- discounts,
- ),
+ total,
checkout_info.checkout.currency,
)
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index 11e90be72c..57df778abe 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -74,12 +74,6 @@ class PluginSample(BasePlugin):
total = Money("1.0", currency=checkout_info.checkout.currency)
return TaxedMoney(total, total)
- def calculate_checkout_subtotal(
- self, checkout_info, lines, address, discounts, previous_value
- ):
- subtotal = Money("1.0", currency=checkout_info.checkout.currency)
- return TaxedMoney(subtotal, subtotal)
-
def calculate_checkout_shipping(
self, checkout_info, lines, address, discounts, previous_value
):