Affichage articles dans K-Psul

- Ajout de la liste des articles dans K-Psul
- Synchro des stocks de cette liste avec les autres instances de K-Psul

- Utilisation des attributs "data-..." pour stocker les id des articles,
  groupes d'opérations et opérations dans K-Psul
- Correction de la vue kpsul_cancel_operations sur le calcul des
  nouvelles balances de caisses
- Même vue : multiples requêtes dans la récupération des nouveaux
  montants de groupes d'opérations remplacées par 1 seule requête pour
  le websocket
- Même vue : ajout des données de nouvelles balances de caisses pour le
  websocket
This commit is contained in:
Aurélien Delobelle 2016-08-14 23:37:05 +02:00
parent 7c2a18bb5d
commit 55fd10f9d4
3 changed files with 71 additions and 49 deletions

View file

@ -277,39 +277,31 @@ $(document).ready(function() {
}); });
// ----- // -----
// Articles data management // Articles data
// ----- // -----
var articles_data = {}
var articles_container = $('#articles_data tbody'); var articles_container = $('#articles_data tbody');
var articles_new_html = '<tr><td class="article_name"></td><td class="article_category_name"></td><td class="article_price"></td><td class="article_stock"></td></tr>'; var article_default_html = '<tr><td class="name"></td><td class="category__name"></td><td class="price"></td><td class="stock"></td></tr>';
function addArticle(article) { function addArticle(article) {
var article_new = $(articles_new_html); var article_html = $(article_default_html);
article_new.filter('tr').attr('id', 'article-' + article['id']) article_html.attr('data-article', article['id'])
for (var elem in article) { 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) { function getArticlesData() {
if (data['articles']['new']) {
for (var article in data['articles']['new']) {
addArticle(article);
}
}
}
function retrieveArticlesData() {
$.ajax({ $.ajax({
dataType: "json", dataType: "json",
url : "", url : "{% url 'kfet.kpsul.articles_data' %}",
method : "GET", method : "GET",
data : { 'articles' : articles_data },
}) })
.done(function(data) { .done(function(data) {
storeArticlesData(data); for (var i=0; i<data['articles'].length; i++) {
addArticle(data['articles'][i]);
}
}); });
} }
@ -335,8 +327,7 @@ $(document).ready(function() {
// Préparation ajout groupe d'opés à l'historique // Préparation ajout groupe d'opés à l'historique
var opegroup_html = $(history_operationgroup_html); var opegroup_html = $(history_operationgroup_html);
opegroup_html opegroup_html.attr('data-opegroup', opegroup['id']);
.attr('id', 'opegroup-'+opegroup['id']);
opegroup_html.find('.general').html( opegroup_html.find('.general').html(
opegroup['at'] opegroup['at']
+' '+opegroup['on_acc__trigramme'] +' '+opegroup['on_acc__trigramme']
@ -351,7 +342,7 @@ $(document).ready(function() {
ope_html.addClass('canceled') ope_html.addClass('canceled')
} }
ope_html ope_html
.attr('id', 'ope-'+ope['id']) .attr('data-ope', ope['id'])
.text(getTextOpe(ope)); .text(getTextOpe(ope));
opegroup_html.append(ope_html); opegroup_html.append(ope_html);
} }
@ -366,19 +357,19 @@ $(document).ready(function() {
method : "GET", method : "GET",
}) })
.done(function(data) { .done(function(data) {
for (var opegroup_id in data['opegroups']) { for (var opegroup_id in data) {
addOpeGroup(data['opegroups'][opegroup_id]); addOpeGroup(data[opegroup_id]);
} }
}); });
} }
function cancelOpeGroup(opegroup) { function cancelOpeGroup(opegroup) {
var opegroup_html = history_container.find('#opegroup-'+opegroup['id']); var opegroup_html = history_container.find('[data-opegroup='+opegroup['id']+']');
opegroup_html.find('.amount').text(opegroup['amount']); opegroup_html.find('.amount').text(opegroup['amount']);
} }
function cancelOpe(ope) { function cancelOpe(ope) {
var ope_html = history_container.find('#ope-'+ope['id']); var ope_html = history_container.find('[data-ope='+ope['id']+']');
ope_html.addClass('canceled'); ope_html.addClass('canceled');
} }
@ -392,6 +383,7 @@ $(document).ready(function() {
data['opegroups'] = data['opegroups'] || []; data['opegroups'] = data['opegroups'] || [];
data['opes'] = data['opes'] || []; data['opes'] = data['opes'] || [];
data['checkouts'] = data['checkouts'] || []; data['checkouts'] = data['checkouts'] || [];
data['articles'] = data['articles'] || [];
for (var i=0; i<data['opegroups'].length; i++) { for (var i=0; i<data['opegroups'].length; i++) {
if (data['opegroups'][i]['add']) { if (data['opegroups'][i]['add']) {
@ -411,6 +403,11 @@ $(document).ready(function() {
displayCheckoutData(); displayCheckoutData();
} }
} }
for (var i=0; i<data['articles'].length; i++) {
article = data['articles'][i];
articles_container.find('[data-article='+article['id']+'] .stock')
.text(article['stock']);
}
} }
// ----- // -----
@ -419,6 +416,7 @@ $(document).ready(function() {
resetAccountData(); resetAccountData();
resetCheckoutData(); resetCheckoutData();
getHistory(); getHistory();
getArticlesData();
}); });
</script> </script>

View file

@ -105,4 +105,6 @@ urlpatterns = [
name = 'kfet.kpsul.cancel_operations'), name = 'kfet.kpsul.cancel_operations'),
url('^k-psul/history$', views.kpsul_history, url('^k-psul/history$', views.kpsul_history,
name = 'kfet.kpsul.history'), name = 'kfet.kpsul.history'),
url('^k-psul/articles_data', views.kpsul_articles_data,
name = 'kfet.kpsul.articles_data')
] ]

View file

@ -453,22 +453,6 @@ def kpsul_checkout_data(request):
raise http404 raise http404
return JsonResponse(data) 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): 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)]
@ -628,12 +612,21 @@ def kpsul_perform_operations(request):
'canceled_by__trigramme': None, 'canceled_at': None, 'canceled_by__trigramme': None, 'canceled_at': None,
} }
websocket_data['opegroups'][0]['opes'].append(ope_data) websocket_data['opegroups'][0]['opes'].append(ope_data)
# Need refresh from db cause we used update on queryset
operationgroup.checkout.refresh_from_db() operationgroup.checkout.refresh_from_db()
websocket_data['checkouts'] = [{ websocket_data['checkouts'] = [{
'id': operationgroup.checkout.pk, 'id': operationgroup.checkout.pk,
'balance': operationgroup.checkout.balance, '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) consumers.KPsul.group_send('kfet.kpsul', websocket_data)
return JsonResponse(data) return JsonResponse(data)
@ -691,7 +684,7 @@ def kpsul_cancel_operations(request):
if ope.group.on_acc.is_cash: if ope.group.on_acc.is_cash:
to_checkouts_balances[ope.group.on_acc] -= - ope.amount to_checkouts_balances[ope.group.on_acc] -= - ope.amount
else: else:
to_checkouts_balances[ope.group.on_acc] -= ope.amount to_checkouts_balances[ope.group.checkout] -= ope.amount
# Pour les stocks d'articles # Pour les stocks d'articles
if ope.article and ope.article_nb: if ope.article and ope.article_nb:
to_articles_stocks[ope.article] += 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]) stock = F('stock') + to_articles_stocks[article])
# Websocket data # Websocket data
websocket_data = { 'opegroups': [], 'opes': [] } websocket_data = { 'opegroups': [], 'opes': [], 'checkouts': [], 'articles': [] }
for opegroup in to_groups_amounts: # 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({ websocket_data['opegroups'].append({
'cancellation': True, 'cancellation': True,
'id': opegroup.pk, 'id': opegroup['id'],
'amount': OperationGroup.objects.get(pk=opegroup.pk).amount, 'amount': opegroup['amount'],
}) })
for ope in opes: for ope in opes:
websocket_data['opes'].append({ websocket_data['opes'].append({
@ -753,6 +750,21 @@ def kpsul_cancel_operations(request):
'canceled_by__trigramme': canceled_by.trigramme, 'canceled_by__trigramme': canceled_by.trigramme,
'canceled_at': canceled_at, '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) consumers.KPsul.group_send('kfet.kpsul', websocket_data)
data['canceled'] = opes data['canceled'] = opes
@ -779,4 +791,14 @@ def kpsul_history(request):
.filter(group__in=opegroups.keys())) .filter(group__in=opegroups.keys()))
for ope in opes: for ope in opes:
opegroups[ope['group_id']]['opes'].append(ope) 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) })