示例#1
0
    def discount_price(self, context, product, price_info):
        shop = context.shop
        basket = getattr(context, "basket", None)
        potential_discounts = get_potential_discounts_for_product(
            context,
            product).values_list("discounted_price_value",
                                 "discount_amount_value",
                                 "discount_percentage", "coupon_code__code")

        discounted_prices = []
        for discounted_price_value, discount_amount_value, discount_percentage, coupon_code in potential_discounts:
            if basket and coupon_code and not CouponCode.is_usable(
                    shop, coupon_code, customer=basket.customer):
                # TODO: Revise! This will cause some queries. Are do we want those? Maybe some cache for this check?
                # Maybe somewhere we should just remove coupon codes that is not usable from basket all together?
                continue

            if discounted_price_value:  # Applies the new product price per item
                discounted_prices.append(
                    min(
                        price_info.price,
                        max(
                            shop.create_price(discounted_price_value) *
                            price_info.quantity, shop.create_price(0))))

            if discount_amount_value:  # Discount amount value per item
                discounted_prices.append(
                    max(
                        price_info.price -
                        shop.create_price(discount_amount_value) *
                        price_info.quantity, shop.create_price(0)))

            if discount_percentage:  # Discount percentage per item
                discounted_prices.append(
                    max(
                        price_info.price -
                        price_info.price * discount_percentage,
                        shop.create_price(0)))

        if discounted_prices:
            minimum_price_values = list(
                ShopProduct.objects.filter(product_id=product.pk,
                                           shop=shop).values_list(
                                               "minimum_price_value",
                                               flat=True))

            minimum_price_value = minimum_price_values[
                0] if minimum_price_values else 0

            price_info.price = max(
                min(discounted_prices),
                shop.create_price(minimum_price_value or 0)
                or shop.create_price(0))

        price_expiration = get_price_expiration(context, product)
        if price_expiration and (not price_info.expires_on
                                 or price_expiration < price_info.expires_on):
            price_info.expires_on = price_expiration

        return price_info
示例#2
0
文件: modules.py 项目: ruqaiya/shuup
    def discount_price(self, context, product, price_info):
        shop = context.shop
        basket = getattr(context, "basket", None)
        potential_discounts = get_potential_discounts_for_product(context, product).values_list(
            "discounted_price_value", "discount_amount_value", "discount_percentage", "coupon_code__code"
        )

        discounted_prices = []
        for discounted_price_value, discount_amount_value, discount_percentage, coupon_code in potential_discounts:
            if basket and coupon_code and not CouponCode.is_usable(shop, coupon_code, customer=basket.customer):
                # TODO: Revise! This will cause some queries. Are do we want those? Maybe some cache for this check?
                # Maybe somewhere we should just remove coupon codes that is not usable from basket all together?
                continue

            if discounted_price_value:  # Applies the new product price per item
                discounted_prices.append(
                    min(
                        price_info.price,
                        max(
                            shop.create_price(discounted_price_value) * price_info.quantity,
                            shop.create_price(0)
                        )
                    )
                )

            if discount_amount_value:  # Discount amount value per item
                discounted_prices.append(
                    max(
                        price_info.price - shop.create_price(discount_amount_value) * price_info.quantity,
                        shop.create_price(0)
                    )
                )

            if discount_percentage:  # Discount percentage per item
                discounted_prices.append(
                    max(
                        price_info.price - price_info.price * discount_percentage,
                        shop.create_price(0)
                    )
                )

        if discounted_prices:
            product_id = (product if isinstance(product, six.integer_types) else product.pk)
            minimum_price_values = list(ShopProduct.objects.filter(
                product_id=product_id, shop=shop).values_list("minimum_price_value", flat=True))

            minimum_price_value = minimum_price_values[0] if minimum_price_values else 0

            price_info.price = max(
                min(discounted_prices),
                shop.create_price(minimum_price_value or 0) or shop.create_price(0)
            )

        price_expiration = get_price_expiration(context, product)
        if price_expiration and (not price_info.expires_on or price_expiration < price_info.expires_on):
            price_info.expires_on = price_expiration

        return price_info
示例#3
0
文件: modules.py 项目: rrosajp/shuup
    def discount_price(self, context, product, price_info):
        shop = context.shop
        potential_discounts = get_potential_discounts_for_product(
            context, product).values_list(
                "discounted_price_value",
                "discount_amount_value",
                "discount_percentage",
            )

        discounted_prices = []
        for discounted_price_value, discount_amount_value, discount_percentage in potential_discounts:
            if discounted_price_value:  # Applies the new product price per item
                discounted_prices.append(
                    min(
                        price_info.price,
                        max(
                            shop.create_price(discounted_price_value) *
                            price_info.quantity, shop.create_price(0)),
                    ))

            if discount_amount_value:  # Discount amount value per item
                discounted_prices.append(
                    max(
                        price_info.price -
                        shop.create_price(discount_amount_value) *
                        price_info.quantity,
                        shop.create_price(0),
                    ))

            if discount_percentage:  # Discount percentage per item
                discounted_prices.append(
                    max(
                        price_info.price -
                        price_info.price * discount_percentage,
                        shop.create_price(0)))

        new_price_info = PriceInfo(
            price=price_info.price,
            base_price=price_info.base_price,
            quantity=price_info.quantity,
            expires_on=price_info.expires_on,
        )

        if discounted_prices:
            product_id = product if isinstance(
                product, six.integer_types) else product.pk
            minimum_price_values = list(
                ShopProduct.objects.filter(product_id=product_id,
                                           shop=shop).values_list(
                                               "minimum_price_value",
                                               flat=True))

            minimum_price_value = minimum_price_values[
                0] if minimum_price_values else 0
            new_price_info.price = max(
                min(discounted_prices),
                shop.create_price(minimum_price_value or 0)
                or shop.create_price(0))

        price_expiration = get_price_expiration(context, product)
        if price_expiration and (not price_info.expires_on
                                 or price_expiration < price_info.expires_on):
            new_price_info.expires_on = price_expiration

        return new_price_info