Exemplo n.º 1
0
def get_voucher_discount_info(benefit, price):
    """
    Get discount info that will describe the effect that benefit has on product price.

    Args:
        benefit (Benefit): Benefit provided by an applied voucher.
        price (Decimal): Product price.

    Returns:
        dict
    """

    if benefit and price > 0:
        benefit_value = float(benefit.value)
        price = float(price)
        if benefit.type == Benefit.PERCENTAGE:
            return {
                'discount_percentage':
                benefit_value,
                'discount_value':
                get_discount_value(discount_percentage=benefit_value,
                                   product_price=price),
                'is_discounted':
                True if benefit.value < 100 else False
            }
        else:
            discount_percentage = get_discount_percentage(
                discount_value=benefit_value, product_price=price)
            if discount_percentage > 100:
                discount_percentage = 100.00
                discount_value = price
            else:
                discount_percentage = discount_percentage
                discount_value = benefit_value
            return {
                'discount_percentage': discount_percentage,
                'discount_value': float(discount_value),
                'is_discounted': True if discount_percentage < 100 else False,
            }
    else:
        return {
            'discount_percentage': 0.00,
            'discount_value': 0.00,
            'is_discounted': False
        }
Exemplo n.º 2
0
def _get_basket_discount_value(basket, offer):
    """Calculate the discount value based on benefit type and value"""
    sum_basket_lines = basket.all_lines().aggregate(
        total=Sum('stockrecord__price_excl_tax'))['total'] or Decimal(0.0)
    # calculate discount value that will be covered by the offer
    benefit_type = get_benefit_type(offer.benefit)
    benefit_value = offer.benefit.value
    if benefit_type == Benefit.PERCENTAGE:
        discount_value = get_discount_value(float(offer.benefit.value),
                                            float(sum_basket_lines))
        discount_value = Decimal(discount_value)
    else:  # Benefit.FIXED
        # There is a possibility that the discount value could be greater than the sum of basket lines
        # ie, discount value is $100, basket lines are $75, in this case the full price of the basket lines
        # will be covered and learner will owe $0 to checkout.
        if benefit_value > sum_basket_lines:
            discount_value = sum_basket_lines
        else:
            discount_value = benefit_value
    return discount_value
Exemplo n.º 3
0
def _get_course_discount_value(basket, offer):
    """Calculate the discount value based on benefit type and value"""
    product = basket.lines.first().product
    seat = product.course.seat_products.get(id=product.id)
    stock_record = StockRecord.objects.get(product=seat,
                                           partner=product.course.partner)
    course_price = stock_record.price_excl_tax

    # calculate discount value that will be covered by the offer
    benefit_type = get_benefit_type(offer.benefit)
    benefit_value = offer.benefit.value
    if benefit_type == Benefit.PERCENTAGE:
        discount_value = get_discount_value(float(offer.benefit.value),
                                            float(course_price))
        discount_value = Decimal(discount_value)
    else:  # Benefit.FIXED
        # There is a possibility that the discount value could be greater than the course price
        # ie, discount value is $100, course price is $75, in this case the full price of the course will be covered
        # and learner will owe $0 to checkout.
        if benefit_value > course_price:
            discount_value = course_price
        else:
            discount_value = benefit_value
    return discount_value