def get_price_info(self, context, product, quantity=1): shop = context.shop if isinstance(product, six.integer_types): product_id = product shop_product = ShopProduct.objects.get(product_id=product_id, shop=shop) else: shop_product = product.get_shop_instance(shop) product_id = product.pk base_price = (shop_product.default_price_value or 0) * quantity period_modifiers = self.get_period_modifiers(product_id, quantity, context.start_date, context.end_date) per_person_modifiers = self.get_per_person_modifiers( product, context.persons) * quantity total_price = base_price + period_modifiers + per_person_modifiers price_info = PriceInfo( price=shop.create_price(total_price), base_price=shop.create_price(total_price), quantity=quantity, ) # Add some custom information to PriceInfo before returning it price_info.period_modifiers = Decimal(period_modifiers) price_info.per_person_modifiers = Decimal(per_person_modifiers) return price_info
def get_effective_price_info(self, source, **kwargs): """ Get price of this method for given OrderSource. :param source: source object :type source: shoop.core.order_creator.OrderSource :param kwargs: Other kwargs for future expansion :rtype: shoop.core.pricing.PriceInfo """ price_value = self.get_options().get("price", 0) normal_price = source.shop.create_price(price_value) if self._is_price_waived(source): return PriceInfo(source.shop.create_price(0), normal_price, 1) return PriceInfo(normal_price, normal_price, 1)
def test_repr(): pi1 = PriceInfo(price('0.3'), price(42), Decimal('1.3')) r1 = ( "PriceInfo(" "TaxlessPrice('0.3', 'EUR'), TaxlessPrice('42', 'EUR'), Decimal('1.3')" ")") assert repr(pi1) == r1 pi2 = PriceInfo(price('1.3'), price(42), Decimal('1.3'), expires_on=1483272000) r2 = ("PriceInfo(" "TaxlessPrice('1.3', 'EUR'), TaxlessPrice('42', 'EUR')," " Decimal('1.3'), expires_on=1483272000)") assert repr(pi2) == r2
def plus(pi1, pi2): assert pi1.quantity == pi2.quantity return PriceInfo( pi1.price + pi2.price, pi1.base_price + pi2.base_price, quantity=pi1.quantity, )
def get_price_info(self, context, product, quantity=1): shop = context.shop if isinstance(product, six.integer_types): product_id = product shop_product = ShopProduct.objects.get(product_id=product_id, shop=shop) else: shop_product = product.get_shop_instance(shop) product_id = product.pk default_price = (shop_product.default_price_value or 0) result = (DiscountedProductPrice.objects.filter( product=product_id, shop=shop).order_by("price_value")[:1].values_list("price_value", flat=True)) price = (min(result[0], default_price) if result else default_price) return PriceInfo( price=shop.create_price(price * quantity), base_price=shop.create_price(default_price * quantity), quantity=quantity, )
def get_price_info(self, context, product, quantity=1): shop = context.shop if isinstance(product, six.integer_types): product_id = product shop_product = ShopProduct.objects.get(product_id=product_id, shop=shop) else: shop_product = product.get_shop_instance(shop) product_id = product.pk default_price = (shop_product.default_price_value or 0) if context.customer_group_ids: filter = Q(price_value__gt=0, product=product_id, shop=shop, group__in=context.customer_group_ids) result = (SimpleProductPrice.objects.filter(filter).order_by( "price_value")[:1].values_list("price_value", flat=True)) else: result = None if result: price = result[0] if default_price > 0: price = min([default_price, price]) else: price = default_price return PriceInfo( price=shop.create_price(price * quantity), base_price=shop.create_price(price * quantity), quantity=quantity, )
def test_quantity_not_one_with_discounts(): pi = PriceInfo(price('27.5'), price(100), Decimal(2.5)) assert pi.price == price('27.5') assert pi.base_price == price(100) assert pi.discount_amount == price('72.5') assert pi.discount_percentage == Decimal('72.5') assert pi.discounted_unit_price == price(11) assert pi.base_unit_price == price(40) assert pi.unit_discount_amount == price(29)
def test_quantity_not_one_without_discounts(): pi = PriceInfo(price(123), price(123), 10) assert pi.price == price(123) assert pi.base_price == price(123) assert pi.discount_amount == price(0) assert pi.discount_percentage == 0 assert pi.discounted_unit_price == price('12.3') assert pi.base_unit_price == price('12.3') assert pi.unit_discount_amount == price(0)
def get_price_info(self, context, product, quantity=1): """ Return a `PriceInfo` calculated from `ShopProduct.default_price` Since `ShopProduct.default_price` can be `None` it will be set to zero (0) if `None`. """ shop_product = ShopProduct.objects.get(product=product, shop=context.shop) default_price = (shop_product.default_price or 0) price_cls = (TaxfulPrice if context.shop.prices_include_tax else TaxlessPrice) return PriceInfo(price=price_cls(default_price), base_price=price_cls(default_price))
def get_price_info(self, context, product, quantity=1): """ Return a `PriceInfo` calculated from `ShopProduct.default_price` Since `ShopProduct.default_price` can be `None` it will be set to zero (0) if `None`. """ shop = context.shop shop_product = ShopProduct.objects.get(product=product, shop=shop) default_price = (shop_product.default_price_value or 0) return PriceInfo( price=shop.create_price(default_price * quantity), base_price=shop.create_price(default_price * quantity), quantity=quantity, )
def _sum_costs(costs, source): """ Sum price info of given costs and return the sum as PriceInfo. :type costs: Iterable[ServiceCost] :type source: shoop.core.order_creator.OrderSource :rtype: PriceInfo """ def plus(pi1, pi2): assert pi1.quantity == pi2.quantity return PriceInfo( pi1.price + pi2.price, pi1.base_price + pi2.base_price, quantity=pi1.quantity, ) zero_price = source.create_price(0) zero_pi = PriceInfo(zero_price, zero_price, quantity=1) return functools.reduce(plus, (x.price_info for x in costs), zero_pi)
def get_price_info(self, context, product, quantity=1): if isinstance(product, six.integer_types): product_id = product shop_product = ShopProduct.objects.get(product_id=product_id, shop_id=context.shop.pk) else: shop_product = product.get_shop_instance(context.shop) product_id = product.pk default_price = (shop_product.default_price or 0) includes_tax = context.shop.prices_include_tax if context.customer_group_ids: filter = Q(price__gt=0, product=product_id, shop=context.shop, group__in=context.customer_group_ids) result = (SimpleProductPrice.objects.filter(filter).order_by( "price")[:1].values_list("price", flat=True)) else: result = None if result: price = result[0] if default_price > 0: price = min([default_price, price]) else: price = default_price base_price = price price_cls = (TaxfulPrice if includes_tax else TaxlessPrice) return PriceInfo(price=price_cls(price), base_price=price_cls(base_price))
def price_info(self): return PriceInfo(self.price, self.base_price, quantity=1)
def get_effective_price_info(self, source, **kwargs): four = source.shop.create_price('4.00') five = source.shop.create_price('5.00') if source.shipping_address and source.shipping_address.country == "SE": return PriceInfo(five, four, 1) return PriceInfo(four, four, 1)
def _get_price_info(shop, product=None, quantity=2): if not product: product = Product(sku='6.0745') # SKU of product defines the price :) price = shop.create_price(product.sku) return PriceInfo(quantity * price, quantity * 4 * price, quantity)
def _get_price_info(price_cls, quantity=2): price = quantity * price_cls(240, 'USD') base_price = quantity * price_cls(330, 'USD') return PriceInfo(price, base_price, quantity)
def test_discount_percentage_special_cases(): pi1 = PriceInfo(price(10), price(0), quantity=1) assert pi1.discount_percentage == 0
def test_no_discount(): pi = PriceInfo(price(100), price(100), 1) assert not pi.is_discounted assert pi.discount_percentage == 0 assert pi.discount_amount == price(0)
def test_with_discounts(): pi = PriceInfo(price(75), price(100), 1) assert pi.is_discounted assert pi.discount_percentage == 25 assert pi.discount_amount == price(25)