diff --git a/kfet/templates/kfet/kpsul.html b/kfet/templates/kfet/kpsul.html index 57bff2b1..1212f625 100644 --- a/kfet/templates/kfet/kpsul.html +++ b/kfet/templates/kfet/kpsul.html @@ -277,39 +277,31 @@ $(document).ready(function() { }); // ----- - // Articles data management + // Articles data // ----- - var articles_data = {} var articles_container = $('#articles_data tbody'); - var articles_new_html = ''; + var article_default_html = ''; function addArticle(article) { - var article_new = $(articles_new_html); - article_new.filter('tr').attr('id', 'article-' + article['id']) + var article_html = $(article_default_html); + article_html.attr('data-article', article['id']) for (var elem in article) { - article_new.filter('.article_'+elem).text(article[elem]) + article_html.find('.'+elem).text(article[elem]) } - articles_container.append(article_new); + articles_container.append(article_html); } - function storeArticlesData(data) { - if (data['articles']['new']) { - for (var article in data['articles']['new']) { - addArticle(article); - } - } - } - - function retrieveArticlesData() { + function getArticlesData() { $.ajax({ dataType: "json", - url : "", + url : "{% url 'kfet.kpsul.articles_data' %}", method : "GET", - data : { 'articles' : articles_data }, }) .done(function(data) { - storeArticlesData(data); + for (var i=0; i diff --git a/kfet/urls.py b/kfet/urls.py index a28abcba..b61c51ef 100644 --- a/kfet/urls.py +++ b/kfet/urls.py @@ -105,4 +105,6 @@ urlpatterns = [ name = 'kfet.kpsul.cancel_operations'), url('^k-psul/history$', views.kpsul_history, name = 'kfet.kpsul.history'), + url('^k-psul/articles_data', views.kpsul_articles_data, + name = 'kfet.kpsul.articles_data') ] diff --git a/kfet/views.py b/kfet/views.py index 3a9872dc..cd867bf5 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -453,22 +453,6 @@ def kpsul_checkout_data(request): raise http404 return JsonResponse(data) -@permission_required('kfet.is_team') -def kpsul_articles_data(request): - articles_old = request.GET.get('articles', []) - data = { 'articles' : {'new': [], 'deleted': [], 'updated': []} } - - articles = (Article.objects - .annotate(category_name=F('category__name')) - .values('id', 'name', 'price', 'stock', 'category_name') - .select_related('category') - .filter(is_sold=True) - ) - for article in articles: - if article['id'] not in articles_old['id']: - pass - return JsonResponse(data) - def get_missing_perms(required_perms, user): missing_perms_codenames = [ (perm.split('.'))[1] for perm in required_perms if not user.has_perm(perm)] @@ -628,12 +612,21 @@ def kpsul_perform_operations(request): 'canceled_by__trigramme': None, 'canceled_at': None, } websocket_data['opegroups'][0]['opes'].append(ope_data) + # Need refresh from db cause we used update on queryset operationgroup.checkout.refresh_from_db() websocket_data['checkouts'] = [{ 'id': operationgroup.checkout.pk, 'balance': operationgroup.checkout.balance, }] - print(operationgroup.checkout.balance) + websocket_data['articles'] = [] + # Need refresh from db cause we used update on querysets + articles_pk = [ article.pk for article in to_articles_stocks] + articles = Article.objects.values('id', 'stock').filter(pk__in=articles_pk) + for article in articles: + websocket_data['articles'].append({ + 'id': article['id'], + 'stock': article['stock'] + }) consumers.KPsul.group_send('kfet.kpsul', websocket_data) return JsonResponse(data) @@ -691,7 +684,7 @@ def kpsul_cancel_operations(request): if ope.group.on_acc.is_cash: to_checkouts_balances[ope.group.on_acc] -= - ope.amount else: - to_checkouts_balances[ope.group.on_acc] -= ope.amount + to_checkouts_balances[ope.group.checkout] -= ope.amount # Pour les stocks d'articles if ope.article and ope.article_nb: to_articles_stocks[ope.article] += ope.article_nb @@ -739,12 +732,16 @@ def kpsul_cancel_operations(request): stock = F('stock') + to_articles_stocks[article]) # Websocket data - websocket_data = { 'opegroups': [], 'opes': [] } - for opegroup in to_groups_amounts: + 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').filter(pk__in=opegroups_pk)) + for opegroup in opegroups: websocket_data['opegroups'].append({ 'cancellation': True, - 'id': opegroup.pk, - 'amount': OperationGroup.objects.get(pk=opegroup.pk).amount, + 'id': opegroup['id'], + 'amount': opegroup['amount'], }) for ope in opes: websocket_data['opes'].append({ @@ -753,6 +750,21 @@ def kpsul_cancel_operations(request): 'canceled_by__trigramme': canceled_by.trigramme, 'canceled_at': canceled_at, }) + # 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)) + for checkout in checkouts: + websocket_data['checkouts'].append({ + 'id': checkout['id'], + 'balance': checkout['balance']}) + # Need refresh from db cause we used update on querysets + articles_pk = [ article.pk for articles in to_articles_stocks] + articles = Article.objects.values('id', 'stock').filter(pk__in=articles_pk) + for article in articles: + websocket_data['articles'].append({ + 'id': article['id'], + 'stock': article['stock']}) consumers.KPsul.group_send('kfet.kpsul', websocket_data) data['canceled'] = opes @@ -779,4 +791,14 @@ def kpsul_history(request): .filter(group__in=opegroups.keys())) for ope in opes: opegroups[ope['group_id']]['opes'].append(ope) - return JsonResponse({ 'opegroups': opegroups }) + return JsonResponse(opegroups) + +@permission_required('kfet.is_team') +def kpsul_articles_data(request): + articles = ( + Article.objects + .values('id', 'name', 'price', 'stock', 'category_id', 'category__name') + .filter(is_sold=True)) + print(vars(articles)) + print(type(articles)) + return JsonResponse({ 'articles': list(articles) })