def test_price_infos(rf): request, shop, group = initialize_test(rf, True) price = shop.create_price product_one = create_product("Product_1", shop, default_price=150) product_two = create_product("Product_2", shop, default_price=250) spp = CgpPrice(product=product_one, shop=shop, group=group, price_value=100) spp.save() spp = CgpPrice(product=product_two, shop=shop, group=group, price_value=200) spp.save() product_ids = [product_one.pk, product_two.pk] spm = get_pricing_module() assert isinstance(spm, CustomerGroupPricingModule) pricing_context = spm.get_context_from_request(request) price_infos = spm.get_price_infos(pricing_context, product_ids) assert len(price_infos) == 2 assert product_one.pk in price_infos assert product_two.pk in price_infos assert price_infos[product_one.pk].price == price(100) assert price_infos[product_two.pk].price == price(200) assert price_infos[product_one.pk].base_price == price(100) assert price_infos[product_two.pk].base_price == price(200)
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)
def get_price_info(shop, customer, product, quantity): """ Get price info of given product for given context parameters. :type shop: wshop.core.models.Shop :type customer: wshop.core.models.Contact :type product: wshop.core.models.Product :type quantity: numbers.Number """ pricing_mod = get_pricing_module() pricing_ctx = pricing_mod.get_context_from_data( shop=shop, customer=(customer or AnonymousContact()), ) return product.get_price_info(pricing_ctx, quantity=quantity)
def _get_custom_order(regular_user, **kwargs): prices_include_tax = kwargs.pop("prices_include_tax", False) include_basket_campaign = kwargs.pop("include_basket_campaign", False) include_catalog_campaign = kwargs.pop("include_catalog_campaign", False) shop = get_shop(prices_include_tax=prices_include_tax) supplier = get_simple_supplier() if include_basket_campaign: _add_basket_campaign(shop) if include_catalog_campaign: _add_catalog_campaign(shop) _add_taxes() contact = get_person_contact(regular_user) source = BasketishOrderSource(shop) source.status = get_initial_order_status() source.customer = contact ctx = get_pricing_module().get_context_from_data(shop, contact) for product_data in _get_product_data(): quantity = product_data.pop("quantity") product = create_product(sku=product_data.pop("sku"), shop=shop, supplier=supplier, stock_behavior=StockBehavior.STOCKED, tax_class=get_default_tax_class(), **product_data) shop_product = product.get_shop_instance(shop) shop_product.categories.add(get_default_category()) shop_product.save() supplier.adjust_stock(product.id, INITIAL_PRODUCT_QUANTITY) pi = product.get_price_info(ctx) source.add_line(type=OrderLineType.PRODUCT, product=product, supplier=supplier, quantity=quantity, base_unit_price=pi.base_unit_price, discount_amount=pi.discount_amount) oc = OrderCreator() order = oc.create_order(source) return order
def _get_order(prices_include_tax=False, include_basket_campaign=False, include_catalog_campaign=False): shop = get_shop(prices_include_tax=prices_include_tax) supplier = get_simple_supplier() if include_basket_campaign: _add_basket_campaign(shop) if include_catalog_campaign: _add_catalog_campaign(shop) _add_taxes() source = BasketishOrderSource(shop) source.status = get_initial_order_status() ctx = get_pricing_module().get_context_from_data(shop, AnonymousContact()) for product_data in _get_product_data(): quantity = product_data.pop("quantity") product = create_product( sku=product_data.pop("sku"), shop=shop, supplier=supplier, stock_behavior=StockBehavior.STOCKED, tax_class=get_default_tax_class(), **product_data) shop_product = product.get_shop_instance(shop) shop_product.categories.add(get_default_category()) shop_product.save() supplier.adjust_stock(product.id, INITIAL_PRODUCT_QUANTITY) pi = product.get_price_info(ctx) source.add_line( type=OrderLineType.PRODUCT, product=product, supplier=supplier, quantity=quantity, base_unit_price=pi.base_unit_price, discount_amount=pi.discount_amount ) oc = OrderCreator() order = oc.create_order(source) order.create_payment(Money("1", "EUR")) assert not order.has_refunds() assert order.can_create_refund() assert order.shipping_status == ShippingStatus.NOT_SHIPPED assert order.payment_status == PaymentStatus.PARTIALLY_PAID return order
def test_pricing_module_is_active(): """ Make sure that our custom pricing module is active. """ shop = Shop(currency='USD', prices_include_tax=False) customer = AnonymousContact() product = Product(sku='6.0745') pricing_mod = get_pricing_module() pricing_ctx = pricing_mod.get_context_from_data(shop, customer) pi = product.get_price_info(pricing_ctx, quantity=2) price = shop.create_price assert pi.price == price('12.149') assert pi.base_price == price('48.596') assert pi.quantity == 2 assert pi.discounted_unit_price == price('6.0745') assert pi.base_unit_price == price('24.298') assert pi.discount_rate == Decimal('0.75')
def _get_pricing_context(shop, customer=None): return get_pricing_module().get_context_from_data( shop=shop, customer=(customer or AnonymousContact()), )
def test_module_is_active( ): # this test is because we want to make sure `CustomerGroupPricing` is active module = get_pricing_module() assert isinstance(module, DefaultPricingModule)
def test_module_is_active(): """ Check that CustomerGroupPricingModule is active. """ module = get_pricing_module() assert isinstance(module, CustomerGroupPricingModule)