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

View file

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

View file

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