Example #1
0
    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
        )
Example #2
0
    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)
Example #3
0
    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
Example #4
0
    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
Example #5
0
    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
Example #6
0
    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
Example #7
0
    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
Example #8
0
 def get_price(self, document, prefix, currency, **kwargs):
     return Price.calculate(product=document, currency=currency, **kwargs)
Example #9
0
    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))
Example #10
0
 def test_not_equal(self):
     self.assertNotEqual(Price(100), Price(200))
     self.assertNotEqual(Price(10, component=A), Price(10, component=B))
Example #11
0
 def test_equal(self):
     self.assertEqual(Price(100), Price(100))
     self.assertEqual(Price(10, component=A), Price(10, component=A))
Example #12
0
 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)
Example #13
0
 def get_base_costs(self, order):
     return Price(0)