2016-09-01 00:45:44 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import (absolute_import, division,
|
|
|
|
print_function, unicode_literals)
|
|
|
|
from builtins import *
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
from django.db import models
|
2016-08-04 05:21:04 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-08-07 17:02:01 +02:00
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
2016-08-02 10:40:46 +02:00
|
|
|
from django.core.validators import RegexValidator
|
2016-08-08 12:46:43 +02:00
|
|
|
from django.contrib.auth.models import User
|
2017-02-09 21:04:32 +01:00
|
|
|
from cof.models import CofProfile
|
2016-08-02 10:40:46 +02:00
|
|
|
from django.utils.six.moves import reduce
|
2016-08-08 07:44:05 +02:00
|
|
|
from django.utils import timezone
|
2016-09-01 01:02:15 +02:00
|
|
|
from django.utils.encoding import python_2_unicode_compatible
|
2016-08-11 15:14:23 +02:00
|
|
|
from django.db import transaction
|
2016-08-23 00:15:17 +02:00
|
|
|
from django.db.models import F
|
2016-08-20 17:18:41 +02:00
|
|
|
from django.core.cache import cache
|
2016-08-08 07:44:05 +02:00
|
|
|
from datetime import date, timedelta
|
2016-08-03 04:38:54 +02:00
|
|
|
import re
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
def choices_length(choices):
|
|
|
|
return reduce(lambda m, choice: max(m, len(choice[0])), choices, 0)
|
|
|
|
|
|
|
|
def default_promo():
|
2016-08-08 07:44:05 +02:00
|
|
|
now = date.today()
|
2016-08-02 10:40:46 +02:00
|
|
|
return now.month <= 8 and now.year-1 or now.year
|
|
|
|
|
2016-09-01 01:02:15 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-08-02 10:40:46 +02:00
|
|
|
class Account(models.Model):
|
|
|
|
cofprofile = models.OneToOneField(
|
|
|
|
CofProfile, on_delete = models.PROTECT,
|
|
|
|
related_name = "account_kfet")
|
|
|
|
trigramme = models.CharField(
|
|
|
|
unique = True,
|
|
|
|
max_length = 3,
|
2016-08-04 17:38:58 +02:00
|
|
|
validators = [RegexValidator(regex='^[^a-z]{3}$')],
|
|
|
|
db_index = True)
|
2016-08-02 10:40:46 +02:00
|
|
|
balance = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
|
|
|
default = 0)
|
2016-08-04 06:00:47 +02:00
|
|
|
is_frozen = models.BooleanField(default = False)
|
2016-09-05 13:59:14 +02:00
|
|
|
created_at = models.DateTimeField(auto_now_add = True, null = True)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
2016-08-08 07:44:05 +02:00
|
|
|
PROMO_CHOICES = [(r,r) for r in range(1980, date.today().year+1)]
|
2016-08-02 10:40:46 +02:00
|
|
|
promo = models.IntegerField(
|
|
|
|
choices = PROMO_CHOICES,
|
|
|
|
blank = True, null = True, default = default_promo())
|
|
|
|
nickname = models.CharField(
|
|
|
|
max_length = 255,
|
|
|
|
blank = True, default = "")
|
|
|
|
password = models.CharField(
|
|
|
|
max_length = 255,
|
|
|
|
unique = True,
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-08-04 05:21:04 +02:00
|
|
|
return '%s (%s)' % (self.trigramme, self.name)
|
|
|
|
|
|
|
|
# Propriétés pour accéder aux attributs de user et cofprofile et user
|
|
|
|
@property
|
|
|
|
def user(self):
|
|
|
|
return self.cofprofile.user
|
|
|
|
@property
|
|
|
|
def username(self):
|
|
|
|
return self.cofprofile.user.username
|
|
|
|
@property
|
|
|
|
def first_name(self):
|
|
|
|
return self.cofprofile.user.first_name
|
|
|
|
@property
|
|
|
|
def last_name(self):
|
|
|
|
return self.cofprofile.user.last_name
|
|
|
|
@property
|
|
|
|
def email(self):
|
|
|
|
return self.cofprofile.user.email
|
|
|
|
@property
|
|
|
|
def departement(self):
|
|
|
|
return self.cofprofile.departement
|
|
|
|
@property
|
|
|
|
def is_cof(self):
|
|
|
|
return self.cofprofile.is_cof
|
|
|
|
|
|
|
|
# Propriétés supplémentaires
|
|
|
|
@property
|
|
|
|
def real_balance(self):
|
|
|
|
if (hasattr(self, 'negative')):
|
2016-09-05 19:19:09 +02:00
|
|
|
return self.balance - self.negative.balance_offset
|
2016-08-04 05:21:04 +02:00
|
|
|
return self.balance
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-08-21 02:53:35 +02:00
|
|
|
return self.user.get_full_name()
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-08 12:46:43 +02:00
|
|
|
@property
|
|
|
|
def is_cash(self):
|
|
|
|
return self.trigramme == 'LIQ'
|
|
|
|
|
2016-08-23 15:43:16 +02:00
|
|
|
@property
|
|
|
|
def need_comment(self):
|
|
|
|
return self.trigramme == '#13'
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
@staticmethod
|
2016-08-03 04:38:54 +02:00
|
|
|
def is_validandfree(trigramme):
|
|
|
|
data = { 'is_valid' : False, 'is_free' : False }
|
|
|
|
pattern = re.compile("^[^a-z]{3}$")
|
|
|
|
data['is_valid'] = pattern.match(trigramme) and True or False
|
2016-08-02 10:40:46 +02:00
|
|
|
try:
|
2016-08-03 04:38:54 +02:00
|
|
|
account = Account.objects.get(trigramme=trigramme)
|
2016-08-02 10:40:46 +02:00
|
|
|
except Account.DoesNotExist:
|
2016-08-03 04:38:54 +02:00
|
|
|
data['is_free'] = True
|
|
|
|
return data
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-26 15:30:40 +02:00
|
|
|
def perms_to_perform_operation(self, amount):
|
|
|
|
overdraft_duration_max = Settings.OVERDRAFT_DURATION()
|
|
|
|
overdraft_amount_max = Settings.OVERDRAFT_AMOUNT()
|
2016-08-09 11:02:26 +02:00
|
|
|
perms = set()
|
2016-08-08 07:44:05 +02:00
|
|
|
stop_ope = False
|
2016-08-08 12:46:43 +02:00
|
|
|
# Checking is cash account
|
|
|
|
if self.is_cash:
|
|
|
|
# Yes, so no perms and no stop
|
2016-08-14 19:59:36 +02:00
|
|
|
return set(), False
|
2016-08-23 15:43:16 +02:00
|
|
|
if self.need_comment:
|
|
|
|
perms.add('kfet.perform_commented_operations')
|
2016-08-08 07:44:05 +02:00
|
|
|
# Checking is frozen account
|
2016-08-08 03:49:10 +02:00
|
|
|
if self.is_frozen:
|
2016-08-09 11:02:26 +02:00
|
|
|
perms.add('kfet.override_frozen_protection')
|
2016-08-08 03:49:10 +02:00
|
|
|
new_balance = self.balance + amount
|
2016-08-08 07:44:05 +02:00
|
|
|
if new_balance < 0 and amount < 0:
|
|
|
|
# Retrieving overdraft amount limit
|
|
|
|
if (hasattr(self, 'negative')
|
|
|
|
and self.negative.authz_overdraft_amount is not None):
|
|
|
|
overdraft_amount = - self.negative.authz_overdraft_amount
|
|
|
|
else:
|
2016-08-09 11:02:26 +02:00
|
|
|
overdraft_amount = - overdraft_amount_max
|
2016-08-08 07:44:05 +02:00
|
|
|
# Retrieving overdraft datetime limit
|
|
|
|
if (hasattr(self, 'negative')
|
|
|
|
and self.negative.authz_overdraft_until is not None):
|
|
|
|
overdraft_until = self.negative.authz_overdraft_until
|
|
|
|
elif hasattr(self, 'negative'):
|
|
|
|
overdraft_until = \
|
2016-08-09 11:02:26 +02:00
|
|
|
self.negative.start + overdraft_duration_max
|
2016-08-08 07:44:05 +02:00
|
|
|
else:
|
2016-08-09 11:02:26 +02:00
|
|
|
overdraft_until = timezone.now() + overdraft_duration_max
|
2016-08-08 07:44:05 +02:00
|
|
|
# Checking it doesn't break 1 rule
|
2016-08-20 01:20:06 +02:00
|
|
|
if new_balance < overdraft_amount or timezone.now() > overdraft_until:
|
2016-08-08 07:44:05 +02:00
|
|
|
stop_ope = True
|
2016-08-09 11:02:26 +02:00
|
|
|
perms.add('kfet.perform_negative_operations')
|
2016-08-08 07:44:05 +02:00
|
|
|
return perms, stop_ope
|
2016-08-07 23:41:46 +02:00
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
# Surcharge Méthode save() avec gestions de User et CofProfile
|
2016-08-02 10:40:46 +02:00
|
|
|
# Args:
|
|
|
|
# - data : datas pour User et CofProfile
|
|
|
|
# Action:
|
|
|
|
# - Enregistre User, CofProfile à partir de "data"
|
|
|
|
# - Enregistre Account
|
2016-08-03 04:38:54 +02:00
|
|
|
def save(self, data = {}, *args, **kwargs):
|
2016-08-08 07:44:05 +02:00
|
|
|
if self.pk and data:
|
2016-08-02 10:40:46 +02:00
|
|
|
# Account update
|
|
|
|
|
|
|
|
# Updating User with data
|
2016-08-04 05:21:04 +02:00
|
|
|
user = self.user
|
2016-08-02 10:40:46 +02:00
|
|
|
user.first_name = data.get("first_name", user.first_name)
|
|
|
|
user.last_name = data.get("last_name", user.last_name)
|
|
|
|
user.email = data.get("email", user.email)
|
|
|
|
user.save()
|
|
|
|
# Updating CofProfile with data
|
|
|
|
cof = self.cofprofile
|
|
|
|
cof.departement = data.get("departement", cof.departement)
|
|
|
|
cof.save()
|
2016-08-08 07:44:05 +02:00
|
|
|
elif data:
|
2016-08-02 10:40:46 +02:00
|
|
|
# New account
|
|
|
|
|
2016-08-03 04:38:54 +02:00
|
|
|
# Checking if user has already an account
|
|
|
|
username = data.get("username")
|
|
|
|
try:
|
|
|
|
user = User.objects.get(username=username)
|
|
|
|
if hasattr(user.profile, "account_kfet"):
|
|
|
|
trigramme = user.profile.account_kfet.trigramme
|
|
|
|
raise Account.UserHasAccount(trigramme)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
pass
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
# Creating or updating User instance
|
|
|
|
(user, _) = User.objects.get_or_create(username=username)
|
|
|
|
if "first_name" in data:
|
|
|
|
user.first_name = data['first_name']
|
|
|
|
if "last_name" in data:
|
|
|
|
user.last_name = data['last_name']
|
|
|
|
if "email" in data:
|
|
|
|
user.email = data['email']
|
|
|
|
user.save()
|
|
|
|
# Creating or updating CofProfile instance
|
|
|
|
(cof, _) = CofProfile.objects.get_or_create(user=user)
|
|
|
|
if "login_clipper" in data:
|
|
|
|
cof.login_clipper = data['login_clipper']
|
|
|
|
if "departement" in data:
|
|
|
|
cof.departement = data['departement']
|
|
|
|
cof.save()
|
2016-08-08 07:44:05 +02:00
|
|
|
if data:
|
|
|
|
self.cofprofile = cof
|
2016-08-03 04:38:54 +02:00
|
|
|
super(Account, self).save(*args, **kwargs)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
# Surcharge de delete
|
|
|
|
# Pas de suppression possible
|
|
|
|
# Cas à régler plus tard
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class UserHasAccount(Exception):
|
|
|
|
def __init__(self, trigramme):
|
|
|
|
self.trigramme = trigramme
|
|
|
|
|
|
|
|
class AccountNegative(models.Model):
|
|
|
|
account = models.OneToOneField(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "negative")
|
2016-08-08 07:44:05 +02:00
|
|
|
start = models.DateTimeField(
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-03 04:38:54 +02:00
|
|
|
balance_offset = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
2016-08-08 07:44:05 +02:00
|
|
|
blank = True, null = True, default = None)
|
|
|
|
authz_overdraft_amount = models.DecimalField(
|
2016-08-02 10:40:46 +02:00
|
|
|
max_digits = 6, decimal_places = 2,
|
2016-08-08 07:44:05 +02:00
|
|
|
blank = True, null = True, default = None)
|
|
|
|
authz_overdraft_until = models.DateTimeField(
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-02 10:40:46 +02:00
|
|
|
comment = models.CharField(max_length = 255, blank = True)
|
|
|
|
|
2016-09-01 01:02:15 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-08-02 10:40:46 +02:00
|
|
|
class Checkout(models.Model):
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "+")
|
|
|
|
name = models.CharField(max_length = 45)
|
|
|
|
valid_from = models.DateTimeField()
|
|
|
|
valid_to = models.DateTimeField()
|
2016-08-04 05:21:04 +02:00
|
|
|
balance = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
|
|
|
default = 0)
|
2016-08-02 10:40:46 +02:00
|
|
|
is_protected = models.BooleanField(default = False)
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('kfet.checkout.read', kwargs={'pk': self.pk})
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['-valid_to']
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class CheckoutTransfer(models.Model):
|
|
|
|
from_checkout = models.ForeignKey(
|
|
|
|
Checkout, on_delete = models.PROTECT,
|
|
|
|
related_name = "transfers_from")
|
|
|
|
to_checkout = models.ForeignKey(
|
|
|
|
Checkout, on_delete = models.PROTECT,
|
|
|
|
related_name = "transfers_to")
|
|
|
|
amount = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2)
|
|
|
|
|
2016-09-01 01:02:15 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-08-11 06:49:39 +02:00
|
|
|
class CheckoutStatement(models.Model):
|
2016-08-02 10:40:46 +02:00
|
|
|
by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "+")
|
|
|
|
checkout = models.ForeignKey(
|
|
|
|
Checkout, on_delete = models.PROTECT,
|
|
|
|
related_name = "statements")
|
|
|
|
balance_old = models.DecimalField(max_digits = 6, decimal_places = 2)
|
|
|
|
balance_new = models.DecimalField(max_digits = 6, decimal_places = 2)
|
|
|
|
amount_taken = models.DecimalField(max_digits = 6, decimal_places = 2)
|
|
|
|
amount_error = models.DecimalField(max_digits = 6, decimal_places = 2)
|
|
|
|
at = models.DateTimeField(auto_now_add = True)
|
2016-08-23 02:45:49 +02:00
|
|
|
not_count = models.BooleanField(default=False)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-23 00:15:17 +02:00
|
|
|
taken_001 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_002 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_005 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_01 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_02 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_05 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_1 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_2 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_5 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_10 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_20 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_50 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_100 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_200 = models.PositiveSmallIntegerField(default=0)
|
|
|
|
taken_500 = models.PositiveSmallIntegerField(default=0)
|
2016-08-23 02:45:49 +02:00
|
|
|
taken_cheque = models.DecimalField(default=0, max_digits=6, decimal_places=2)
|
2016-08-23 00:15:17 +02:00
|
|
|
|
2016-08-11 15:14:23 +02:00
|
|
|
def __str__(self):
|
|
|
|
return '%s %s' % (self.checkout, self.at)
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.pk:
|
|
|
|
checkout_id = self.checkout_id
|
|
|
|
self.balance_old = (Checkout.objects
|
|
|
|
.values_list('balance', flat=True).get(pk=checkout_id))
|
2016-08-23 02:45:49 +02:00
|
|
|
if self.not_count:
|
|
|
|
self.balance_new = self.balance_old - self.amount_taken
|
2016-08-11 15:14:23 +02:00
|
|
|
self.amount_error = (
|
2016-08-23 02:45:49 +02:00
|
|
|
self.balance_new + self.amount_taken - self.balance_old)
|
2016-08-11 15:14:23 +02:00
|
|
|
with transaction.atomic():
|
|
|
|
Checkout.objects.filter(pk=checkout_id).update(balance=self.balance_new)
|
|
|
|
super(CheckoutStatement, self).save(*args, **kwargs)
|
|
|
|
else:
|
2016-08-23 00:15:17 +02:00
|
|
|
self.amount_error = (
|
|
|
|
self.balance_new + self.amount_taken - self.balance_old)
|
|
|
|
# Si on modifie le dernier relevé d'une caisse et que la nouvelle
|
|
|
|
# balance est modifiée alors on modifie la balance actuelle de la caisse
|
|
|
|
last_statement = (CheckoutStatement.objects
|
|
|
|
.filter(checkout=self.checkout)
|
|
|
|
.order_by('at')
|
|
|
|
.last())
|
|
|
|
if (last_statement.pk == self.pk
|
|
|
|
and last_statement.balance_new != self.balance_new):
|
|
|
|
Checkout.objects.filter(pk=self.checkout_id).update(
|
|
|
|
balance=F('balance') - last_statement.balance_new + self.balance_new)
|
2016-08-11 15:14:23 +02:00
|
|
|
super(CheckoutStatement, self).save(*args, **kwargs)
|
|
|
|
|
2016-09-01 01:02:15 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-08-02 10:40:46 +02:00
|
|
|
class ArticleCategory(models.Model):
|
|
|
|
name = models.CharField(max_length = 45)
|
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-09-01 01:02:15 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-08-02 10:40:46 +02:00
|
|
|
class Article(models.Model):
|
2016-08-04 08:23:34 +02:00
|
|
|
name = models.CharField(max_length = 45)
|
|
|
|
is_sold = models.BooleanField(default = True)
|
|
|
|
price = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
|
|
|
default = 0)
|
|
|
|
stock = models.IntegerField(default = 0)
|
2016-08-02 10:40:46 +02:00
|
|
|
category = models.ForeignKey(
|
|
|
|
ArticleCategory, on_delete = models.PROTECT,
|
|
|
|
related_name = "articles")
|
2016-08-28 05:39:34 +02:00
|
|
|
BOX_TYPE_CHOICES = (
|
|
|
|
("caisse", "caisse"),
|
|
|
|
("carton", "carton"),
|
|
|
|
("palette", "palette"),
|
|
|
|
("fût", "fût"),
|
|
|
|
)
|
|
|
|
box_type = models.CharField(
|
|
|
|
choices = BOX_TYPE_CHOICES,
|
|
|
|
max_length = choices_length(BOX_TYPE_CHOICES),
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
box_capacity = models.PositiveSmallIntegerField(
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
def __str__(self):
|
|
|
|
return '%s - %s' % (self.category.name, self.name)
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse('kfet.article.read', kwargs={'pk': self.pk})
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class ArticleRule(models.Model):
|
|
|
|
article_on = models.OneToOneField(
|
|
|
|
Article, on_delete = models.PROTECT,
|
|
|
|
related_name = "rule_on")
|
|
|
|
article_to = models.OneToOneField(
|
|
|
|
Article, on_delete = models.PROTECT,
|
|
|
|
related_name = "rule_to")
|
|
|
|
ratio = models.PositiveSmallIntegerField()
|
|
|
|
|
|
|
|
class Inventory(models.Model):
|
|
|
|
articles = models.ManyToManyField(
|
|
|
|
Article,
|
|
|
|
through = 'InventoryArticle',
|
|
|
|
related_name = "inventories")
|
|
|
|
by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "+")
|
|
|
|
at = models.DateTimeField(auto_now_add = True)
|
|
|
|
# Optional
|
|
|
|
order = models.OneToOneField(
|
|
|
|
'Order', on_delete = models.PROTECT,
|
|
|
|
related_name = "inventory",
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
|
2016-08-27 14:12:01 +02:00
|
|
|
class Meta:
|
|
|
|
ordering = ['-at']
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class InventoryArticle(models.Model):
|
|
|
|
inventory = models.ForeignKey(
|
|
|
|
Inventory, on_delete = models.PROTECT)
|
|
|
|
article = models.ForeignKey(
|
|
|
|
Article, on_delete = models.PROTECT)
|
|
|
|
stock_old = models.IntegerField()
|
|
|
|
stock_new = models.IntegerField()
|
|
|
|
stock_error = models.IntegerField(default = 0)
|
|
|
|
|
2016-08-27 21:43:19 +02:00
|
|
|
def save(self, *args, **kwargs):
|
2016-08-30 15:35:30 +02:00
|
|
|
# S'il s'agit d'un inventaire provenant d'une livraison, il n'y a pas
|
|
|
|
# d'erreur
|
2016-09-03 18:04:00 +02:00
|
|
|
if not self.inventory.order:
|
2016-08-30 15:35:30 +02:00
|
|
|
self.stock_error = self.stock_new - self.stock_old
|
2016-08-27 21:43:19 +02:00
|
|
|
super(InventoryArticle, self).save(*args, **kwargs)
|
|
|
|
|
2016-09-01 01:02:15 +02:00
|
|
|
@python_2_unicode_compatible
|
2016-08-02 10:40:46 +02:00
|
|
|
class Supplier(models.Model):
|
|
|
|
articles = models.ManyToManyField(
|
|
|
|
Article,
|
|
|
|
through = 'SupplierArticle',
|
|
|
|
related_name = "suppliers")
|
|
|
|
name = models.CharField(max_length = 45)
|
|
|
|
address = models.TextField()
|
|
|
|
email = models.EmailField()
|
|
|
|
phone = models.CharField(max_length = 10)
|
|
|
|
comment = models.TextField()
|
|
|
|
|
2016-08-26 23:44:57 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class SupplierArticle(models.Model):
|
|
|
|
supplier = models.ForeignKey(
|
|
|
|
Supplier, on_delete = models.PROTECT)
|
|
|
|
article = models.ForeignKey(
|
|
|
|
Article, on_delete = models.PROTECT)
|
2016-08-30 15:35:30 +02:00
|
|
|
at = models.DateTimeField(auto_now_add = True)
|
2016-08-26 23:44:57 +02:00
|
|
|
price_HT = models.DecimalField(
|
|
|
|
max_digits = 7, decimal_places = 4,
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
TVA = models.DecimalField(
|
|
|
|
max_digits = 4, decimal_places = 2,
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
rights = models.DecimalField(
|
|
|
|
max_digits = 7, decimal_places = 4,
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class Order(models.Model):
|
|
|
|
supplier = models.ForeignKey(
|
|
|
|
Supplier, on_delete = models.PROTECT,
|
|
|
|
related_name = "orders")
|
|
|
|
articles = models.ManyToManyField(
|
|
|
|
Article,
|
|
|
|
through = "OrderArticle",
|
|
|
|
related_name = "orders")
|
|
|
|
at = models.DateTimeField(auto_now_add = True)
|
2016-08-28 05:39:34 +02:00
|
|
|
amount = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2, default = 0)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ['-at']
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class OrderArticle(models.Model):
|
|
|
|
order = models.ForeignKey(
|
|
|
|
Order, on_delete = models.PROTECT)
|
|
|
|
article = models.ForeignKey(
|
|
|
|
Article, on_delete = models.PROTECT)
|
|
|
|
quantity_ordered = models.IntegerField()
|
2016-08-28 05:39:34 +02:00
|
|
|
quantity_received = models.IntegerField(default = 0)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class TransferGroup(models.Model):
|
|
|
|
at = models.DateTimeField(auto_now_add = True)
|
|
|
|
# Optional
|
|
|
|
comment = models.CharField(
|
|
|
|
max_length = 255,
|
|
|
|
blank = True, default = "")
|
|
|
|
valid_by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "+",
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
|
|
|
|
class Transfer(models.Model):
|
|
|
|
group = models.ForeignKey(
|
|
|
|
TransferGroup, on_delete = models.PROTECT,
|
|
|
|
related_name = "transfers")
|
|
|
|
from_acc = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "transfers_from")
|
|
|
|
to_acc = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "transfers_to")
|
|
|
|
amount = models.DecimalField(max_digits = 6, decimal_places = 2)
|
|
|
|
# Optional
|
|
|
|
canceled_by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
null = True, blank = True, default = None,
|
|
|
|
related_name = "+")
|
|
|
|
canceled_at = models.DateTimeField(
|
|
|
|
null = True, blank = True, default = None)
|
|
|
|
|
|
|
|
class OperationGroup(models.Model):
|
|
|
|
on_acc = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
2016-08-20 17:18:41 +02:00
|
|
|
related_name = "opesgroup")
|
2016-08-02 10:40:46 +02:00
|
|
|
checkout = models.ForeignKey(
|
|
|
|
Checkout, on_delete = models.PROTECT,
|
2016-08-20 17:18:41 +02:00
|
|
|
related_name = "opesgroup")
|
2016-08-02 10:40:46 +02:00
|
|
|
at = models.DateTimeField(auto_now_add = True)
|
2016-08-06 22:19:52 +02:00
|
|
|
amount = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
|
|
|
default = 0)
|
2016-08-02 10:40:46 +02:00
|
|
|
is_cof = models.BooleanField(default = False)
|
|
|
|
# Optional
|
|
|
|
comment = models.CharField(
|
|
|
|
max_length = 255,
|
|
|
|
blank = True, default = "")
|
|
|
|
valid_by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "+",
|
2016-08-06 22:19:52 +02:00
|
|
|
blank = True, null = True, default = None)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class Operation(models.Model):
|
|
|
|
PURCHASE = 'purchase'
|
|
|
|
DEPOSIT = 'deposit'
|
|
|
|
WITHDRAW = 'withdraw'
|
2016-09-05 07:31:54 +02:00
|
|
|
INITIAL = 'initial'
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
TYPE_ORDER_CHOICES = (
|
|
|
|
(PURCHASE, 'Achat'),
|
|
|
|
(DEPOSIT, 'Charge'),
|
|
|
|
(WITHDRAW, 'Retrait'),
|
2016-09-05 07:31:54 +02:00
|
|
|
(INITIAL, 'Initial'),
|
2016-08-02 10:40:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
group = models.ForeignKey(
|
|
|
|
OperationGroup, on_delete = models.PROTECT,
|
2016-08-20 17:18:41 +02:00
|
|
|
related_name = "opes")
|
2016-08-02 10:40:46 +02:00
|
|
|
type = models.CharField(
|
|
|
|
choices = TYPE_ORDER_CHOICES,
|
|
|
|
max_length = choices_length(TYPE_ORDER_CHOICES))
|
2016-08-07 17:02:01 +02:00
|
|
|
amount = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
|
|
|
blank = True, default = 0)
|
2016-08-06 22:19:52 +02:00
|
|
|
is_checkout = models.BooleanField(default = True)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
|
|
|
article = models.ForeignKey(
|
|
|
|
Article, on_delete = models.PROTECT,
|
|
|
|
related_name = "operations",
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-06 22:19:52 +02:00
|
|
|
article_nb = models.PositiveSmallIntegerField(
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-02 10:40:46 +02:00
|
|
|
canceled_by = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "+",
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
canceled_at = models.DateTimeField(
|
|
|
|
blank = True, null = True, default = None)
|
|
|
|
addcost_for = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
related_name = "addcosts",
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-06 22:19:52 +02:00
|
|
|
addcost_amount = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
2016-08-08 02:50:04 +02:00
|
|
|
blank = True, null = True, default = None)
|
2016-08-03 04:38:54 +02:00
|
|
|
|
|
|
|
class GlobalPermissions(models.Model):
|
|
|
|
class Meta:
|
|
|
|
managed = False
|
|
|
|
permissions = (
|
|
|
|
('is_team', 'Is part of the team'),
|
2016-08-08 03:49:10 +02:00
|
|
|
('perform_deposit', 'Effectuer une charge'),
|
|
|
|
('perform_negative_operations',
|
|
|
|
'Enregistrer des commandes en négatif'),
|
|
|
|
('override_frozen_protection', "Forcer le gel d'un compte"),
|
2016-08-09 11:02:26 +02:00
|
|
|
('cancel_old_operations', 'Annuler des commandes non récentes'),
|
2016-08-22 05:41:31 +02:00
|
|
|
('manage_perms', 'Gérer les permissions K-Fêt'),
|
|
|
|
('manage_addcosts', 'Gérer les majorations'),
|
2016-08-23 15:43:16 +02:00
|
|
|
('perform_commented_operations', 'Enregistrer des commandes avec commentaires'),
|
2016-08-23 20:31:31 +02:00
|
|
|
('view_negs', 'Voir la liste des négatifs'),
|
2016-08-31 01:36:58 +02:00
|
|
|
('order_to_inventory', "Générer un inventaire à partir d'une commande"),
|
|
|
|
('edit_balance_account', "Modifier la balance d'un compte"),
|
2016-09-01 16:31:18 +02:00
|
|
|
('change_account_password', "Modifier le mot de passe d'une personne de l'équipe"),
|
2016-09-05 07:31:54 +02:00
|
|
|
('special_add_account', "Créer un compte avec une balance initiale")
|
2016-08-03 04:38:54 +02:00
|
|
|
)
|
2016-08-07 18:37:06 +02:00
|
|
|
|
|
|
|
class Settings(models.Model):
|
2016-08-07 18:41:09 +02:00
|
|
|
name = models.CharField(
|
|
|
|
max_length = 45,
|
2016-08-09 11:02:26 +02:00
|
|
|
unique = True,
|
|
|
|
db_index = True)
|
2016-08-07 18:37:06 +02:00
|
|
|
value_decimal = models.DecimalField(
|
|
|
|
max_digits = 6, decimal_places = 2,
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-08 02:50:04 +02:00
|
|
|
value_account = models.ForeignKey(
|
|
|
|
Account, on_delete = models.PROTECT,
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-08 07:44:05 +02:00
|
|
|
value_duration = models.DurationField(
|
|
|
|
blank = True, null = True, default = None)
|
2016-08-07 18:37:06 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def setting_inst(name):
|
|
|
|
return Settings.objects.get(name=name)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def SUBVENTION_COF():
|
2016-08-20 17:18:41 +02:00
|
|
|
subvention_cof = cache.get('SUBVENTION_COF')
|
|
|
|
if subvention_cof:
|
|
|
|
return subvention_cof
|
2016-08-07 18:37:06 +02:00
|
|
|
try:
|
2016-08-20 17:18:41 +02:00
|
|
|
subvention_cof = Settings.setting_inst("SUBVENTION_COF").value_decimal
|
2016-08-07 18:37:06 +02:00
|
|
|
except Settings.DoesNotExist:
|
2016-08-20 17:18:41 +02:00
|
|
|
subvention_cof = 0
|
|
|
|
cache.set('SUBVENTION_COF', subvention_cof)
|
|
|
|
return subvention_cof
|
2016-08-07 18:37:06 +02:00
|
|
|
|
2016-08-08 02:50:04 +02:00
|
|
|
@staticmethod
|
|
|
|
def ADDCOST_AMOUNT():
|
|
|
|
try:
|
|
|
|
return Settings.setting_inst("ADDCOST_AMOUNT").value_decimal
|
|
|
|
except Settings.DoesNotExist:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def ADDCOST_FOR():
|
|
|
|
try:
|
|
|
|
return Settings.setting_inst("ADDCOST_FOR").value_account
|
|
|
|
except Settings.DoesNotExist:
|
|
|
|
return None;
|
2016-08-08 07:44:05 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def OVERDRAFT_DURATION():
|
2016-08-26 15:30:40 +02:00
|
|
|
overdraft_duration = cache.get('OVERDRAFT_DURATION')
|
|
|
|
if overdraft_duration:
|
|
|
|
return overdraft_duration
|
2016-08-08 07:44:05 +02:00
|
|
|
try:
|
2016-08-26 15:30:40 +02:00
|
|
|
overdraft_duration = Settings.setting_inst("OVERDRAFT_DURATION").value_duration
|
2016-08-08 07:44:05 +02:00
|
|
|
except Settings.DoesNotExist:
|
2016-08-26 15:30:40 +02:00
|
|
|
overdraft_duration = timedelta()
|
|
|
|
cache.set('OVERDRAFT_DURATION', overdraft_duration)
|
|
|
|
return overdraft_duration
|
2016-08-08 07:44:05 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def OVERDRAFT_AMOUNT():
|
2016-08-26 15:30:40 +02:00
|
|
|
overdraft_amount = cache.get('OVERDRAFT_AMOUNT')
|
|
|
|
if overdraft_amount:
|
|
|
|
return overdraft_amount
|
2016-08-08 07:44:05 +02:00
|
|
|
try:
|
2016-08-26 15:30:40 +02:00
|
|
|
overdraft_amount = Settings.setting_inst("OVERDRAFT_AMOUNT").value_decimal
|
2016-08-08 07:44:05 +02:00
|
|
|
except Settings.DoesNotExist:
|
2016-08-26 15:30:40 +02:00
|
|
|
overdraft_amount = 0
|
|
|
|
cache.set('OVERDRAFT_AMOUNT', overdraft_amount)
|
|
|
|
return overdraft_amount
|
2016-08-09 11:02:26 +02:00
|
|
|
|
2016-09-03 13:50:40 +02:00
|
|
|
@staticmethod
|
2016-08-09 11:02:26 +02:00
|
|
|
def CANCEL_DURATION():
|
2016-09-03 13:50:40 +02:00
|
|
|
cancel_duration = cache.get('CANCEL_DURATION')
|
|
|
|
if cancel_duration:
|
|
|
|
return cancel_duration
|
2016-08-09 11:02:26 +02:00
|
|
|
try:
|
2016-09-03 13:50:40 +02:00
|
|
|
cancel_duration = Settings.setting_inst("CANCEL_DURATION").value_duration
|
2016-08-09 11:02:26 +02:00
|
|
|
except Settings.DoesNotExist:
|
2016-09-03 13:50:40 +02:00
|
|
|
cancel_duration = timedelta()
|
|
|
|
cache.set('CANCEL_DURATION', cancel_duration)
|
|
|
|
return cancel_duration
|
2016-08-20 19:35:45 +02:00
|
|
|
|
2016-08-22 04:21:10 +02:00
|
|
|
@staticmethod
|
|
|
|
def create_missing():
|
|
|
|
s, created = Settings.objects.get_or_create(name='SUBVENTION_COF')
|
|
|
|
if created:
|
|
|
|
s.value_decimal = 25
|
|
|
|
s.save()
|
|
|
|
s, created = Settings.objects.get_or_create(name='ADDCOST_AMOUNT')
|
|
|
|
if created:
|
|
|
|
s.value_decimal = 0.5
|
|
|
|
s.save()
|
|
|
|
s, created = Settings.objects.get_or_create(name='ADDCOST_FOR')
|
|
|
|
s, created = Settings.objects.get_or_create(name='OVERDRAFT_DURATION')
|
|
|
|
if created:
|
|
|
|
s.value_duration = timedelta(days=1) # 24h
|
|
|
|
s.save()
|
|
|
|
s, created = Settings.objects.get_or_create(name='OVERDRAFT_AMOUNT')
|
|
|
|
if created:
|
|
|
|
s.value_decimal = 20
|
|
|
|
s.save()
|
|
|
|
s, created = Settings.objects.get_or_create(name='CANCEL_DURATION')
|
|
|
|
if created:
|
|
|
|
s.value_duration = timedelta(minutes=5) # 5min
|
|
|
|
s.save()
|
|
|
|
|
2016-08-26 15:30:40 +02:00
|
|
|
@staticmethod
|
|
|
|
def empty_cache():
|
|
|
|
cache.delete_many([
|
2016-09-03 18:07:43 +02:00
|
|
|
'SUBVENTION_COF', 'OVERDRAFT_DURATION', 'OVERDRAFT_AMOUNT',
|
|
|
|
'CANCEL_DURATION', 'ADDCOST_AMOUNT', 'ADDCOST_FOR',
|
2016-08-26 15:30:40 +02:00
|
|
|
])
|
|
|
|
|
2016-08-20 19:35:45 +02:00
|
|
|
class GenericTeamToken(models.Model):
|
|
|
|
token = models.CharField(max_length = 50, unique = True)
|