def test_calculate_checkout_shipping(
    reset_sequences,  # pylint: disable=unused-argument
    checkout_with_item,
    shipping_zone,
    discount_info,
    address_usa_tx,
    address,
    site_settings,
    plugin_configuration,
):
    plugin_configuration()
    manager = get_plugins_manager()
    site_settings.company_address = address
    site_settings.save()

    checkout_with_item.shipping_address = address_usa_tx
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    lines = fetch_checkout_lines(checkout_with_item)
    checkout_info = fetch_checkout_info(checkout_with_item, lines,
                                        [discount_info], manager)
    shipping_price = manager.calculate_checkout_shipping(
        checkout_info, lines, address, [discount_info])
    shipping_price = quantize_price(shipping_price, shipping_price.currency)
    assert shipping_price == TaxedMoney(net=Money("10.00", "USD"),
                                        gross=Money("10.00", "USD"))
def test_calculate_checkout_total(
    reset_sequences,  # pylint: disable=unused-argument
    with_discount,
    expected_net,
    expected_gross,
    voucher_amount,
    taxes_in_prices,
    checkout_with_item,
    product_with_single_variant,
    discount_info,
    shipping_zone,
    address_usa_tx,
    address_usa,
    site_settings,
    monkeypatch,
    plugin_configuration,
    non_default_category,
):
    plugin_configuration()
    # Required ATE variant data
    metadata = {
        get_metadata_key("UnitQuantity"): 1,
    }
    ProductVariant.objects.filter(sku="SKU_SINGLE_VARIANT").update(
        sku="202127000", private_metadata=metadata)
    monkeypatch.setattr(
        "saleor.plugins.avatax.excise.plugin.AvataxExcisePlugin._skip_plugin",
        lambda *_: False,
    )
    manager = get_plugins_manager()
    checkout_with_item.shipping_address = address_usa_tx
    checkout_with_item.save()
    site_settings.company_address = address_usa
    site_settings.include_taxes_in_prices = taxes_in_prices
    site_settings.save()

    voucher_amount = Money(voucher_amount, "USD")
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.discount = voucher_amount
    checkout_with_item.save()

    product_with_single_variant.charge_taxes = False
    product_with_single_variant.category = non_default_category
    product_with_single_variant.save()
    discounts = [discount_info] if with_discount else None
    discount_amount = Decimal("1.00") if with_discount else Decimal("0.00")
    checkout_with_item.discount_amount = discount_amount
    checkout_info = fetch_checkout_info(checkout_with_item, [], discounts,
                                        manager)

    add_variant_to_checkout(checkout_info,
                            product_with_single_variant.variants.get())

    lines = fetch_checkout_lines(checkout_with_item)
    total = manager.calculate_checkout_total(checkout_info, lines,
                                             address_usa_tx, discounts)
    total = quantize_price(total, total.currency)
    assert total == TaxedMoney(net=Money(expected_net, "USD"),
                               gross=Money(expected_gross, "USD"))
def test_calculate_checkout_total_skip(skip_mock, checkout_with_item,
                                       address_usa, plugin_configuration):
    skip_mock.return_value = True
    plugin_configuration()
    manager = get_plugins_manager()
    checkout_info = fetch_checkout_info(checkout_with_item, [], [], manager)
    manager.calculate_checkout_total(checkout_info, [], [], [])
    skip_mock.assert_called_once
def test_calculate_checkout_total_invalid_checkout(checkout_with_item,
                                                   address_usa,
                                                   plugin_configuration):
    plugin_configuration()
    manager = get_plugins_manager()
    checkout_info = fetch_checkout_info(checkout_with_item, [], [], manager)
    total = manager.calculate_checkout_total(checkout_info, [], [], [])
    assert total == TaxedMoney(net=Money("0.00", "USD"),
                               gross=Money("0.00", "USD"))
def test_calculate_checkout_line_total(
    reset_sequences,  # pylint: disable=unused-argument
    with_discount,
    expected_net,
    expected_gross,
    taxes_in_prices,
    discount_info,
    checkout_with_item,
    address_usa_tx,
    address_usa,
    site_settings,
    shipping_zone,
    plugin_configuration,
):
    plugin_configuration()
    manager = get_plugins_manager()

    checkout_with_item.shipping_address = address_usa_tx
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    site_settings.company_address = address_usa
    site_settings.include_taxes_in_prices = taxes_in_prices
    site_settings.save()
    line = checkout_with_item.lines.first()
    product = line.variant.product
    product.metadata = {}  # TODO consider adding ATE fields here
    product.charge_taxes = True
    product.save()
    product.product_type.save()

    discounts = [discount_info] if with_discount else None
    discount_amount = Decimal("1.00") if with_discount else Decimal("0.00")
    checkout_with_item.discount_amount = discount_amount

    lines = fetch_checkout_lines(checkout_with_item)
    checkout_info = fetch_checkout_info(checkout_with_item, lines, discounts,
                                        manager)
    checkout_line_info = lines[0]

    total = manager.calculate_checkout_line_total(
        checkout_info,
        lines,
        checkout_line_info,
        checkout_with_item.shipping_address,
        discounts,
    )
    total = quantize_price(total, total.currency)
    assert total == TaxedMoney(net=Money(expected_net, "USD"),
                               gross=Money(expected_gross, "USD"))
def test_calculate_checkout_line_total_metadata(
    reset_sequences,  # pylint: disable=unused-argument
    with_discount,
    expected_net,
    expected_gross,
    taxes_in_prices,
    discount_info,
    checkout_with_item,
    address_usa_tx,
    address_usa,
    site_settings,
    shipping_zone,
    plugin_configuration,
):
    plugin_configuration()
    manager = get_plugins_manager()

    checkout_with_item.shipping_address = address_usa_tx
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    site_settings.company_address = address_usa
    site_settings.include_taxes_in_prices = taxes_in_prices
    site_settings.save()
    line = checkout_with_item.lines.first()
    product = line.variant.product
    product.metadata = {}
    product.save()
    product.product_type.save()
    discounts = [discount_info] if with_discount else None

    lines = fetch_checkout_lines(checkout_with_item)
    checkout_info = fetch_checkout_info(checkout_with_item, lines, discounts,
                                        manager)
    checkout_line_info = lines[0]

    manager.calculate_checkout_line_total(
        checkout_info,
        lines,
        checkout_line_info,
        checkout_with_item.shipping_address,
        discounts,
    )

    checkout = Checkout.objects.filter(token=line.checkout.token).first()
    assert (
        checkout.metadata[get_metadata_key("itemized_taxes")] ==
        '[{"TransactionTaxAmounts": [], "SequenceId": 1, "TransactionLine": 1, "InvoiceLine": 1, "CountryCode": "USA", "Jurisdiction": "TX", "LocalJurisdiction": "48", "ProductCategory": 0.0, "TaxingLevel": "STA", "TaxType": "S", "RateType": "G", "RateSubtype": "NONE", "CalculationTypeInd": "P", "TaxRate": 0.0625, "TaxQuantity": 0.0, "TaxAmount": 1.73, "TaxExemptionInd": "N", "SalesTaxBaseAmount": 27.71, "LicenseNumber": "", "RateDescription": "TX STATE TAX - TEXAS", "Currency": "USD", "SubtotalInd": "C", "StatusCode": "ACTIVE", "QuantityInd": "B"}, {"TransactionTaxAmounts": [], "SequenceId": 2, "TransactionLine": 1, "InvoiceLine": 1, "CountryCode": "USA", "Jurisdiction": "TX", "LocalJurisdiction": "05000", "ProductCategory": 0.0, "TaxingLevel": "CIT", "TaxType": "S", "RateType": "G", "RateSubtype": "NONE", "CalculationTypeInd": "P", "TaxRate": 0.01, "TaxQuantity": 0.0, "TaxAmount": 0.28, "TaxExemptionInd": "N", "SalesTaxBaseAmount": 27.71, "LicenseNumber": "", "RateDescription": "TX CITY TAX - AUSTIN", "Currency": "USD", "SubtotalInd": "C", "StatusCode": "ACTIVE", "QuantityInd": "B"}, {"TransactionTaxAmounts": [], "SequenceId": 3, "TransactionLine": 1, "InvoiceLine": 1, "CountryCode": "USA", "Jurisdiction": "TX", "LocalJurisdiction": "6000814", "ProductCategory": 0.0, "TaxingLevel": "STJ", "TaxType": "S", "RateType": "G", "RateSubtype": "NONE", "CalculationTypeInd": "P", "TaxRate": 0.01, "TaxQuantity": 0.0, "TaxAmount": 0.28, "TaxExemptionInd": "N", "SalesTaxBaseAmount": 27.71, "LicenseNumber": "", "RateDescription": "TX SPECIAL TAX - AUSTIN MTA TRANSIT", "Currency": "USD", "SubtotalInd": "C", "StatusCode": "ACTIVE", "QuantityInd": "B"}]'
    )
def test_preprocess_order_creation_wrong_data(
    checkout_with_item,
    address,
    shipping_zone,
    plugin_configuration,
):
    plugin_configuration()

    manager = get_plugins_manager()

    checkout_with_item.shipping_address = address
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    discounts = []
    checkout_info = fetch_checkout_info(checkout_with_item, [], discounts,
                                        manager)
    lines = fetch_checkout_lines(checkout_with_item)

    with pytest.raises(TaxError) as e:
        manager.preprocess_order_creation(checkout_info, discounts, lines)
    # Fails due to no ATE scenario these from/to addresses
    assert "No Scenario record found" in e._excinfo[1].args[0]
def test_preprocess_order_creation(
    checkout_with_item,
    address,
    address_usa_tx,
    site_settings,
    shipping_zone,
    discount_info,
    plugin_configuration,
):
    plugin_configuration()
    manager = get_plugins_manager()
    site_settings.company_address = address
    site_settings.save()

    checkout_with_item.shipping_address = address_usa_tx
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    discounts = [discount_info]
    lines = fetch_checkout_lines(checkout_with_item)
    checkout_info = fetch_checkout_info(checkout_with_item, lines, discounts,
                                        manager)
    manager.preprocess_order_creation(checkout_info, discounts, lines)
Пример #9
0
def test_calculate_checkout_shipping(
    reset_sequences,  # pylint: disable=unused-argument
    checkout_with_item,
    shipping_zone,
    discount_info,
    address_usa_va,
    address,
    site_settings,
    plugin_configuration,
):
    plugin_configuration()
    manager = get_plugins_manager()
    site_settings.company_address = address
    site_settings.save()

    checkout_with_item.shipping_address = address_usa_va
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    lines, _ = fetch_checkout_lines(checkout_with_item)
    checkout_info = fetch_checkout_info(checkout_with_item, lines,
                                        [discount_info], manager)
    shipping_price = manager.calculate_checkout_shipping(
        checkout_info, lines, address, [discount_info])
    shipping_price = quantize_price(shipping_price, shipping_price.currency)
    assert shipping_price == TaxedMoney(net=Money("10.00", "USD"),
                                        gross=Money("10.00", "USD"))

    checkout_with_item.refresh_from_db()
    taxes_metadata = checkout_with_item.metadata.get(
        get_metadata_key("itemized_taxes"))

    sales_tax = checkout_with_item.metadata.get(get_metadata_key("sales_tax"))
    other_tax = checkout_with_item.metadata.get(get_metadata_key("other_tax"))

    assert taxes_metadata is not None
    assert len(taxes_metadata) > 0
    assert sales_tax >= 0
    assert other_tax >= 0
def test_calculate_checkout_total_excise_data(
    reset_sequences,  # pylint: disable=unused-argument
    expected_net,
    expected_gross,
    taxes_in_prices,
    variant_sku,
    price,
    destination,
    metadata,
    checkout,
    product,
    shipping_zone,
    address_usa,
    site_settings,
    monkeypatch,
    plugin_configuration,
    cigar_product_type,
):
    plugin_configuration()
    monkeypatch.setattr(
        "saleor.plugins.avatax.excise.plugin.AvataxExcisePlugin._skip_plugin",
        lambda *_: False,
    )
    manager = get_plugins_manager()

    address_usa.city = destination["city"]
    address_usa.postal_code = destination["postal_code"]
    address_usa.country_area = destination["country_area"]
    address_usa.save()

    checkout.shipping_address = address_usa
    checkout.billing_address = address_usa
    shipping_method = shipping_zone.shipping_methods.get()
    shipping_method.price_amount = 0
    shipping_method.save()
    checkout.shipping_method = shipping_method

    metadata = {
        get_metadata_key("UnitQuantity"): metadata["UnitQuantity"],
        get_metadata_key("CustomNumeric1"): metadata["CustomNumeric1"],
        get_metadata_key("CustomNumeric2"): metadata["CustomNumeric2"],
        get_metadata_key("CustomNumeric3"): metadata["CustomNumeric3"],
    }

    product.product_type = cigar_product_type
    product.save()

    variant = product.variants.get()
    variant.sku = variant_sku
    variant.private_metadata = metadata
    variant.price_amount = Decimal(price)
    variant.save()
    checkout_info = fetch_checkout_info(checkout, [], [], manager)
    add_variant_to_checkout(checkout_info, variant, 1)
    checkout.save()

    site_settings.company_address = address_usa
    site_settings.include_taxes_in_prices = taxes_in_prices
    site_settings.save()

    lines = fetch_checkout_lines(checkout)
    total = manager.calculate_checkout_total(checkout_info, lines, address_usa,
                                             [])
    total = quantize_price(total, total.currency)
    assert total == TaxedMoney(net=Money(expected_net, "USD"),
                               gross=Money(expected_gross, "USD"))
Пример #11
0
def test_calculate_checkout_total(
    reset_sequences,  # pylint: disable=unused-argument
    with_discount,
    expected_net,
    expected_gross,
    voucher_amount,
    taxes_in_prices,
    checkout_with_item,
    product_with_single_variant,
    discount_info,
    shipping_zone,
    address_usa_va,
    address_usa,
    site_settings,
    monkeypatch,
    plugin_configuration,
    non_default_category,
):
    plugin_configuration()
    monkeypatch.setattr(
        "saleor.plugins.avatax.excise.plugin.AvataxExcisePlugin._skip_plugin",
        lambda *_: False,
    )
    manager = get_plugins_manager()
    checkout_with_item.shipping_address = address_usa_va
    checkout_with_item.save()

    checkout_variant = checkout_with_item.lines.first().variant
    checkout_variant.sku = "202015500"
    checkout_variant.save(update_fields=["sku"])

    site_settings.company_address = address_usa
    site_settings.include_taxes_in_prices = taxes_in_prices
    site_settings.save()

    voucher_amount = Money(voucher_amount, "USD")
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.discount = voucher_amount
    checkout_with_item.save()

    product_with_single_variant.charge_taxes = False
    product_with_single_variant.category = non_default_category
    product_with_single_variant.save()

    variant = product_with_single_variant.variants.first()
    variant.sku = "202165300"
    variant.save(update_fields=["sku"])

    discounts = [discount_info] if with_discount else None
    checkout_info = fetch_checkout_info(checkout_with_item, [], discounts,
                                        manager)

    add_variant_to_checkout(checkout_info,
                            product_with_single_variant.variants.get())

    lines, _ = fetch_checkout_lines(checkout_with_item)
    total = manager.calculate_checkout_total(checkout_info, lines,
                                             address_usa_va, discounts)
    total = quantize_price(total, total.currency)
    assert total == TaxedMoney(net=Money(expected_net, "USD"),
                               gross=Money(expected_gross, "USD"))
Пример #12
0
def test_calculate_checkout_line_total(
    reset_sequences,  # pylint: disable=unused-argument
    with_discount,
    expected_net,
    expected_gross,
    taxes_in_prices,
    discount_info,
    checkout_with_item,
    address_usa_va,
    address_usa,
    site_settings,
    shipping_zone,
    plugin_configuration,
):
    plugin_configuration()
    manager = get_plugins_manager()

    checkout_with_item.shipping_address = address_usa_va
    checkout_with_item.shipping_method = shipping_zone.shipping_methods.get()
    checkout_with_item.save()
    site_settings.company_address = address_usa
    site_settings.include_taxes_in_prices = taxes_in_prices
    site_settings.save()
    line = checkout_with_item.lines.first()
    product = line.variant.product
    product.metadata = {}  # TODO consider adding ATE fields here
    product.charge_taxes = True
    product.save()
    product.product_type.save()

    discounts = [discount_info] if with_discount else None

    lines, _ = fetch_checkout_lines(checkout_with_item)
    checkout_info = fetch_checkout_info(checkout_with_item, lines, discounts,
                                        manager)

    total = manager.calculate_checkout_line_total(
        checkout_info,
        lines,
        lines[0],
        checkout_with_item.shipping_address,
        discounts,
    )
    price_with_discounts = total.price_with_discounts
    price_with_discounts = quantize_price(price_with_discounts,
                                          price_with_discounts.currency)
    assert price_with_discounts == TaxedMoney(net=Money(expected_net, "USD"),
                                              gross=Money(
                                                  expected_gross, "USD"))

    checkout_with_item.refresh_from_db()
    taxes_metadata = checkout_with_item.metadata.get(
        get_metadata_key("itemized_taxes"))

    sales_tax = checkout_with_item.metadata.get(get_metadata_key("sales_tax"))
    other_tax = checkout_with_item.metadata.get(get_metadata_key("other_tax"))

    assert taxes_metadata is not None
    assert len(taxes_metadata) > 0
    assert sales_tax >= 0
    assert other_tax >= 0