LIQ et caisse dans K-Psul

- Les charges et retraits sur des comptes modifient la balance de la
  caisse sélectionnée.
- Comportement particulier pour le compte LIQ :
    Pas de charge, pas de retrait. La balance de LIQ n'est jamais
    modifiée (donc pas d'entrée dans AccountNegative). Les achats sur LIQ
    modifient la balance de lacaisse.
This commit is contained in:
Aurélien Delobelle 2016-08-08 12:46:43 +02:00
parent 5abffb099f
commit 070752bd01
3 changed files with 54 additions and 23 deletions

View file

@ -2,6 +2,7 @@ from django.db import models
from django.core.urlresolvers import reverse
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.validators import RegexValidator
from django.contrib.auth.models import User
from gestioncof.models import CofProfile
from django.utils.six.moves import reduce
from django.utils import timezone
@ -83,6 +84,10 @@ class Account(models.Model):
else:
return self.last_name
@property
def is_cash(self):
return self.trigramme == 'LIQ'
@staticmethod
def is_validandfree(trigramme):
data = { 'is_valid' : False, 'is_free' : False }
@ -97,6 +102,10 @@ class Account(models.Model):
def perms_to_perform_operation(self, amount):
perms = []
stop_ope = False
# Checking is cash account
if self.is_cash:
# Yes, so no perms and no stop
return [], False
# Checking is frozen account
if self.is_frozen:
perms.append('kfet.override_frozen_protection')

View file

@ -53,12 +53,11 @@
// Vérification client de la validité
// et de ladisponibilité du trigramme choisi
old_trigramme = "";
$('#id_trigramme').on('input', function() {
trigramme = $('#id_trigramme').val();
container = '#trigramme_valid';
var trigramme = $('#id_trigramme').val();
var container = '#trigramme_valid';
pattern = /^[^a-z]{3}$/;
var pattern = /^[^a-z]{3}$/;
if (!(trigramme.match(pattern))) {
$(container).text("Non valide");
} else {

View file

@ -31,7 +31,7 @@ def put_cleaned_data_in_dict(dict, form):
# Account - General
@login_required
@permission_required('account.is_team')
@permission_required('kfet.is_team')
def account(request):
accounts = Account.objects.order_by('trigramme')
return render(request, "kfet/account.html", { 'accounts' : accounts })
@ -48,7 +48,7 @@ def account_is_validandfree_ajax(request):
# Account - Create
@login_required
@permission_required('account.is_team')
@permission_required('kfet.is_team')
def account_create(request):
# A envoyer au template
@ -408,6 +408,14 @@ def kpsul_perform_operations(request):
operationgroup = operationgroup_form.save(commit = False)
operations = operation_formset.save(commit = False)
# Specific account's checking
if operationgroup.on_acc.is_cash:
for operation in operations:
if operation.type in [Operation.DEPOSIT, Operation.WITHDRAW]:
data['errors'].append(
{'account': 'Charge et retrait impossible sur LIQ'})
return JsonResponse(data, status=400)
# Retrieving COF grant
cof_grant = Settings.SUBVENTION_COF()
# Retrieving addcosts data
@ -420,6 +428,7 @@ def kpsul_perform_operations(request):
is_addcost = (addcost_for and addcost_amount
and addcost_for != operationgroup.on_acc)
addcost_total = 0
to_checkout_balance = 0
# 1. Calculating amount of each PURCHASE operations
# 1.1 Standard price for n articles
@ -428,24 +437,32 @@ def kpsul_perform_operations(request):
# 2. Updating (no commit) stock of article for PURCHASE operations
# 3. Calculating amount of operation group
# 4. Adding required permissions to perform each operation
# 5. Updating (no commit) new addcost_for's balance if there is one
# 5. Calculating total addcost
# and adding addcost_for in operation instance
# 6. Calculating diff for checkout's balance
for operation in operations:
if operation.type == Operation.PURCHASE:
# 1.1
operation.amount = - operation.article.price * operation.article_nb
if is_addcost:
# 2
# 1.2
operation.addcost_amount = addcost_amount * operation.article_nb
operation.amount -= operation.addcost_amount
# 5
addcost_total += operation.addcost_amount
operation.addcost_for = addcost_for
# 6
if operationgroup.on_acc.is_cash:
to_checkout_balance += -operation.amount
# 1.3
if operationgroup.on_acc.is_cof:
operation.amount = operation.amount / cof_grant_divisor
# 2
operation.article.stock -= operation.article_nb
else:
# Ope.type is deposit or withdraw
# 6 too
to_checkout_balance += operation.amount
# 3
operationgroup.amount += operation.amount
# 4
@ -474,21 +491,27 @@ def kpsul_perform_operations(request):
# Filling cof status for statistics
operationgroup.is_cof = on_acc.is_cof
# Saving account's balance and adding to Negative if not in
on_acc.balance += operationgroup.amount
if on_acc.balance < 0:
if hasattr(on_acc, 'negative'):
if not on_acc.negative.start:
on_acc.negative.start = timezone.now()
on_acc.negative.save()
else:
negative = AccountNegative(
account = on_acc, start = timezone.now())
negative.save()
elif (hasattr(on_acc, 'negative')
and not on_acc.negative.balance_offset):
on_acc.negative.delete()
on_acc.save()
# If not cash account,
# saving account's balance and adding to Negative if not in
if not on_acc.is_cash:
on_acc.balance += operationgroup.amount
on_acc.save()
if on_acc.balance < 0:
if hasattr(on_acc, 'negative'):
if not on_acc.negative.start:
on_acc.negative.start = timezone.now()
on_acc.negative.save()
else:
negative = AccountNegative(
account = on_acc, start = timezone.now())
negative.save()
elif (hasattr(on_acc, 'negative')
and not on_acc.negative.balance_offset):
on_acc.negative.delete()
# Updating checkout's balance
operationgroup.checkout.balance += to_checkout_balance
operationgroup.checkout.save()
# Saving addcost_for with new balance if there is one
if is_addcost: