1. chewable
  2. django-lfs

Commits

Kai Diefenbach  committed 9d3b186 Merge

Decided to keep the voucher tax for. User who doesn't need it can simply let the field empty

  • Participants
  • Parent commits 3304d3e, e5d76b9
  • Branches default

Comments (0)

Files changed (2)

File lfs/voucher/models.py

View file
     end_date = models.DateField(blank=True, null=True)
     kind_of = models.PositiveSmallIntegerField(choices=KIND_OF_CHOICES)
     value = models.FloatField(default=0.0)
+    tax = models.ForeignKey(Tax, verbose_name=_(u"Tax"), blank=True, null=True)
     active = models.BooleanField(default=True)
     used = models.BooleanField(default=False)
     used_date = models.DateTimeField(blank=True, null=True)
     def __unicode__(self):
         return self.number
 
-    def get_price(self, cart=None):
+    def get_price_net(self, cart=None):
+        """Returns the net price of the voucher.
+        """
+        if self.kind_of == ABSOLUTE:
+            return self.value - self.get_tax()
+        else:
+            return cart.get_price_net() * (self.value / 100)
+
+    def get_price_gross(self, cart=None):
         """Returns the gross price of the voucher.
         """
-        if self.is_absolute():
+        if self.kind_of == ABSOLUTE:
             return self.value
         else:
             return cart.get_price_gross() * (self.value / 100)
 
+    def get_tax(self, cart=None):
+        """Returns the absolute tax of the voucher
+        """
+        if self.kind_of == ABSOLUTE:
+            if self.tax:
+                return (self.tax.rate / (100 + self.tax.rate)) * self.value
+            else:
+                return 0.0
+        else:
+            return cart.get_tax()  * (self.value / 100)
+
     def mark_as_used(self):
         """Mark voucher as used.
         """

File lfs/voucher/tests.py

View file
         self.assertEqual(self.v1.used, False)
         self.assertEqual(self.v1.used_date, None)
         self.assertEqual(self.v1.value, 10.0)
+        self.assertEqual(self.v1.tax, None)
 
     def test_prices_absolute(self):
         """
         """
         # No tax
-        price = self.v1.get_price()
-        self.assertEqual(price, 10)
+        price_net = self.v1.get_price_net()
+        self.assertEqual(price_net, 10)
+
+        price_gross = self.v1.get_price_gross()
+        self.assertEqual(price_gross, 10)
+
+        tax = self.v1.get_tax()
+        self.assertEqual(tax, 0.0)
+
+        # With tax
+        self.v1.tax = Tax.objects.create(rate=19.0)
+        self.v1.save()
+
+        price_net = self.v1.get_price_net()
+        self.assertEqual("%.2f" % price_net, "%.2f" % 8.4)
+
+        price_gross = self.v1.get_price_gross()
+        self.assertEqual(price_gross, 10)
+
+        tax = self.v1.get_tax()
+        self.assertEqual("%.2f" % tax, "%.2f" % 1.6)
 
     def test_prices_percentage(self):
         """
         self.v1.value = 10.0
         self.v1.save()
 
-        price = self.v1.get_price(self.cart)
-        self.assertEqual(price, 11.0)
+        # No tax
+        price_gross = self.v1.get_price_gross(self.cart)
+        self.assertEqual(price_gross, 11.0)
+
+        price_net = self.v1.get_price_net(self.cart)
+        self.assertEqual(price_net, 11.0)
+
+        tax = self.v1.get_tax(self.cart)
+        self.assertEqual(tax, 0.0)
+
+        # With tax
+        # Note: If the voucher is pecentage the tax is taken from the several
+        # products not from the voucher itself.
+        tax = Tax.objects.create(rate=19.0)
+        self.p1.tax = tax
+        self.p1.save()
+        self.p2.tax = tax
+        self.p2.save()
+
+        price_gross = self.v1.get_price_gross(self.cart)
+        self.assertEqual(price_gross, 11.0)
+
+        price_net = self.v1.get_price_net(self.cart)
+        self.assertEqual("%.2f" % price_net, "%.2f" % 9.24)
+
+        tax = self.v1.get_tax(self.cart)
+        self.assertEqual("%.2f" % tax, "%.2f" % 1.76)
 
     def test_kind_of(self):
         """