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
|
@teamkfet_required
|
||||||
def kpsul_cancel_operations(request):
|
def kpsul_cancel_operations(request):
|
||||||
# Pour la réponse
|
# Pour la réponse
|
||||||
data = { 'canceled': [], 'warnings': {}, 'errors': {}}
|
data = {'canceled': {}, 'warnings': {}, 'errors': {}}
|
||||||
|
|
||||||
# Checking if BAD REQUEST (opes_pk not int or not existing)
|
# Checking if BAD REQUEST (opes_pk not int or not existing)
|
||||||
try:
|
try:
|
||||||
|
@ -1105,20 +1105,39 @@ def kpsul_cancel_operations(request):
|
||||||
opes_post = set(map(lambda s: int(s.split()[1]),
|
opes_post = set(map(lambda s: int(s.split()[1]),
|
||||||
filter(lambda s: s.split()[0] == 'ope',
|
filter(lambda s: s.split()[0] == 'ope',
|
||||||
request.POST.getlist('operations[]', []))))
|
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:
|
except ValueError:
|
||||||
return JsonResponse(data, status=400)
|
return JsonResponse(data, status=400)
|
||||||
|
|
||||||
opes_all = (
|
opes_all = (
|
||||||
Operation.objects
|
Operation.objects
|
||||||
.select_related('group', 'group__on_acc', 'group__on_acc__negative')
|
.select_related('group', 'group__on_acc', 'group__on_acc__negative')
|
||||||
.filter(pk__in=opes_post))
|
.filter(pk__in=opes_post))
|
||||||
opes_pk = [ ope.pk for ope in opes_all ]
|
opes_pk = [ ope.pk for ope in opes_all ]
|
||||||
opes_notexisting = [ ope for ope in opes_post if ope not in opes_pk ]
|
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:
|
if opes_notexisting:
|
||||||
data['errors']['opes_notexisting'] = opes_notexisting
|
data['errors']['opes_notexisting'] = opes_notexisting
|
||||||
return JsonResponse(data, status=400)
|
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
|
opes = [] # Pas déjà annulée
|
||||||
|
transfers = []
|
||||||
required_perms = set()
|
required_perms = set()
|
||||||
stop_all = False
|
stop_all = False
|
||||||
cancel_duration = Settings.CANCEL_DURATION()
|
cancel_duration = Settings.CANCEL_DURATION()
|
||||||
|
@ -1129,7 +1148,7 @@ def kpsul_cancel_operations(request):
|
||||||
for ope in opes_all:
|
for ope in opes_all:
|
||||||
if ope.canceled_at:
|
if ope.canceled_at:
|
||||||
# Opération déjà annulée, va pour un warning en Response
|
# 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:
|
else:
|
||||||
opes.append(ope.pk)
|
opes.append(ope.pk)
|
||||||
# Si opé il y a plus de CANCEL_DURATION, permission requise
|
# 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:
|
if not last_stock or last_stock.inventory.at < ope.group.at:
|
||||||
to_articles_stocks[ope.article] += ope.article_nb
|
to_articles_stocks[ope.article] += ope.article_nb
|
||||||
|
|
||||||
if not opes:
|
for transfer in transfers_all:
|
||||||
data['warnings']['already_canceled'] = opes_already_canceled
|
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)
|
return JsonResponse(data)
|
||||||
|
|
||||||
negative_accounts = []
|
negative_accounts = []
|
||||||
|
@ -1211,6 +1246,10 @@ def kpsul_cancel_operations(request):
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
(Operation.objects.filter(pk__in=opes)
|
(Operation.objects.filter(pk__in=opes)
|
||||||
.update(canceled_by=canceled_by, canceled_at=canceled_at))
|
.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:
|
for account in to_accounts_balances:
|
||||||
Account.objects.filter(pk=account.pk).update(
|
Account.objects.filter(pk=account.pk).update(
|
||||||
balance=F('balance') + to_accounts_balances[account])
|
balance=F('balance') + to_accounts_balances[account])
|
||||||
|
@ -1225,11 +1264,13 @@ def kpsul_cancel_operations(request):
|
||||||
stock=F('stock') + to_articles_stocks[article])
|
stock=F('stock') + to_articles_stocks[article])
|
||||||
|
|
||||||
# Websocket data
|
# Websocket data
|
||||||
websocket_data = { 'opegroups': [], 'opes': [], 'checkouts': [], 'articles': [] }
|
websocket_data = {'opegroups': [], 'opes': [],
|
||||||
|
'checkouts': [], 'articles': []}
|
||||||
# Need refresh from db cause we used update on querysets
|
# Need refresh from db cause we used update on querysets
|
||||||
opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts]
|
opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts]
|
||||||
opegroups = (OperationGroup.objects
|
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:
|
for opegroup in opegroups:
|
||||||
websocket_data['opegroups'].append({
|
websocket_data['opegroups'].append({
|
||||||
'cancellation': True,
|
'cancellation': True,
|
||||||
|
@ -1248,7 +1289,8 @@ def kpsul_cancel_operations(request):
|
||||||
# Need refresh from db cause we used update on querysets
|
# Need refresh from db cause we used update on querysets
|
||||||
checkouts_pk = [checkout.pk for checkout in to_checkouts_balances]
|
checkouts_pk = [checkout.pk for checkout in to_checkouts_balances]
|
||||||
checkouts = (Checkout.objects
|
checkouts = (Checkout.objects
|
||||||
.values('id', 'balance').filter(pk__in=checkouts_pk))
|
.values('id', 'balance')
|
||||||
|
.filter(pk__in=checkouts_pk))
|
||||||
for checkout in checkouts:
|
for checkout in checkouts:
|
||||||
websocket_data['checkouts'].append({
|
websocket_data['checkouts'].append({
|
||||||
'id': checkout['id'],
|
'id': checkout['id'],
|
||||||
|
@ -1262,9 +1304,10 @@ def kpsul_cancel_operations(request):
|
||||||
'stock': article['stock']})
|
'stock': article['stock']})
|
||||||
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
|
||||||
|
|
||||||
data['canceled'] = opes
|
data['canceled']['opes'] = opes
|
||||||
if opes_already_canceled:
|
data['canceled']['transfers'] = transfers
|
||||||
data['warnings']['already_canceled'] = opes_already_canceled
|
if already_canceled:
|
||||||
|
data['warnings']['already_canceled'] = already_canceled
|
||||||
return JsonResponse(data)
|
return JsonResponse(data)
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
|
Loading…
Reference in a new issue