Migrate invoices and lot of eboutic improvements

This commit is contained in:
Skia
2016-08-18 19:52:20 +02:00
parent 50c452c287
commit 05bd177a9d
19 changed files with 470 additions and 156 deletions

View File

@ -0,0 +1,34 @@
# -*- 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='basketitem',
name='type',
),
migrations.RemoveField(
model_name='invoiceitem',
name='type',
),
migrations.AddField(
model_name='basketitem',
name='type_id',
field=models.IntegerField(default=1, verbose_name='product type id'),
preserve_default=False,
),
migrations.AddField(
model_name='invoiceitem',
name='type_id',
field=models.IntegerField(default=1, verbose_name='product type id'),
preserve_default=False,
),
]

View File

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('eboutic', '0002_auto_20160818_1635'),
]
operations = [
migrations.AlterField(
model_name='invoice',
name='payment_method',
field=models.CharField(verbose_name='payment method', max_length=20, choices=[('CARD', 'Credit card'), ('SITH_ACCOUNT', 'Sith account')]),
),
]

View File

@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('eboutic', '0003_auto_20160818_1738'),
]
operations = [
migrations.RemoveField(
model_name='invoice',
name='payment_method',
),
]

View File

@ -1,8 +1,9 @@
from django.db import models, DataError
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from accounting.models import CurrencyField
from counter.models import Counter, Product, Customer
from counter.models import Counter, Product, Customer, Selling, Refilling
from core.models import User
class Basket(models.Model):
@ -15,7 +16,7 @@ class Basket(models.Model):
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,
BasketItem(basket=self, product_id=p.id, product_name=p.name, type_id=p.product_type.id,
quantity=q, product_unit_price=p.selling_price).save()
else:
item.quantity += q
@ -44,10 +45,11 @@ class Invoice(models.Model):
"""
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 __str__(self):
return "%s - %s - %s" % (self.user, self.get_total(), self.date)
def get_total(self):
total = 0
for i in self.items.all():
@ -59,23 +61,44 @@ class Invoice(models.Model):
raise DataError(_("Invoice already validated"))
from counter.models import Customer
if not Customer.objects.filter(user=self.user).exists():
number = Customer.objects.last().account_id[:-1]
number = Customer.objects.count() + 1
Customer(user=self.user, account_id=Customer.generate_account_id(number), amount=0).save()
if self.payment_method == "SITH_ACCOUNT":
self.user.customer.amount -= self.get_total()
self.user.customer.save()
else:
for i in self.items.filter(type="REFILLING").all():
self.user.customer.amount += i.product_unit_price * i.quantity
self.user.customer.save()
eboutic = Counter.objects.filter(type="EBOUTIC").first()
for i in self.items.all():
if i.type_id == settings.SITH_COUNTER_PRODUCTTYPE_REFILLING:
new = Refilling(
counter=eboutic,
customer=self.user.customer,
operator=self.user,
amount=i.product_unit_price * i.quantity,
payment_method="CARD",
bank="OTHER",
date=self.date,
)
new.save()
else:
product = Product.objects.filter(id=i.product_id).first()
new = Selling(
label=i.product_name,
counter=eboutic,
club=product.club,
product=product,
seller=self.user,
customer=self.user.customer,
unit_price=i.product_unit_price,
quantity=i.quantity,
payment_method="CARD",
is_validated=True,
date=self.date,
)
new.save()
self.validated = True
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)
type_id = models.IntegerField(_('product type id'))
product_unit_price = CurrencyField(_('unit price'))
quantity = models.IntegerField(_('quantity'))

View File

@ -1,5 +1,9 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Eboutic{% endtrans %}
{% endblock %}
{% macro add_product(id, content) %}
<form method="post" action="{{ url('eboutic:main') }}" class="inline" style="display:inline">
{% csrf_token %}
@ -34,11 +38,14 @@
</form>
</div>
<div>
<p><strong>{% trans %}Products: {% endtrans %}</strong>
{% for p in eboutic.products.all() %}
{{ add_product(p.id, p.name) }}
{% for t in categories %}
{% if eboutic.products.filter(product_type=t).exists() %}
<h5>{{ t }}</h5>
{% for p in eboutic.products.filter(product_type=t).all() %}
{{ add_product(p.id, p.name) }}
{% endfor %}
{% endif %}
{% endfor %}
</p>
</div>
{% endblock %}

View File

@ -1,5 +1,9 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans %}Basket state{% endtrans %}
{% endblock %}
{% block content %}
<h3>{% trans %}Eboutic{% endtrans %}</h3>
@ -30,7 +34,7 @@
{% endfor %}
<input type="submit" value="{% trans %}Pay with credit card{% endtrans %}" />
</form>
{% if basket.items.filter(type="REFILLING").exists() %}
{% if basket.items.filter(type_id=settings.SITH_COUNTER_PRODUCTTYPE_REFILLING).exists() %}
<p>{% trans %}AE account payment disabled because your basket contains refilling items.{% endtrans %}</p>
{% else %}
<form method="post" action="{{ url('eboutic:pay_with_sith') }}">

View File

@ -14,10 +14,9 @@ from django.db import transaction, DataError
from django.utils.translation import ugettext as _
from django.conf import settings
from counter.models import Product, Customer, Counter
from counter.models import Product, Customer, Counter, ProductType, Selling
from eboutic.models import Basket, Invoice, BasketItem, InvoiceItem
# Create your views here.
class EbouticMain(TemplateView):
template_name = 'eboutic/eboutic_main.jinja'
@ -72,6 +71,7 @@ class EbouticMain(TemplateView):
kwargs = super(EbouticMain, self).get_context_data(**kwargs)
kwargs['basket'] = self.basket
kwargs['eboutic'] = Counter.objects.filter(type="EBOUTIC").first()
kwargs['categories'] = ProductType.objects.all()
return kwargs
class EbouticCommand(TemplateView):
@ -125,7 +125,7 @@ class EbouticPayWithSith(TemplateView):
if 'basket_id' not in request.session.keys() or not request.user.is_authenticated():
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
b = Basket.objects.filter(id=request.session['basket_id']).first()
if b is None or b.items.filter(type="REFILLING").exists():
if b is None or b.items.filter(type_id=settings.SITH_COUNTER_PRODUCTTYPE_REFILLING).exists():
return HttpResponseRedirect(reverse_lazy('eboutic:main', args=self.args, kwargs=kwargs))
c = Customer.objects.filter(user__id=b.user.id).first()
if c is None:
@ -134,14 +134,20 @@ class EbouticPayWithSith(TemplateView):
if c.amount < b.get_total():
raise DataError(_("You do not have enough money to buy the basket"))
else:
i = Invoice()
i.user = b.user
i.payment_method = "SITH_ACCOUNT"
i.save()
eboutic = Counter.objects.filter(type="EBOUTIC").first()
for it in b.items.all():
InvoiceItem(invoice=i, product_id=it.product_id, product_name=it.product_name, type=it.type,
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
i.validate()
product = Product.objects.filter(id=it.product_id).first()
Selling(
label=it.product_name,
counter=eboutic,
club=product.club,
product=product,
seller=c.user,
customer=c,
unit_price=it.product_unit_price,
quantity=it.quantity,
payment_method="SITH_ACCOUNT",
).save()
b.delete()
kwargs['not_enough'] = False
request.session.pop('basket_id', None)
@ -172,10 +178,10 @@ class EtransactionAutoAnswer(View):
return HttpResponse("Basket does not exists", status=400)
i = Invoice()
i.user = b.user
i.payment_method = "CREDIT_CARD"
i.payment_method = "CARD"
i.save()
for it in b.items.all():
InvoiceItem(invoice=i, product_id=it.product_id, product_name=it.product_name, type=it.type,
InvoiceItem(invoice=i, product_id=it.product_id, product_name=it.product_name, type_id=it.type_id,
product_unit_price=it.product_unit_price, quantity=it.quantity).save()
i.validate()
b.delete()