Ajout des majorations (type concert)

- Ajout des paramètres (modèle Settings) "ADDCOST_AMOUNT" et
  "ADDCOST_FOR" indiquant respectivement le montant et le compte sur lequel
  compter la majoration. Définir l'un de ces paramètres à NULL indique
  qu'il n'y a pas de majoration en cours
- Prise en compte de ces 2 paramètres lors de la validation et
  l'enregistrement d'opérations d'achat (Operation.PURCHASE) dans K-Psul
- Modification du champ "addcost_amount" de Operation. S'il n'y a pas de
  majoration, celui-ci est NULL.
- Correction sur l'enregistrement de "valid_by" dans K-Psul. Celui-ci
  était systématiquement rempli par l'utilisateur connecté ce qui
  n'était pas le comportement souhaité. Il est maintenant rempli
  seulement si une permission autre que kfet.is_team était nécessaire
  pour valider la commande.
- Suppression d'une exception non utilisée dans le modèle Settings
This commit is contained in:
Aurélien Delobelle 2016-08-08 02:50:04 +02:00
parent 03aa26e34f
commit 11452d7633
4 changed files with 87 additions and 12 deletions

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('kfet', '0015_auto_20160807_2324'),
]
operations = [
migrations.AddField(
model_name='settings',
name='value_account',
field=models.ForeignKey(to='kfet.Account', on_delete=django.db.models.deletion.PROTECT, default=None, null=True, blank=True),
),
]

View file

@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('kfet', '0016_settings_value_account'),
]
operations = [
migrations.AlterField(
model_name='operation',
name='addcost_amount',
field=models.DecimalField(blank=True, null=True, decimal_places=2, default=None, max_digits=6),
),
]

View file

@ -415,7 +415,7 @@ class Operation(models.Model):
blank = True, null = True, default = None)
addcost_amount = models.DecimalField(
max_digits = 6, decimal_places = 2,
default = 0)
blank = True, null = True, default = None)
class GlobalPermissions(models.Model):
class Meta:
@ -434,6 +434,9 @@ class Settings(models.Model):
value_decimal = models.DecimalField(
max_digits = 6, decimal_places = 2,
blank = True, null = True, default = None)
value_account = models.ForeignKey(
Account, on_delete = models.PROTECT,
blank = True, null = True, default = None)
@staticmethod
def setting_inst(name):
@ -446,6 +449,16 @@ class Settings(models.Model):
except Settings.DoesNotExist:
return 0
class SettingsError(Exception):
def __init__(self, msg):
self.msg = msg
@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;

View file

@ -409,19 +409,37 @@ def kpsul_perform_operations(request):
# Retrieving COF grant
cof_grant = Settings.SUBVENTION_COF()
cof_grant_divisor = 1 + cof_grant / 100
# Retrieving addcosts data
addcost_amount = Settings.ADDCOST_AMOUNT()
addcost_for = Settings.ADDCOST_FOR()
# Initializing required perms
required_perms = []
# Initializing vars
required_perms = []
cof_grant_divisor = 1 + cof_grant / 100
is_addcost = (addcost_for and addcost_amount
and addcost_for != operationgroup.on_acc)
# 1. Calculating amount of each PURCHASE operations
# 2. and total amount for operation group
# 3. Updating (no commit) stock of article for PURCHASE operations
# 1.1 Standard price for n articles
# 1.2 Adding addcost if there is one
# 1.3 Taking into account cof status
# 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
# and adding addcost_for in operation instance
for operation in operations:
if operation.type == Operation.PURCHASE:
# 1
# 1.1
operation.amount = - operation.article.price * operation.article_nb
if is_addcost:
# 2
operation.addcost_amount = addcost_amount * operation.article_nb
operation.amount -= operation.addcost_amount
# 5
addcost_for.balance += operation.addcost_amount
operation.addcost_for = addcost_for
# 1.3
if operationgroup.on_acc.is_cof:
operation.amount = operation.amount / cof_grant_divisor
# 2
@ -450,7 +468,8 @@ def kpsul_perform_operations(request):
return JsonResponse(data, status=400)
# If 1 perm is required, saving who perform the operations
operationgroup.valid_by = request.user.profile.account_kfet
if len(required_perms) > 0:
operationgroup.valid_by = request.user.profile.account_kfet
# Filling cof status for statistics
operationgroup.is_cof = operationgroup.on_acc.is_cof
@ -458,7 +477,7 @@ def kpsul_perform_operations(request):
# Updating (no commit) account's balance
operationgroup.on_acc.balance += operationgroup.amount
# Apply all saves in a transaction to ensure database integrity
# Saving in a transaction to ensure database integrity
try:
with transaction.atomic():
# Saving operation group
@ -468,6 +487,10 @@ def kpsul_perform_operations(request):
# Saving account with new balance
operationgroup.on_acc.save()
# Saving addcost_for with new balance if there is one
if is_addcost:
addcost_for.save()
# Filling operationgroup id for each operations and saving
# Saving articles with new stock
for operation in operations: