Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Rename tax method to more meaningful. Remove unneded method for calculating variant's taxes
Maciej Korycinski8c60f929d88a
diff --git a/saleor/core/taxes/avatax/interface.py b/saleor/core/taxes/avatax/interface.pyindex 6944dd5ce4..c6098ee8c2 100644--- a/saleor/core/taxes/avatax/interface.py+++ b/saleor/core/taxes/avatax/interface.py@@ -27,7 +27,7 @@ if TYPE_CHECKING:from ....order.models import Order, OrderLine-def get_total_gross(checkout: "checkout_models.Checkout", discounts):+def calculate_checkout_total(checkout: "checkout_models.Checkout", discounts):checkout_total = checkout.get_total(discounts=discounts)if not validate_checkout(checkout):return TaxedMoney(net=checkout_total, gross=checkout_total)@@ -43,7 +43,7 @@ def get_total_gross(checkout: "checkout_models.Checkout", discounts):return TaxedMoney(net=total_net, gross=total_gross)-def get_subtotal_gross(checkout: "checkout_models.Checkout", discounts):+def calculate_checkout_subtotal(checkout: "checkout_models.Checkout", discounts):sub_total = checkout.get_subtotal(discounts)if not validate_checkout(checkout):return TaxedMoney(net=sub_total, gross=sub_total)@@ -66,7 +66,7 @@ def get_subtotal_gross(checkout: "checkout_models.Checkout", discounts):return TaxedMoney(net=sub_total_net, gross=sub_total_gross)-def get_shipping_gross(checkout: "checkout_models.Checkout", discounts):+def calculate_checkout_shipping(checkout: "checkout_models.Checkout", discounts):shipping_price = checkout.get_shipping_price()if not validate_checkout(checkout):return TaxedMoney(net=shipping_price, gross=shipping_price)@@ -128,7 +128,9 @@ def postprocess_order_creation(order: "Order"):api_post_request(transaction_url, data)-def get_line_total_gross(checkout_line: "checkout_models.CheckoutLine", discounts):+def calculate_checkout_line_total(+ checkout_line: "checkout_models.CheckoutLine", discounts+):checkout = checkout_line.checkouttaxes_data = get_checkout_tax_data(checkout, discounts)currency = taxes_data.get("currencyCode")@@ -144,7 +146,7 @@ def get_line_total_gross(checkout_line: "checkout_models.CheckoutLine", discountreturn TaxedMoney(net=total, gross=total)-def refresh_order_line_unit_price(order_line: "OrderLine"):+def calculate_order_line_unit(order_line: "OrderLine"):order = order_line.orderif validate_order(order):taxes_data = get_order_tax_data(order)diff --git a/saleor/core/taxes/interface.py b/saleor/core/taxes/interface.pyindex c9b6b9f524..4ac7b89974 100644--- a/saleor/core/taxes/interface.py+++ b/saleor/core/taxes/interface.py@@ -11,7 +11,7 @@ from .vatlayer import interface as vatlayer_interfaceif TYPE_CHECKING:from ...discount.models import SaleQuerysetfrom ...checkout.models import Checkout, CheckoutLine- from ...product.models import Product, ProductVariant+ from ...product.models import Productfrom ...account.models import Addressfrom ...order.models import OrderLine, Order@@ -22,9 +22,9 @@ def calculate_checkout_total("""Calculate total gross for checkout"""if settings.VATLAYER_ACCESS_KEY:- return vatlayer_interface.get_total_gross(checkout, discounts)+ return vatlayer_interface.calculate_checkout_total(checkout, discounts)elif settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:- return avatax_interface.get_total_gross(checkout, discounts)+ return avatax_interface.calculate_checkout_total(checkout, discounts)total = checkout.get_total(discounts)return TaxedMoney(net=total, gross=total)@@ -36,9 +36,9 @@ def calculate_checkout_subtotal("""Calculate subtotal gross for checkout"""if settings.VATLAYER_ACCESS_KEY:- return vatlayer_interface.get_subtotal_gross(checkout, discounts)+ return vatlayer_interface.calculate_checkout_subtotal(checkout, discounts)elif settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:- return avatax_interface.get_subtotal_gross(checkout, discounts)+ return avatax_interface.calculate_checkout_subtotal(checkout, discounts)subtotal = checkout.get_subtotal(discounts)return TaxedMoney(net=subtotal, gross=subtotal)@@ -51,9 +51,9 @@ def calculate_checkout_shipping(if not checkout.shipping_method:return ZERO_TAXED_MONEYif settings.VATLAYER_ACCESS_KEY:- return vatlayer_interface.get_shipping_gross(checkout, discounts)+ return vatlayer_interface.calculate_checkout_shipping(checkout, discounts)elif settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:- return avatax_interface.get_shipping_gross(checkout, discounts)+ return avatax_interface.calculate_checkout_shipping(checkout, discounts)total = checkout.shipping_method.get_total()return TaxedMoney(net=total, gross=total)@@ -94,37 +94,30 @@ def calculate_checkout_line_total(checkout_line: "CheckoutLine", discounts: "SaleQueryset"):if settings.VATLAYER_ACCESS_KEY:- return vatlayer_interface.get_line_total_gross(checkout_line, discounts)+ return vatlayer_interface.calculate_checkout_line_total(+ checkout_line, discounts+ )if settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:- return avatax_interface.get_line_total_gross(checkout_line, discounts)+ return avatax_interface.calculate_checkout_line_total(checkout_line, discounts)total = checkout_line.get_total(discounts)return TaxedMoney(net=total, gross=total)-def refresh_order_line_unit_price(order_line: "OrderLine"):+def calculate_order_line_unit(order_line: "OrderLine"):"""It updates unit_price for a given order line based on current price of variant"""if settings.VATLAYER_ACCESS_KEY:# FIXME Should be inside vatlayer moduleaddress = order_line.order.shipping_address or order_line.order.billing_addresscountry = address.country if address else Nonevariant = order_line.variant- return vatlayer_interface.apply_taxes_to_variant(- variant, order_line.unit_price_net, country+ return vatlayer_interface.apply_taxes_to_product(+ variant.product, order_line.unit_price_net, country)if settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:- return avatax_interface.refresh_order_line_unit_price(order_line)+ return avatax_interface.calculate_order_line_unit(order_line)return order_line.unit_price-def apply_taxes_to_variant(variant: "ProductVariant", price: Money, country: Country):- if settings.VATLAYER_ACCESS_KEY:- return vatlayer_interface.apply_taxes_to_variant(variant, price, country)- if settings.AVATAX_USERNAME_OR_ACCOUNT and settings.AVATAX_PASSWORD_OR_LICENSE:- # FIXME for know we don't know how to get product prices- return TaxedMoney(net=price, gross=price)- return TaxedMoney(net=price, gross=price)--def apply_taxes_to_product(product: "Product", price: Money, country: Country):if settings.VATLAYER_ACCESS_KEY:return vatlayer_interface.apply_taxes_to_product(product, price, country)diff --git a/saleor/core/taxes/vatlayer/interface.py b/saleor/core/taxes/vatlayer/interface.pyindex 6aca7810f1..5be4d06e4c 100644--- a/saleor/core/taxes/vatlayer/interface.py+++ b/saleor/core/taxes/vatlayer/interface.py@@ -17,36 +17,40 @@ from . import (if TYPE_CHECKING:from ....discount.models import SaleQuerysetfrom ....checkout.models import Checkout, CheckoutLine- from ....product.models import Product, ProductVariant+ from ....product.models import Productfrom ....account.models import AddressMETA_FIELD = "vatlayer"-def get_total_gross(checkout: "Checkout", discounts: "SaleQueryset") -> TaxedMoney:+def calculate_checkout_total(+ checkout: "Checkout", discounts: "SaleQueryset"+) -> TaxedMoney:"""Calculate total gross for checkout by using vatlayer"""return (- get_subtotal_gross(checkout, discounts)- + get_shipping_gross(checkout, discounts)+ calculate_checkout_subtotal(checkout, discounts)+ + calculate_checkout_shipping(checkout, discounts)- checkout.discount_amount)-def get_subtotal_gross(checkout: "Checkout", discounts: "SaleQueryset") -> TaxedMoney:+def calculate_checkout_subtotal(+ checkout: "Checkout", discounts: "SaleQueryset"+) -> TaxedMoney:"""Calculate subtotal gross for checkout"""address = checkout.shipping_address or checkout.billing_addresslines = checkout.lines.prefetch_related("variant__product__product_type")lines_total = ZERO_TAXED_MONEYfor line in lines:price = line.variant.get_price(discounts)- lines_total += line.quantity * apply_taxes_to_variant(- line.variant, price, address.country if address else None+ lines_total += line.quantity * apply_taxes_to_product(+ line.variant.product, price, address.country if address else None)return lines_total-def get_shipping_gross(checkout: "Checkout", _: "SaleQueryset") -> TaxedMoney:+def calculate_checkout_shipping(checkout: "Checkout", _: "SaleQueryset") -> TaxedMoney:"""Calculate shipping gross for checkout"""address = checkout.shipping_address or checkout.billing_addresstaxes = get_taxes_for_address(address)@@ -55,14 +59,16 @@ def get_shipping_gross(checkout: "Checkout", _: "SaleQueryset") -> TaxedMoney:return get_taxed_shipping_price(checkout.shipping_method.price, taxes)-def get_line_total_gross(checkout_line: "CheckoutLine", discounts: "SaleQueryset"):+def calculate_checkout_line_total(+ checkout_line: "CheckoutLine", discounts: "SaleQueryset"+):address = (checkout_line.checkout.shipping_addressor checkout_line.checkout.billing_address)price = checkout_line.variant.get_price(discounts) * checkout_line.quantitycountry = address.country if address else None- return apply_taxes_to_variant(checkout_line.variant, price, country)+ return apply_taxes_to_product(checkout_line.variant.product, price, country)def apply_taxes_to_shipping(price: Money, shipping_address: "Address"):@@ -79,19 +85,6 @@ def get_tax_rate_type_choices() -> List[TaxType]:return sorted(choices, key=lambda x: x.code)-def apply_taxes_to_variant(- variant: "ProductVariant", price: "Money", country: "Country"-) -> TaxedMoney:- taxes = None- if variant.product.charge_taxes and country:- taxes = get_taxes_for_country(country)- product_tax_rate = get_tax_from_object_meta(variant.product).code- tax_rate = (- product_tax_rate or get_tax_from_object_meta(variant.product.product_type).code- )- return apply_tax_to_price(taxes, tax_rate, price)--def apply_taxes_to_product(product: "Product", price: Money, country: Country) -> TaxedMoney:diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex f08bfcc3d3..74415b234d 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -378,7 +378,7 @@ def create_order_lines(order, discounts, how_many=10):ProductVariant.objects.bulk_update(variants, ["quantity", "quantity_allocated"])lines = OrderLine.objects.bulk_create(lines)for line in lines:- unit_price = tax_interface.refresh_order_line_unit_price(line)+ unit_price = tax_interface.calculate_order_line_unit(line)line.unit_price_net = unit_price.netline.unit_price_gross = unit_price.grossline.tax_rate = unit_price.tax / unit_price.netdiff --git a/saleor/dashboard/product/views.py b/saleor/dashboard/product/views.pyindex 29339640cf..0f3a9f7e06 100644--- a/saleor/dashboard/product/views.py+++ b/saleor/dashboard/product/views.py@@ -314,8 +314,8 @@ def variant_details(request, product_pk, variant_pk):images = variant.images.all()margin = get_margin_for_variant(variant)- discounted_price = tax_interface.apply_taxes_to_variant(- variant,+ discounted_price = tax_interface.apply_taxes_to_product(+ variant.product,variant.get_price(discounts=Sale.objects.active(date.today())),request.country,).grossdiff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 033b552f4f..9c9b9be4c8 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -9,10 +9,7 @@ from prices import Money, TaxedMoneyfrom ..account.utils import store_user_addressfrom ..checkout import AddressTypefrom ..core.taxes import ZERO_MONEY-from ..core.taxes.interface import (- calculate_order_shipping,- refresh_order_line_unit_price,-)+from ..core.taxes.interface import calculate_order_line_unit, calculate_order_shippingfrom ..core.weight import zero_weightfrom ..dashboard.order.utils import get_voucher_discount_for_orderfrom ..discount.models import NotApplicable@@ -164,7 +161,7 @@ def update_order_prices(order, discounts):line.unit_price_gross = unit_priceline.save(update_fields=["unit_price_net", "unit_price_gross"])- price = refresh_order_line_unit_price(line)+ price = calculate_order_line_unit(line)line.unit_price = priceline.tax_rate = 0 # Fixme we can calulcate tax_rate based on pricesline.save()@@ -294,7 +291,7 @@ def add_variant_to_order(variant=variant,)- unit_price = refresh_order_line_unit_price(line)+ unit_price = calculate_order_line_unit(line)line.unit_price_net = unit_price.netline.unit_price_gross = unit_price.grossline.tax_rate = unit_price.tax / unit_price.netdiff --git a/saleor/product/forms.py b/saleor/product/forms.pyindex 6ca1bf0730..21de09a3ec 100644--- a/saleor/product/forms.py+++ b/saleor/product/forms.py@@ -7,7 +7,7 @@ from django_prices.templatetags.prices_i18n import amountfrom ..checkout.forms import AddToCheckoutFormfrom ..core.taxes import display_gross_prices-from ..core.taxes.interface import apply_taxes_to_variant+from ..core.taxes.interface import apply_taxes_to_productclass VariantChoiceField(forms.ModelChoiceField):@@ -17,7 +17,9 @@ class VariantChoiceField(forms.ModelChoiceField):def label_from_instance(self, obj):variant_label = smart_text(obj)- price = apply_taxes_to_variant(obj, obj.get_price(self.discounts), self.country)+ price = apply_taxes_to_product(+ obj.product, obj.get_price(self.discounts), self.country+ )price = price.gross if self.display_gross else price.netlabel = pgettext_lazy("Variant choice field label", "%(variant_label)s - %(price)s"diff --git a/saleor/product/utils/availability.py b/saleor/product/utils/availability.pyindex 6172022123..d2915e85c0 100644--- a/saleor/product/utils/availability.py+++ b/saleor/product/utils/availability.py@@ -7,7 +7,7 @@ from prices import TaxedMoneyRangefrom saleor.graphql.core.types import MoneyRangefrom saleor.product.models import Product, ProductVariant-from ...core.taxes.interface import apply_taxes_to_product, apply_taxes_to_variant+from ...core.taxes.interface import apply_taxes_to_productfrom ...core.utils import to_local_currencyfrom .. import ProductAvailabilityStatus, VariantAvailabilityStatus@@ -144,10 +144,10 @@ def get_variant_availability(variant: ProductVariant, discounts=None, country=None, local_currency=None) -> VariantAvailability:- discounted = apply_taxes_to_variant(- variant, variant.get_price(discounts=discounts), country+ discounted = apply_taxes_to_product(+ variant.product, variant.get_price(discounts=discounts), country)- undiscounted = apply_taxes_to_variant(variant, variant.get_price(), country)+ undiscounted = apply_taxes_to_product(variant.product, variant.get_price(), country)discount = _get_total_discount(undiscounted, discounted)diff --git a/saleor/product/utils/variants_picker.py b/saleor/product/utils/variants_picker.pyindex 0bb6a55295..fbf835a580 100644--- a/saleor/product/utils/variants_picker.py+++ b/saleor/product/utils/variants_picker.py@@ -4,7 +4,7 @@ from django_prices.templatetags import prices_i18nfrom ...core.taxes import display_gross_pricesfrom ...core.taxes.interface import (- apply_taxes_to_variant,+ apply_taxes_to_product,get_tax_from_object_meta,show_taxes_on_storefront,)@@ -27,9 +27,11 @@ def get_variant_picker_data(filter_available_variants = defaultdict(list)for variant in variants:- price = apply_taxes_to_variant(variant, variant.get_price(discounts), country)- price_undiscounted = apply_taxes_to_variant(- variant, variant.get_price(), country+ price = apply_taxes_to_product(+ variant.product, variant.get_price(discounts), country+ )+ price_undiscounted = apply_taxes_to_product(+ variant.product, variant.get_price(), country)if local_currency:price_local_currency = to_local_currency(price, local_currency)diff --git a/tests/api/test_variant_pricing.py b/tests/api/test_variant_pricing.pyindex a7618455c5..236add566a 100644--- a/tests/api/test_variant_pricing.py+++ b/tests/api/test_variant_pricing.py@@ -102,7 +102,7 @@ def test_get_variant_pricing_not_on_sale(api_client, product):def test_variant_pricing(variant: ProductVariant, monkeypatch, settings, taxes):taxed_price = TaxedMoney(Money("10.0", "USD"), Money("12.30", "USD"))monkeypatch.setattr(- "saleor.product.utils.availability.apply_taxes_to_variant",+ "saleor.product.utils.availability.apply_taxes_to_product",Mock(return_value=taxed_price),)