mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 11:59:23 +00:00
Finish the launderette click view
This commit is contained in:
35
counter/migrations/0012_auto_20160801_2016.py
Normal file
35
counter/migrations/0012_auto_20160801_2016.py
Normal file
@ -0,0 +1,35 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('counter', '0011_counter_sellers'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='refilling',
|
||||
name='is_validated',
|
||||
field=models.BooleanField(default=False, verbose_name='is validated'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='selling',
|
||||
name='is_validated',
|
||||
field=models.BooleanField(default=False, verbose_name='is validated'),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='selling',
|
||||
name='label',
|
||||
field=models.CharField(max_length=30, default='troll', verbose_name='label'),
|
||||
preserve_default=False,
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='selling',
|
||||
name='product',
|
||||
field=models.ForeignKey(related_name='sellings', to='counter.Product', blank=True),
|
||||
),
|
||||
]
|
19
counter/migrations/0013_auto_20160801_2255.py
Normal file
19
counter/migrations/0013_auto_20160801_2255.py
Normal file
@ -0,0 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('counter', '0012_auto_20160801_2016'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='selling',
|
||||
name='product',
|
||||
field=models.ForeignKey(to='counter.Product', null=True, related_name='sellings', blank=True),
|
||||
),
|
||||
]
|
@ -1,8 +1,9 @@
|
||||
from django.db import models
|
||||
from django.db import models, DataError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils import timezone
|
||||
from django.conf import settings
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.forms import ValidationError
|
||||
|
||||
from datetime import timedelta
|
||||
from random import randrange
|
||||
@ -32,6 +33,11 @@ class Customer(models.Model):
|
||||
def generate_account_id():
|
||||
return randrange(0, 4000) # TODO: improve me!
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if self.amount < 0:
|
||||
raise ValidationError(_("Not enough money"))
|
||||
super(Customer, self).save(*args, **kwargs)
|
||||
|
||||
class ProductType(models.Model):
|
||||
"""
|
||||
This describes a product type
|
||||
@ -190,6 +196,7 @@ class Refilling(models.Model):
|
||||
choices=settings.SITH_COUNTER_PAYMENT_METHOD, default='cash')
|
||||
bank = models.CharField(_('bank'), max_length=255,
|
||||
choices=settings.SITH_COUNTER_BANK, default='other')
|
||||
is_validated = models.BooleanField(_('is validated'), default=False)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("refilling")
|
||||
@ -202,38 +209,41 @@ class Refilling(models.Model):
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.full_clean()
|
||||
self.customer.amount += self.amount
|
||||
self.customer.save()
|
||||
if not self.is_validated:
|
||||
self.customer.amount += self.amount
|
||||
self.customer.save()
|
||||
self.is_validated = True
|
||||
super(Refilling, self).save(*args, **kwargs)
|
||||
|
||||
class Selling(models.Model):
|
||||
"""
|
||||
Handle the sellings
|
||||
"""
|
||||
product = models.ForeignKey(Product, related_name="sellings", blank=False)
|
||||
label = models.CharField(_("label"), max_length=30)
|
||||
product = models.ForeignKey(Product, related_name="sellings", null=True, blank=True)
|
||||
counter = models.ForeignKey(Counter, related_name="sellings", blank=False)
|
||||
unit_price = CurrencyField(_('unit price'))
|
||||
quantity = models.IntegerField(_('quantity'))
|
||||
seller = models.ForeignKey(User, related_name="sellings_as_operator", blank=False)
|
||||
customer = models.ForeignKey(Customer, related_name="buyings", blank=False)
|
||||
date = models.DateTimeField(_('date'), auto_now=True)
|
||||
is_validated = models.BooleanField(_('is validated'), default=False)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("selling")
|
||||
|
||||
def __str__(self):
|
||||
return "Selling: %d x %s (%f) for %s" % (self.quantity, self.product.name,
|
||||
return "Selling: %d x %s (%f) for %s" % (self.quantity, self.label,
|
||||
self.quantity*self.unit_price, self.customer.user.get_display_name())
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
self.full_clean()
|
||||
self.customer.amount -= self.quantity * self.unit_price
|
||||
self.customer.save()
|
||||
if not self.is_validated:
|
||||
self.customer.amount -= self.quantity * self.unit_price
|
||||
self.customer.save()
|
||||
self.is_validated = True
|
||||
super(Selling, self).save(*args, **kwargs)
|
||||
|
||||
# def get_absolute_url(self):
|
||||
# return reverse('counter:details', kwargs={'counter_id': self.id})
|
||||
|
||||
class Permanency(models.Model):
|
||||
"""
|
||||
This class aims at storing a traceability of who was barman where and when
|
||||
|
@ -35,7 +35,7 @@
|
||||
<tr>
|
||||
<td>{% trans %}Date{% endtrans %}</td>
|
||||
<td>{% trans %}Barman{% endtrans %}</td>
|
||||
<td>{% trans %}Product{% endtrans %}</td>
|
||||
<td>{% trans %}Label{% endtrans %}</td>
|
||||
<td>{% trans %}Quantity{% endtrans %}</td>
|
||||
<td>{% trans %}Total{% endtrans %}</td>
|
||||
</tr>
|
||||
@ -45,7 +45,7 @@
|
||||
<tr>
|
||||
<td>{{ i.date|localtime|date(DATETIME_FORMAT) }} - {{ i.date|localtime|time(DATETIME_FORMAT) }}</td>
|
||||
<td>{{ i.seller }}</td>
|
||||
<td>{{ i.product }}</td>
|
||||
<td>{{ i.label }}</td>
|
||||
<td>{{ i.quantity }}</td>
|
||||
<td>{{ i.quantity * i.unit_price }} €</td>
|
||||
</tr>
|
||||
|
@ -13,9 +13,11 @@ from django.conf import settings
|
||||
from django.db import DataError, transaction
|
||||
|
||||
import re
|
||||
from datetime import date, timedelta
|
||||
|
||||
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
|
||||
from subscription.models import Subscriber
|
||||
from subscription.views import get_subscriber
|
||||
from counter.models import Counter, Customer, Product, Selling, Refilling, ProductType
|
||||
|
||||
class GetUserForm(forms.Form):
|
||||
@ -28,7 +30,7 @@ class GetUserForm(forms.Form):
|
||||
"""
|
||||
code = forms.CharField(label="Code", max_length=10, required=False)
|
||||
id = forms.IntegerField(label="ID", required=False)
|
||||
# TODO: add a nice JS widget to search for users
|
||||
# TODO: add a nice JS widget to search for users
|
||||
|
||||
def as_p(self):
|
||||
self.fields['code'].widget.attrs['autofocus'] = True
|
||||
@ -36,14 +38,16 @@ class GetUserForm(forms.Form):
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super(GetUserForm, self).clean()
|
||||
user = None
|
||||
cus = None
|
||||
if cleaned_data['code'] != "":
|
||||
user = Customer.objects.filter(account_id=cleaned_data['code']).first()
|
||||
cus = Customer.objects.filter(account_id=cleaned_data['code']).first()
|
||||
elif cleaned_data['id'] is not None:
|
||||
user = Customer.objects.filter(user=cleaned_data['id']).first()
|
||||
if user is None:
|
||||
cus = Customer.objects.filter(user=cleaned_data['id']).first()
|
||||
sub = get_subscriber(cus.user) if cus is not None else None
|
||||
if cus is None or sub is None or (date.today() - sub.subscriptions.last().subscription_end) > timedelta(days=90):
|
||||
raise forms.ValidationError(_("User not found"))
|
||||
cleaned_data['user_id'] = user.user.id
|
||||
cleaned_data['user_id'] = cus.user.id
|
||||
cleaned_data['user'] = cus.user
|
||||
return cleaned_data
|
||||
|
||||
class RefillForm(forms.ModelForm):
|
||||
@ -238,7 +242,7 @@ class CounterClick(DetailView):
|
||||
if uprice * infos['qty'] > self.customer.amount:
|
||||
raise DataError(_("You have not enough money to buy all the basket"))
|
||||
request.session['last_basket'].append("%d x %s" % (infos['qty'], p.name))
|
||||
s = Selling(product=p, counter=self.object, unit_price=uprice,
|
||||
s = Selling(label=p.name, product=p, counter=self.object, unit_price=uprice,
|
||||
quantity=infos['qty'], seller=self.operator, customer=self.customer)
|
||||
s.save()
|
||||
request.session['last_customer'] = self.customer.user.get_display_name()
|
||||
|
Reference in New Issue
Block a user