Esempio n. 1
0
def initialize_simple_report(cls, data_overrides={}):
    product_price = 100
    product_count = 2
    tax_rate = Decimal("0.10")
    line_count = 1
    expected_taxful_total, expected_taxless_total, shop, order = initialize_report_test(
        product_price, product_count, tax_rate, line_count)
    data = {
        "report": cls.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.THIS_YEAR,
        "writer": "json",
        "force_download": 1,
    }
    data.update(data_overrides)

    report = cls(**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"))
    return InfoTest(
        **{
            "expected_taxful_total": expected_taxful_total,
            "expected_taxless_total": expected_taxless_total,
            "json_data": json_data,
            "product_count": product_count,
            "shop": shop,
            "writer": writer,
            "report": report,
            "order": order,
        })
Esempio n. 2
0
def test_total_sales_customers_report(rf):
    shop = get_default_shop()
    supplier = get_default_supplier()
    p1 = create_product("p1", shop=shop, supplier=supplier, default_price="5")
    p2 = create_product("p2", shop=shop, supplier=supplier, default_price="20")

    # orders for person 1
    person1 = create_random_person()
    order1 = create_random_order(customer=person1,
                                 completion_probability=1,
                                 products=[p1, p2])
    order2 = create_random_order(customer=person1,
                                 completion_probability=1,
                                 products=[p1, p2])

    # orders for person 2
    person2 = create_random_person()
    order3 = create_random_order(customer=person2,
                                 completion_probability=1,
                                 products=[p1, p2])
    order4 = create_random_order(customer=person2,
                                 completion_probability=1,
                                 products=[p1, p2])
    order5 = create_random_order(customer=person2,
                                 completion_probability=1,
                                 products=[p1, p2])

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

    data = {
        "report": TotalSales.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
    }
    report = TotalSales(**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(TotalSales.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")[0]

    avg_sales = (order1.taxful_total_price + order2.taxful_total_price +
                 order3.taxful_total_price + order4.taxful_total_price +
                 order5.taxful_total_price) / Decimal(5)

    assert int(data["customers"]) == 2
    assert int(data["order_amount"]) == 5
    assert data["customer_avg_sale"] == str(
        avg_sales.value.quantize(Decimal('0.01')))
Esempio n. 3
0
def test_excel_writer(rf):
    expected_taxful_total, expected_taxless_total, shop, order = initialize_report_test(10, 1, 0, 1)
    data = {
        "report": SalesTestReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.THIS_YEAR,
        "writer": "excel",
        "force_download": 1,
    }
    report = SalesTestReport(**data)
    writer = get_writer_instance(data["writer"])
    assert str(writer) == data["writer"]
    rendered_report = writer.get_rendered_output()
    assert rendered_report is not None
Esempio n. 4
0
def test_html_writer(rf):
    expected_taxful_total, expected_taxless_total, shop, order = initialize_report_test(10, 1, 0, 1)
    data = {
        "report": SalesTestReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.THIS_YEAR,
        "writer": "html",
        "force_download": 1,
    }
    report = SalesTestReport(**data)
    writer = get_writer_instance(data["writer"])
    assert str(writer) == data["writer"]
    rendered_report = writer.render_report(report=report)

    soup = BeautifulSoup(rendered_report)
    assert force_text(SalesTestReport.title) in str(soup)
    assert str(expected_taxless_total) in str(soup)
    assert str(expected_taxful_total) in str(soup)

    rendered_report = writer.render_report(report=report, inline=True)
    assert type(rendered_report) == SafeText
    assert force_text(SalesTestReport.title) in rendered_report
    assert str(expected_taxless_total) in rendered_report
    assert str(expected_taxful_total) in rendered_report
Esempio n. 5
0
def test_coupons_usage_report(rf):
    shop = get_default_shop()
    tax_class = get_default_tax_class()
    creator = OrderCreator()

    coupon1 = Coupon.objects.create(code="coupon1", active=True)
    coupon2 = Coupon.objects.create(code="coupon2", active=True)

    campaign1 = get_default_campaign(coupon1, "10")
    campaign2 = get_default_campaign(coupon2, "25")

    source1 = seed_source(coupon1)
    source2 = seed_source(coupon1)
    source3 = seed_source(coupon1)
    source4 = seed_source(coupon2)

    creator.create_order(source1)
    creator.create_order(source2)
    creator.create_order(source3)
    creator.create_order(source4)

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

    data = {
        "report": CouponsUsageReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
    }
    report = CouponsUsageReport(**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(CouponsUsageReport.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")
    assert len(data) == Order.objects.count()

    expected_data = []

    orders = Order.objects.all().order_by("order_date")
    for order in orders:
        discount = order.shop.create_price(0)
        for dt in order.lines.discounts():
            discount += dt.taxful_price

        expected_data.append({
            "date":
            format_date(order.order_date, locale=get_current_babel_locale()),
            "coupon":
            order.codes[0],
            "order":
            str(order),
            "taxful_total":
            str(order.taxful_total_price.as_rounded().value),
            "taxful_subtotal":
            str((order.taxful_total_price - discount).as_rounded().value),
            "total_discount":
            str(discount.as_rounded().value)
        })

    assert len(expected_data) == len(data)

    for ix, d in enumerate(data):
        for k, v in d.items():
            assert expected_data[ix][k] == v
Esempio n. 6
0
def test_refunds_report(rf):
    shop = get_default_shop()
    get_default_tax_class()
    creator = OrderCreator()

    source1 = seed_source()
    source2 = seed_source()
    source3 = seed_source()
    source4 = seed_source()

    order1 = creator.create_order(source1)
    order2 = creator.create_order(source2)
    order3 = creator.create_order(source3)
    order4 = creator.create_order(source4)

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

    order1.create_full_refund()
    order2.create_refund([{
        "line":
        order2.lines.first(),
        "amount":
        order2.taxful_total_price.amount * Decimal(0.5),
        "quantity":
        1
    }])
    order3.create_refund([{
        "line":
        order3.lines.first(),
        "amount":
        order3.taxful_total_price.amount * Decimal(0.3),
        "quantity":
        1
    }])
    order4.create_refund([{
        "line":
        order4.lines.first(),
        "amount":
        order4.taxful_total_price.amount * Decimal(0.1),
        "quantity":
        1
    }])

    total_refunded = (order1.get_total_refunded_amount() +
                      order2.get_total_refunded_amount() +
                      order3.get_total_refunded_amount() +
                      order4.get_total_refunded_amount())

    data = {
        "report": RefundedSalesReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
    }
    report = RefundedSalesReport(**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(RefundedSalesReport.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")
    assert len(data) == 1
    data = data[0]

    expected_data = {
        "refunded_orders": "4",
        "total_refunded": str(total_refunded.value)
    }

    for k, v in expected_data.items():
        assert data[k] == v
Esempio n. 7
0
def test_shipping_report(rf):
    shop = get_default_shop()
    tax_class = get_default_tax_class()
    creator = OrderCreator()

    carrier1 = CustomCarrier.objects.create(name="Carrier1")
    sm1 = carrier1.create_service(None,
                                  shop=get_default_shop(),
                                  enabled=True,
                                  tax_class=tax_class,
                                  name="SM #1")
    sm1.behavior_components.add(
        FixedCostBehaviorComponent.objects.create(price_value=Decimal(10)))
    sm2 = carrier1.create_service(None,
                                  shop=get_default_shop(),
                                  enabled=True,
                                  tax_class=tax_class,
                                  name="SM #2")
    sm2.behavior_components.add(
        FixedCostBehaviorComponent.objects.create(price_value=Decimal(99)))
    sm2.behavior_components.add(
        FixedCostBehaviorComponent.objects.create(price_value=Decimal(4)))

    carrier2 = CustomCarrier.objects.create(name="Carrier2")
    sm3 = carrier2.create_service(None,
                                  shop=get_default_shop(),
                                  enabled=True,
                                  tax_class=tax_class,
                                  name="SM #3")
    sm3.behavior_components.add(
        FixedCostBehaviorComponent.objects.create(price_value=Decimal(5)))

    source1 = seed_source(sm1)
    source2 = seed_source(sm1)
    source3 = seed_source(sm2)
    source4 = seed_source(sm3)

    creator.create_order(source1)
    creator.create_order(source2)
    creator.create_order(source3)
    creator.create_order(source4)

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

    data = {
        "report": ShippingReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
    }
    report = ShippingReport(**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(ShippingReport.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")
    assert len(data) == 3

    expected_result = [
        {
            "carrier":
            carrier1.name,
            "shipping_method":
            sm1.name,
            "order_count":
            2,
            "total_charged":
            sum([bc.price_value
                 for bc in sm1.behavior_components.all()]) * 2  # 2 orders
        },
        {
            "carrier":
            carrier1.name,
            "shipping_method":
            sm2.name,
            "order_count":
            1,
            "total_charged":
            sum([bc.price_value for bc in sm2.behavior_components.all()])
        },
        {
            "carrier":
            carrier2.name,
            "shipping_method":
            sm3.name,
            "order_count":
            1,
            "total_charged":
            sum([bc.price_value for bc in sm3.behavior_components.all()])
        }
    ]

    for ix, shipping in enumerate(data):
        assert shipping["carrier"] == expected_result[ix]["carrier"]
        assert shipping["shipping_method"] == expected_result[ix][
            "shipping_method"]
        assert shipping["order_count"] == str(
            expected_result[ix]["order_count"])
        assert shipping["total_charged"] == str(
            expected_result[ix]["total_charged"].quantize(Decimal("0.01")))
Esempio n. 8
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"])
Esempio n. 9
0
def test_customer_sales_report(rf, order_by):
    shop = get_default_shop()
    supplier = get_default_supplier()
    product1 = create_product("p1", shop=shop, supplier=supplier)
    product2 = create_product("p2", shop=shop, supplier=supplier)
    product3 = create_product("p3", shop=shop, supplier=supplier)
    tax_rate = Decimal("0.3")

    # 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_rate,
                                       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",
                                       n_lines=1,
                                       shop=shop)
    order2.customer = person1
    order2.save()

    person1_taxful_total_sales = (order1.taxful_total_price +
                                  order2.taxful_total_price)
    person1_taxless_total_sales = (order1.taxless_total_price +
                                   order2.taxless_total_price)
    person1_avg_sales = (person1_taxful_total_sales / Decimal(2.0))

    # orders for person 2
    person2 = create_random_person()
    order3 = create_order_with_product(product=product1,
                                       supplier=supplier,
                                       quantity=2,
                                       taxless_base_unit_price="5",
                                       tax_rate=tax_rate,
                                       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="50",
                                       n_lines=1,
                                       shop=shop)
    order4.customer = person2
    order4.save()

    order5 = create_order_with_product(product=product3,
                                       supplier=supplier,
                                       quantity=2,
                                       taxless_base_unit_price="20",
                                       tax_rate=tax_rate,
                                       n_lines=1,
                                       shop=shop)
    order5.customer = person2
    order5.save()
    person2_taxful_total_sales = (order3.taxful_total_price +
                                  order4.taxful_total_price +
                                  order5.taxful_total_price)
    person2_taxless_total_sales = (order3.taxless_total_price +
                                   order4.taxless_total_price +
                                   order5.taxless_total_price)
    person2_avg_sales = (person2_taxful_total_sales / Decimal(3.0)).quantize(
        Decimal('0.01'))

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

    data = {
        "report": CustomerSalesReport.get_name(),
        "shop": shop.pk,
        "date_range": DateRangeChoices.ALL_TIME,
        "writer": "json",
        "force_download": 1,
        "order_by": order_by
    }
    report = CustomerSalesReport(**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(CustomerSalesReport.title) in json_data.get("heading")
    data = json_data.get("tables")[0].get("data")

    assert len(data) == 2

    if order_by == "order_count":
        person1_data = data[1]
        person2_data = data[0]

    elif order_by == "average_sales":
        if person1_avg_sales > person2_avg_sales:
            person1_data = data[0]
            person2_data = data[1]
        else:
            person1_data = data[1]
            person2_data = data[0]

    elif order_by == "taxless_total":
        if person1_taxless_total_sales > person2_taxless_total_sales:
            person1_data = data[0]
            person2_data = data[1]
        else:
            person1_data = data[1]
            person2_data = data[0]

    elif order_by == "taxful_total":
        if person1_taxful_total_sales > person2_taxful_total_sales:
            person1_data = data[0]
            person2_data = data[1]
        else:
            person1_data = data[1]
            person2_data = data[0]

    assert person1_data["customer"] == person1.name
    assert person1_data["order_count"] == "2"
    assert person1_data["average_sales"] == str(person1_avg_sales.value)
    assert person1_data["taxless_total"] == str(
        person1_taxless_total_sales.value.quantize(Decimal("0.01")))
    assert person1_data["taxful_total"] == str(
        person1_taxful_total_sales.value.quantize(Decimal("0.01")))

    assert person2_data["customer"] == person2.name
    assert person2_data["order_count"] == "3"
    assert person2_data["average_sales"] == str(person2_avg_sales.value)
    assert person2_data["taxless_total"] == str(
        person2_taxless_total_sales.value.quantize(Decimal("0.01")))
    assert person2_data["taxful_total"] == str(
        person2_taxful_total_sales.value.quantize(Decimal("0.01")))