2018-10-06 12:35:49 +02:00
|
|
|
import re
|
|
|
|
from datetime import date
|
2018-01-06 12:37:00 +01:00
|
|
|
|
2016-08-08 12:46:43 +02:00
|
|
|
from django.contrib.auth.models import User
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.core.validators import RegexValidator
|
|
|
|
from django.db import models, transaction
|
|
|
|
from django.db.models import F
|
2017-11-19 18:41:39 +01:00
|
|
|
from django.urls import reverse
|
2016-08-08 07:44:05 +02:00
|
|
|
from django.utils import timezone
|
2017-08-16 23:04:22 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-10-06 12:35:49 +02:00
|
|
|
|
|
|
|
from gestioncof.models import CofProfile
|
2019-06-26 18:57:16 +02:00
|
|
|
from shared.utils import choices_length
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2019-05-23 16:09:19 +02:00
|
|
|
from . import KFET_DELETED_TRIGRAMME
|
2017-09-21 23:39:27 +02:00
|
|
|
from .auth import KFET_GENERIC_TRIGRAMME
|
2017-09-13 01:57:31 +02:00
|
|
|
from .auth.models import GenericTeamToken # noqa
|
2017-06-12 01:51:10 +02:00
|
|
|
from .config import kfet_config
|
|
|
|
from .utils import to_ukf
|
2017-04-03 20:32:16 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
def default_promo():
|
2016-08-08 07:44:05 +02:00
|
|
|
now = date.today()
|
2018-10-06 12:35:49 +02:00
|
|
|
return now.month <= 8 and now.year - 1 or now.year
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-04-06 18:23:27 +02:00
|
|
|
|
|
|
|
class AccountManager(models.Manager):
|
|
|
|
"""Manager for Account Model."""
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
"""Always append related data to this Account."""
|
2018-10-06 12:35:49 +02:00
|
|
|
return super().get_queryset().select_related("cofprofile__user", "negative")
|
2017-04-06 18:23:27 +02:00
|
|
|
|
2017-09-21 23:39:27 +02:00
|
|
|
def get_generic(self):
|
|
|
|
"""
|
|
|
|
Get the kfet generic account instance.
|
|
|
|
"""
|
|
|
|
return self.get(trigramme=KFET_GENERIC_TRIGRAMME)
|
|
|
|
|
2017-09-22 23:31:46 +02:00
|
|
|
def get_by_password(self, password):
|
|
|
|
"""
|
|
|
|
Get a kfet generic account by clear password.
|
|
|
|
|
|
|
|
Raises Account.DoesNotExist if no Account has this password.
|
|
|
|
"""
|
|
|
|
from .auth.utils import hash_password
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2017-09-22 23:31:46 +02:00
|
|
|
if password is None:
|
|
|
|
raise self.model.DoesNotExist
|
|
|
|
return self.get(password=hash_password(password))
|
|
|
|
|
2017-04-06 18:23:27 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Account(models.Model):
|
2017-04-06 18:23:27 +02:00
|
|
|
objects = AccountManager()
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
cofprofile = models.OneToOneField(
|
2018-10-06 12:35:49 +02:00
|
|
|
CofProfile, on_delete=models.PROTECT, related_name="account_kfet"
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
trigramme = models.CharField(
|
2018-10-06 12:35:49 +02:00
|
|
|
unique=True,
|
|
|
|
max_length=3,
|
|
|
|
validators=[RegexValidator(regex="^[^a-z]{3}$")],
|
|
|
|
db_index=True,
|
|
|
|
)
|
|
|
|
balance = models.DecimalField(max_digits=6, decimal_places=2, default=0)
|
|
|
|
is_frozen = models.BooleanField("est gelé", default=False)
|
2017-04-06 00:31:04 +02:00
|
|
|
created_at = models.DateTimeField(default=timezone.now)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
2018-10-06 12:35:49 +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(
|
2018-10-06 12:35:49 +02:00
|
|
|
choices=PROMO_CHOICES, blank=True, null=True, default=default_promo()
|
|
|
|
)
|
|
|
|
nickname = models.CharField("surnom(s)", max_length=255, blank=True, default="")
|
2016-08-02 10:40:46 +02:00
|
|
|
password = models.CharField(
|
2018-10-06 12:35:49 +02:00
|
|
|
max_length=255, unique=True, blank=True, null=True, default=None
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-05-23 13:47:40 +02:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-10-06 12:35:49 +02:00
|
|
|
("is_team", "Is part of the team"),
|
|
|
|
("manage_perms", "Gérer les permissions K-Fêt"),
|
|
|
|
("manage_addcosts", "Gérer les majorations"),
|
|
|
|
("edit_balance_account", "Modifier la balance d'un compte"),
|
|
|
|
(
|
|
|
|
"change_account_password",
|
|
|
|
"Modifier le mot de passe d'une personne de l'équipe",
|
|
|
|
),
|
|
|
|
("special_add_account", "Créer un compte avec une balance initiale"),
|
|
|
|
("can_force_close", "Fermer manuellement la K-Fêt"),
|
|
|
|
("see_config", "Voir la configuration K-Fêt"),
|
|
|
|
("change_config", "Modifier la configuration K-Fêt"),
|
2021-02-19 10:48:24 +01:00
|
|
|
("access_old_history", "Peut accéder à l'historique plus ancien"),
|
2017-05-23 13:47:40 +02:00
|
|
|
)
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
def __str__(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "%s (%s)" % (self.trigramme, self.name)
|
2016-08-04 05:21:04 +02:00
|
|
|
|
2017-05-30 20:44:30 +02:00
|
|
|
# Propriétés pour accéder aux attributs de cofprofile et user
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def user(self):
|
|
|
|
return self.cofprofile.user
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def username(self):
|
|
|
|
return self.cofprofile.user.username
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def first_name(self):
|
|
|
|
return self.cofprofile.user.first_name
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def last_name(self):
|
|
|
|
return self.cofprofile.user.last_name
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def email(self):
|
|
|
|
return self.cofprofile.user.email
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def departement(self):
|
|
|
|
return self.cofprofile.departement
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@property
|
|
|
|
def is_cof(self):
|
|
|
|
return self.cofprofile.is_cof
|
|
|
|
|
|
|
|
# Propriétés supplémentaires
|
2017-06-12 01:51:10 +02:00
|
|
|
@property
|
|
|
|
def balance_ukf(self):
|
|
|
|
return to_ukf(self.balance, is_cof=self.is_cof)
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
@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):
|
2018-10-06 12:35:49 +02:00
|
|
|
return self.trigramme == "LIQ"
|
2016-08-08 12:46:43 +02:00
|
|
|
|
2016-08-23 15:43:16 +02:00
|
|
|
@property
|
|
|
|
def need_comment(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return self.trigramme == "#13"
|
2016-08-23 15:43:16 +02:00
|
|
|
|
2017-05-30 20:44:30 +02:00
|
|
|
@property
|
|
|
|
def readable(self):
|
2019-05-24 16:16:20 +02:00
|
|
|
return self.trigramme not in [KFET_DELETED_TRIGRAMME, KFET_GENERIC_TRIGRAMME]
|
2017-05-30 20:44:30 +02:00
|
|
|
|
2020-03-11 22:30:47 +01:00
|
|
|
@property
|
|
|
|
def editable(self):
|
|
|
|
return self.trigramme not in [
|
|
|
|
KFET_DELETED_TRIGRAMME,
|
|
|
|
KFET_GENERIC_TRIGRAMME,
|
|
|
|
"LIQ",
|
|
|
|
"#13",
|
|
|
|
]
|
|
|
|
|
2017-05-30 20:44:30 +02:00
|
|
|
@property
|
|
|
|
def is_team(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return self.has_perm("kfet.is_team")
|
2017-05-30 20:44:30 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
@staticmethod
|
2016-08-03 04:38:54 +02:00
|
|
|
def is_validandfree(trigramme):
|
2018-10-06 12:35:49 +02:00
|
|
|
data = {"is_valid": False, "is_free": False}
|
2016-08-03 04:38:54 +02:00
|
|
|
pattern = re.compile("^[^a-z]{3}$")
|
2018-10-06 12:35:49 +02:00
|
|
|
data["is_valid"] = pattern.match(trigramme) and True or False
|
2016-08-02 10:40:46 +02:00
|
|
|
try:
|
2018-10-06 12:47:19 +02:00
|
|
|
Account.objects.get(trigramme=trigramme)
|
2016-08-02 10:40:46 +02:00
|
|
|
except Account.DoesNotExist:
|
2018-10-06 12:35:49 +02:00
|
|
|
data["is_free"] = True
|
2016-08-03 04:38:54 +02:00
|
|
|
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):
|
2017-04-03 20:32:16 +02:00
|
|
|
overdraft_duration_max = kfet_config.overdraft_duration
|
|
|
|
overdraft_amount_max = kfet_config.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:
|
2018-10-06 12:35:49 +02:00
|
|
|
perms.add("kfet.perform_commented_operations")
|
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
|
2018-10-06 12:35:49 +02:00
|
|
|
if (
|
|
|
|
hasattr(self, "negative")
|
|
|
|
and self.negative.authz_overdraft_amount is not None
|
|
|
|
):
|
|
|
|
overdraft_amount = -self.negative.authz_overdraft_amount
|
2016-08-08 07:44:05 +02:00
|
|
|
else:
|
2018-10-06 12:35:49 +02:00
|
|
|
overdraft_amount = -overdraft_amount_max
|
2016-08-08 07:44:05 +02:00
|
|
|
# Retrieving overdraft datetime limit
|
2018-10-06 12:35:49 +02:00
|
|
|
if (
|
|
|
|
hasattr(self, "negative")
|
|
|
|
and self.negative.authz_overdraft_until is not None
|
|
|
|
):
|
2016-08-08 07:44:05 +02:00
|
|
|
overdraft_until = self.negative.authz_overdraft_until
|
2018-10-06 12:35:49 +02:00
|
|
|
elif hasattr(self, "negative"):
|
|
|
|
overdraft_until = 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
|
2018-10-06 12:35:49 +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
|
2018-10-06 12:35:49 +02:00
|
|
|
def save(self, data={}, *args, **kwargs):
|
2017-01-10 15:58:35 +01:00
|
|
|
|
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)
|
2018-10-06 12:35:49 +02:00
|
|
|
user.last_name = data.get("last_name", user.last_name)
|
|
|
|
user.email = data.get("email", user.email)
|
2016-08-02 10:40:46 +02:00
|
|
|
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:
|
2018-10-06 12:35:49 +02:00
|
|
|
user.first_name = data["first_name"]
|
2016-08-02 10:40:46 +02:00
|
|
|
if "last_name" in data:
|
2018-10-06 12:35:49 +02:00
|
|
|
user.last_name = data["last_name"]
|
2016-08-02 10:40:46 +02:00
|
|
|
if "email" in data:
|
2018-10-06 12:35:49 +02:00
|
|
|
user.email = data["email"]
|
2016-08-02 10:40:46 +02:00
|
|
|
user.save()
|
|
|
|
# Creating or updating CofProfile instance
|
|
|
|
(cof, _) = CofProfile.objects.get_or_create(user=user)
|
|
|
|
if "login_clipper" in data:
|
2018-10-06 12:35:49 +02:00
|
|
|
cof.login_clipper = data["login_clipper"]
|
2016-08-02 10:40:46 +02:00
|
|
|
if "departement" in data:
|
2018-10-06 12:35:49 +02:00
|
|
|
cof.departement = data["departement"]
|
2016-08-02 10:40:46 +02:00
|
|
|
cof.save()
|
2016-08-08 07:44:05 +02:00
|
|
|
if data:
|
|
|
|
self.cofprofile = cof
|
2018-01-16 16:22:52 +01:00
|
|
|
super().save(*args, **kwargs)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-09-22 23:31:46 +02:00
|
|
|
def change_pwd(self, clear_password):
|
|
|
|
from .auth.utils import hash_password
|
2018-10-06 12:35:49 +02:00
|
|
|
|
2017-09-22 23:31:46 +02:00
|
|
|
self.password = hash_password(clear_password)
|
2017-01-10 15:58:35 +01:00
|
|
|
|
2017-04-13 15:48:13 +02:00
|
|
|
def update_negative(self):
|
2021-02-18 17:57:59 +01:00
|
|
|
if self.balance < 0:
|
2018-10-06 12:35:49 +02:00
|
|
|
if hasattr(self, "negative") and not self.negative.start:
|
2017-04-13 15:15:59 +02:00
|
|
|
self.negative.start = timezone.now()
|
|
|
|
self.negative.save()
|
2018-10-06 12:35:49 +02:00
|
|
|
elif not hasattr(self, "negative"):
|
|
|
|
self.negative = AccountNegative.objects.create(
|
|
|
|
account=self, start=timezone.now()
|
2017-04-13 15:15:59 +02:00
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
elif hasattr(self, "negative"):
|
2021-02-18 17:57:59 +01:00
|
|
|
# self.balance >= 0
|
|
|
|
# TODO: méchanisme pour éviter de contourner le délai de négatif ?
|
2017-04-13 15:15:59 +02:00
|
|
|
self.negative.delete()
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class UserHasAccount(Exception):
|
|
|
|
def __init__(self, trigramme):
|
|
|
|
self.trigramme = trigramme
|
|
|
|
|
2017-04-06 19:07:13 +02:00
|
|
|
|
2019-05-23 16:09:19 +02:00
|
|
|
def get_deleted_account():
|
|
|
|
return Account.objects.get(trigramme=KFET_DELETED_TRIGRAMME)
|
|
|
|
|
|
|
|
|
2017-04-06 19:07:13 +02:00
|
|
|
class AccountNegativeManager(models.Manager):
|
|
|
|
"""Manager for AccountNegative model."""
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return super().get_queryset().select_related("account__cofprofile__user")
|
2017-04-06 19:07:13 +02:00
|
|
|
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class AccountNegative(models.Model):
|
2017-04-06 19:07:13 +02:00
|
|
|
objects = AccountNegativeManager()
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
account = models.OneToOneField(
|
2019-05-21 22:31:07 +02:00
|
|
|
Account, on_delete=models.CASCADE, related_name="negative"
|
2017-04-06 19:07:13 +02:00
|
|
|
)
|
|
|
|
start = models.DateTimeField(blank=True, null=True, default=None)
|
2016-08-08 07:44:05 +02:00
|
|
|
authz_overdraft_amount = models.DecimalField(
|
2017-03-31 02:31:16 +02:00
|
|
|
"négatif autorisé",
|
2018-10-06 12:35:49 +02:00
|
|
|
max_digits=6,
|
|
|
|
decimal_places=2,
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
2017-04-06 19:07:13 +02:00
|
|
|
)
|
2016-08-08 07:44:05 +02:00
|
|
|
authz_overdraft_until = models.DateTimeField(
|
2018-10-06 12:35:49 +02:00
|
|
|
"expiration du négatif", blank=True, null=True, default=None
|
2017-04-06 19:07:13 +02:00
|
|
|
)
|
|
|
|
comment = models.CharField("commentaire", max_length=255, blank=True)
|
|
|
|
|
2017-05-23 13:47:40 +02:00
|
|
|
class Meta:
|
2018-10-06 12:35:49 +02:00
|
|
|
permissions = (("view_negs", "Voir la liste des négatifs"),)
|
2017-05-23 13:47:40 +02:00
|
|
|
|
2017-06-12 01:51:10 +02:00
|
|
|
@property
|
|
|
|
def until_default(self):
|
|
|
|
return self.start + kfet_config.overdraft_duration
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2018-01-15 16:56:38 +01:00
|
|
|
class CheckoutQuerySet(models.QuerySet):
|
|
|
|
def is_valid(self):
|
|
|
|
now = timezone.now()
|
|
|
|
return self.filter(valid_from__lte=now, valid_to__gte=now)
|
|
|
|
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Checkout(models.Model):
|
2019-05-23 16:09:19 +02:00
|
|
|
created_by = models.ForeignKey(
|
|
|
|
Account, on_delete=models.SET(get_deleted_account), related_name="+"
|
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
name = models.CharField(max_length=45)
|
2016-08-02 10:40:46 +02:00
|
|
|
valid_from = models.DateTimeField()
|
2018-10-06 12:35:49 +02:00
|
|
|
valid_to = models.DateTimeField()
|
|
|
|
balance = models.DecimalField(max_digits=6, decimal_places=2, default=0)
|
|
|
|
is_protected = models.BooleanField(default=False)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2018-01-15 16:56:38 +01:00
|
|
|
objects = CheckoutQuerySet.as_manager()
|
|
|
|
|
2016-08-04 05:21:04 +02:00
|
|
|
def get_absolute_url(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return reverse("kfet.checkout.read", kwargs={"pk": self.pk})
|
2016-08-04 05:21:04 +02:00
|
|
|
|
|
|
|
class Meta:
|
2018-10-06 12:35:49 +02:00
|
|
|
ordering = ["-valid_to"]
|
2016-08-04 05:21:04 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-01-16 15:59:17 +01:00
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
created = self.pk is None
|
|
|
|
|
|
|
|
ret = super().save(*args, **kwargs)
|
|
|
|
|
|
|
|
if created:
|
|
|
|
self.statements.create(
|
|
|
|
amount_taken=0,
|
|
|
|
balance_old=self.balance,
|
|
|
|
balance_new=self.balance,
|
|
|
|
by=self.created_by,
|
|
|
|
)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2016-08-11 06:49:39 +02:00
|
|
|
class CheckoutStatement(models.Model):
|
2019-05-23 16:09:19 +02:00
|
|
|
by = models.ForeignKey(
|
|
|
|
Account, on_delete=models.SET(get_deleted_account), related_name="+"
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
checkout = models.ForeignKey(
|
2019-05-21 22:31:07 +02:00
|
|
|
Checkout, on_delete=models.CASCADE, related_name="statements"
|
2018-10-06 12:35:49 +02:00
|
|
|
)
|
|
|
|
balance_old = models.DecimalField(
|
|
|
|
"ancienne balance", max_digits=6, decimal_places=2
|
|
|
|
)
|
|
|
|
balance_new = models.DecimalField(
|
|
|
|
"nouvelle balance", max_digits=6, decimal_places=2
|
|
|
|
)
|
|
|
|
amount_taken = models.DecimalField("montant pris", max_digits=6, decimal_places=2)
|
|
|
|
amount_error = models.DecimalField(
|
|
|
|
"montant de l'erreur", max_digits=6, decimal_places=2
|
|
|
|
)
|
|
|
|
at = models.DateTimeField(auto_now_add=True)
|
2017-03-31 02:38:16 +02:00
|
|
|
not_count = models.BooleanField("caisse non comptée", default=False)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-03-31 02:38:16 +02:00
|
|
|
taken_001 = models.PositiveSmallIntegerField("pièces de 1¢", default=0)
|
|
|
|
taken_002 = models.PositiveSmallIntegerField("pièces de 2¢", default=0)
|
|
|
|
taken_005 = models.PositiveSmallIntegerField("pièces de 5¢", default=0)
|
|
|
|
taken_01 = models.PositiveSmallIntegerField("pièces de 10¢", default=0)
|
|
|
|
taken_02 = models.PositiveSmallIntegerField("pièces de 20¢", default=0)
|
|
|
|
taken_05 = models.PositiveSmallIntegerField("pièces de 50¢", default=0)
|
|
|
|
taken_1 = models.PositiveSmallIntegerField("pièces de 1€", default=0)
|
|
|
|
taken_2 = models.PositiveSmallIntegerField("pièces de 2€", default=0)
|
|
|
|
taken_5 = models.PositiveSmallIntegerField("billets de 5€", default=0)
|
|
|
|
taken_10 = models.PositiveSmallIntegerField("billets de 10€", default=0)
|
|
|
|
taken_20 = models.PositiveSmallIntegerField("billets de 20€", default=0)
|
|
|
|
taken_50 = models.PositiveSmallIntegerField("billets de 50€", default=0)
|
|
|
|
taken_100 = models.PositiveSmallIntegerField("billets de 100€", default=0)
|
|
|
|
taken_200 = models.PositiveSmallIntegerField("billets de 200€", default=0)
|
|
|
|
taken_500 = models.PositiveSmallIntegerField("billets de 500€", default=0)
|
2017-03-31 02:10:50 +02:00
|
|
|
taken_cheque = models.DecimalField(
|
2018-10-06 12:35:49 +02:00
|
|
|
"montant des chèques", 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):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "%s %s" % (self.checkout, self.at)
|
2016-08-11 15:14:23 +02:00
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
|
if not self.pk:
|
|
|
|
checkout_id = self.checkout_id
|
2018-10-06 12:35:49 +02:00
|
|
|
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
|
2018-10-06 12:35:49 +02:00
|
|
|
self.amount_error = 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)
|
2018-01-16 16:22:52 +01:00
|
|
|
super().save(*args, **kwargs)
|
2016-08-11 15:14:23 +02:00
|
|
|
else:
|
2018-10-06 12:35:49 +02:00
|
|
|
self.amount_error = self.balance_new + self.amount_taken - self.balance_old
|
2016-08-23 00:15:17 +02:00
|
|
|
# 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
|
2018-10-06 12:35:49 +02:00
|
|
|
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
|
|
|
|
):
|
2016-08-23 00:15:17 +02:00
|
|
|
Checkout.objects.filter(pk=self.checkout_id).update(
|
2018-10-06 12:35:49 +02:00
|
|
|
balance=F("balance") - last_statement.balance_new + self.balance_new
|
|
|
|
)
|
2018-01-16 16:22:52 +01:00
|
|
|
super().save(*args, **kwargs)
|
2016-08-11 15:14:23 +02:00
|
|
|
|
2017-03-10 18:28:48 +01:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class ArticleCategory(models.Model):
|
2017-04-05 15:34:28 +02:00
|
|
|
name = models.CharField("nom", max_length=45)
|
2018-10-06 12:35:49 +02:00
|
|
|
has_addcost = models.BooleanField(
|
|
|
|
"majorée",
|
|
|
|
default=True,
|
|
|
|
help_text="Si oui et qu'une majoration "
|
|
|
|
"est active, celle-ci sera "
|
|
|
|
"appliquée aux articles de "
|
|
|
|
"cette catégorie.",
|
|
|
|
)
|
2019-11-27 14:11:53 +01:00
|
|
|
has_reduction = models.BooleanField(
|
|
|
|
"réduction COF",
|
|
|
|
default=True,
|
|
|
|
help_text="Si oui, la réduction COF s'applique"
|
|
|
|
" aux articles de cette catégorie",
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-04 08:23:34 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-03-10 18:28:48 +01:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Article(models.Model):
|
2018-10-06 12:35:49 +02:00
|
|
|
name = models.CharField("nom", max_length=45)
|
|
|
|
is_sold = models.BooleanField("en vente", default=True)
|
|
|
|
hidden = models.BooleanField(
|
|
|
|
"caché",
|
|
|
|
default=False,
|
|
|
|
help_text="Si oui, ne sera pas affiché "
|
|
|
|
"au public ; par exemple "
|
|
|
|
"sur la carte.",
|
|
|
|
)
|
|
|
|
price = models.DecimalField("prix", max_digits=6, decimal_places=2, default=0)
|
|
|
|
stock = models.IntegerField(default=0)
|
2016-08-02 10:40:46 +02:00
|
|
|
category = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
ArticleCategory,
|
|
|
|
on_delete=models.PROTECT,
|
|
|
|
related_name="articles",
|
|
|
|
verbose_name="catégorie",
|
|
|
|
)
|
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(
|
2017-03-31 00:32:11 +02:00
|
|
|
"type de contenant",
|
2018-10-06 12:35:49 +02:00
|
|
|
choices=BOX_TYPE_CHOICES,
|
|
|
|
max_length=choices_length(BOX_TYPE_CHOICES),
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
2016-08-28 05:39:34 +02:00
|
|
|
box_capacity = models.PositiveSmallIntegerField(
|
2018-10-06 12:35:49 +02:00
|
|
|
"capacité du contenant", 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):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "%s - %s" % (self.category.name, self.name)
|
2016-08-04 08:23:34 +02:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return reverse("kfet.article.read", kwargs={"pk": self.pk})
|
2016-08-04 08:23:34 +02:00
|
|
|
|
2017-06-12 01:51:10 +02:00
|
|
|
def price_ukf(self):
|
|
|
|
return to_ukf(self.price)
|
|
|
|
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Inventory(models.Model):
|
|
|
|
articles = models.ManyToManyField(
|
2018-10-06 12:35:49 +02:00
|
|
|
Article, through="InventoryArticle", related_name="inventories"
|
|
|
|
)
|
2019-05-23 16:09:19 +02:00
|
|
|
by = models.ForeignKey(
|
|
|
|
Account, on_delete=models.SET(get_deleted_account), related_name="+"
|
|
|
|
)
|
2018-10-06 12:35:49 +02:00
|
|
|
at = models.DateTimeField(auto_now_add=True)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
|
|
|
order = models.OneToOneField(
|
2018-10-06 12:35:49 +02:00
|
|
|
"Order",
|
2019-06-03 22:43:47 +02:00
|
|
|
on_delete=models.PROTECT,
|
2018-10-06 12:35:49 +02:00
|
|
|
related_name="inventory",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-27 14:12:01 +02:00
|
|
|
class Meta:
|
2018-10-06 12:35:49 +02:00
|
|
|
ordering = ["-at"]
|
2017-05-23 13:47:40 +02:00
|
|
|
permissions = (
|
2018-10-06 12:35:49 +02:00
|
|
|
("order_to_inventory", "Générer un inventaire à partir d'une commande"),
|
2017-05-23 13:47:40 +02:00
|
|
|
)
|
|
|
|
|
2016-08-27 14:12:01 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class InventoryArticle(models.Model):
|
2019-05-21 22:31:07 +02:00
|
|
|
inventory = models.ForeignKey(Inventory, on_delete=models.CASCADE)
|
|
|
|
article = models.ForeignKey(Article, on_delete=models.CASCADE)
|
2018-10-06 12:35:49 +02:00
|
|
|
stock_old = models.IntegerField()
|
|
|
|
stock_new = models.IntegerField()
|
|
|
|
stock_error = models.IntegerField(default=0)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
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
|
2018-01-16 16:22:52 +01:00
|
|
|
super().save(*args, **kwargs)
|
2016-08-27 21:43:19 +02:00
|
|
|
|
2017-08-16 23:04:22 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Supplier(models.Model):
|
|
|
|
articles = models.ManyToManyField(
|
|
|
|
Article,
|
2017-08-16 23:04:22 +02:00
|
|
|
verbose_name=_("articles vendus"),
|
2018-10-06 12:35:49 +02:00
|
|
|
through="SupplierArticle",
|
|
|
|
related_name="suppliers",
|
2017-08-16 23:04:22 +02:00
|
|
|
)
|
|
|
|
name = models.CharField(_("nom"), max_length=45)
|
|
|
|
address = models.TextField(_("adresse"), blank=True)
|
|
|
|
email = models.EmailField(_("adresse mail"), blank=True)
|
|
|
|
phone = models.CharField(_("téléphone"), max_length=20, blank=True)
|
|
|
|
comment = models.TextField(_("commentaire"), blank=True)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-08-26 23:44:57 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2017-08-16 23:04:22 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class SupplierArticle(models.Model):
|
2019-05-21 22:31:07 +02:00
|
|
|
supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
|
|
|
|
article = models.ForeignKey(Article, on_delete=models.CASCADE)
|
2018-10-06 12:35:49 +02:00
|
|
|
at = models.DateTimeField(auto_now_add=True)
|
2016-08-26 23:44:57 +02:00
|
|
|
price_HT = models.DecimalField(
|
2018-10-06 12:35:49 +02:00
|
|
|
max_digits=7, decimal_places=4, blank=True, null=True, default=None
|
|
|
|
)
|
2016-08-26 23:44:57 +02:00
|
|
|
TVA = models.DecimalField(
|
2018-10-06 12:35:49 +02:00
|
|
|
max_digits=4, decimal_places=2, blank=True, null=True, default=None
|
|
|
|
)
|
2016-08-26 23:44:57 +02:00
|
|
|
rights = models.DecimalField(
|
2018-10-06 12:35:49 +02:00
|
|
|
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(
|
2019-05-21 22:31:07 +02:00
|
|
|
Supplier, on_delete=models.CASCADE, related_name="orders"
|
2018-10-06 12:35:49 +02:00
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
articles = models.ManyToManyField(
|
2018-10-06 12:35:49 +02:00
|
|
|
Article, through="OrderArticle", related_name="orders"
|
|
|
|
)
|
|
|
|
at = models.DateTimeField(auto_now_add=True)
|
|
|
|
amount = models.DecimalField(max_digits=6, decimal_places=2, default=0)
|
2016-08-28 05:39:34 +02:00
|
|
|
|
|
|
|
class Meta:
|
2018-10-06 12:35:49 +02:00
|
|
|
ordering = ["-at"]
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class OrderArticle(models.Model):
|
2019-05-21 22:31:07 +02:00
|
|
|
order = models.ForeignKey(Order, on_delete=models.CASCADE)
|
|
|
|
article = models.ForeignKey(Article, on_delete=models.CASCADE)
|
2016-08-02 10:40:46 +02:00
|
|
|
quantity_ordered = models.IntegerField()
|
2018-10-06 12:35:49 +02:00
|
|
|
quantity_received = models.IntegerField(default=0)
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class TransferGroup(models.Model):
|
2017-03-21 00:19:04 +01:00
|
|
|
at = models.DateTimeField(default=timezone.now)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
2018-10-06 12:35:49 +02:00
|
|
|
comment = models.CharField(max_length=255, blank=True, default="")
|
2016-08-02 10:40:46 +02:00
|
|
|
valid_by = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Account,
|
2019-06-03 22:43:47 +02:00
|
|
|
on_delete=models.SET(get_deleted_account),
|
2018-10-06 12:35:49 +02:00
|
|
|
related_name="+",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2017-04-02 16:49:41 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Transfer(models.Model):
|
|
|
|
group = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
TransferGroup, on_delete=models.PROTECT, related_name="transfers"
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
from_acc = models.ForeignKey(
|
2019-05-23 16:09:19 +02:00
|
|
|
Account,
|
|
|
|
on_delete=models.SET(get_deleted_account),
|
|
|
|
related_name="transfers_from",
|
2018-10-06 12:35:49 +02:00
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
to_acc = models.ForeignKey(
|
2019-05-23 16:09:19 +02:00
|
|
|
Account, on_delete=models.SET(get_deleted_account), related_name="transfers_to"
|
2018-10-06 12:35:49 +02:00
|
|
|
)
|
2017-04-02 16:49:41 +02:00
|
|
|
amount = models.DecimalField(max_digits=6, decimal_places=2)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
|
|
|
canceled_by = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Account,
|
2019-05-23 16:09:19 +02:00
|
|
|
on_delete=models.SET(get_deleted_account),
|
2018-10-06 12:35:49 +02:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
default=None,
|
|
|
|
related_name="+",
|
|
|
|
)
|
|
|
|
canceled_at = models.DateTimeField(null=True, blank=True, default=None)
|
2017-04-02 16:49:41 +02:00
|
|
|
|
|
|
|
def __str__(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return "{} -> {}: {}€".format(self.from_acc, self.to_acc, self.amount)
|
2017-04-02 16:49:41 +02:00
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
class OperationGroup(models.Model):
|
|
|
|
on_acc = models.ForeignKey(
|
2019-05-23 16:09:19 +02:00
|
|
|
Account, on_delete=models.SET(get_deleted_account), related_name="opesgroup"
|
2018-10-06 12:35:49 +02:00
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
checkout = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Checkout, on_delete=models.PROTECT, related_name="opesgroup"
|
|
|
|
)
|
2017-03-21 00:19:04 +01:00
|
|
|
at = models.DateTimeField(default=timezone.now)
|
2018-10-06 12:35:49 +02:00
|
|
|
amount = models.DecimalField(max_digits=6, decimal_places=2, default=0)
|
|
|
|
is_cof = models.BooleanField(default=False)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
2018-10-06 12:35:49 +02:00
|
|
|
comment = models.CharField(max_length=255, blank=True, default="")
|
2016-08-02 10:40:46 +02:00
|
|
|
valid_by = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Account,
|
2019-06-03 22:43:47 +02:00
|
|
|
on_delete=models.SET(get_deleted_account),
|
2018-10-06 12:35:49 +02:00
|
|
|
related_name="+",
|
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2016-12-24 12:33:04 +01:00
|
|
|
def __str__(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return ", ".join(map(str, self.opes.all()))
|
2016-12-24 12:33:04 +01:00
|
|
|
|
|
|
|
|
2016-08-02 10:40:46 +02:00
|
|
|
class Operation(models.Model):
|
2018-10-06 12:35:49 +02:00
|
|
|
PURCHASE = "purchase"
|
|
|
|
DEPOSIT = "deposit"
|
|
|
|
WITHDRAW = "withdraw"
|
|
|
|
INITIAL = "initial"
|
|
|
|
EDIT = "edit"
|
2016-08-02 10:40:46 +02:00
|
|
|
|
|
|
|
TYPE_ORDER_CHOICES = (
|
2018-10-06 12:35:49 +02:00
|
|
|
(PURCHASE, "Achat"),
|
|
|
|
(DEPOSIT, "Charge"),
|
|
|
|
(WITHDRAW, "Retrait"),
|
|
|
|
(INITIAL, "Initial"),
|
|
|
|
(EDIT, "Édition"),
|
2016-08-02 10:40:46 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
group = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
OperationGroup, on_delete=models.PROTECT, related_name="opes"
|
|
|
|
)
|
2016-08-02 10:40:46 +02:00
|
|
|
type = models.CharField(
|
2018-10-06 12:35:49 +02:00
|
|
|
choices=TYPE_ORDER_CHOICES, max_length=choices_length(TYPE_ORDER_CHOICES)
|
|
|
|
)
|
|
|
|
amount = models.DecimalField(max_digits=6, decimal_places=2, blank=True, default=0)
|
2016-08-02 10:40:46 +02:00
|
|
|
# Optional
|
|
|
|
article = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Article,
|
2019-05-21 22:31:07 +02:00
|
|
|
on_delete=models.SET_NULL,
|
2017-03-25 14:43:02 +01:00
|
|
|
related_name="operations",
|
2018-10-06 12:35:49 +02:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
article_nb = models.PositiveSmallIntegerField(blank=True, null=True, default=None)
|
2016-08-02 10:40:46 +02:00
|
|
|
canceled_by = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Account,
|
2019-05-23 16:09:19 +02:00
|
|
|
on_delete=models.SET(get_deleted_account),
|
2017-03-25 14:43:02 +01:00
|
|
|
related_name="+",
|
2018-10-06 12:35:49 +02:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
canceled_at = models.DateTimeField(blank=True, null=True, default=None)
|
2016-08-02 10:40:46 +02:00
|
|
|
addcost_for = models.ForeignKey(
|
2018-10-06 12:35:49 +02:00
|
|
|
Account,
|
2019-05-23 16:09:19 +02:00
|
|
|
on_delete=models.SET(get_deleted_account),
|
2017-03-25 14:43:02 +01:00
|
|
|
related_name="addcosts",
|
2018-10-06 12:35:49 +02:00
|
|
|
blank=True,
|
|
|
|
null=True,
|
|
|
|
default=None,
|
|
|
|
)
|
2016-08-06 22:19:52 +02:00
|
|
|
addcost_amount = models.DecimalField(
|
2018-10-06 12:35:49 +02:00
|
|
|
max_digits=6, decimal_places=2, blank=True, null=True, default=None
|
|
|
|
)
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2017-05-23 13:47:40 +02:00
|
|
|
class Meta:
|
|
|
|
permissions = (
|
2018-10-06 12:35:49 +02:00
|
|
|
("perform_deposit", "Effectuer une charge"),
|
|
|
|
("perform_negative_operations", "Enregistrer des commandes en négatif"),
|
|
|
|
("cancel_old_operations", "Annuler des commandes non récentes"),
|
|
|
|
(
|
|
|
|
"perform_commented_operations",
|
|
|
|
"Enregistrer des commandes avec commentaires",
|
|
|
|
),
|
2017-05-23 13:47:40 +02:00
|
|
|
)
|
|
|
|
|
2017-03-24 22:50:57 +01:00
|
|
|
@property
|
|
|
|
def is_checkout(self):
|
2018-10-06 12:35:49 +02:00
|
|
|
return (
|
|
|
|
self.type == Operation.DEPOSIT
|
|
|
|
or self.type == Operation.WITHDRAW
|
|
|
|
or (self.type == Operation.PURCHASE and self.group.on_acc.is_cash)
|
|
|
|
)
|
2016-08-03 04:38:54 +02:00
|
|
|
|
2016-12-24 12:33:04 +01:00
|
|
|
def __str__(self):
|
|
|
|
templates = {
|
2018-10-06 12:35:49 +02:00
|
|
|
self.PURCHASE: "{nb} {article.name} ({amount}€)",
|
|
|
|
self.DEPOSIT: "charge ({amount}€)",
|
|
|
|
self.WITHDRAW: "retrait ({amount}€)",
|
|
|
|
self.INITIAL: "initial ({amount}€)",
|
|
|
|
self.EDIT: "édition ({amount}€)",
|
|
|
|
}
|
|
|
|
return templates[self.type].format(
|
|
|
|
nb=self.article_nb, article=self.article, amount=self.amount
|
|
|
|
)
|