mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Add limit for ecocup recording
This commit is contained in:
19
counter/migrations/0013_customer_recorded_products.py
Normal file
19
counter/migrations/0013_customer_recorded_products.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_20170515_2202'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='customer',
|
||||
name='recorded_products',
|
||||
field=models.IntegerField(verbose_name='recorded items', default=0),
|
||||
),
|
||||
]
|
@ -51,6 +51,7 @@ class Customer(models.Model):
|
||||
user = models.OneToOneField(User, primary_key=True)
|
||||
account_id = models.CharField(_('account id'), max_length=10, unique=True)
|
||||
amount = CurrencyField(_('amount'))
|
||||
recorded_products = models.IntegerField(_('recorded product'), default=0)
|
||||
|
||||
class Meta:
|
||||
verbose_name = _('customer')
|
||||
@ -60,6 +61,13 @@ class Customer(models.Model):
|
||||
def __str__(self):
|
||||
return "%s - %s" % (self.user.username, self.account_id)
|
||||
|
||||
@property
|
||||
def can_record(self):
|
||||
return self.recorded_products > -settings.SITH_RECORD_LIMIT
|
||||
|
||||
def can_record_more(self, number):
|
||||
return self.recorded_products - number >= -settings.SITH_RECORD_LIMIT
|
||||
|
||||
@property
|
||||
def can_buy(self):
|
||||
return (self.user.subscriptions.last() and
|
||||
@ -143,6 +151,20 @@ class Product(models.Model):
|
||||
class Meta:
|
||||
verbose_name = _('product')
|
||||
|
||||
@property
|
||||
def is_record_product(self):
|
||||
for product in settings.SITH_RECORD_PRODUCT:
|
||||
if product == self.id:
|
||||
return True
|
||||
return False
|
||||
|
||||
@property
|
||||
def is_unrecord_product(self):
|
||||
for product in settings.SITH_UNRECORD_PRODUCT:
|
||||
if product == self.id:
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_owned_by(self, user):
|
||||
"""
|
||||
Method to see if that object can be edited by the given user
|
||||
|
@ -330,6 +330,28 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
except:
|
||||
return 0
|
||||
|
||||
def compute_record_product(self, request, product=None):
|
||||
recorded = 0
|
||||
basket = request.session['basket']
|
||||
|
||||
if product:
|
||||
if product.is_record_product:
|
||||
recorded -= 1
|
||||
elif product.is_unrecord_product:
|
||||
recorded += 1
|
||||
|
||||
for p in basket:
|
||||
bproduct = self.get_product(str(p))
|
||||
if bproduct.is_record_product:
|
||||
recorded -= basket[p]['qty']
|
||||
elif bproduct.is_unrecord_product:
|
||||
recorded += basket[p]['qty']
|
||||
return recorded
|
||||
|
||||
def is_record_product_ok(self, request, product):
|
||||
return self.customer.can_record_more(
|
||||
self.compute_record_product(request, product))
|
||||
|
||||
def add_product(self, request, q=1, p=None):
|
||||
"""
|
||||
Add a product to the basket
|
||||
@ -359,6 +381,9 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
if self.customer.amount < (total + round(q * float(price), 2)): # Check for enough money
|
||||
request.session['not_enough'] = True
|
||||
return False
|
||||
if not self.is_record_product_ok(request, product):
|
||||
request.session['not_allowed'] = True
|
||||
return False
|
||||
if product.limit_age >= 18 and not self.customer.user.date_of_birth:
|
||||
request.session['no_age'] = True
|
||||
return False
|
||||
@ -438,6 +463,8 @@ class CounterClick(CounterTabsMixin, CanViewMixin, DetailView):
|
||||
s = Selling(label=p.name + " (Plateau)", product=p, club=p.club, counter=self.object, unit_price=0,
|
||||
quantity=infos['bonus_qty'], seller=self.operator, customer=self.customer)
|
||||
s.save()
|
||||
self.customer.recorded_products -= self.compute_record_product(request)
|
||||
self.customer.save()
|
||||
request.session['last_customer'] = self.customer.user.get_display_name()
|
||||
request.session['last_total'] = "%0.2f" % self.sum_basket(request)
|
||||
request.session['new_customer_amount'] = str(self.customer.amount)
|
||||
|
Reference in New Issue
Block a user