forked from DGNum/gestioCOF
PEP8 for perform_operation
This commit is contained in:
parent
a107fa0309
commit
de724a2c0d
1 changed files with 54 additions and 39 deletions
|
@ -921,13 +921,14 @@ def kpsul_update_addcost(request):
|
||||||
addcost_form = AddcostForm(request.POST)
|
addcost_form = AddcostForm(request.POST)
|
||||||
|
|
||||||
if not addcost_form.is_valid():
|
if not addcost_form.is_valid():
|
||||||
data = { 'errors': { 'addcost': list(addcost_form.errors) } }
|
data = {'errors': {'addcost': list(addcost_form.errors)}}
|
||||||
return JsonResponse(data, status=400)
|
return JsonResponse(data, status=400)
|
||||||
required_perms = ['kfet.manage_addcosts']
|
required_perms = ['kfet.manage_addcosts']
|
||||||
if not request.user.has_perms(required_perms):
|
if not request.user.has_perms(required_perms):
|
||||||
data = {
|
data = {
|
||||||
'errors': {
|
'errors': {
|
||||||
'missing_perms': get_missing_perms(required_perms, request.user)
|
'missing_perms': get_missing_perms(required_perms,
|
||||||
|
request.user)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return JsonResponse(data, status=403)
|
return JsonResponse(data, status=403)
|
||||||
|
@ -935,7 +936,8 @@ def kpsul_update_addcost(request):
|
||||||
trigramme = addcost_form.cleaned_data['trigramme']
|
trigramme = addcost_form.cleaned_data['trigramme']
|
||||||
account = trigramme and Account.objects.get(trigramme=trigramme) or None
|
account = trigramme and Account.objects.get(trigramme=trigramme) or None
|
||||||
Settings.objects.filter(name='ADDCOST_FOR').update(value_account=account)
|
Settings.objects.filter(name='ADDCOST_FOR').update(value_account=account)
|
||||||
Settings.objects.filter(name='ADDCOST_AMOUNT').update(value_decimal=addcost_form.cleaned_data['amount'])
|
(Settings.objects.filter(name='ADDCOST_AMOUNT')
|
||||||
|
.update(value_decimal=addcost_form.cleaned_data['amount']))
|
||||||
cache.delete('ADDCOST_FOR')
|
cache.delete('ADDCOST_FOR')
|
||||||
cache.delete('ADDCOST_AMOUNT')
|
cache.delete('ADDCOST_AMOUNT')
|
||||||
data = {
|
data = {
|
||||||
|
@ -947,20 +949,23 @@ def kpsul_update_addcost(request):
|
||||||
consumers.KPsul.group_send('kfet.kpsul', data)
|
consumers.KPsul.group_send('kfet.kpsul', data)
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
|
||||||
def get_missing_perms(required_perms, user):
|
def get_missing_perms(required_perms, user):
|
||||||
missing_perms_codenames = [ (perm.split('.'))[1]
|
missing_perms_codenames = [(perm.split('.'))[1]
|
||||||
for perm in required_perms if not user.has_perm(perm)]
|
for perm in required_perms
|
||||||
|
if not user.has_perm(perm)]
|
||||||
missing_perms = list(
|
missing_perms = list(
|
||||||
Permission.objects
|
Permission.objects
|
||||||
.filter(codename__in=missing_perms_codenames)
|
.filter(codename__in=missing_perms_codenames)
|
||||||
.values_list('name', flat=True))
|
.values_list('name', flat=True))
|
||||||
return missing_perms
|
return missing_perms
|
||||||
|
|
||||||
|
|
||||||
@teamkfet_required
|
@teamkfet_required
|
||||||
def kpsul_perform_operations(request):
|
def kpsul_perform_operations(request):
|
||||||
# Initializing response data
|
# Initializing response data
|
||||||
data = { 'operationgroup': 0, 'operations': [],
|
data = {'operationgroup': 0, 'operations': [],
|
||||||
'warnings': {}, 'errors': {} }
|
'warnings': {}, 'errors': {}}
|
||||||
|
|
||||||
# Checking operationgroup
|
# Checking operationgroup
|
||||||
operationgroup_form = KPsulOperationGroupForm(request.POST)
|
operationgroup_form = KPsulOperationGroupForm(request.POST)
|
||||||
|
@ -968,7 +973,7 @@ def kpsul_perform_operations(request):
|
||||||
data['errors']['operation_group'] = list(operationgroup_form.errors)
|
data['errors']['operation_group'] = list(operationgroup_form.errors)
|
||||||
|
|
||||||
# Checking operation_formset
|
# Checking operation_formset
|
||||||
operation_formset = KPsulOperationFormSet(request.POST)
|
operation_formset = KPsulOperationFormSet(request.POST)
|
||||||
if not operation_formset.is_valid():
|
if not operation_formset.is_valid():
|
||||||
data['errors']['operations'] = list(operation_formset.errors)
|
data['errors']['operations'] = list(operation_formset.errors)
|
||||||
|
|
||||||
|
@ -977,34 +982,36 @@ def kpsul_perform_operations(request):
|
||||||
return JsonResponse(data, status=400)
|
return JsonResponse(data, status=400)
|
||||||
|
|
||||||
# Pre-saving (no commit)
|
# Pre-saving (no commit)
|
||||||
operationgroup = operationgroup_form.save(commit = False)
|
operationgroup = operationgroup_form.save(commit=False)
|
||||||
operations = operation_formset.save(commit = False)
|
operations = operation_formset.save(commit=False)
|
||||||
|
|
||||||
# Retrieving COF grant
|
# Retrieving COF grant
|
||||||
cof_grant = Settings.SUBVENTION_COF()
|
cof_grant = Settings.SUBVENTION_COF()
|
||||||
# Retrieving addcosts data
|
# Retrieving addcosts data
|
||||||
addcost_amount = Settings.ADDCOST_AMOUNT()
|
addcost_amount = Settings.ADDCOST_AMOUNT()
|
||||||
addcost_for = Settings.ADDCOST_FOR()
|
addcost_for = Settings.ADDCOST_FOR()
|
||||||
|
|
||||||
# Initializing vars
|
# Initializing vars
|
||||||
required_perms = set() # Required perms to perform all operations
|
required_perms = set() # Required perms to perform all operations
|
||||||
cof_grant_divisor = 1 + cof_grant / 100
|
cof_grant_divisor = 1 + cof_grant / 100
|
||||||
to_addcost_for_balance = 0 # For balance of addcost_for
|
to_addcost_for_balance = 0 # For balance of addcost_for
|
||||||
to_checkout_balance = 0 # For balance of selected checkout
|
to_checkout_balance = 0 # For balance of selected checkout
|
||||||
to_articles_stocks = defaultdict(lambda:0) # For stocks articles
|
to_articles_stocks = defaultdict(lambda: 0) # For stocks articles
|
||||||
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)
|
||||||
need_comment = operationgroup.on_acc.need_comment
|
need_comment = operationgroup.on_acc.need_comment
|
||||||
|
|
||||||
# Filling data of each operations + operationgroup + calculating other stuffs
|
# Filling data of each operations
|
||||||
|
# + operationgroup + calculating other stuffs
|
||||||
for operation in operations:
|
for operation in operations:
|
||||||
if operation.type == Operation.PURCHASE:
|
if operation.type == Operation.PURCHASE:
|
||||||
operation.amount = - operation.article.price * operation.article_nb
|
operation.amount = - operation.article.price * operation.article_nb
|
||||||
if is_addcost & operation.article.category.has_addcost:
|
if is_addcost & operation.article.category.has_addcost:
|
||||||
operation.addcost_for = addcost_for
|
operation.addcost_for = addcost_for
|
||||||
operation.addcost_amount = addcost_amount * operation.article_nb
|
operation.addcost_amount = addcost_amount \
|
||||||
operation.amount -= operation.addcost_amount
|
* operation.article_nb
|
||||||
to_addcost_for_balance += operation.addcost_amount
|
operation.amount -= operation.addcost_amount
|
||||||
|
to_addcost_for_balance += operation.addcost_amount
|
||||||
if operationgroup.on_acc.is_cash:
|
if operationgroup.on_acc.is_cash:
|
||||||
operation.is_checkout = True
|
operation.is_checkout = True
|
||||||
to_checkout_balance += -operation.amount
|
to_checkout_balance += -operation.amount
|
||||||
|
@ -1012,12 +1019,14 @@ def kpsul_perform_operations(request):
|
||||||
operation.is_checkout = False
|
operation.is_checkout = False
|
||||||
if operationgroup.on_acc.is_cof:
|
if operationgroup.on_acc.is_cof:
|
||||||
if is_addcost & operation.article.category.has_addcost:
|
if is_addcost & operation.article.category.has_addcost:
|
||||||
operation.addcost_amount = operation.addcost_amount / cof_grant_divisor
|
operation.addcost_amount = operation.addcost_amount \
|
||||||
|
/ cof_grant_divisor
|
||||||
operation.amount = operation.amount / cof_grant_divisor
|
operation.amount = operation.amount / cof_grant_divisor
|
||||||
to_articles_stocks[operation.article] -= operation.article_nb
|
to_articles_stocks[operation.article] -= operation.article_nb
|
||||||
else:
|
else:
|
||||||
if operationgroup.on_acc.is_cash:
|
if operationgroup.on_acc.is_cash:
|
||||||
data['errors']['account'] = 'Charge et retrait impossible sur LIQ'
|
data['errors']['account'] = ("Charge et retrait"
|
||||||
|
" impossible sur LIQ")
|
||||||
to_checkout_balance += operation.amount
|
to_checkout_balance += operation.amount
|
||||||
operationgroup.amount += operation.amount
|
operationgroup.amount += operation.amount
|
||||||
if operation.type == Operation.DEPOSIT:
|
if operation.type == Operation.DEPOSIT:
|
||||||
|
@ -1029,8 +1038,10 @@ def kpsul_perform_operations(request):
|
||||||
if operationgroup.on_acc.is_cof:
|
if operationgroup.on_acc.is_cof:
|
||||||
to_addcost_for_balance = to_addcost_for_balance / cof_grant_divisor
|
to_addcost_for_balance = to_addcost_for_balance / cof_grant_divisor
|
||||||
|
|
||||||
(perms, stop) = operationgroup.on_acc.perms_to_perform_operation(
|
(perms, stop) = (operationgroup.on_acc
|
||||||
amount = operationgroup.amount)
|
.perms_to_perform_operation(
|
||||||
|
amount=operationgroup.amount)
|
||||||
|
)
|
||||||
required_perms |= perms
|
required_perms |= perms
|
||||||
|
|
||||||
if need_comment:
|
if need_comment:
|
||||||
|
@ -1059,7 +1070,7 @@ def kpsul_perform_operations(request):
|
||||||
# saving account's balance and adding to Negative if not in
|
# saving account's balance and adding to Negative if not in
|
||||||
if not operationgroup.on_acc.is_cash:
|
if not operationgroup.on_acc.is_cash:
|
||||||
Account.objects.filter(pk=operationgroup.on_acc.pk).update(
|
Account.objects.filter(pk=operationgroup.on_acc.pk).update(
|
||||||
balance = F('balance') + operationgroup.amount)
|
balance=F('balance') + operationgroup.amount)
|
||||||
operationgroup.on_acc.refresh_from_db()
|
operationgroup.on_acc.refresh_from_db()
|
||||||
if operationgroup.on_acc.balance < 0:
|
if operationgroup.on_acc.balance < 0:
|
||||||
if hasattr(operationgroup.on_acc, 'negative'):
|
if hasattr(operationgroup.on_acc, 'negative'):
|
||||||
|
@ -1068,7 +1079,7 @@ def kpsul_perform_operations(request):
|
||||||
operationgroup.on_acc.negative.save()
|
operationgroup.on_acc.negative.save()
|
||||||
else:
|
else:
|
||||||
negative = AccountNegative(
|
negative = AccountNegative(
|
||||||
account = operationgroup.on_acc, start = timezone.now())
|
account=operationgroup.on_acc, start = timezone.now())
|
||||||
negative.save()
|
negative.save()
|
||||||
elif (hasattr(operationgroup.on_acc, 'negative')
|
elif (hasattr(operationgroup.on_acc, 'negative')
|
||||||
and not operationgroup.on_acc.negative.balance_offset):
|
and not operationgroup.on_acc.negative.balance_offset):
|
||||||
|
@ -1077,12 +1088,12 @@ def kpsul_perform_operations(request):
|
||||||
# Updating checkout's balance
|
# Updating checkout's balance
|
||||||
if to_checkout_balance:
|
if to_checkout_balance:
|
||||||
Checkout.objects.filter(pk=operationgroup.checkout.pk).update(
|
Checkout.objects.filter(pk=operationgroup.checkout.pk).update(
|
||||||
balance = F('balance') + to_checkout_balance)
|
balance=F('balance') + to_checkout_balance)
|
||||||
|
|
||||||
# Saving addcost_for with new balance if there is one
|
# Saving addcost_for with new balance if there is one
|
||||||
if is_addcost and to_addcost_for_balance:
|
if is_addcost and to_addcost_for_balance:
|
||||||
Account.objects.filter(pk=addcost_for.pk).update(
|
Account.objects.filter(pk=addcost_for.pk).update(
|
||||||
balance = F('balance') + to_addcost_for_balance)
|
balance=F('balance') + to_addcost_for_balance)
|
||||||
|
|
||||||
# Saving operation group
|
# Saving operation group
|
||||||
operationgroup.save()
|
operationgroup.save()
|
||||||
|
@ -1097,7 +1108,7 @@ def kpsul_perform_operations(request):
|
||||||
# Updating articles stock
|
# Updating articles stock
|
||||||
for article in to_articles_stocks:
|
for article in to_articles_stocks:
|
||||||
Article.objects.filter(pk=article.pk).update(
|
Article.objects.filter(pk=article.pk).update(
|
||||||
stock = F('stock') + to_articles_stocks[article])
|
stock=F('stock') + to_articles_stocks[article])
|
||||||
|
|
||||||
# Websocket data
|
# Websocket data
|
||||||
websocket_data = {}
|
websocket_data = {}
|
||||||
|
@ -1109,18 +1120,21 @@ def kpsul_perform_operations(request):
|
||||||
'at': operationgroup.at,
|
'at': operationgroup.at,
|
||||||
'is_cof': operationgroup.is_cof,
|
'is_cof': operationgroup.is_cof,
|
||||||
'comment': operationgroup.comment,
|
'comment': operationgroup.comment,
|
||||||
'valid_by__trigramme': ( operationgroup.valid_by and
|
'valid_by__trigramme': (operationgroup.valid_by and
|
||||||
operationgroup.valid_by.trigramme or None),
|
operationgroup.valid_by.trigramme or None),
|
||||||
'on_acc__trigramme': operationgroup.on_acc.trigramme,
|
'on_acc__trigramme': operationgroup.on_acc.trigramme,
|
||||||
'opes': [],
|
'opes': [],
|
||||||
}]
|
}]
|
||||||
for operation in operations:
|
for operation in operations:
|
||||||
ope_data = {
|
ope_data = {
|
||||||
'id': operation.pk, 'type': operation.type, 'amount': operation.amount,
|
'id': operation.pk, 'type': operation.type,
|
||||||
|
'amount': operation.amount,
|
||||||
'addcost_amount': operation.addcost_amount,
|
'addcost_amount': operation.addcost_amount,
|
||||||
'addcost_for__trigramme': is_addcost and addcost_for.trigramme or None,
|
'addcost_for__trigramme': is_addcost
|
||||||
|
and addcost_for.trigramme or None,
|
||||||
'is_checkout': operation.is_checkout,
|
'is_checkout': operation.is_checkout,
|
||||||
'article__name': operation.article and operation.article.name or None,
|
'article__name': operation.article
|
||||||
|
and operation.article.name or None,
|
||||||
'article_nb': operation.article_nb,
|
'article_nb': operation.article_nb,
|
||||||
'group_id': operationgroup.pk,
|
'group_id': operationgroup.pk,
|
||||||
'canceled_by__trigramme': None, 'canceled_at': None,
|
'canceled_by__trigramme': None, 'canceled_at': None,
|
||||||
|
@ -1134,7 +1148,7 @@ def kpsul_perform_operations(request):
|
||||||
}]
|
}]
|
||||||
websocket_data['articles'] = []
|
websocket_data['articles'] = []
|
||||||
# Need refresh from db cause we used update on querysets
|
# Need refresh from db cause we used update on querysets
|
||||||
articles_pk = [ article.pk for article in to_articles_stocks]
|
articles_pk = [article.pk for article in to_articles_stocks]
|
||||||
articles = Article.objects.values('id', 'stock').filter(pk__in=articles_pk)
|
articles = Article.objects.values('id', 'stock').filter(pk__in=articles_pk)
|
||||||
for article in articles:
|
for article in articles:
|
||||||
websocket_data['articles'].append({
|
websocket_data['articles'].append({
|
||||||
|
@ -1144,6 +1158,7 @@ def kpsul_perform_operations(request):
|
||||||
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
|
|
||||||
@teamkfet_required
|
@teamkfet_required
|
||||||
def kpsul_cancel_operations(request):
|
def kpsul_cancel_operations(request):
|
||||||
# Pour la réponse
|
# Pour la réponse
|
||||||
|
|
Loading…
Reference in a new issue