Commits

Kai Diefenbach committed eafe867

lfs.voucher: first implementation of percentage voucher.

Comments (0)

Files changed (2)

lfs/voucher/models.py

         if self.kind_of == ABSOLUTE:
             return self.value - self.get_tax()
         else:
-            raise NotImplemented
+            return self.get_price_gross(cart) - self.get_tax(cart)
 
     def get_price_gross(self, cart=None):
         """Returns the gross price of the voucher.
         if self.kind_of == ABSOLUTE:
             return self.value
         else:
-            raise NotImplemented
+            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:
                 return (self.tax.rate / (100 + self.tax.rate)) * self.value
             else:
-                raise NotImplemented
+                return (self.tax.rate / (100 + self.tax.rate)) * self.get_price_gross(cart)
         else:
             return 0.0
 

lfs/voucher/tests.py

 # django imports
 from django.contrib.auth.models import User
 from django.contrib.sessions.backends.file import SessionStore
-from django.core.urlresolvers import reverse
 from django.test import TestCase
 
 # lfs imports
+from lfs.cart.models import Cart
+from lfs.cart.models import CartItem
+from lfs.catalog.models import Product
 from lfs.tests.utils import RequestFactory
 from lfs.tax.models import Tax
 from lfs.voucher.models import Voucher
             value = 10.0,
         )
 
+        self.p1 = Product.objects.create(name="Product 1", slug="product-1", price=10.0)
+        self.p2 = Product.objects.create(name="Product 2", slug="product-2", price=100.0)
+
+        self.cart = Cart.objects.create()
+        CartItem.objects.create(cart=self.cart, product=self.p1, amount=1)
+        CartItem.objects.create(cart=self.cart, product=self.p2, amount=1)
+
     def test_defaults(self):
         """
         """
         self.assertEqual(self.v1.value, 10.0)
         self.assertEqual(self.v1.tax, None)
 
-    def test_prices(self):
+    def test_prices_absolute(self):
         """
         """
-        # Absolute without tax
+        # No tax
         price_net = self.v1.get_price_net()
         self.assertEqual(price_net, 10)
 
         tax = self.v1.get_tax()
         self.assertEqual(tax, 0.0)
 
-        # Absolute with tax
+        # With tax
         self.v1.tax = Tax.objects.create(rate=19.0)
         self.v1.save()
 
         tax = self.v1.get_tax()
         self.assertEqual("%.2f" % tax, "%.2f" % 1.6)
 
+    def test_prices_percentage(self):
+        """
+        """
+        # 10% discount
+        self.v1.kind_of = PERCENTAGE
+        self.v1.value = 10.0
+        self.v1.save()
+
+        # 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
+        self.v1.tax = Tax.objects.create(rate=19.0)
+        self.v1.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):
         """
         """