示例#1
0
 def plus(pi1, pi2):
     assert pi1.quantity == pi2.quantity
     return PriceInfo(
         pi1.price + pi2.price,
         pi1.base_price + pi2.base_price,
         quantity=pi1.quantity,
     )
示例#2
0
 def get_price_info_mock(context, product, quantity=1):
     if context.customer.get_default_group() == AnonymousContact(
     ).get_default_group():
         price = context.shop.create_price(anonymous_price)
     else:
         price = context.shop.create_price(customer_price)
     return PriceInfo(quantity * price, quantity * price, quantity)
示例#3
0
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
示例#4
0
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)
示例#5
0
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)
示例#6
0
    def get_price_info(self, context, product, quantity=1):
        shop = context.shop

        try:
            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
        except ShopProduct.DoesNotExist:
            # shop product does not exist, zero price
            return PriceInfo(price=shop.create_price(0),
                             base_price=shop.create_price(0),
                             quantity=quantity)

        default_price = (shop_product.default_price_value or 0)

        filter = Q(product=product_id,
                   shop=shop,
                   price_value__gt=0,
                   group__in=context.customer.groups.all())
        result = (CgpPrice.objects.filter(filter).order_by("price_value")
                  [:1].values_list("price_value", flat=True))

        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,
        )
示例#7
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 = 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,
        )
示例#8
0
def _sum_costs(costs, source):
    """
    Sum price info of given costs and return the sum as PriceInfo.

    :type costs: Iterable[ServiceCost]
    :type source: wshop.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)
示例#9
0
 def price_info(self):
     return PriceInfo(self.price, self.base_price, quantity=1)
示例#10
0
def test_discount_percentage_special_cases():
    pi1 = PriceInfo(price(10), price(0), quantity=1)
    assert pi1.discount_percentage == 0
示例#11
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)
示例#12
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)
示例#13
0
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)
示例#14
0
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)