2016-08-01 22:32:55 +00:00
|
|
|
from django.db import models, DataError
|
2016-03-28 12:54:35 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-04-19 17:58:57 +00:00
|
|
|
from django.utils import timezone
|
2016-03-28 12:54:35 +00:00
|
|
|
from django.conf import settings
|
2016-03-29 08:30:24 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-08-01 22:32:55 +00:00
|
|
|
from django.forms import ValidationError
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2016-08-18 19:06:10 +00:00
|
|
|
from datetime import timedelta
|
2016-08-04 22:50:25 +00:00
|
|
|
import random
|
|
|
|
import string
|
2016-04-19 17:58:57 +00:00
|
|
|
|
2016-03-28 12:54:35 +00:00
|
|
|
from club.models import Club
|
2016-05-30 10:23:59 +00:00
|
|
|
from accounting.models import CurrencyField
|
|
|
|
from core.models import Group, User
|
2016-08-01 14:36:16 +00:00
|
|
|
from subscription.models import Subscriber
|
|
|
|
from subscription.views import get_subscriber
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2016-05-30 10:23:59 +00:00
|
|
|
class Customer(models.Model):
|
|
|
|
"""
|
|
|
|
This class extends a user to make a customer. It adds some basic customers informations, such as the accound ID, and
|
|
|
|
is used by other accounting classes as reference to the customer, rather than using User
|
|
|
|
"""
|
|
|
|
user = models.OneToOneField(User, primary_key=True)
|
|
|
|
account_id = models.CharField(_('account id'), max_length=10, unique=True)
|
2016-05-31 17:32:15 +00:00
|
|
|
amount = CurrencyField(_('amount'))
|
2016-05-30 10:23:59 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _('customer')
|
|
|
|
verbose_name_plural = _('customers')
|
2016-08-04 22:50:25 +00:00
|
|
|
ordering = ['account_id',]
|
2016-05-30 10:23:59 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-08-14 17:28:14 +00:00
|
|
|
return "%s - %s" % (self.user.username, self.account_id)
|
2016-05-30 10:23:59 +00:00
|
|
|
|
2016-08-04 22:50:25 +00:00
|
|
|
def generate_account_id(number):
|
|
|
|
number = str(number)
|
|
|
|
letter = random.choice(string.ascii_lowercase)
|
|
|
|
while Customer.objects.filter(account_id=number+letter).exists():
|
|
|
|
letter = random.choice(string.ascii_lowercase)
|
|
|
|
return number+letter
|
2016-07-19 22:28:49 +00:00
|
|
|
|
2016-08-01 22:32:55 +00:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if self.amount < 0:
|
|
|
|
raise ValidationError(_("Not enough money"))
|
|
|
|
super(Customer, self).save(*args, **kwargs)
|
|
|
|
|
2016-05-30 10:23:59 +00:00
|
|
|
class ProductType(models.Model):
|
|
|
|
"""
|
|
|
|
This describes a product type
|
|
|
|
Useful only for categorizing, changes are made at the product level for now
|
|
|
|
"""
|
|
|
|
name = models.CharField(_('name'), max_length=30)
|
|
|
|
description = models.TextField(_('description'), null=True, blank=True)
|
|
|
|
icon = models.ImageField(upload_to='products', null=True, blank=True)
|
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('product type')
|
|
|
|
|
2016-05-30 10:23:59 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('counter:producttype_list')
|
|
|
|
|
2016-05-30 10:23:59 +00:00
|
|
|
class Product(models.Model):
|
|
|
|
"""
|
|
|
|
This describes a product, with all its related informations
|
|
|
|
"""
|
2016-08-18 01:04:50 +00:00
|
|
|
name = models.CharField(_('name'), max_length=64)
|
2016-05-30 10:23:59 +00:00
|
|
|
description = models.TextField(_('description'), blank=True)
|
2016-08-20 20:12:46 +00:00
|
|
|
product_type = models.ForeignKey(ProductType, related_name='products', verbose_name=_("product type"), null=True, blank=True,
|
|
|
|
on_delete=models.SET_NULL)
|
2016-08-18 01:04:50 +00:00
|
|
|
code = models.CharField(_('code'), max_length=16, blank=True)
|
2016-05-30 10:23:59 +00:00
|
|
|
purchase_price = CurrencyField(_('purchase price'))
|
|
|
|
selling_price = CurrencyField(_('selling price'))
|
|
|
|
special_selling_price = CurrencyField(_('special selling price'))
|
2016-08-20 20:12:46 +00:00
|
|
|
icon = models.ImageField(upload_to='products', null=True, blank=True, verbose_name=_("icon"))
|
|
|
|
club = models.ForeignKey(Club, related_name="products", verbose_name=_("club"))
|
2016-08-20 00:55:48 +00:00
|
|
|
limit_age = models.IntegerField(_('limit age'), default=0)
|
|
|
|
tray = models.BooleanField(_('tray price'), default=False)
|
2016-08-20 20:12:46 +00:00
|
|
|
parent_product = models.ForeignKey('self', related_name='children_products', verbose_name=_("parent product"), null=True,
|
|
|
|
blank=True, on_delete=models.SET_NULL)
|
|
|
|
buying_groups = models.ManyToManyField(Group, related_name='products', verbose_name=_("buying groups"))
|
2016-05-30 10:23:59 +00:00
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('product')
|
|
|
|
|
2016-07-20 16:48:18 +00:00
|
|
|
def is_owned_by(self, user):
|
2016-05-30 10:23:59 +00:00
|
|
|
"""
|
|
|
|
Method to see if that object can be edited by the given user
|
|
|
|
"""
|
|
|
|
if user.is_in_group(settings.SITH_GROUPS['accounting-admin']['name']):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-07-27 15:23:02 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('counter:product_list')
|
|
|
|
|
2016-03-28 12:54:35 +00:00
|
|
|
class Counter(models.Model):
|
|
|
|
name = models.CharField(_('name'), max_length=30)
|
|
|
|
club = models.ForeignKey(Club, related_name="counters")
|
|
|
|
products = models.ManyToManyField(Product, related_name="counters", blank=True)
|
2016-08-18 01:04:50 +00:00
|
|
|
type = models.CharField(_('counter type'),
|
2016-03-29 08:30:24 +00:00
|
|
|
max_length=255,
|
2016-07-21 18:03:31 +00:00
|
|
|
choices=[('BAR',_('Bar')), ('OFFICE',_('Office')), ('EBOUTIC',_('Eboutic'))])
|
2016-08-01 14:36:16 +00:00
|
|
|
sellers = models.ManyToManyField(Subscriber, verbose_name=_('sellers'), related_name='counters', blank=True)
|
2016-03-28 12:54:35 +00:00
|
|
|
edit_groups = models.ManyToManyField(Group, related_name="editable_counters", blank=True)
|
|
|
|
view_groups = models.ManyToManyField(Group, related_name="viewable_counters", blank=True)
|
2016-04-12 08:00:47 +00:00
|
|
|
barmen_session = {}
|
2016-03-28 12:54:35 +00:00
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _('counter')
|
|
|
|
|
2016-03-28 12:54:35 +00:00
|
|
|
def __getattribute__(self, name):
|
2016-08-01 14:36:16 +00:00
|
|
|
if name == "edit_groups":
|
|
|
|
return Group.objects.filter(name=self.club.unix_name+settings.SITH_BOARD_SUFFIX).all()
|
2016-03-28 12:54:35 +00:00
|
|
|
return object.__getattribute__(self, name)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-03-29 08:30:24 +00:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2016-07-22 11:34:34 +00:00
|
|
|
if self.type == "EBOUTIC":
|
|
|
|
return reverse('eboutic:main')
|
2016-03-29 08:30:24 +00:00
|
|
|
return reverse('counter:details', kwargs={'counter_id': self.id})
|
|
|
|
|
2016-08-01 14:36:16 +00:00
|
|
|
def is_owned_by(self, user):
|
2016-07-17 22:47:56 +00:00
|
|
|
return user.is_in_group(settings.SITH_GROUPS['counter-admin']['name'])
|
|
|
|
|
2016-03-29 08:30:24 +00:00
|
|
|
def can_be_viewed_by(self, user):
|
2016-08-01 14:36:16 +00:00
|
|
|
if self.type == "BAR" or self.type == "EBOUTIC":
|
|
|
|
return True
|
|
|
|
sub = get_subscriber(request.user)
|
|
|
|
return user.is_in_group(settings.SITH_MAIN_BOARD_GROUP) or sub in self.sellers
|
2016-04-19 17:58:57 +00:00
|
|
|
|
2016-07-18 11:22:50 +00:00
|
|
|
def add_barman(counter_id, user_id):
|
|
|
|
"""
|
|
|
|
Logs a barman in to the given counter
|
|
|
|
A user is stored as a tuple with its login time
|
|
|
|
"""
|
|
|
|
counter_id = int(counter_id)
|
|
|
|
user_id = int(user_id)
|
|
|
|
if counter_id not in Counter.barmen_session.keys():
|
|
|
|
Counter.barmen_session[counter_id] = {'users': {(user_id, timezone.now())}, 'time': timezone.now()}
|
|
|
|
else:
|
|
|
|
Counter.barmen_session[counter_id]['users'].add((user_id, timezone.now()))
|
|
|
|
|
|
|
|
def del_barman(counter_id, user_id):
|
|
|
|
"""
|
|
|
|
Logs a barman out and store its permanency
|
|
|
|
"""
|
|
|
|
counter_id = int(counter_id)
|
|
|
|
user_id = int(user_id)
|
|
|
|
user_tuple = None
|
|
|
|
for t in Counter.barmen_session[counter_id]['users']:
|
|
|
|
if t[0] == user_id: user_tuple = t
|
|
|
|
Counter.barmen_session[counter_id]['users'].remove(user_tuple)
|
|
|
|
u = User.objects.filter(id=user_id).first()
|
|
|
|
c = Counter.objects.filter(id=counter_id).first()
|
|
|
|
Permanency(user=u, counter=c, start=user_tuple[1], end=Counter.barmen_session[counter_id]['time']).save()
|
|
|
|
|
2016-08-06 10:37:36 +00:00
|
|
|
def get_barmen_list(self):
|
2016-07-18 11:22:50 +00:00
|
|
|
"""
|
2016-07-21 18:03:31 +00:00
|
|
|
Returns the barman list as list of User
|
2016-07-18 11:22:50 +00:00
|
|
|
|
|
|
|
Also handle the timeout of the barmen
|
|
|
|
"""
|
2016-04-19 17:58:57 +00:00
|
|
|
bl = []
|
2016-08-06 10:37:36 +00:00
|
|
|
counter_id = self.id
|
2016-04-19 17:58:57 +00:00
|
|
|
if counter_id in list(Counter.barmen_session.keys()):
|
2016-07-18 11:22:50 +00:00
|
|
|
for b in Counter.barmen_session[counter_id]['users']:
|
|
|
|
# Reminder: user is stored as a tuple with its login time
|
2016-07-21 18:03:31 +00:00
|
|
|
bl.append(User.objects.filter(id=b[0]).first())
|
2016-04-19 17:58:57 +00:00
|
|
|
if (timezone.now() - Counter.barmen_session[counter_id]['time']) < timedelta(minutes=settings.SITH_BARMAN_TIMEOUT):
|
|
|
|
Counter.barmen_session[counter_id]['time'] = timezone.now()
|
|
|
|
else:
|
2016-07-18 11:22:50 +00:00
|
|
|
for b in bl:
|
|
|
|
Counter.del_barman(counter_id, b.id)
|
|
|
|
bl = []
|
2016-04-19 17:58:57 +00:00
|
|
|
Counter.barmen_session[counter_id]['users'] = set()
|
|
|
|
return bl
|
|
|
|
|
2016-08-06 10:37:36 +00:00
|
|
|
def get_random_barman(self):
|
|
|
|
bl = self.get_barmen_list()
|
2016-08-18 19:06:10 +00:00
|
|
|
return bl[random.randrange(0, len(bl))]
|
2016-05-31 17:32:15 +00:00
|
|
|
|
2016-08-05 18:01:23 +00:00
|
|
|
def is_open(self):
|
|
|
|
response = False
|
2016-08-06 10:37:36 +00:00
|
|
|
if len(self.get_barmen_list()) > 0:
|
2016-08-05 18:01:23 +00:00
|
|
|
response = True
|
|
|
|
return response
|
|
|
|
|
2016-08-06 10:37:36 +00:00
|
|
|
def barman_list(self):
|
|
|
|
return [b.id for b in self.get_barmen_list()]
|
|
|
|
|
2016-05-31 17:32:15 +00:00
|
|
|
class Refilling(models.Model):
|
|
|
|
"""
|
|
|
|
Handle the refilling
|
|
|
|
"""
|
|
|
|
counter = models.ForeignKey(Counter, related_name="refillings", blank=False)
|
|
|
|
amount = CurrencyField(_('amount'))
|
2016-07-17 10:38:02 +00:00
|
|
|
operator = models.ForeignKey(User, related_name="refillings_as_operator", blank=False)
|
|
|
|
customer = models.ForeignKey(Customer, related_name="refillings", blank=False)
|
2016-08-18 17:52:20 +00:00
|
|
|
date = models.DateTimeField(_('date'))
|
2016-05-31 17:32:15 +00:00
|
|
|
payment_method = models.CharField(_('payment method'), max_length=255,
|
2016-08-18 01:04:50 +00:00
|
|
|
choices=settings.SITH_COUNTER_PAYMENT_METHOD, default='CASH')
|
2016-07-17 09:38:19 +00:00
|
|
|
bank = models.CharField(_('bank'), max_length=255,
|
2016-08-18 01:04:50 +00:00
|
|
|
choices=settings.SITH_COUNTER_BANK, default='OTHER')
|
2016-08-01 22:32:55 +00:00
|
|
|
is_validated = models.BooleanField(_('is validated'), default=False)
|
2016-05-31 17:32:15 +00:00
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("refilling")
|
|
|
|
|
2016-05-31 17:32:15 +00:00
|
|
|
def __str__(self):
|
2016-06-26 18:07:29 +00:00
|
|
|
return "Refilling: %.2f for %s" % (self.amount, self.customer.user.get_display_name())
|
2016-05-31 17:32:15 +00:00
|
|
|
|
2016-08-18 19:06:10 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
return user.can_edit(self.counter) and self.payment_method != "CARD"
|
|
|
|
|
2016-05-31 17:32:15 +00:00
|
|
|
# def get_absolute_url(self):
|
|
|
|
# return reverse('counter:details', kwargs={'counter_id': self.id})
|
|
|
|
|
2016-08-18 19:06:10 +00:00
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
self.customer.amount -= self.amount
|
|
|
|
self.customer.save()
|
|
|
|
super(Refilling, self).delete(*args, **kwargs)
|
|
|
|
|
2016-05-31 17:32:15 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2016-08-18 17:52:20 +00:00
|
|
|
if not self.date:
|
2016-08-18 19:06:10 +00:00
|
|
|
self.date = timezone.now()
|
2016-05-31 17:32:15 +00:00
|
|
|
self.full_clean()
|
2016-08-01 22:32:55 +00:00
|
|
|
if not self.is_validated:
|
|
|
|
self.customer.amount += self.amount
|
|
|
|
self.customer.save()
|
|
|
|
self.is_validated = True
|
2016-06-26 18:07:29 +00:00
|
|
|
super(Refilling, self).save(*args, **kwargs)
|
2016-05-31 17:32:15 +00:00
|
|
|
|
|
|
|
class Selling(models.Model):
|
|
|
|
"""
|
|
|
|
Handle the sellings
|
|
|
|
"""
|
2016-08-18 01:04:50 +00:00
|
|
|
label = models.CharField(_("label"), max_length=64)
|
2016-08-18 17:52:20 +00:00
|
|
|
product = models.ForeignKey(Product, related_name="sellings", null=True, blank=True, on_delete=models.SET_NULL)
|
|
|
|
counter = models.ForeignKey(Counter, related_name="sellings", null=True, blank=False, on_delete=models.SET_NULL)
|
|
|
|
club = models.ForeignKey(Club, related_name="sellings", null=True, blank=False, on_delete=models.SET_NULL)
|
2016-05-31 17:32:15 +00:00
|
|
|
unit_price = CurrencyField(_('unit price'))
|
|
|
|
quantity = models.IntegerField(_('quantity'))
|
2016-08-18 17:52:20 +00:00
|
|
|
seller = models.ForeignKey(User, related_name="sellings_as_operator", null=True, blank=False, on_delete=models.SET_NULL)
|
|
|
|
customer = models.ForeignKey(Customer, related_name="buyings", null=True, blank=False, on_delete=models.SET_NULL)
|
|
|
|
date = models.DateTimeField(_('date'))
|
|
|
|
payment_method = models.CharField(_('payment method'), max_length=255,
|
|
|
|
choices=[('SITH_ACCOUNT', _('Sith account')), ('CARD', _('Credit card'))], default='SITH_ACCOUNT')
|
2016-08-01 22:32:55 +00:00
|
|
|
is_validated = models.BooleanField(_('is validated'), default=False)
|
2016-05-31 17:32:15 +00:00
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("selling")
|
|
|
|
|
2016-05-31 17:32:15 +00:00
|
|
|
def __str__(self):
|
2016-08-01 22:32:55 +00:00
|
|
|
return "Selling: %d x %s (%f) for %s" % (self.quantity, self.label,
|
2016-05-31 17:32:15 +00:00
|
|
|
self.quantity*self.unit_price, self.customer.user.get_display_name())
|
|
|
|
|
2016-08-18 19:06:10 +00:00
|
|
|
def is_owned_by(self, user):
|
|
|
|
return user.can_edit(self.counter) and self.payment_method != "CARD"
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
self.customer.amount += self.quantity * self.unit_price
|
|
|
|
self.customer.save()
|
|
|
|
super(Selling, self).delete(*args, **kwargs)
|
|
|
|
|
2016-05-31 17:32:15 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2016-08-18 17:52:20 +00:00
|
|
|
if not self.date:
|
2016-08-18 19:06:10 +00:00
|
|
|
self.date = timezone.now()
|
2016-05-31 17:32:15 +00:00
|
|
|
self.full_clean()
|
2016-08-01 22:32:55 +00:00
|
|
|
if not self.is_validated:
|
|
|
|
self.customer.amount -= self.quantity * self.unit_price
|
|
|
|
self.customer.save()
|
|
|
|
self.is_validated = True
|
2016-05-31 17:32:15 +00:00
|
|
|
super(Selling, self).save(*args, **kwargs)
|
|
|
|
|
2016-07-18 11:22:50 +00:00
|
|
|
class Permanency(models.Model):
|
|
|
|
"""
|
|
|
|
This class aims at storing a traceability of who was barman where and when
|
|
|
|
"""
|
|
|
|
user = models.ForeignKey(User, related_name="permanencies")
|
|
|
|
counter = models.ForeignKey(Counter, related_name="permanencies")
|
|
|
|
start = models.DateTimeField(_('start date'))
|
|
|
|
end = models.DateTimeField(_('end date'))
|
|
|
|
|
2016-07-27 18:05:45 +00:00
|
|
|
class Meta:
|
|
|
|
verbose_name = _("permanency")
|
|
|
|
|
2016-07-18 11:22:50 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "%s in %s from %s to %s" % (self.user, self.counter,
|
|
|
|
self.start.strftime("%Y-%m-%d %H:%M:%S"), self.end.strftime("%Y-%m-%d %H:%M:%S"))
|
|
|
|
|
2016-05-30 10:23:59 +00:00
|
|
|
|