partlist: fix product alignment, add product sum
parent
8304f30cb6
commit
1d91c5da38
|
|
@ -24,5 +24,8 @@
|
|||
<tr>
|
||||
<td>Summe:</td>
|
||||
<td>{{len}}</td>
|
||||
{% for product in product_sums %}
|
||||
<td>{{product}}</td>
|
||||
{% endfor %}
|
||||
<td>Teile</td>
|
||||
</table>
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
from collections import defaultdict
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.urls import reverse
|
||||
|
|
@ -45,17 +46,16 @@ def parts(request):
|
|||
products = Product.objects.order_by('name')
|
||||
parts = BrandedPart.objects.order_by('number')
|
||||
quantities = []
|
||||
for part in parts: # FIXME!
|
||||
qtys = part.usage_set.values('productusage__product__name').annotate(Sum('productusage__quantity')).order_by('productusage__product__name')
|
||||
#prod_qtys = {}
|
||||
prod_qtys = []
|
||||
for obj in qtys:
|
||||
# FIXME: make sure there are n(==products) entries
|
||||
#prod_qtys['productusage__product__name'] = obj['productusage__quantity__sum']
|
||||
qty = obj['productusage__quantity__sum']
|
||||
if not qty:
|
||||
qty = 0
|
||||
prod_qtys.append(qty)
|
||||
product_sums = defaultdict(lambda: 0)
|
||||
for part in parts: # TODO: replace by sophisticated query
|
||||
qtys = defaultdict(lambda: 0)
|
||||
for result in part.usage_set.values('productusage__product__name').annotate(Sum('productusage__quantity')).order_by('productusage__product__name'):
|
||||
prod_name = result['productusage__product__name']
|
||||
prod_sum = result['productusage__quantity__sum']
|
||||
if prod_sum:
|
||||
qtys[prod_name] = prod_sum
|
||||
product_sums[prod_name] += prod_sum
|
||||
prod_qtys = [qtys[prod.name] for prod in products]
|
||||
quantities.append({
|
||||
"name": part.get_name(),
|
||||
"number": part.number,
|
||||
|
|
@ -64,7 +64,13 @@ def parts(request):
|
|||
"id": part.id,
|
||||
})
|
||||
|
||||
context = {'products': products, 'parts': parts, 'quantities': quantities, 'len': len(quantities)}
|
||||
context = {
|
||||
'products': products,
|
||||
'parts': parts,
|
||||
'quantities': quantities,
|
||||
'len': len(quantities),
|
||||
'product_sums': [product_sums[prod.name] for prod in products]
|
||||
}
|
||||
return render(request, 'parts/parts.html', context)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue