forked from DGNum/gestioCOF
unite cancel_ope and cancel_transfer
This commit is contained in:
parent
49bef61e53
commit
66304359c0
1 changed files with 70 additions and 27 deletions
|
@ -1097,7 +1097,7 @@ def kpsul_perform_operations(request):
|
|||
@teamkfet_required
|
||||
def kpsul_cancel_operations(request):
|
||||
# Pour la réponse
|
||||
data = { 'canceled': [], 'warnings': {}, 'errors': {}}
|
||||
data = {'canceled': {}, 'warnings': {}, 'errors': {}}
|
||||
|
||||
# Checking if BAD REQUEST (opes_pk not int or not existing)
|
||||
try:
|
||||
|
@ -1105,20 +1105,39 @@ def kpsul_cancel_operations(request):
|
|||
opes_post = set(map(lambda s: int(s.split()[1]),
|
||||
filter(lambda s: s.split()[0] == 'ope',
|
||||
request.POST.getlist('operations[]', []))))
|
||||
transfers_post = \
|
||||
set(map(lambda s: int(s.split()[1]),
|
||||
filter(lambda s: s.split()[0] == 'transfer',
|
||||
request.POST.getlist('operations[]', []))))
|
||||
except ValueError:
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
opes_all = (
|
||||
Operation.objects
|
||||
.select_related('group', 'group__on_acc', 'group__on_acc__negative')
|
||||
.filter(pk__in=opes_post))
|
||||
opes_pk = [ ope.pk for ope in opes_all ]
|
||||
opes_notexisting = [ ope for ope in opes_post if ope not in opes_pk ]
|
||||
|
||||
transfers_all = (
|
||||
Transfer.objects
|
||||
.select_related('group', 'from_acc', 'from_acc__negative',
|
||||
'to_acc', 'to_acc__negative')
|
||||
.filter(pk__in=transfers_post))
|
||||
transfers_pk = [transfer.pk for transfer in transfers_all]
|
||||
transfers_notexisting = [transfer for transfer in transfers_post
|
||||
if transfer not in transfers_pk]
|
||||
|
||||
if transfers_notexisting or opes_notexisting:
|
||||
if transfers_notexisting:
|
||||
data['errors']['transfers_notexisting'] = transfers_notexisting
|
||||
if opes_notexisting:
|
||||
data['errors']['opes_notexisting'] = opes_notexisting
|
||||
return JsonResponse(data, status=400)
|
||||
|
||||
opes_already_canceled = [] # Déjà annulée
|
||||
already_canceled = {} # Opération/Transfert déjà annulé
|
||||
opes = [] # Pas déjà annulée
|
||||
transfers = []
|
||||
required_perms = set()
|
||||
stop_all = False
|
||||
cancel_duration = Settings.CANCEL_DURATION()
|
||||
|
@ -1129,7 +1148,7 @@ def kpsul_cancel_operations(request):
|
|||
for ope in opes_all:
|
||||
if ope.canceled_at:
|
||||
# Opération déjà annulée, va pour un warning en Response
|
||||
opes_already_canceled.append(ope.pk)
|
||||
already_canceled['opes'].append(ope.pk)
|
||||
else:
|
||||
opes.append(ope.pk)
|
||||
# Si opé il y a plus de CANCEL_DURATION, permission requise
|
||||
|
@ -1183,8 +1202,24 @@ def kpsul_cancel_operations(request):
|
|||
if not last_stock or last_stock.inventory.at < ope.group.at:
|
||||
to_articles_stocks[ope.article] += ope.article_nb
|
||||
|
||||
if not opes:
|
||||
data['warnings']['already_canceled'] = opes_already_canceled
|
||||
for transfer in transfers_all:
|
||||
if transfer.canceled_at:
|
||||
# Transfert déjà annulé, va pour un warning en Response
|
||||
already_canceled['transfers'].append(transfer.pk)
|
||||
else:
|
||||
transfers.append(transfer.pk)
|
||||
# Si transfer il y a plus de CANCEL_DURATION, permission requise
|
||||
if transfer.group.at + cancel_duration < timezone.now():
|
||||
required_perms.add('kfet.cancel_old_operations')
|
||||
|
||||
# Calcul de toutes modifs à faire en cas de validation
|
||||
|
||||
# Pour les balances de comptes
|
||||
to_accounts_balances[transfer.from_acc] += transfer.amount
|
||||
to_accounts_balances[transfer.to_acc] += -transfer.amount
|
||||
|
||||
if not opes and not transfers:
|
||||
data['warnings']['already_canceled'] = already_canceled
|
||||
return JsonResponse(data)
|
||||
|
||||
negative_accounts = []
|
||||
|
@ -1211,6 +1246,10 @@ def kpsul_cancel_operations(request):
|
|||
with transaction.atomic():
|
||||
(Operation.objects.filter(pk__in=opes)
|
||||
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
||||
|
||||
(Transfer.objects.filter(pk__in=transfers)
|
||||
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
||||
|
||||
for account in to_accounts_balances:
|
||||
Account.objects.filter(pk=account.pk).update(
|
||||
balance=F('balance') + to_accounts_balances[account])
|
||||
|
@ -1225,11 +1264,13 @@ def kpsul_cancel_operations(request):
|
|||
stock=F('stock') + to_articles_stocks[article])
|
||||
|
||||
# Websocket data
|
||||
websocket_data = { 'opegroups': [], 'opes': [], 'checkouts': [], 'articles': [] }
|
||||
websocket_data = {'opegroups': [], 'opes': [],
|
||||
'checkouts': [], 'articles': []}
|
||||
# Need refresh from db cause we used update on querysets
|
||||
opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts]
|
||||
opegroups = (OperationGroup.objects
|
||||
.values('id','amount','is_cof').filter(pk__in=opegroups_pk))
|
||||
.values('id', 'amount', 'is_cof')
|
||||
.filter(pk__in=opegroups_pk))
|
||||
for opegroup in opegroups:
|
||||
websocket_data['opegroups'].append({
|
||||
'cancellation': True,
|
||||
|
@ -1248,7 +1289,8 @@ def kpsul_cancel_operations(request):
|
|||
# Need refresh from db cause we used update on querysets
|
||||
checkouts_pk = [checkout.pk for checkout in to_checkouts_balances]
|
||||
checkouts = (Checkout.objects
|
||||
.values('id', 'balance').filter(pk__in=checkouts_pk))
|
||||
.values('id', 'balance')
|
||||
.filter(pk__in=checkouts_pk))
|
||||
for checkout in checkouts:
|
||||
websocket_data['checkouts'].append({
|
||||
'id': checkout['id'],
|
||||
|
@ -1262,9 +1304,10 @@ def kpsul_cancel_operations(request):
|
|||
'stock': article['stock']})
|
||||
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
||||
|
||||
data['canceled'] = opes
|
||||
if opes_already_canceled:
|
||||
data['warnings']['already_canceled'] = opes_already_canceled
|
||||
data['canceled']['opes'] = opes
|
||||
data['canceled']['transfers'] = transfers
|
||||
if already_canceled:
|
||||
data['warnings']['already_canceled'] = already_canceled
|
||||
return JsonResponse(data)
|
||||
|
||||
@login_required
|
||||
|
|
Loading…
Reference in a new issue