def variation_choice(cls, variation, attributes=None, **kwargs): result = (yield chaining.forward).result if result is not None: return variant = variation.variant.downcast() price_adjustment = None if variant.is_variant: a = Price.calculate(product=variant, raw=True) b = Price.calculate(product=variant.product, raw=True) price_adjustment = Price(0) if a is not None and b is not None: price_adjustment = (a - b) values = variation.values.all().order_by('attribute') if attributes is not None: values = values.filter(attribute__in=attributes) values = VARIATION_VALUE_SEPERATOR.join( [unicode(value) for value in values] ) yield call_or_format( VARIATION_CHOICE_FORMAT, variation=variation, variant=variant, values=values, price_adjustment=price_adjustment )
def variation_choice(cls, variation, attributes=None, **kwargs): result = (yield chaining.forward).result if result is not None: return variant = variation.variant.downcast() price_adjustment = None if variant.is_variant: a = Price.calculate(product=variant, raw=True) b = Price.calculate(product=variant.product, raw=True) price_adjustment = Price(0) if a is not None and b is not None: price_adjustment = (a - b) values = variation.values.all().order_by('attribute') if attributes is not None: values = values.filter(attribute__in=attributes) values = VARIATION_VALUE_SEPERATOR.join( [unicode(value) for value in values]) yield call_or_format(VARIATION_CHOICE_FORMAT, variation=variation, variant=variant, values=values, price_adjustment=price_adjustment)
def apply(self, price): tax_inclusive = settings_manager['tax_inclusive'] # Now we can create the tax tax_amount = self.amount tax = Price(tax_amount, currency=price.currency, component=self) # Apply tax price += tax # Handle tax inclusive if tax_inclusive: price -= Price(tax_amount, currency=price.currency) return price
def apply(self, price): discount_amount = self.amount # Now we can create the discount discount = Price(discount_amount, currency=price.currency, component=self) # Apply discount price -= discount return price
def apply(self, price): # First we get the base amount # to apply the rate against. base_amount = price.amount # Now we can create the discount discount_amount = base_amount * self.rate discount = Price(discount_amount, currency=price.currency, component=self) # Apply discount price -= discount return price
def calculate(self): unit_price = Price.calculate( qty=self.qty, **self.get_unit_price_kwargs() ) total = None if unit_price is not None: total = Price.calculate.with_result(self.qty * unit_price)( purchase=self, **self.get_total_kwargs() ) recalculated = unit_price != self.unit_price or total != self.total self.unit_price = unit_price self.total = total self.calculated = timezone.now() return recalculated
def calculate(self): subtotal = Price(0) recalculated = False for purchase in self: if purchase.needs_calculation(): recalculated |= purchase.calculate() if purchase.total is not None: subtotal += purchase.total if recalculated: self._mutate_purchases() total = Price.calculate.with_result(subtotal)(cart=self) recalculated |= subtotal != self.subtotal or total != self.total self.subtotal = subtotal self.total = total self.calculated = timezone.now() return recalculated
def get_price(self, document, prefix, currency, **kwargs): return Price.calculate(product=document, currency=currency, **kwargs)
def test_addition(self): self.assertEqual(Price(100) + Price(100), Price(200)) self.assertEqual((Price(50) + Price(10, component=A) + Price(50))[A], Price(10, component=A))
def test_not_equal(self): self.assertNotEqual(Price(100), Price(200)) self.assertNotEqual(Price(10, component=A), Price(10, component=B))
def test_equal(self): self.assertEqual(Price(100), Price(100)) self.assertEqual(Price(10, component=A), Price(10, component=A))
def test_init(self): self.assertEqual(Price(100).amount, 100) self.assertEqual(Price(100.5).amount, 100.5) self.assertEqual(Price('100.513').amount, Decimal('100.513')) self.assertEqual(Price(10, component=A).amount, 10)
def get_base_costs(self, order): return Price(0)