Ejemplo n.º 1
0
def test_taxes_report(rf):
    shop = get_default_shop()
    supplier = get_default_supplier(shop)
    product1 = create_product("p1", shop=shop, supplier=supplier)
    product2 = create_product("p2", shop=shop, supplier=supplier)
    create_product("p3", shop=shop, supplier=supplier)
    tax_rate1 = Decimal("0.3")
    tax_rate2 = Decimal("0.45")

    tax_rate1_instance = get_test_tax(tax_rate1)
    tax_rate2_instance = get_test_tax(tax_rate2)

    # orders for person 1
    person1 = create_random_person()
    order1 = create_order_with_product(product=product1,
                                       supplier=supplier,
                                       quantity=2,
                                       taxless_base_unit_price="5",
                                       tax_rate=tax_rate1,
                                       n_lines=1,
                                       shop=shop)
    order1.customer = person1
    order1.save()
    order2 = create_order_with_product(product=product2,
                                       supplier=supplier,
                                       quantity=1,
                                       taxless_base_unit_price="10",
                                       tax_rate=tax_rate1,
                                       n_lines=1,
                                       shop=shop)
    order2.customer = person1
    order2.save()

    # orders for person 2
    person2 = create_random_person()
    order3 = create_order_with_product(product=product1,
                                       supplier=supplier,
                                       quantity=1,
                                       taxless_base_unit_price="2",
                                       tax_rate=tax_rate2,
                                       n_lines=1,
                                       shop=shop)
    order3.customer = person2
    order3.save()

    order4 = create_order_with_product(product=product2,
                                       supplier=supplier,
                                       quantity=2,
                                       taxless_base_unit_price="8",
                                       tax_rate=tax_rate1,
                                       n_lines=1,
                                       shop=shop)
    order4.customer = person2
    order4.save()

    # pay orders
    [o.create_payment(o.taxful_total_price) for o in Order.objects.all()]

    data = {
        "report": TaxesReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
    }
    report = TaxesReport(**data)
    writer = get_writer_instance(data["writer"])
    response = writer.get_response(report=report)
    if hasattr(response, "render"):
        response.render()
    json_data = json.loads(response.content.decode("utf-8"))
    assert force_text(TaxesReport.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")
    assert len(data) == 2

    tax1_rate1_total = (
        (order1.taxful_total_price_value - order1.taxless_total_price_value) +
        (order2.taxful_total_price_value - order2.taxless_total_price_value) +
        (order4.taxful_total_price_value - order4.taxless_total_price_value))
    tax1_pretax_total = (order1.taxless_total_price_value +
                         order2.taxless_total_price_value +
                         order4.taxless_total_price_value)
    tax1_total = (order1.taxful_total_price_value +
                  order2.taxful_total_price_value +
                  order4.taxful_total_price_value)
    tax2_rate2_total = (order3.taxful_total_price_value -
                        order3.taxless_total_price_value)

    # the report data order is the total charged ascending
    expected_result = [{
        "tax": tax_rate2_instance.name,
        "tax_rate": tax_rate2,
        "order_count": 1,
        "total_pretax_amount": order3.taxless_total_price_value,
        "total_tax_amount": tax2_rate2_total,
        "total": order3.taxful_total_price_value,
    }, {
        "tax": tax_rate1_instance.name,
        "tax_rate": tax_rate1,
        "order_count": 3,
        "total_pretax_amount": tax1_pretax_total,
        "total_tax_amount": tax1_rate1_total,
        "total": tax1_total,
    }]
    for ix, tax in enumerate(data):
        assert tax["tax"] == expected_result[ix]["tax"]
        assert Decimal(tax["tax_rate"]
                       ) == expected_result[ix]["tax_rate"] * Decimal(100.0)
        assert tax["order_count"] == str(expected_result[ix]["order_count"])
        assert tax["total_tax_amount"] == str(
            expected_result[ix]["total_tax_amount"])
        assert tax["total_pretax_amount"] == str(
            expected_result[ix]["total_pretax_amount"])
        assert tax["total"] == str(expected_result[ix]["total"])
Ejemplo n.º 2
0
def test_taxes_report(rf):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product1 = create_product("p1", shop=shop, supplier=supplier)
    product2 = create_product("p2", shop=shop, supplier=supplier)
    create_product("p3", shop=shop, supplier=supplier)
    tax_rate1 = Decimal("0.3")
    tax_rate2 = Decimal("0.45")

    tax_rate1_instance = get_test_tax(tax_rate1)
    tax_rate2_instance = get_test_tax(tax_rate2)

    # orders for person 1
    person1 = create_random_person()
    order1 = create_order_with_product(product=product1, supplier=supplier, quantity=2,
                                       taxless_base_unit_price="5", tax_rate=tax_rate1, n_lines=1, shop=shop)
    order1.customer = person1
    order1.save()
    order2 = create_order_with_product(product=product2, supplier=supplier, quantity=1,
                                       taxless_base_unit_price="10", tax_rate=tax_rate1, n_lines=1, shop=shop)
    order2.customer = person1
    order2.save()

    # orders for person 2
    person2 = create_random_person()
    order3 = create_order_with_product(product=product1, supplier=supplier, quantity=1,
                                       taxless_base_unit_price="2", tax_rate=tax_rate2, n_lines=1, shop=shop)
    order3.customer = person2
    order3.save()

    order4 = create_order_with_product(product=product2, supplier=supplier, quantity=2,
                                       taxless_base_unit_price="8", tax_rate=tax_rate1, n_lines=1, shop=shop)
    order4.customer = person2
    order4.save()

    # pay orders
    [o.create_payment(o.taxful_total_price) for o in Order.objects.all()]

    data = {
        "report": TaxesReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
    }
    report = TaxesReport(**data)
    writer = get_writer_instance(data["writer"])
    response = writer.get_response(report=report)
    if hasattr(response, "render"):
        response.render()
    json_data = json.loads(response.content.decode("utf-8"))
    assert force_text(TaxesReport.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")
    assert len(data) == 2

    tax1_rate1_total = (
        (order1.taxful_total_price_value - order1.taxless_total_price_value) +
        (order2.taxful_total_price_value - order2.taxless_total_price_value) +
        (order4.taxful_total_price_value - order4.taxless_total_price_value)
    )
    tax1_pretax_total = (
        order1.taxless_total_price_value +
        order2.taxless_total_price_value +
        order4.taxless_total_price_value
    )
    tax1_total = (
        order1.taxful_total_price_value +
        order2.taxful_total_price_value +
        order4.taxful_total_price_value
    )
    tax2_rate2_total = (order3.taxful_total_price_value - order3.taxless_total_price_value)

    # the report data order is the total charged ascending
    expected_result = [
        {
            "tax": tax_rate2_instance.name,
            "tax_rate": tax_rate2,
            "order_count": 1,
            "total_pretax_amount": order3.taxless_total_price_value,
            "total_tax_amount": tax2_rate2_total,
            "total": order3.taxful_total_price_value,
        },
        {
            "tax": tax_rate1_instance.name,
            "tax_rate": tax_rate1,
            "order_count": 3,
            "total_pretax_amount": tax1_pretax_total,
            "total_tax_amount": tax1_rate1_total,
            "total": tax1_total,
        }
    ]
    for ix, tax in enumerate(data):
        assert tax["tax"] == expected_result[ix]["tax"]
        assert Decimal(tax["tax_rate"]) == expected_result[ix]["tax_rate"] * Decimal(100.0)
        assert tax["order_count"] == str(expected_result[ix]["order_count"])
        assert tax["total_tax_amount"] == str(expected_result[ix]["total_tax_amount"])
        assert tax["total_pretax_amount"] == str(expected_result[ix]["total_pretax_amount"])
        assert tax["total"] == str(expected_result[ix]["total"])