Пример #1
0
def get_line_taxes_for(order_line):
    get_default_tax()  # Creates the Tax and TaxRule
    tax_module = DefaultTaxModule()
    tax_ctx = tax_module.get_context_from_order_source(order_line.order)
    product = order_line.product
    price = order_line.price
    taxed_price = tax_module.get_taxed_price_for(tax_ctx, product, price)
    return taxed_price.taxes
def get_line_taxes_for(order_line):
    get_default_tax()  # Creates the Tax and TaxRule
    tax_module = DefaultTaxModule()
    tax_ctx = tax_module.get_context_from_order_source(order_line.order)
    product = order_line.product
    price = order_line.price
    taxed_price = tax_module.get_taxed_price_for(tax_ctx, product, price)
    return taxed_price.taxes
Пример #3
0
def _get_template_engine_and_context():
    engine = django.template.engines['jinja2']
    assert isinstance(engine, django_jinja.backend.Jinja2)

    shop = get_default_shop()
    shop.currency = 'USD'
    shop.prices_include_tax = False
    shop.save()

    request = RequestFactory().get('/')
    request.shop = shop
    request.customer = AnonymousContact()
    request.person = request.customer
    PriceDisplayOptions(include_taxes=False).set_for_request(request)
    tax = get_default_tax()
    create_default_tax_rule(tax)
    tax_class = get_default_tax_class()
    order, order_line = _get_order_and_order_line(request)

    product = create_product(sku='6.0745', shop=shop, tax_class=tax_class)

    context = {
        'request': request,
        'prod': product,
        # TODO: Test also with variant products
        'sline': _get_source_line(request),
        'bline': _get_basket_line(request),
        'oline': order_line,
        'order': order
    }

    return (engine, context)
Пример #4
0
def test_complex_order_tax(include_taxes):
    tax = get_default_tax()
    quantities = [44, 23, 65]
    product = get_default_product()
    supplier = get_default_supplier()
    shop = get_default_shop()
    shop.prices_include_tax = include_taxes
    shop.save()

    order = create_empty_order(shop=shop)
    order.full_clean()
    order.save()

    pricing_context = get_pricing_module().get_context_from_data(
        shop=shop,
        customer=order.customer or AnonymousContact(),
    )

    total_price = Decimal("0")
    price = Decimal("50")

    for quantity in quantities:
        total_price += quantity * price
        add_product_to_order(order, supplier, product, quantity, price, tax.rate, pricing_context)
    order.cache_prices()
    order.save()

    currency = "EUR"
    summary = order.get_tax_summary()[0]

    assert summary.tax_rate == tax.rate
    assert summary.based_on == Money(total_price, currency)
    assert summary.tax_amount == Money(total_price * tax.rate, currency)
    assert summary.taxful == summary.based_on + summary.tax_amount
    assert order.get_total_tax_amount() == Money(total_price * tax.rate, currency)
Пример #5
0
def test_complex_order_tax(include_taxes):
    tax = get_default_tax()
    quantities = [44, 23, 65]
    product = get_default_product()
    supplier = get_default_supplier()
    shop = get_default_shop()
    shop.prices_include_tax = include_taxes
    shop.save()

    order = create_empty_order(shop=shop)
    order.full_clean()
    order.save()

    pricing_context = get_pricing_module().get_context_from_data(
        shop=shop,
        customer=order.customer or AnonymousContact(),
    )

    total_price = Decimal("0")
    price = Decimal("50")

    for quantity in quantities:
        total_price += quantity * price
        add_product_to_order(order, supplier, product, quantity, price, tax.rate, pricing_context)
    order.cache_prices()
    order.save()

    currency = "EUR"
    summary = order.get_tax_summary()[0]

    assert summary.tax_rate == tax.rate
    assert summary.based_on == Money(total_price, currency)
    assert summary.tax_amount == Money(total_price * tax.rate, currency)
    assert summary.taxful == summary.based_on + summary.tax_amount
    assert order.get_total_tax_amount() == Money(total_price * tax.rate, currency)
Пример #6
0
def _get_template_engine_and_context():
    engine = django.template.engines['jinja2']
    assert isinstance(engine, django_jinja.backend.Jinja2)

    shop = get_default_shop()
    shop.currency = 'USD'
    shop.prices_include_tax = False
    shop.save()

    request = RequestFactory().get('/')
    request.shop = shop
    request.customer = AnonymousContact()
    request.person = request.customer
    PriceDisplayOptions(include_taxes=False).set_for_request(request)
    tax = get_default_tax()
    create_default_tax_rule(tax)
    tax_class = get_default_tax_class()
    order, order_line = _get_order_and_order_line(request)

    product = create_product(sku='6.0745', shop=shop, tax_class=tax_class)

    context = {
        'request': request,
        'prod': product,
        # TODO: Test also with variant products
        'sline': _get_source_line(request),
        'bline': _get_basket_line(request),
        'oline': order_line,
        'order': order
    }

    return (engine, context)
Пример #7
0
def test_price_info_cache_bump(rf):
    initial_price = 10
    shop = factories.get_default_shop()
    tax = factories.get_default_tax()
    tax_class = factories.get_default_tax_class()
    product = factories.create_product(
        "product",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=initial_price)
    request = apply_request_middleware(rf.get("/"))

    def assert_cache_product():
        cache_price_info(request, product, 1, product.get_price_info(request))
        assert get_cached_price_info(
            request, product, 1).price == shop.create_price(initial_price)

    def assert_nothing_is_cached():
        # nothing is cached
        assert get_cached_price_info(request, product, 1) is None

    # cache the item
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    tax.save()
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    tax_class.save()
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    product.save()
    assert_nothing_is_cached()
    assert_cache_product()

    shop_product = product.get_shop_instance(shop)

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.save()
    assert_nothing_is_cached()
    assert_cache_product()

    category = factories.get_default_category()

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.categories.add(category)
    assert_nothing_is_cached()
    assert_cache_product()
Пример #8
0
def _get_template_engine_and_context(product_sku="6.0745",
                                     create_var_product=False):
    engine = django.template.engines["jinja2"]
    assert isinstance(engine, django_jinja.backend.Jinja2)

    shop = get_default_shop()
    shop.currency = "USD"
    shop.prices_include_tax = False
    shop.save()

    request = RequestFactory().get("/")
    request.shop = shop
    request.customer = AnonymousContact()
    request.person = request.customer
    PriceDisplayOptions(include_taxes=False).set_for_request(request)
    tax = get_default_tax()
    create_default_tax_rule(tax)
    tax_class = get_default_tax_class()
    order, order_line = _get_order_and_order_line(request)

    product = create_product(sku=product_sku, shop=shop, tax_class=tax_class)
    supplier = get_default_supplier(shop)

    if create_var_product:
        var_product = create_product(sku="32.9",
                                     shop=shop,
                                     tax_class=tax_class)
        child_product_1 = create_product(sku="4.50",
                                         shop=shop,
                                         tax_class=tax_class,
                                         supplier=supplier,
                                         default_price="4.5")
        child_product_2 = create_product(sku="12.00",
                                         shop=shop,
                                         tax_class=tax_class,
                                         supplier=supplier,
                                         default_price="12")
        child_product_1.link_to_parent(var_product, variables={"color": "red"})
        child_product_2.link_to_parent(var_product,
                                       variables={"color": "blue"})

    context = {
        "request": request,
        "prod": product,
        "var_prod": var_product if create_var_product else None,
        # TODO: Test also with variant products
        "sline": _get_source_line(request),
        "bline": _get_basket_line(request),
        "oline": order_line,
        "order": order,
    }

    return (engine, context)
Пример #9
0
def test_price_info_cache_bump(rf):
    initial_price = 10
    shop = factories.get_default_shop()
    tax = factories.get_default_tax()
    tax_class = factories.get_default_tax_class()
    product = factories.create_product(
        "product",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=initial_price
    )
    request = apply_request_middleware(rf.get("/"))

    def assert_cache_product():
        cache_price_info(request, product, 1, product.get_price_info(request))
        assert get_cached_price_info(request, product, 1).price == shop.create_price(initial_price)

    def assert_nothing_is_cached():
        # nothing is cached
        assert get_cached_price_info(request, product, 1) is None

    # cache the item
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    tax.save()
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    tax_class.save()
    assert_nothing_is_cached()
    assert_cache_product()

    # cache bumped - the cache should be dropped - then, cache again
    product.save()
    assert_nothing_is_cached()
    assert_cache_product()

    shop_product = product.get_shop_instance(shop)

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.save()
    assert_nothing_is_cached()
    assert_cache_product()

    category = factories.get_default_category()

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.categories.add(category)
    assert_nothing_is_cached()
    assert_cache_product()
Пример #10
0
def test_product_price_info(admin_user, prices_include_tax, product_price, discount, tax_rate, taxful_price, taxless_price):
    shop = get_default_shop()
    shop.prices_include_tax = prices_include_tax
    shop.save()

    customer = create_random_person()
    group = customer.get_default_group()
    customer.user = admin_user
    customer.groups.add(group)
    customer.save()

    tax = factories.get_default_tax()
    tax.rate = Decimal(tax_rate)
    tax.save()

    product = create_product("Just-A-Product", shop, default_price=product_price)
    CgpDiscount.objects.create(product=product, shop=shop, group=group, discount_amount_value=discount)

    client = _get_client(admin_user)
    response = client.get("/api/shuup/front/shop_products/", format="json")
    data = response.data[0]

    discounted_price = (product_price - discount)
    price = (discounted_price if prices_include_tax else discounted_price * (1 + tax_rate))
    base_price = (product_price if prices_include_tax else (product_price * (1 + tax_rate)))
    discount_value = (discount if prices_include_tax else (discount * (1 + tax_rate)))

    price_info = data["price_info"]

    def money_round(value):
        return Money(value, shop.currency).as_rounded(2)

    assert money_round(data["price"]) == money_round(price)
    assert money_round(price_info['base_price']) == money_round(base_price)
    assert money_round(price_info['taxful_price']) == money_round(taxful_price)

    if prices_include_tax:
        assert 'taxless_price' not in price_info
        assert 'taxless_base_price' not in price_info
        assert 'tax_amount' not in price_info
    else:
        assert money_round(price_info['taxless_base_price']) == money_round(product_price)
        assert money_round(price_info['taxful_base_price']) == money_round(base_price)
        assert money_round(price_info['taxless_price']) == money_round(taxless_price)
        assert money_round(price_info['tax_amount']) == money_round((product_price - discount) * tax_rate)

    assert money_round(price_info['price']) == money_round(price)
    assert money_round(price_info['discount_amount']) == money_round(discount_value)
    assert money_round(price_info['discount_rate']) == money_round(discount_value / price if discount else 0)
    assert price_info['is_discounted'] is (True if discount else False)
Пример #11
0
def test_line_discount_more():
    order = create_empty_order()
    order.save()
    ol = OrderLine(order=order, type=OrderLineType.OTHER)
    ol.quantity = 5
    ol.base_unit_price = order.shop.create_price(30)
    ol.discount_amount = order.shop.create_price(50)
    ol.save()
    currency = order.shop.currency
    assert ol.taxless_base_unit_price == TaxlessPrice(30, currency)
    assert ol.taxless_discount_amount == TaxlessPrice(50, currency)
    assert ol.taxless_price == TaxlessPrice(5 * 30 - 50, currency)
    ol.taxes.add(OrderLineTax.from_tax(
        get_default_tax(), ol.taxless_price.amount, order_line=ol))
    assert ol.taxless_discount_amount == TaxlessPrice(50, currency)
    assert ol.taxful_discount_amount == TaxfulPrice(75, currency)
    assert ol.taxless_price == TaxlessPrice(100, currency)
    assert ol.taxful_price == TaxfulPrice(150, currency)
    assert ol.taxless_base_unit_price == TaxlessPrice(30, currency)
    assert ol.taxful_base_unit_price == TaxfulPrice(45, currency)
Пример #12
0
def test_line_discount_more():
    order = create_empty_order()
    order.save()
    ol = OrderLine(order=order, type=OrderLineType.OTHER)
    ol.quantity = 5
    ol.base_unit_price = order.shop.create_price(30)
    ol.discount_amount = order.shop.create_price(50)
    ol.save()
    currency = order.shop.currency
    assert ol.taxless_base_unit_price == TaxlessPrice(30, currency)
    assert ol.taxless_discount_amount == TaxlessPrice(50, currency)
    assert ol.taxless_price == TaxlessPrice(5 * 30 - 50, currency)
    ol.taxes.add(OrderLineTax.from_tax(
        get_default_tax(), ol.taxless_price.amount, order_line=ol))
    assert ol.taxless_discount_amount == TaxlessPrice(50, currency)
    assert ol.taxful_discount_amount == TaxfulPrice(75, currency)
    assert ol.taxless_price == TaxlessPrice(100, currency)
    assert ol.taxful_price == TaxfulPrice(150, currency)
    assert ol.taxless_base_unit_price == TaxlessPrice(30, currency)
    assert ol.taxful_base_unit_price == TaxfulPrice(45, currency)
Пример #13
0
def _get_template_engine_and_context(product_sku='6.0745', create_var_product=False):
    engine = django.template.engines['jinja2']
    assert isinstance(engine, django_jinja.backend.Jinja2)

    shop = get_default_shop()
    shop.currency = 'USD'
    shop.prices_include_tax = False
    shop.save()

    request = RequestFactory().get('/')
    request.shop = shop
    request.customer = AnonymousContact()
    request.person = request.customer
    PriceDisplayOptions(include_taxes=False).set_for_request(request)
    tax = get_default_tax()
    create_default_tax_rule(tax)
    tax_class = get_default_tax_class()
    order, order_line = _get_order_and_order_line(request)

    product = create_product(sku=product_sku, shop=shop, tax_class=tax_class)

    if create_var_product:
        var_product = create_product(sku="32.9", shop=shop, tax_class=tax_class)
        child_product_1 = create_product(sku="4.50", shop=shop, tax_class=tax_class, supplier=get_default_supplier())
        child_product_2 = create_product(sku="12.00", shop=shop, tax_class=tax_class, supplier=get_default_supplier())
        child_product_1.link_to_parent(var_product, variables={"color": "red"})
        child_product_2.link_to_parent(var_product, variables={"color": "blue"})

    context = {
        'request': request,
        'prod': product,
        'var_prod': var_product if create_var_product else None,
        # TODO: Test also with variant products
        'sline': _get_source_line(request),
        'bline': _get_basket_line(request),
        'oline': order_line,
        'order': order
    }

    return (engine, context)
Пример #14
0
def _get_template_engine_and_context():
    engine = django.template.engines['jinja2']
    assert isinstance(engine, django_jinja.backend.Jinja2)

    request = RequestFactory().get('/')
    request.shop = Shop(currency='USD', prices_include_tax=False)
    request.customer = AnonymousContact()
    request.person = request.customer
    tax = get_default_tax()
    create_default_tax_rule(tax)
    tax_class = get_default_tax_class()

    context = {
        'request': request,
        'prod': Product(sku='6.0745', tax_class=tax_class),
        # TODO: Test also with variant products
        'sline': _get_source_line(request),
        'bline': _get_basket_line(request),
        'oline': _get_order_line(request),
    }

    return (engine, context)
Пример #15
0
def test_line_discount():
    order = create_empty_order(prices_include_tax=False)
    order.save()
    currency = order.shop.currency
    ol = OrderLine(
        order=order,
        type=OrderLineType.OTHER,
        quantity=5,
        text="Thing"
    )
    ol.discount_amount = order.shop.create_price(50)
    ol.base_unit_price = order.shop.create_price(40)
    ol.save()
    ol.taxes.add(OrderLineTax.from_tax(
        get_default_tax(), ol.taxless_price.amount, order_line=ol))
    assert ol.taxless_discount_amount == order.shop.create_price(50)
    assert ol.taxful_discount_amount == TaxfulPrice(75, currency)
    assert ol.taxless_price == order.shop.create_price(150)
    assert ol.taxful_price == TaxfulPrice(150 + 75, currency)
    assert ol.taxless_base_unit_price == order.shop.create_price(40)
    assert ol.taxful_base_unit_price == TaxfulPrice(60, currency)
    assert "Thing" in six.text_type(ol)
Пример #16
0
def test_line_discount():
    order = create_empty_order(prices_include_tax=False)
    order.save()
    currency = order.shop.currency
    ol = OrderLine(
        order=order,
        type=OrderLineType.OTHER,
        quantity=5,
        text="Thing"
    )
    ol.discount_amount = order.shop.create_price(50)
    ol.base_unit_price = order.shop.create_price(40)
    ol.save()
    ol.taxes.add(OrderLineTax.from_tax(
        get_default_tax(), ol.taxless_price.amount, order_line=ol))
    assert ol.taxless_discount_amount == order.shop.create_price(50)
    assert ol.taxful_discount_amount == TaxfulPrice(75, currency)
    assert ol.taxless_price == order.shop.create_price(150)
    assert ol.taxful_price == TaxfulPrice(150 + 75, currency)
    assert ol.taxless_base_unit_price == order.shop.create_price(40)
    assert ol.taxful_base_unit_price == TaxfulPrice(60, currency)
    assert "Thing" in six.text_type(ol)
Пример #17
0
def test_many_price_info_cache_bump(rf):
    initial_price = 10
    shop = factories.get_default_shop()
    tax = factories.get_default_tax()
    tax_class = factories.get_default_tax_class()
    product = factories.create_product(
        "product", shop=shop, supplier=factories.get_default_supplier(), default_price=initial_price
    )
    child1 = factories.create_product("child1", shop=shop, supplier=factories.get_default_supplier(), default_price=5)
    child2 = factories.create_product("child2", shop=shop, supplier=factories.get_default_supplier(), default_price=9)
    child1.link_to_parent(product, variables={"color": "red"})
    child2.link_to_parent(product, variables={"color": "blue"})

    request = apply_request_middleware(rf.get("/"))
    child1_pi = child1.get_price_info(request)
    child2_pi = child1.get_price_info(request)

    def assert_cache_products():
        cache_many_price_info(request, product, 1, [child1_pi, child2_pi])
        assert get_many_cached_price_info(request, product, 1)[0].price == child1_pi.price
        assert get_many_cached_price_info(request, product, 1)[1].price == child2_pi.price

    def assert_nothing_is_cached():
        # nothing is cached
        assert get_many_cached_price_info(request, product, 1) is None

    # cache the item
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    tax.save()
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    tax_class.save()
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    product.save()
    assert_nothing_is_cached()
    assert_cache_products()

    shop_product = product.get_shop_instance(shop)

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.save()
    assert_nothing_is_cached()
    assert_cache_products()

    category = factories.get_default_category()

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.categories.add(category)
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    supplier = shop_product.suppliers.first()
    supplier.enabled = False
    supplier.save()
    assert_nothing_is_cached()
    assert_cache_products()
Пример #18
0
def test_create_order(admin_user, currency):
    create_default_order_statuses()
    shop = get_default_shop()
    shop.currency = currency
    tax = get_default_tax()
    Currency.objects.get_or_create(code=currency, decimal_places=2)
    shop.save()
    sm = get_default_shipping_method()
    pm = get_default_payment_method()
    contact = create_random_person(locale="en_US", minimum_name_comp_len=5)
    product = create_product(sku=printable_gibberish(),
                             supplier=get_default_supplier(),
                             shop=shop)
    assert not Order.objects.count()
    client = _get_client(admin_user)
    lines = [
        {
            "type": "product",
            "product": product.id,
            "quantity": "1",
            "base_unit_price_value": "5.00"
        },
        {
            "type": "product",
            "product": product.id,
            "quantity": "2",
            "base_unit_price_value": "1.00",
            "discount_amount_value": "0.50"
        },
        {
            "type": "other",
            "sku": "hello",
            "text": "A greeting",
            "quantity": 1,
            "base_unit_price_value": "3.5"
        },
        {
            "type": "text",
            "text": "This was an order!",
            "quantity": 0
        },
    ]
    response = client.post("/api/shuup/order/",
                           content_type="application/json",
                           data=json.dumps({
                               "shop": shop.pk,
                               "shipping_method": sm.pk,
                               "payment_method": pm.pk,
                               "customer": contact.pk,
                               "lines": lines
                           }))
    assert response.status_code == 201
    assert Order.objects.count() == 1
    order = Order.objects.first()
    assert order.shop == shop
    assert order.shipping_method == sm
    assert order.payment_method == pm
    assert order.customer == contact
    assert order.creator == admin_user
    assert order.billing_address == contact.default_billing_address.to_immutable(
    )
    assert order.shipping_address == contact.default_shipping_address.to_immutable(
    )
    assert order.payment_status == PaymentStatus.NOT_PAID
    assert order.shipping_status == ShippingStatus.NOT_SHIPPED
    assert order.status == OrderStatus.objects.get_default_initial()
    assert order.taxful_total_price_value == decimal.Decimal(10)
    assert order.lines.count(
    ) == 6  # shipping line, payment line, 2 product lines, 2 other lines
    assert order.currency == currency
    for idx, line in enumerate(order.lines.all()[:4]):
        assert line.quantity == decimal.Decimal(lines[idx].get("quantity"))
        assert line.base_unit_price_value == decimal.Decimal(lines[idx].get(
            "base_unit_price_value", 0))
        assert line.discount_amount_value == decimal.Decimal(lines[idx].get(
            "discount_amount_value", 0))

    # Test tax summary
    response_data = json.loads(response.content.decode("utf-8"))
    # Tax summary should not be present here
    assert "summary" not in response_data

    response = client.get('/api/shuup/order/{}/taxes/'.format(order.pk))
    assert response.status_code == status.HTTP_200_OK
    response_data = json.loads(response.content.decode("utf-8"))

    assert "lines" in response_data
    assert "summary" in response_data
    line_summary = response_data["lines"]
    summary = response_data["summary"]
    first_tax_summary = summary[0]

    assert int(first_tax_summary["tax_id"]) == tax.id
    assert first_tax_summary["tax_rate"] == tax.rate

    first_line_summary = line_summary[0]
    assert "tax" in first_line_summary
Пример #19
0
def test_shop_products_query(price_includes_tax, product_price, tax_rate, discount):
    shop = factories.get_default_shop()
    shop.prices_include_tax = price_includes_tax
    shop.save()

    tax = factories.get_default_tax()
    tax.rate = Decimal(tax_rate)
    tax.save()

    query = """
    {
        shopProducts {
            id,
            product {
                name,
                description,
                slug,
                shortDescription,
                salesUnit {
                    id,
                    name,
                    symbol
                },
                primaryImage {
                    id, url
                },
                images {
                    id,
                    url
                }
            },
            purchasable,
            primaryImage {
                id, url
            },
            priceInfo {
                basePrice,
                price,
                discountAmount,
                discountRate,
                discountPercentage,
                isDiscounted,
                taxfulPrice,
                taxfulBasePrice,
                taxlessPrice,
                taxlessBasePrice,
                taxAmount
            },
            suppliers {
                id, name
            },
            primaryCategory {
                id, name
            },
            categories {
                id, name
            }
        }
    }
    """

    client = Client()
    response = client.get("/gql/", data={"query": query})

    # no product
    assert response.status_code == 200
    data = json.loads(response.content.decode("utf-8"))
    assert data["data"]["shopProducts"] == []

    supplier = factories.get_default_supplier()
    product = factories.create_product("p1", shop, supplier=supplier, default_price=product_price)
    product.name = "My Product"
    product.description = "Special product"
    product.save()

    # add 3 images for product
    medias = create_images_for_product(shop, product)
    product.primary_image = medias[1]
    product.save()

    create_categories(shop)
    shop_product = product.get_shop_instance(shop)
    shop_product.shop_primary_image = medias[0]
    shop_product.categories = Category.objects.all()
    shop_product.primary_category = Category.objects.first()
    shop_product.save()

    if discount:
        group = AnonymousContact.get_default_group()
        CgpDiscount.objects.create(product=product, shop=shop, group=group, discount_amount_value=discount)

    # returns something
    response = client.get("/gql/", data={"query": query})
    assert response.status_code == 200
    data = json.loads(response.content.decode("utf-8"))
    assert len(data["data"]["shopProducts"]) == 1

    discount_amount = discount * (1 if price_includes_tax else (1 + tax_rate))
    assert data["data"]["shopProducts"][0]["id"] == product.id

    assert data["data"]["shopProducts"][0]["product"]["name"] == product.name
    assert data["data"]["shopProducts"][0]["product"]["description"] == product.description
    assert data["data"]["shopProducts"][0]["product"]["slug"] == product.slug
    assert data["data"]["shopProducts"][0]["product"]["shortDescription"] == product.short_description

    assert data["data"]["shopProducts"][0]["product"]["salesUnit"]["id"] == product.sales_unit.id
    assert data["data"]["shopProducts"][0]["product"]["salesUnit"]["name"] == product.sales_unit.name
    assert data["data"]["shopProducts"][0]["product"]["salesUnit"]["symbol"] == product.sales_unit.symbol

    assert data["data"]["shopProducts"][0]["product"]["primaryImage"]["id"] == medias[1].id
    assert data["data"]["shopProducts"][0]["product"]["primaryImage"]["url"].endswith(medias[1].url)
    assert len(data["data"]["shopProducts"][0]["product"]["images"]) == len(medias)

    assert data["data"]["shopProducts"][0]["purchasable"]
    assert data["data"]["shopProducts"][0]["primaryImage"]["id"] == medias[0].id
    assert data["data"]["shopProducts"][0]["primaryImage"]["url"].endswith(medias[0].url)

    assert data["data"]["shopProducts"][0]["suppliers"][0]["id"] == supplier.id
    assert data["data"]["shopProducts"][0]["suppliers"][0]["name"] == supplier.name

    assert len(data["data"]["shopProducts"][0]["categories"]) == Category.objects.count()
    assert data["data"]["shopProducts"][0]["primaryCategory"]["id"] == Category.objects.first().id

    assert data["data"]["shopProducts"][0]["priceInfo"]["discountAmount"] == discount_amount
    assert data["data"]["shopProducts"][0]["priceInfo"]["discountRate"] == (discount / product_price)

    if price_includes_tax:
        assert data["data"]["shopProducts"][0]["priceInfo"]["basePrice"] == product_price
        assert data["data"]["shopProducts"][0]["priceInfo"]["taxfulPrice"] == product_price - discount
        assert data["data"]["shopProducts"][0]["priceInfo"]["price"] == product_price - discount
    else:
        discounted_base_price = product_price - discount
        taxful_price = product_price * (1 + tax_rate)
        price = discounted_base_price * (1 + tax_rate)
        assert data["data"]["shopProducts"][0]["priceInfo"]["taxfulPrice"] == price
        assert data["data"]["shopProducts"][0]["priceInfo"]["price"] == price
        assert data["data"]["shopProducts"][0]["priceInfo"]["basePrice"] == taxful_price
        assert data["data"]["shopProducts"][0]["priceInfo"]["taxfulBasePrice"] == taxful_price
        assert data["data"]["shopProducts"][0]["priceInfo"]["taxlessPrice"] == discounted_base_price
        assert data["data"]["shopProducts"][0]["priceInfo"]["taxlessBasePrice"] == product_price
        assert data["data"]["shopProducts"][0]["priceInfo"]["taxAmount"] == (discounted_base_price * tax_rate)
Пример #20
0
def test_many_price_info_cache_bump(rf):
    initial_price = 10
    shop = factories.get_default_shop()
    tax = factories.get_default_tax()
    tax_class = factories.get_default_tax_class()
    product = factories.create_product(
        "product",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=initial_price
    )
    child1 = factories.create_product(
        "child1",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=5
    )
    child2 = factories.create_product(
        "child2",
        shop=shop,
        supplier=factories.get_default_supplier(),
        default_price=9
    )
    child1.link_to_parent(product, variables={"color": "red"})
    child2.link_to_parent(product, variables={"color": "blue"})

    request = apply_request_middleware(rf.get("/"))
    child1_pi = child1.get_price_info(request)
    child2_pi = child1.get_price_info(request)

    def assert_cache_products():
        cache_many_price_info(request, product, 1, [child1_pi, child2_pi])
        assert get_many_cached_price_info(request, product, 1)[0].price == child1_pi.price
        assert get_many_cached_price_info(request, product, 1)[1].price == child2_pi.price

    def assert_nothing_is_cached():
        # nothing is cached
        assert get_many_cached_price_info(request, product, 1) is None

    # cache the item
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    tax.save()
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    tax_class.save()
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    product.save()
    assert_nothing_is_cached()
    assert_cache_products()

    shop_product = product.get_shop_instance(shop)

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.save()
    assert_nothing_is_cached()
    assert_cache_products()

    category = factories.get_default_category()

    # cache bumped - the cache should be dropped - then, cache again
    shop_product.categories.add(category)
    assert_nothing_is_cached()
    assert_cache_products()

    # cache bumped - the cache should be dropped - then, cache again
    supplier = shop_product.suppliers.first()
    supplier.enabled = False
    supplier.save()
    assert_nothing_is_cached()
    assert_cache_products()
Пример #21
0
def test_broken_order(admin_user):
    """"""
    quantities = [44, 23, 65]
    expected = sum(quantities) * 50
    expected_based_on = expected / 1.5

    # Shuup is calculating taxes per line so there will be some "errors"
    expected_based_on = ensure_decimal_places(
        Decimal("%s" % (expected_based_on + 0.01)))

    shop = get_default_shop()

    supplier = get_default_supplier()
    product1 = create_product("simple-test-product1", shop, supplier, 50)
    product2 = create_product("simple-test-product2", shop, supplier, 50)
    product3 = create_product("simple-test-product3", shop, supplier, 50)

    tax = get_default_tax()

    source = BasketishOrderSource(get_default_shop())
    billing_address = get_address(country="US")
    shipping_address = get_address(name="Test street", country="US")
    source.status = get_initial_order_status()
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = create_random_person()
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=product1,
        supplier=get_default_supplier(),
        quantity=quantities[0],
        base_unit_price=source.create_price(50),
    )

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=product2,
        supplier=get_default_supplier(),
        quantity=quantities[1],
        base_unit_price=source.create_price(50),
    )

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=product3,
        supplier=get_default_supplier(),
        quantity=quantities[2],
        base_unit_price=source.create_price(50),
    )

    currency = "EUR"
    summary = source.get_tax_summary()

    assert len(summary) == 1
    summary = summary[0]

    assert summary.taxful == Money(expected, "EUR")
    assert summary.based_on == Money(expected_based_on, "EUR")

    # originally non-rounded value
    assert bankers_round(source.get_total_tax_amount()) == summary.tax_amount

    assert source.taxless_total_price.value == expected_based_on
    assert summary.taxful.value == source.taxful_total_price.value

    assert summary.tax_amount == Money(
        bankers_round(source.taxful_total_price.value -
                      source.taxless_total_price.value), currency)
    assert summary.taxful == summary.raw_based_on + summary.tax_amount

    assert summary.tax_rate == tax.rate
    assert summary.taxful.value == (
        summary.based_on + summary.tax_amount).value - Decimal("%s" % 0.01)

    # create order from basket
    creator = OrderCreator()
    order = creator.create_order(source)
    assert order.taxless_total_price.value == expected_based_on

    # originally non-rounded value
    assert bankers_round(order.get_total_tax_amount()) == summary.tax_amount
Пример #22
0
def test_broken_order(admin_user):
    """
    """
    quantities = [44, 23, 65]
    expected = sum(quantities) * 50
    expected_based_on = expected / 1.5

    # Shuup is calculating taxes per line so there will be some "errors"
    expected_based_on = ensure_decimal_places(Decimal("%s" % (expected_based_on + 0.01)))

    shop = get_default_shop()

    supplier = get_default_supplier()
    product1 = create_product("simple-test-product1", shop, supplier, 50)
    product2 = create_product("simple-test-product2", shop, supplier, 50)
    product3 = create_product("simple-test-product3", shop, supplier, 50)

    tax = get_default_tax()

    source = BasketishOrderSource(get_default_shop())
    billing_address = get_address(country="US")
    shipping_address = get_address(name="Test street", country="US")
    source.status = get_initial_order_status()
    source.billing_address = billing_address
    source.shipping_address = shipping_address
    source.customer = create_random_person()
    source.payment_method = get_default_payment_method()
    source.shipping_method = get_default_shipping_method()

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=product1,
        supplier=get_default_supplier(),
        quantity=quantities[0],
        base_unit_price=source.create_price(50),
    )

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=product2,
        supplier=get_default_supplier(),
        quantity=quantities[1],
        base_unit_price=source.create_price(50),
    )

    source.add_line(
        type=OrderLineType.PRODUCT,
        product=product3,
        supplier=get_default_supplier(),
        quantity=quantities[2],
        base_unit_price=source.create_price(50),
    )

    currency = "EUR"
    summary = source.get_tax_summary()


    assert len(summary) == 1
    summary = summary[0]

    assert summary.taxful == Money(expected, "EUR")
    assert summary.based_on == Money(expected_based_on, "EUR")

    # originally non-rounded value
    assert bankers_round(source.get_total_tax_amount()) == summary.tax_amount

    assert source.taxless_total_price.value == expected_based_on
    assert summary.taxful.value == source.taxful_total_price.value

    assert summary.tax_amount == Money(bankers_round(source.taxful_total_price.value - source.taxless_total_price.value), currency)
    assert summary.taxful == summary.raw_based_on + summary.tax_amount

    assert summary.tax_rate == tax.rate
    assert summary.taxful.value == (summary.based_on + summary.tax_amount).value - Decimal("%s" % 0.01)

    # create order from basket
    creator = OrderCreator()
    order = creator.create_order(source)
    assert order.taxless_total_price.value == expected_based_on

    # originally non-rounded value
    assert bankers_round(order.get_total_tax_amount()) == summary.tax_amount