diff --git a/core/static/core/style.css b/core/static/core/style.css index 26394f67..2842c840 100644 --- a/core/static/core/style.css +++ b/core/static/core/style.css @@ -129,6 +129,12 @@ tbody>tr:hover { background: yellow; width: 100%; } +#basket { + width: 40%; + background: lightgrey; + float: right; + padding: 10px; +} /*-----------------------------USER PROFILE----------------------------*/ .user_profile { diff --git a/eboutic/__init__.py b/eboutic/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eboutic/admin.py b/eboutic/admin.py new file mode 100644 index 00000000..1e56ae1c --- /dev/null +++ b/eboutic/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin + +from eboutic.models import * + +admin.site.register(Basket) +admin.site.register(Invoice) diff --git a/eboutic/migrations/0001_initial.py b/eboutic/migrations/0001_initial.py new file mode 100644 index 00000000..8aa7d844 --- /dev/null +++ b/eboutic/migrations/0001_initial.py @@ -0,0 +1,70 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +from django.conf import settings +import accounting.models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('counter', '0009_auto_20160721_1902'), + ] + + operations = [ + migrations.CreateModel( + name='Basket', + fields=[ + ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), + ('date', models.DateTimeField(auto_now=True, verbose_name='date')), + ('user', models.ForeignKey(verbose_name='user', related_name='baskets', to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='BasketItem', + fields=[ + ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), + ('product_name', models.CharField(max_length=255, verbose_name='product name')), + ('product_unit_price', accounting.models.CurrencyField(max_digits=12, decimal_places=2, verbose_name='unit price')), + ('quantity', models.IntegerField(verbose_name='quantity')), + ('basket', models.ForeignKey(verbose_name='basket', related_name='items', to='eboutic.Basket')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='Invoice', + fields=[ + ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), + ('date', models.DateTimeField(auto_now=True, verbose_name='date')), + ('payment_method', models.CharField(max_length=20, choices=[('CREDIT_CARD', 'Credit card'), ('SITH_ACCOUNT', 'Sith account')], verbose_name='payment method')), + ('products', models.ManyToManyField(related_name='invoices', to='counter.Product', blank=True, verbose_name='products')), + ('user', models.ForeignKey(verbose_name='user', related_name='invoices', to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.CreateModel( + name='InvoiceItem', + fields=[ + ('id', models.AutoField(auto_created=True, serialize=False, primary_key=True, verbose_name='ID')), + ('product_name', models.CharField(max_length=255, verbose_name='product name')), + ('product_unit_price', accounting.models.CurrencyField(max_digits=12, decimal_places=2, verbose_name='unit price')), + ('quantity', models.IntegerField(verbose_name='quantity')), + ('invoice', models.ForeignKey(verbose_name='invoice', related_name='items', to='eboutic.Invoice')), + ], + options={ + 'abstract': False, + }, + ), + migrations.CreateModel( + name='Eboutic', + fields=[ + ], + options={ + 'proxy': True, + }, + bases=('counter.counter',), + ), + ] diff --git a/eboutic/migrations/0002_auto_20160722_0100.py b/eboutic/migrations/0002_auto_20160722_0100.py new file mode 100644 index 00000000..31503bac --- /dev/null +++ b/eboutic/migrations/0002_auto_20160722_0100.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('eboutic', '0001_initial'), + ] + + operations = [ + migrations.RemoveField( + model_name='invoice', + name='products', + ), + migrations.AddField( + model_name='invoice', + name='validated', + field=models.BooleanField(default=False, verbose_name='validated'), + ), + ] diff --git a/eboutic/migrations/__init__.py b/eboutic/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/eboutic/models.py b/eboutic/models.py new file mode 100644 index 00000000..27a81bc6 --- /dev/null +++ b/eboutic/models.py @@ -0,0 +1,68 @@ +from django.db import models, DataError +from django.utils.translation import ugettext_lazy as _ + +from accounting.models import CurrencyField +from counter.models import Counter, Product +from core.models import User + +class Eboutic(Counter): + class Meta: + proxy = True + +class Basket(models.Model): + """ + Basket is built when the user validate its session basket and asks for payment + """ + user = models.ForeignKey(User, related_name='baskets', verbose_name=_('user'), blank=False) + date = models.DateTimeField(_('date'), auto_now=True) + + def get_total(self): + total = 0 + for i in self.items.all(): + total += i.quantity * i.product_unit_price + return total + +class Invoice(models.Model): + """ + Invoices are generated once the payment has been validated + """ + user = models.ForeignKey(User, related_name='invoices', verbose_name=_('user'), blank=False) + date = models.DateTimeField(_('date'), auto_now=True) + payment_method = models.CharField(choices=[('CREDIT_CARD', _('Credit card')), ('SITH_ACCOUNT', _('Sith account'))], + max_length=20, verbose_name=_('payment method')) + validated = models.BooleanField(_("validated"), default=False) + + + def get_total(self): + total = 0 + for i in self.items.all(): + total += i.quantity * i.product_unit_price + return total + + def validate(self, *args, **kwargs): + if self.validated: + raise DataError(_("Invoice already validated")) + if self.payment_method == "SITH_ACCOUNT": + self.user.customer.amount -= self.get_total() + self.user.customer.save() + self.validated = True + self.save() + + + +class AbstractBaseItem(models.Model): + product_name = models.CharField(_('product name'), max_length=255) + product_unit_price = CurrencyField(_('unit price')) + quantity = models.IntegerField(_('quantity')) + + class Meta: + abstract = True + + def __str__(self): + return "Item: %s (%s) x%d" % (self.product_name, self.product_unit_price, self.quantity) + +class BasketItem(AbstractBaseItem): + basket = models.ForeignKey(Basket, related_name='items', verbose_name=_('basket')) + +class InvoiceItem(AbstractBaseItem): + invoice = models.ForeignKey(Invoice, related_name='items', verbose_name=_('invoice')) diff --git a/eboutic/templates/eboutic/eboutic_main.jinja b/eboutic/templates/eboutic/eboutic_main.jinja new file mode 100644 index 00000000..ba1a767a --- /dev/null +++ b/eboutic/templates/eboutic/eboutic_main.jinja @@ -0,0 +1,49 @@ +{% extends "core/base.jinja" %} + +{% macro add_product(id, content) %} +
+{% endmacro %} + +{% macro del_product(id, content) %} + +{% endmacro %} + +{% block content %} +{% trans %}Basket: {% endtrans %}
+{% trans %}Total: {% endtrans %}{{ "%0.2f"|format(basket_total) }} €
+ +{% trans %}Products: {% endtrans %} + {% for p in eboutic.products.all() %} + {{ add_product(p.id, p.name) }} + {% endfor %} +
+{% trans %}Basket: {% endtrans %}
+Article | +Quantity | +Unit price | +
{{ item.product_name }} | +{{ item.quantity }} | +{{ item.product_unit_price }} € | +