WIP: Aureplop/kpsul js refactor #501

Draft
delobell wants to merge 215 commits from aureplop/kpsul_js_refactor into master
Showing only changes of commit b404c989ff - Show all commits

View file

@ -1095,6 +1095,7 @@ def kpsul_perform_operations(request):
consumers.KPsul.group_send('kfet.kpsul', websocket_data)
return JsonResponse(data)
@teamkfet_required
def kpsul_cancel_operations(request):
# Pour la réponse
@ -1117,8 +1118,8 @@ def kpsul_cancel_operations(request):
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 ]
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
@ -1127,7 +1128,7 @@ def kpsul_cancel_operations(request):
.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 transfer not in transfers_pk]
if transfers_notexisting or opes_notexisting:
if transfers_notexisting:
@ -1140,12 +1141,16 @@ def kpsul_cancel_operations(request):
opes = [] # Pas déjà annulée
transfers = []
required_perms = set()
stop_all = False
stop_all = False
cancel_duration = Settings.CANCEL_DURATION()
to_accounts_balances = defaultdict(lambda:0) # Modifs à faire sur les balances des comptes
to_groups_amounts = defaultdict(lambda:0) # ------ sur les montants des groupes d'opé
to_checkouts_balances = defaultdict(lambda:0) # ------ sur les balances de caisses
to_articles_stocks = defaultdict(lambda:0) # ------ sur les stocks d'articles
# Modifs à faire sur les balances des comptes
to_accounts_balances = defaultdict(lambda: 0)
# ------ sur les montants des groupes d'opé
to_groups_amounts = defaultdict(lambda: 0)
# ------ sur les balances de caisses
to_checkouts_balances = defaultdict(lambda: 0)
# ------ sur les stocks d'articles
to_articles_stocks = defaultdict(lambda: 0)
for ope in opes_all:
if ope.canceled_at:
# Opération déjà annulée, va pour un warning en Response
@ -1176,10 +1181,11 @@ def kpsul_cancel_operations(request):
# par `.save()`, amount_error est recalculé automatiquement,
# ce qui n'est pas le cas en faisant un update sur queryset
# TODO ? : Maj les balance_old de relevés pour modifier l'erreur
last_statement = (CheckoutStatement.objects
.filter(checkout=ope.group.checkout)
.order_by('at')
.last())
last_statement = \
(CheckoutStatement.objects
.filter(checkout=ope.group.checkout)
.order_by('at')
.last())
if not last_statement or last_statement.at < ope.group.at:
if ope.type == Operation.PURCHASE:
if ope.group.on_acc.is_cash:
@ -1321,13 +1327,14 @@ def kpsul_cancel_operations(request):
data['warnings']['already_canceled'] = already_canceled
return JsonResponse(data)
@login_required
def history_json(request):
# Récupération des paramètres
from_date = request.POST.get('from', None)
to_date = request.POST.get('to', None)
to_date = request.POST.get('to', None)
checkouts = request.POST.getlist('checkouts[]', None)
accounts = request.POST.getlist('accounts[]', None)
accounts = request.POST.getlist('accounts[]', None)
transfers_only = request.POST.get('transfersonly', None)
# Construction de la requête (sur les opérations) pour le prefetch
@ -1335,7 +1342,7 @@ def history_json(request):
'canceled_by__trigramme', 'addcost_for__trigramme',
'article__name')
ope_prefetch = Prefetch('opes',
queryset = ope_queryset_prefetch)
queryset=ope_queryset_prefetch)
transfer_queryset_prefetch = Transfer.objects.select_related(
'from_acc__trigramme', 'to_acc__trigramme',
@ -1356,19 +1363,21 @@ def history_json(request):
queryset=transfer_queryset_prefetch,
to_attr='filtered_transfers')
# Construction de la requête principale
opegroups = (OperationGroup.objects
.prefetch_related(ope_prefetch)
.select_related('on_acc__trigramme', 'valid_by__trigramme')
.order_by('at')
opegroups = (
OperationGroup.objects
.prefetch_related(ope_prefetch)
.select_related('on_acc__trigramme',
'valid_by__trigramme')
.order_by('at')
)
transfergroups = (
TransferGroup.objects
.prefetch_related(transfer_prefetch)
.select_related('valid_by__trigramme')
.order_by('at'))
.order_by('at')
)
# Application des filtres
if from_date:
@ -1388,18 +1397,17 @@ def history_json(request):
if not request.user.has_perm('kfet.is_team'):
opegroups = opegroups.filter(on_acc=request.user.profile.account_kfet)
# Construction de la réponse
opegroups_list = []
for opegroup in opegroups:
opegroup_dict = {
'type' : 'opegroup',
'id' : opegroup.id,
'amount' : opegroup.amount,
'at' : opegroup.at,
'is_cof' : opegroup.is_cof,
'comment' : opegroup.comment,
'opes' : [],
'type': 'opegroup',
'id': opegroup.id,
'amount': opegroup.amount,
'at': opegroup.at,
'is_cof': opegroup.is_cof,
'comment': opegroup.comment,
'opes': [],
'on_acc__trigramme':
opegroup.on_acc and opegroup.on_acc.trigramme or None,
}
@ -1408,13 +1416,13 @@ def history_json(request):
opegroup.valid_by and opegroup.valid_by.trigramme or None)
for ope in opegroup.opes.all():
ope_dict = {
'id' : ope.id,
'type' : ope.type,
'amount' : ope.amount,
'article_nb' : ope.article_nb,
'is_checkout' : ope.is_checkout,
'id': ope.id,
'type': ope.type,
'amount': ope.amount,
'article_nb': ope.article_nb,
'is_checkout': ope.is_checkout,
'addcost_amount': ope.addcost_amount,
'canceled_at' : ope.canceled_at,
'canceled_at': ope.canceled_at,
'article__name':
ope.article and ope.article.name or None,
'addcost_for__trigramme':
@ -1437,9 +1445,9 @@ def history_json(request):
}
if request.user.has_perm('kfet.is_team'):
transfergroup_dict['valid_by__trigramme'] = (
transfergroup.valid_by
and transfergroup.valid_by.trigramme
or None)
transfergroup.valid_by and
transfergroup.valid_by.trigramme or
None)
for transfer in transfergroup.filtered_transfers:
transfer_dict = {
@ -1451,9 +1459,9 @@ def history_json(request):
}
if request.user.has_perm('kfet.is_team'):
transfer_dict['canceled_by__trigramme'] = (
transfer.canceled_by
and transfer.canceled_by.trigramme
or None)
transfer.canceled_by and
transfer.canceled_by.trigramme or
None)
transfergroup_dict['opes'].append(transfer_dict)
opegroups_list.append(transfergroup_dict)
@ -1527,20 +1535,24 @@ def transfers_create(request):
return render(request, 'kfet/transfers_create.html',
{ 'transfer_formset': transfer_formset })
@teamkfet_required
def perform_transfers(request):
data = { 'errors': {}, 'transfers': [], 'transfergroup': 0 }
data = {'errors': {}, 'transfers': [], 'transfergroup': 0}
# Checking transfer_formset
transfer_formset = TransferFormSet(request.POST)
if not transfer_formset.is_valid():
return JsonResponse({ 'errors': list(transfer_formset.errors)}, status=400)
return JsonResponse({'errors': list(transfer_formset.errors)},
status=400)
transfers = transfer_formset.save(commit = False)
transfers = transfer_formset.save(commit=False)
# Initializing vars
required_perms = set(['kfet.add_transfer']) # Required perms to perform all transfers
to_accounts_balances = defaultdict(lambda:0) # For balances of accounts
# Required perms to perform all transfers
required_perms = set(['kfet.add_transfer'])
# For balances of accounts
to_accounts_balances = defaultdict(lambda: 0)
for transfer in transfers:
to_accounts_balances[transfer.from_acc] -= transfer.amount
@ -1552,7 +1564,7 @@ def perform_transfers(request):
# Checking if ok on all accounts
for account in to_accounts_balances:
(perms, stop) = account.perms_to_perform_operation(
amount = to_accounts_balances[account])
amount=to_accounts_balances[account])
required_perms |= perms
stop_all = stop_all or stop
if stop:
@ -1578,7 +1590,7 @@ def perform_transfers(request):
# Updating balances accounts
for account in to_accounts_balances:
Account.objects.filter(pk=account.pk).update(
balance = F('balance') + to_accounts_balances[account])
balance=F('balance') + to_accounts_balances[account])
account.refresh_from_db()
if account.balance < 0:
if hasattr(account, 'negative'):
@ -1587,10 +1599,10 @@ def perform_transfers(request):
account.negative.save()
else:
negative = AccountNegative(
account = account, start = timezone.now())
account=account, start=timezone.now())
negative.save()
elif (hasattr(account, 'negative')
and not account.negative.balance_offset):
elif (hasattr(account, 'negative') and
not account.negative.balance_offset):
account.negative.delete()
# Saving transfer group
@ -1611,8 +1623,8 @@ def perform_transfers(request):
'id': transfergroup.pk,
'at': transfergroup.at,
'comment': transfergroup.comment,
'valid_by__trigramme': ( transfergroup.valid_by and
transfergroup.valid_by.trigramme or None),
'valid_by__trigramme': (transfergroup.valid_by and
transfergroup.valid_by.trigramme or None),
'opes': [],
}]
for transfer in transfers: