Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Something weird with taxes #1168

Open
tulimaki opened this issue Dec 22, 2017 · 0 comments
Open

Something weird with taxes #1168

tulimaki opened this issue Dec 22, 2017 · 0 comments

Comments

@tulimaki
Copy link
Contributor

I started creating a test to test tax summaries and bumped into a very odd error. This error won't happen if create_default_tax_rule(tax) is not being called for tax. Any idea what could cause this?

The Error:

        for product, q in products_and_quantities:
            basket.add_product(supplier=supplier, shop=shop, product=product, quantity=q)
>           basket.save()

shuup_tests/front/test_basket.py:310: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
shuup/core/basket/objects.py:150: in save
    stored_basket = self.storage.save(basket=self, data=self._data)
shuup/front/basket/storage.py:80: in save
    stored_basket = super(DatabaseBasketStorage, self).save(basket, data)
shuup/core/basket/storage.py:119: in save
    stored_basket.taxless_total_price = basket.taxless_total_price_or_none
shuup/core/order_creator/_source.py:95: in __get__
    return self.prop.__get__(instance)
shuup/core/order_creator/_source.py:73: in __get__
    lines = getattr(instance, self.line_getter)()
shuup/core/order_creator/_source.py:442: in get_final_lines
    self._calculate_taxes(lines)
shuup/core/order_creator/_source.py:452: in _calculate_taxes
    tax_module.add_taxes(self, lines)
shuup/core/taxing/_module.py:100: in add_taxes
    line.taxes = self._get_line_taxes(context, line)
shuup/core/taxing/_module.py:126: in _get_line_taxes
    taxed_price = self.get_taxed_price_for(context, line, line.price)
shuup/core/taxing/_module.py:146: in get_taxed_price_for
    return self.get_taxed_price(context, price, item.tax_class)
shuup/default_tax/module.py:24: in get_taxed_price
    return _calculate_taxes(price, context, tax_class)
shuup/default_tax/module.py:30: in _calculate_taxes
    return calculate_compounded_added_taxes(price, tax_groups)
shuup/core/taxing/utils.py:112: in calculate_compounded_added_taxes
    return _calc_compounded_added_taxes_from_taxful(price, tax_groups)
shuup/core/taxing/utils.py:121: in _calc_compounded_added_taxes_from_taxful
    taxed_price = stacked_value_added_taxes(base_price, taxes)
shuup/core/taxing/utils.py:85: in stacked_value_added_taxes
    for tax in taxes
shuup/core/taxing/_line_tax.py:64: in from_tax
    **kwargs
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <[AttributeError("'SourceLineTax' object has no attribute 'tax'") raised in repr()] SafeRepr object at 0x113e5b830>, tax = <Tax:1-simple-tax-1>, name = 'Simple tax 1', amount = Money('628.5714285714285714285714285', u'EUR')
base_amount = Money('1257.142857142857142857142857', u'EUR')

    def __init__(self, tax, name, amount, base_amount):
        """
            Initialize line tax from given values.
    
            :type tax: shuup.core.models.Tax
            :type name: six.text_type
            :type amount: shuup.utils.money.Money
            :type base_amount: shuup.utils.money.Money
            """
        assert isinstance(tax, shuup.core.models.Tax)
>       assert isinstance(name, six.text_type)
E       AssertionError

shuup/core/taxing/_line_tax.py:79: AssertionError

The test:

@pytest.mark.django_db
def test_complex_basket_tax_summary(rf):
    activate("en")
    StoredBasket.objects.all().delete()
    quantities = [44, 23, 65]

    # create taxes
    tax1 = get_tax("simple-tax-1", "Simple tax 1", Decimal("0.50"))
    tax2 = get_tax("simple-tax-2", "Simple tax 2", Decimal("0.25"))
    create_default_tax_rule(tax1)
    create_default_tax_rule(tax2)

    tax_class = get_default_tax_class()
    currency = "EUR"

    # create shop
    shop = get_default_shop()
    shop.prices_include_tax = True
    shop.save()

    default_price = 50

    get_default_payment_method()  # Can't create baskets without payment methods
    supplier = get_default_supplier()
    products_and_quantities = []
    for quantity in quantities:
        product = create_product(printable_gibberish(), shop=shop, supplier=supplier, default_price=default_price)
        product.tax_class = tax_class
        product.save()
        products_and_quantities.append((product, quantity))

    request = rf.get("/")
    request.session = {}
    request.shop = shop
    apply_request_middleware(request)
    basket = get_basket(request)
    price = Decimal("0")
    for product, q in products_and_quantities:
        basket.add_product(supplier=supplier, shop=shop, product=product, quantity=q)
        basket.save()
        price += default_price * q

    expected_total = Money(price, currency)

    basket = get_basket(request)
    summaries = []
    for line in basket.get_lines():
        summary = line.tax_summary
        assert summary
        assert len(summary) == 2
        summaries.append(summary)
        assert summary[0].tax_rate * 100 == 50
        assert summary[1].tax_rate * 100 == 25

    summary = basket.get_tax_summary()
    assert "lines" in summary
    assert "combined" in summary
    combined1 = summary["combined"][0]
    combined2 = summary["combined"][0]

    combined1_tax_amount = Money(
        bankers_round(basket.taxful_total_price.value - basket.taxless_total_price.value), currency)
    assert combined1.tax_rate * 100 == 50
    assert combined1.tax_amount == combined1_tax_amount
    assert combined1.taxful == combined1.based_on + combined1.tax_amount
    assert combined1.taxful == expected_total
    assert basket.get_total_tax_amount() == combined1.tax_amount
    assert combined1.tax_rate == tax1.rate

    assert combined2.tax_rate * 100 == 25
    assert combined2.tax_amount == Money(
        bankers_round(basket.taxful_total_price.value - basket.taxless_total_price.value), currency)
    assert combined2.taxful == combined2.based_on + combined2.tax_amount
    assert basket.get_total_tax_amount() == combined2.tax_amount
    assert combined2.tax_rate == tax1.rate
    basket.finalize()```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant