Skip to content

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

Delete vatlayer module from tax logic

Maciej Korycinski8ff9319c1df6
diff --git a/saleor/core/taxes/vatlayer/__init__.py b/saleor/core/taxes/vatlayer/__init__.py
deleted file mode 100644
index fc5e8b2f97..0000000000
--- a/saleor/core/taxes/vatlayer/__init__.py
+++ /dev/null
@@ -1,155 +0,0 @@
-from django.conf import settings
-from django.utils.translation import pgettext_lazy
-from django_countries.fields import Country
-from django_prices_vatlayer.utils import get_tax_for_rate, get_tax_rates_for_country
-from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
-
-from .. import charge_taxes_on_shipping, include_taxes_in_prices
-
-
-class TaxRateType:
- ACCOMMODATION = "accommodation"
- ADMISSION_TO_CULTURAL_EVENTS = "admission to cultural events"
- ADMISSION_TO_ENTERTAINMENT_EVENTS = "admission to entertainment events"
- ADMISSION_TO_SPORTING_EVENTS = "admission to sporting events"
- ADVERTISING = "advertising"
- AGRICULTURAL_SUPPLIES = "agricultural supplies"
- BABY_FOODSTUFFS = "baby foodstuffs"
- BIKES = "bikes"
- BOOKS = "books"
- CHILDRENDS_CLOTHING = "childrens clothing"
- DOMESTIC_FUEL = "domestic fuel"
- DOMESTIC_SERVICES = "domestic services"
- E_BOOKS = "e-books"
- FOODSTUFFS = "foodstuffs"
- HOTELS = "hotels"
- MEDICAL = "medical"
- NEWSPAPERS = "newspapers"
- PASSENGER_TRANSPORT = "passenger transport"
- PHARMACEUTICALS = "pharmaceuticals"
- PROPERTY_RENOVATIONS = "property renovations"
- RESTAURANTS = "restaurants"
- SOCIAL_HOUSING = "social housing"
- STANDARD = "standard"
- WATER = "water"
- WINE = "wine"
-
- CHOICES = (
- (ACCOMMODATION, pgettext_lazy("VAT rate type", "accommodation")),
- (
- ADMISSION_TO_CULTURAL_EVENTS,
- pgettext_lazy("VAT rate type", "admission to cultural events"),
- ),
- (
- ADMISSION_TO_ENTERTAINMENT_EVENTS,
- pgettext_lazy("VAT rate type", "admission to entertainment events"),
- ),
- (
- ADMISSION_TO_SPORTING_EVENTS,
- pgettext_lazy("VAT rate type", "admission to sporting events"),
- ),
- (ADVERTISING, pgettext_lazy("VAT rate type", "advertising")),
- (
- AGRICULTURAL_SUPPLIES,
- pgettext_lazy("VAT rate type", "agricultural supplies"),
- ),
- (BABY_FOODSTUFFS, pgettext_lazy("VAT rate type", "baby foodstuffs")),
- (BIKES, pgettext_lazy("VAT rate type", "bikes")),
- (BOOKS, pgettext_lazy("VAT rate type", "books")),
- (CHILDRENDS_CLOTHING, pgettext_lazy("VAT rate type", "childrens clothing")),
- (DOMESTIC_FUEL, pgettext_lazy("VAT rate type", "domestic fuel")),
- (DOMESTIC_SERVICES, pgettext_lazy("VAT rate type", "domestic services")),
- (E_BOOKS, pgettext_lazy("VAT rate type", "e-books")),
- (FOODSTUFFS, pgettext_lazy("VAT rate type", "foodstuffs")),
- (HOTELS, pgettext_lazy("VAT rate type", "hotels")),
- (MEDICAL, pgettext_lazy("VAT rate type", "medical")),
- (NEWSPAPERS, pgettext_lazy("VAT rate type", "newspapers")),
- (PASSENGER_TRANSPORT, pgettext_lazy("VAT rate type", "passenger transport")),
- (PHARMACEUTICALS, pgettext_lazy("VAT rate type", "pharmaceuticals")),
- (PROPERTY_RENOVATIONS, pgettext_lazy("VAT rate type", "property renovations")),
- (RESTAURANTS, pgettext_lazy("VAT rate type", "restaurants")),
- (SOCIAL_HOUSING, pgettext_lazy("VAT rate type", "social housing")),
- (STANDARD, pgettext_lazy("VAT rate type", "standard")),
- (WATER, pgettext_lazy("VAT rate type", "water")),
- (WINE, pgettext_lazy("VAT rate type", "wine")),
- )
-
-
-DEFAULT_TAX_RATE_NAME = TaxRateType.STANDARD
-
-
-def apply_tax_to_price(taxes, rate_name, base):
- if not taxes or not rate_name:
- # Naively convert Money to TaxedMoney for consistency with price
- # handling logic across the codebase, passthrough other money types
- if isinstance(base, Money):
- return TaxedMoney(net=base, gross=base)
- if isinstance(base, MoneyRange):
- return TaxedMoneyRange(
- apply_tax_to_price(taxes, rate_name, base.start),
- apply_tax_to_price(taxes, rate_name, base.stop),
- )
- if isinstance(base, (TaxedMoney, TaxedMoneyRange)):
- return base
- raise TypeError("Unknown base for flat_tax: %r" % (base,))
-
- if rate_name in taxes:
- tax_to_apply = taxes[rate_name]["tax"]
- else:
- tax_to_apply = taxes[DEFAULT_TAX_RATE_NAME]["tax"]
-
- keep_gross = include_taxes_in_prices()
- return tax_to_apply(base, keep_gross=keep_gross)
-
-
-def get_taxes_for_country(country):
- tax_rates = get_tax_rates_for_country(country.code)
- if tax_rates is None:
- return None
-
- taxes = {
- DEFAULT_TAX_RATE_NAME: {
- "value": tax_rates["standard_rate"],
- "tax": get_tax_for_rate(tax_rates),
- }
- }
- if tax_rates["reduced_rates"]:
- taxes.update(
- {
- rate_name: {
- "value": tax_rates["reduced_rates"][rate_name],
- "tax": get_tax_for_rate(tax_rates, rate_name),
- }
- for rate_name in tax_rates["reduced_rates"]
- }
- )
- return taxes
-
-
-def get_taxes_for_address(address):
- """Return proper taxes for address or default country."""
- if address is not None:
- country = address.country
- else:
- country = Country(settings.DEFAULT_COUNTRY)
-
- return get_taxes_for_country(country)
-
-
-def get_tax_rate_by_name(rate_name, taxes=None):
- """Return value of tax rate for current taxes."""
- if not taxes or not rate_name:
- tax_rate = 0
- elif rate_name in taxes:
- tax_rate = taxes[rate_name]["value"]
- else:
- tax_rate = taxes[DEFAULT_TAX_RATE_NAME]["value"]
-
- return tax_rate
-
-
-def get_taxed_shipping_price(shipping_price, taxes):
- """Calculate shipping price based on settings and taxes."""
- if not charge_taxes_on_shipping():
- taxes = None
- return apply_tax_to_price(taxes, DEFAULT_TAX_RATE_NAME, shipping_price)
diff --git a/saleor/core/taxes/vatlayer/interface.py b/saleor/core/taxes/vatlayer/interface.py
deleted file mode 100644
index c1a1ee2c47..0000000000
--- a/saleor/core/taxes/vatlayer/interface.py
+++ /dev/null
@@ -1,147 +0,0 @@
-from typing import TYPE_CHECKING, List, Union
-
-from django_countries.fields import Country
-from django_prices_vatlayer.utils import get_tax_rate_types
-from prices import Money, MoneyRange, TaxedMoney, TaxedMoneyRange
-
-from .. import TaxType, zero_taxed_money
-from . import (
- DEFAULT_TAX_RATE_NAME,
- TaxRateType,
- apply_tax_to_price,
- get_taxed_shipping_price,
- get_taxes_for_address,
- get_taxes_for_country,
-)
-
-if TYPE_CHECKING:
- from ....checkout.models import Checkout, CheckoutLine
- from ....product.models import Product
- from ....account.models import Address
- from ....order.models import Order, OrderLine
-
-
-META_FIELD = "vatlayer"
-
-
-def calculate_checkout_total(
- checkout: "Checkout", discounts: List["DiscountInfo"]
-) -> TaxedMoney:
- """Calculate total gross for checkout by using vatlayer"""
- return (
- calculate_checkout_subtotal(checkout, discounts)
- + calculate_checkout_shipping(checkout, discounts)
- - checkout.discount_amount
- )
-
-
-def calculate_checkout_subtotal(
- checkout: "Checkout", discounts: List["DiscountInfo"]
-) -> TaxedMoney:
- """Calculate subtotal gross for checkout"""
- address = checkout.shipping_address or checkout.billing_address
- lines = checkout.lines.prefetch_related("variant__product__product_type")
- total = checkout.get_total()
- lines_total = zero_taxed_money(currency=total.currency)
- for line in lines:
- price = line.variant.get_price(discounts)
- lines_total += line.quantity * apply_taxes_to_product(
- line.variant.product, price, address.country if address else None
- )
- return lines_total
-
-
-def calculate_checkout_shipping(
- checkout: "Checkout", _: List["DiscountInfo"]
-) -> TaxedMoney:
- """Calculate shipping gross for checkout"""
- address = checkout.shipping_address or checkout.billing_address
- taxes = get_taxes_for_address(address)
- if not checkout.shipping_method:
- total = checkout.get_total()
- return zero_taxed_money(total.currency)
- return get_taxed_shipping_price(checkout.shipping_method.price, taxes)
-
-
-def calculate_order_shipping(order: "Order") -> TaxedMoney:
- address = order.shipping_address or order.billing_address
- taxes = get_taxes_for_address(address)
- if not order.shipping_method:
- return zero_taxed_money(order.total.currency)
- return get_taxed_shipping_price(order.shipping_method.price, taxes)
-
-
-def calculate_order_line_unit(order_line: "OrderLine") -> TaxedMoney:
- address = order_line.order.shipping_address or order_line.order.billing_address
- country = address.country if address else None
- variant = order_line.variant
- return apply_taxes_to_product(variant.product, order_line.unit_price_net, country)
-
-
-def calculate_checkout_line_total(
- checkout_line: "CheckoutLine", discounts: List["DiscountInfo"]
-):
- address = (
- checkout_line.checkout.shipping_address
- or checkout_line.checkout.billing_address
- )
- price = checkout_line.variant.get_price(discounts) * checkout_line.quantity
- country = address.country if address else None
- return apply_taxes_to_product(checkout_line.variant.product, price, country)
-
-
-def apply_taxes_to_shipping(price: Money, shipping_address: "Address"):
- taxes = get_taxes_for_country(shipping_address.country)
- return get_taxed_shipping_price(price, taxes)
-
-
-def get_tax_rate_type_choices() -> List[TaxType]:
- rate_types = get_tax_rate_types() + [DEFAULT_TAX_RATE_NAME]
- choices = [
- TaxType(code=rate_name, description=rate_name) for rate_name in rate_types
- ]
- # sort choices alphabetically by translations
- return sorted(choices, key=lambda x: x.code)
-
-
-def apply_taxes_to_product(
- product: "Product", price: Money, country: Country, **kwargs
-) -> TaxedMoney:
- taxes = kwargs.get("taxes")
- if country and not taxes:
- # FIXME After we introduce plugin architecture, taxes will be cached on the
- # plugin level and there will be no need to pass it from view functions.
- # This is only temporary approach to limit redis and db hits
- taxes = get_taxes_for_country(country)
- if not product.charge_taxes:
- taxes = None
- product_tax_rate = get_tax_from_object_meta(product).code
- tax_rate = product_tax_rate or get_tax_from_object_meta(product.product_type).code
- return apply_tax_to_price(taxes, tax_rate, price)
-
-
-def apply_taxes_to_shipping_price_range(
- prices: MoneyRange, country: "Country"
-) -> TaxedMoneyRange:
- taxes = None
- if country:
- taxes = get_taxes_for_country(country)
- return get_taxed_shipping_price(prices, taxes)
-
-
-def assign_tax_to_object_meta(obj: Union["Product", "ProductType"], tax_code: str):
- if tax_code not in dict(TaxRateType.CHOICES):
- return
- if not hasattr(obj, "meta"):
- return
- if "taxes" not in obj.meta:
- obj.meta["taxes"] = {}
- obj.meta["taxes"][META_FIELD] = {"code": tax_code, "description": tax_code}
-
-
-def get_tax_from_object_meta(obj: Union["Product", "ProductType"]) -> TaxType:
- if not hasattr(obj, "meta"):
- return TaxType(code="", description="")
- tax = obj.meta.get("taxes", {}).get(META_FIELD, {})
-
- return TaxType(code=tax.get("code", ""), description=tax.get("description", ""))