def get_total(self): return Money(self.total, self.currency or settings.DEFAULT_CURRENCY)
def test_discount_as_negative_for_html(): discount = Money(10, "USD") result = discount_as_negative(discount, True) assert result == '-<span class="currency">$</span>10.00'
def money(self): return Money(fake.pydecimal(2, 2, positive=True), settings.DEFAULT_CURRENCY)
def test_manager_apply_taxes_to_shipping(shipping_method, address, plugins, price_amount): expected_price = Money(price_amount, "USD") taxed_price = ExtensionsManager(plugins=plugins).apply_taxes_to_shipping( shipping_method.price, address) assert TaxedMoney(expected_price, expected_price) == taxed_price
def test_discount_as_negative(): discount = Money(10, "USD") result = discount_as_negative(discount) assert result == "-$10.00"
def sum_order_totals(qs, currency_code): zero = Money(0, currency=currency_code) taxed_zero = TaxedMoney(zero, zero) return sum([order.total for order in qs], taxed_zero)
def test_no_items(): generator = enhanced_purchase('trans-01', [], Money(0, 'USD'), '/cart/') with pytest.raises(ValueError): list(generator)
def test_format_money(): money = Money("123.99", "USD") assert format_money(money) == "$123.99"
def test_append_checkout_details_tax_included( mocked_checkout_line_total, mocked_plugins_manager, dummy_payment_data, payment_dummy, checkout_ready_to_complete, ): # given net = Money(100, "USD") gross = Money(123, "USD") # tax 23 % mocked_checkout_line_total.return_value = quantize_price( TaxedMoney(net=net, gross=gross), "USD") manager = get_plugins_manager() unit_price = mock.MagicMock() unit_price.return_value = quantize_price(TaxedMoney(net=net, gross=gross), "USD") manager.calculate_checkout_line_unit_price = unit_price mocked_plugins_manager.return_value = manager country_code = checkout_ready_to_complete.get_country() checkout_ready_to_complete.payments.add(payment_dummy) line = checkout_ready_to_complete.lines.first() payment_data = { "reference": "test", } # when result = append_checkout_details(dummy_payment_data, payment_data) # then expected_result = { "reference": "test", "shopperLocale": get_shopper_locale_value(country_code), "countryCode": country_code, "lineItems": [ { "description": f"{line.variant.product.name}, {line.variant.name}", "quantity": line.quantity, "id": line.variant.sku, "taxAmount": price_to_minor_unit((gross - net).amount, "USD"), "taxPercentage": 2300, "amountExcludingTax": price_to_minor_unit(net.amount, "USD"), "amountIncludingTax": price_to_minor_unit(gross.amount, "USD"), }, { "amountExcludingTax": "1000", "amountIncludingTax": "1000", "description": "Shipping - DHL", "id": f"Shipping:{checkout_ready_to_complete.shipping_method.id}", "quantity": 1, "taxAmount": "0", "taxPercentage": 0, }, ], } assert result == expected_result
def get_captured_price(self): return TaxedMoney( net=Money(self.captured_amount, self.currency), gross=Money(self.captured_amount, self.currency))
def sum_task_totals(qs): zero = Money(0, currency=settings.DEFAULT_CURRENCY) taxed_zero = TaxedMoney(zero, zero) return sum([task.total for task in qs], taxed_zero)
def get_total_price(self): return TaxedMoney( net=Money(self.total - self.tax, self.currency), gross=Money(self.total, self.currency))
def test_format_money(): money = Money('123.99', 'USD') assert format_money(money) == '$123.99'
def get_amount(self): return Money(self.amount, self.currency or settings.DEFAULT_CURRENCY)
def test_get_default_order_payload(order_line): order_line.refresh_from_db() order = order_line.order order_line_payload = get_order_line_payload(order_line) redirect_url = "http://redirect.com/path" subtotal = order.get_subtotal() order.total = subtotal + order.shipping_price tax = order.total_gross_amount - order.total_net_amount value = Decimal("20") discount = partial(fixed_discount, discount=Money(value, order.currency)) order.undiscounted_total = order.total order.total = discount(order.total) order.discounts.create( value_type=DiscountValueType.FIXED, value=value, reason="Discount reason", amount=(order.undiscounted_total - order.total).gross, # type: ignore ) order.save() payload = get_default_order_payload(order, redirect_url) assert payload == { "discounts": [{ "amount_value": Decimal("20.000"), "name": None, "reason": "Discount reason", "translated_name": None, "type": "manual", "value": Decimal("20.000"), "value_type": "fixed", }], "channel_slug": order.channel.slug, "id": order.id, "number": order.id, "token": order.token, "created": str(order.created), "display_gross_prices": order.display_gross_prices, "currency": order.currency, "total_gross_amount": order.total_gross_amount, "total_net_amount": order.total_net_amount, "shipping_method_name": order.shipping_method_name, "collection_point_name": order.collection_point_name, "status": order.status, "metadata": order.metadata, "private_metadata": order.private_metadata, "shipping_price_net_amount": order.shipping_price_net_amount, "shipping_price_gross_amount": order.shipping_price_gross_amount, "order_details_url": f"{redirect_url}?token={order.token}", "email": order.get_customer_email(), "subtotal_gross_amount": subtotal.gross.amount, "subtotal_net_amount": subtotal.net.amount, "tax_amount": tax, "lines": [order_line_payload], "billing_address": get_address_payload(order.billing_address), "shipping_address": get_address_payload(order.shipping_address), "language_code": order.language_code, "discount_amount": Decimal("20.000"), "undiscounted_total_gross_amount": order.undiscounted_total.gross.amount, "undiscounted_total_net_amount": order.undiscounted_total.net.amount, "voucher_discount": None, }
def money(amount): return Money(amount, 'USD')
def sum_order_totals(qs): zero = Money(0, currency=settings.DEFAULT_CURRENCY) taxed_zero = TaxedMoney(zero, zero) return sum([order.total for order in qs], taxed_zero)
net=Money(min_price["net"], min_price["currency"]), gross=Money(min_price["gross"], min_price["currency"]), ) max_price = picker_data["availability"]["priceRange"]["maxPrice"] max_price = TaxedMoney( net=Money(max_price["net"], max_price["currency"]), gross=Money(max_price["gross"], max_price["currency"]), ) assert min_price == start assert max_price == stop @pytest.mark.parametrize("price, cost", [(Money("0", "USD"), Money("1", "USD")), (Money("2", "USD"), None)]) def test_costs_get_margin_for_variant(variant, price, cost): variant.cost_price = cost variant.price_override = price assert not get_margin_for_variant(variant) @pytest.mark.parametrize( "value, error", ( ({ 123: [] }, ["The key 123 should be of type str (got <class 'int'>)"]), ( {
) tax_rate = PluginsManager(plugins=plugins).get_checkout_line_tax_rate( checkout_with_item, checkout_line_info, checkout_with_item.shipping_address, [discount_info], unit_price, ) assert tax_rate == Decimal("0.08") @pytest.mark.parametrize( "unit_price, expected_tax_rate", [ (TaxedMoney(Money(12, "USD"), Money(15, "USD")), Decimal("0.25")), (Decimal("0.0"), Decimal("0.0")), ], ) def test_manager_get_checkout_line_tax_rate_no_plugins(checkout_with_item, discount_info, unit_price, expected_tax_rate): line = checkout_with_item.lines.all()[0] variant = line.variant checkout_line_info = CheckoutLineInfo( line=line, variant=variant, channel_listing=variant.channel_listings.first(), product=variant.product, collections=[],
def test_variant_picker_data_price_range(product_type, category): product = models.Product.objects.create(product_type=product_type, category=category, price=Money("15.00", "USD")) product.variants.create(sku="1") product.variants.create(sku="2", price_override=Money("20.00", "USD")) product.variants.create(sku="3", price_override=Money("11.00", "USD")) start = TaxedMoney(net=Money("11.00", "USD"), gross=Money("11.00", "USD")) stop = TaxedMoney(net=Money("20.00", "USD"), gross=Money("20.00", "USD")) picker_data = get_variant_picker_data(product, discounts=None, local_currency=None) min_price = picker_data["availability"]["priceRange"]["minPrice"] min_price = TaxedMoney( net=Money(min_price["net"], min_price["currency"]), gross=Money(min_price["gross"], min_price["currency"]), ) max_price = picker_data["availability"]["priceRange"]["maxPrice"] max_price = TaxedMoney( net=Money(max_price["net"], max_price["currency"]), gross=Money(max_price["gross"], max_price["currency"]), ) assert min_price == start assert max_price == stop
def test_manager_calculates_order_line(order_line, plugins, amount): currency = order_line.unit_price.currency expected_price = Money(amount, currency) unit_price = ExtensionsManager( plugins=plugins).calculate_order_line_unit(order_line) assert expected_price == unit_price.gross
def voucher_with_high_min_spent_amount(): return Voucher.objects.create(code="mirumee", discount_value=10, min_spent=Money(1000000, "USD"))
VoucherCustomer, ) from ..templatetags.voucher import discount_as_negative from ..utils import ( add_voucher_usage_by_customer, decrease_voucher_usage, get_product_discount_on_sale, increase_voucher_usage, remove_voucher_usage_by_customer, validate_voucher, ) @pytest.mark.parametrize( "min_spent_amount, value", [(Money(5, "USD"), Money(10, "USD")), (Money(10, "USD"), Money(10, "USD"))], ) def test_valid_voucher_min_spent_amount(min_spent_amount, value, channel_USD): voucher = Voucher.objects.create( code="unique", type=VoucherType.SHIPPING, discount_value_type=DiscountValueType.FIXED, ) VoucherChannelListing.objects.create( voucher=voucher, channel=channel_USD, discount=Money(10, "USD"), min_spent=min_spent_amount, )
def gift_card_used(staff_user): return GiftCard.objects.create( code="gift_card_used", initial_balance=Money(150, "USD"), current_balance=Money(100, "USD"), )
def test_discount_as_negative_for_zero_value(): discount = Money(0, "USD") result = discount_as_negative(discount) assert result == "$0.00"
def gift_card_created_by_staff(staff_user): return GiftCard.objects.create( code="mirumee_staff", initial_balance=Money(5, "USD"), current_balance=Money(5, "USD"), )
def set_field_as_money(defaults, field): amount_field = f"{field}_amount" if amount_field in defaults and defaults[amount_field] is not None: defaults[field] = Money(defaults[amount_field], settings.DEFAULT_CURRENCY)
def order_with_lines(order, product_type, category, shipping_zone): product = Product.objects.create( name="Test product", price=Money("10.00", "USD"), product_type=product_type, category=category, ) variant = ProductVariant.objects.create( product=product, sku="SKU_A", cost_price=Money(1, "USD"), quantity=5, quantity_allocated=3, ) net = variant.get_price() gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency) order.lines.create( product_name=str(variant.product), variant_name=str(variant), product_sku=variant.sku, is_shipping_required=variant.is_shipping_required(), quantity=3, variant=variant, unit_price=TaxedMoney(net=net, gross=gross), tax_rate=23, ) product = Product.objects.create( name="Test product 2", price=Money("20.00", "USD"), product_type=product_type, category=category, ) variant = ProductVariant.objects.create( product=product, sku="SKU_B", cost_price=Money(2, "USD"), quantity=2, quantity_allocated=2, ) net = variant.get_price() gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency) order.lines.create( product_name=str(variant.product), variant_name=str(variant), product_sku=variant.sku, is_shipping_required=variant.is_shipping_required(), quantity=2, variant=variant, unit_price=TaxedMoney(net=net, gross=gross), tax_rate=23, ) order.shipping_address = order.billing_address.get_copy() method = shipping_zone.shipping_methods.get() order.shipping_method_name = method.name order.shipping_method = method net = method.get_total() gross = Money(amount=net.amount * Decimal(1.23), currency=net.currency) order.shipping_price = TaxedMoney(net=net, gross=gross) order.save() recalculate_order(order) order.refresh_from_db() return order
def money(amount): return Money(amount, "USD")
from unittest.mock import Mock import pytest from measurement.measures import Weight from prices import Money, TaxedMoney from saleor.core.utils import format_money from saleor.core.utils.taxes import get_taxed_shipping_price from saleor.shipping.models import (ShippingMethod, ShippingMethodType, ShippingZone) from .utils import money @pytest.mark.parametrize( 'price, charge_taxes, expected_price', [(Money(10, 'RUB'), False, TaxedMoney(net=Money(10, 'RUB'), gross=Money(10, 'RUB'))), (Money(10, 'RUB'), True, TaxedMoney(net=Money('8.13', 'RUB'), gross=Money(10, 'RUB')))]) def test_get_taxed_shipping_price(site_settings, vatlayer, price, charge_taxes, expected_price): site_settings.charge_taxes_on_shipping = charge_taxes site_settings.save() shipping_price = get_taxed_shipping_price(price, taxes=vatlayer) assert shipping_price == expected_price def test_shipping_get_total(monkeypatch, shipping_zone, vatlayer): method = shipping_zone.shipping_methods.get()