diff --git a/eboutic/migrations/0004_auto_20160726_1902.py b/eboutic/migrations/0004_auto_20160726_1902.py new file mode 100644 index 00000000..275ce142 --- /dev/null +++ b/eboutic/migrations/0004_auto_20160726_1902.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('eboutic', '0003_auto_20160726_1708'), + ] + + operations = [ + migrations.AddField( + model_name='basketitem', + name='product_id', + field=models.IntegerField(verbose_name='product id', default=0), + preserve_default=False, + ), + migrations.AddField( + model_name='invoiceitem', + name='product_id', + field=models.IntegerField(verbose_name='product id', default=0), + preserve_default=False, + ), + ] diff --git a/eboutic/models.py b/eboutic/models.py index dd0eb7d9..0379c3e6 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -7,17 +7,37 @@ from core.models import User class Basket(models.Model): """ - Basket is built when the user validate its session basket and asks for payment + Basket is built when the user connects to an eboutic page """ user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False) date = models.DateTimeField(_('date'), auto_now=True) + def add_product(self, p, q = 1): + item = self.items.filter(product_id=p.id).first() + if item is None: + BasketItem(basket=self, product_id=p.id, product_name=p.name, type=p.product_type.name, + quantity=q, product_unit_price=p.selling_price).save() + else: + item.quantity += q + item.save() + + def del_product(self, p, q = 1): + item = self.items.filter(product_id=p.id).first() + if item is not None: + item.quantity -= q + item.save() + if item.quantity <= 0: + item.delete() + def get_total(self): total = 0 for i in self.items.all(): total += i.quantity * i.product_unit_price return total + def __str__(self): + return "Basket (%d items)" % self.items.all().count() + class Invoice(models.Model): """ Invoices are generated once the payment has been validated @@ -49,6 +69,7 @@ class Invoice(models.Model): self.save() class AbstractBaseItem(models.Model): + product_id = models.IntegerField(_('product id')) product_name = models.CharField(_('product name'), max_length=255) type = models.CharField(_('product type'), max_length=255) product_unit_price = CurrencyField(_('unit price')) diff --git a/eboutic/templates/eboutic/eboutic_main.jinja b/eboutic/templates/eboutic/eboutic_main.jinja index 5aa67529..68d4f7e8 100644 --- a/eboutic/templates/eboutic/eboutic_main.jinja +++ b/eboutic/templates/eboutic/eboutic_main.jinja @@ -22,13 +22,12 @@
{% trans %}Basket: {% endtrans %}
{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket_total) }} €
+{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket.get_total()) }} €