def test_precision(self): test_amount = Price('1.01', currency='BTC') discount = percentage_discount(value=50, name='Half off') p = test_amount | discount self.assertEqual(p.net, decimal.Decimal('0.51')) self.assertEqual(p.net, decimal.Decimal('0.51')) self.assertEqual(p.currency, 'BTC')
def test_precision(self): test_amount = Price('1.01', currency='BTC') discount = percentage_discount(value=50, name='Half off') p = test_amount | discount self.assertEqual(p.net, decimal.Decimal('0.51')) self.assertEqual(p.net, decimal.Decimal('0.51')) self.assertEqual(p.currency, 'BTC')
def test_discount(self): test_amount = Price(100, currency='BTC') discount = percentage_discount(value=10, name='Ten percent off') p = test_amount | discount self.assertEqual(p.net, 90) self.assertEqual(p.gross, 90) self.assertEqual(p.currency, 'BTC')
def test_discount(self): test_amount = Price(100, currency='BTC') discount = percentage_discount(value=10, name='Ten percent off') p = test_amount | discount self.assertEqual(p.net, 90) self.assertEqual(p.gross, 90) self.assertEqual(p.currency, 'BTC')
def get_discount(self): if self.type == self.FIXED: discount_price = Price(net=self.value, currency=settings.DEFAULT_CURRENCY) return FixedDiscount(amount=discount_price, name=self.name) elif self.type == self.PERCENTAGE: return percentage_discount(value=self.value, name=self.name) raise NotImplementedError('Unknown discount type')
def get_discount(self): if self.type == DiscountValueType.FIXED: discount_price = Price(net=self.value, currency=settings.DEFAULT_CURRENCY) return FixedDiscount(amount=discount_price, name=self.name) elif self.type == DiscountValueType.PERCENTAGE: return percentage_discount(value=self.value, name=self.name) raise NotImplementedError('Unknown discount type')
def test_discount(): price = Price(Amount(100, 'BTC'), Amount(100, 'BTC')) discount = percentage_discount(value=10, name='Ten percent off') result = discount.apply(price) assert result.net == Amount(90, 'BTC') assert result.gross == Amount(90, 'BTC') price_range = PriceRange(price, price) result = discount.apply(price_range) assert result.min_price == Price(Amount(90, 'BTC'), Amount(90, 'BTC')) assert result.max_price == Price(Amount(90, 'BTC'), Amount(90, 'BTC'))
def get_fixed_discount_for(self, amount): if self.discount_value_type == DiscountValueType.FIXED: discount_price = Price( net=self.discount_value, currency=settings.DEFAULT_CURRENCY) discount = FixedDiscount( amount=discount_price, name=smart_text(self)) elif self.discount_value_type == DiscountValueType.PERCENTAGE: discount = percentage_discount( value=self.discount_value, name=smart_text(self)) fixed_discount_value = amount - discount.apply(amount) discount = FixedDiscount( amount=fixed_discount_value, name=smart_text(self)) else: raise NotImplementedError('Unknown discount value type') if discount.amount > amount: return FixedDiscount(amount, name=smart_text(self)) return discount
def get_fixed_discount_for(self, amount): if self.discount_value_type == DiscountValueType.FIXED: discount_price = Price(net=self.discount_value, currency=settings.DEFAULT_CURRENCY) discount = FixedDiscount(amount=discount_price, name=smart_text(self)) elif self.discount_value_type == DiscountValueType.PERCENTAGE: discount = percentage_discount(value=self.discount_value, name=smart_text(self)) fixed_discount_value = amount - discount.apply(amount) discount = FixedDiscount(amount=fixed_discount_value, name=smart_text(self)) else: raise NotImplementedError('Unknown discount value type') if discount.amount > amount: return FixedDiscount(amount, name=smart_text(self)) return discount
def get_fixed_discount_for(self, amount): if self.discount_value_type == self.DISCOUNT_VALUE_FIXED: discount_price = Price(net=self.discount_value, currency=settings.DEFAULT_CURRENCY) discount = FixedDiscount(amount=discount_price, name=smart_text(self)) elif self.discount_value_type == self.DISCOUNT_VALUE_PERCENTAGE: discount = percentage_discount(value=self.discount_value, name=smart_text(self)) fixed_discount_value = amount - discount.apply(amount) discount = FixedDiscount(amount=fixed_discount_value, name=smart_text(self)) else: raise NotImplementedError('Tipo de desconto desconhecido') if discount.amount > amount: return FixedDiscount(amount, name=smart_text(self)) else: return discount
def test_templatetag_discount_amount_for(): price = Price(30, currency='BTC') discount = percentage_discount(50) discount_amount = tags.discount_amount_for(discount, price) assert discount_amount == Price(-15, currency='BTC')
def compute_discount(price=None, tax=0, discount_amount=0, discount_percent=0, from_gross=True): """Apply a discount amount or percent on a price The computation can be net or gross based (gross as default) If both discount_amount and discount_percent are set, only computation on discount_amount will be done :param price: Price :type price: TaxedMoney :param tax: Tax (a value between 0 and 1) :type tax: int, float, decimal :param discount_amount: Discount amount (Must be a positive value) :type discount_amount: int, float, decimal :param discount_percent: Discount percent (Must be a value between 0 and 1) :type discount_percent: int, float, decimal :param from_gross: A boolean to force discount computation on gross price Default value is True :return: TaxedMoney from `prices` python package :rtype: TaxedMoney Example: >>> price = compute_price(gross=100, tax=0.2, currency='EUR') >>> price >>> TaxedMoney(net=Money('83.33', 'EUR'), gross=Money('100.00', 'EUR')) # Discount amount 10 EUR on gross price >>> compute_discount(price=price, tax=0.2, discount_amount=10) >>> TaxedMoney(net=Money('75.00', 'EUR'), gross=Money('90.00', 'EUR')) # Discount 10% on gross price >>> compute_discount(price=price, tax=0.2, discount_percent=0.1) >>> TaxedMoney(net=Money('75.00', 'EUR'), gross=Money('90.00', 'EUR')) # Discount amount 10 EUR on net price >>> compute_discount( >>> price=price, tax=0.2, discount_amount=10, from_gross=False) >>> TaxedMoney(net=Money('73.33', 'EUR'), gross=Money('88.00', 'EUR')) # Discount 10% on net price >>> compute_discount( >>> price=price, tax=0.2, discount_percent=0.1, from_gross=False) """ if price is None: return None if tax == 0 and price.net.amount != price.gross.amount: raise Exception("Tax is set to 0 but gross and net price amount are" " different") if discount_amount == 0 and discount_percent == 0: return price if discount_amount != 0: if discount_amount < 0: raise Exception("Discount amount must be a positive value") discount = fixed_discount(price, discount=Money( D(discount_amount), currency=price.currency)).quantize() if from_gross: return compute_price(gross=discount.gross.amount, tax=compute_tax(tax), currency=price.currency, keep_gross=True) else: return compute_price(net=discount.net.amount, tax=compute_tax(tax), currency=price.currency, keep_gross=False) if discount_percent != 0: if discount_percent < 0 or discount_percent > 1: raise Exception("Discount percent must be a value between 0 and 1") discount = percentage_discount(price, percentage=D(discount_percent * 100), from_gross=from_gross).quantize() if from_gross: return compute_price(gross=discount.gross.amount, tax=compute_tax(tax), currency=price.currency, keep_gross=True) else: return compute_price(net=discount.net.amount, tax=compute_tax(tax), currency=price.currency, keep_gross=False)
def test_templatetag_discount_amount_for(): price = Price(Amount(30, 'BTC'), Amount(30, 'BTC')) discount = percentage_discount(50) discount_amount = prices.discount_amount_for(discount, price) assert discount_amount == Price(Amount(-15, 'BTC'), Amount(-15, 'BTC'))
def test_templatetag_discount_amount_for(django_setup): price = Price(30, currency='BTC') discount = percentage_discount(50) discount_amount = tags.discount_amount_for(discount, price) assert discount_amount == Price(-15, currency='BTC')
def test_precision(): price = TaxedMoney(Money('1.01', 'BTC'), Money('1.01', 'BTC')) result = percentage_discount(price, percentage=50) assert result.net == Money('0.51', 'BTC') assert result.net == Money('0.51', 'BTC')
def test_precision(): price = Price(Amount('1.01', 'BTC'), Amount('1.01', 'BTC')) discount = percentage_discount(value=50, name='Half off') result = discount.apply(price) assert result.net == Amount('0.51', 'BTC') assert result.net == Amount('0.51', 'BTC')