fix for part with no bom-price #1678

This commit is contained in:
Matthias 2021-06-17 18:21:11 +02:00
parent bc46cca9f0
commit 997d68694e
2 changed files with 26 additions and 17 deletions

View File

@ -198,18 +198,18 @@ The part single price is the current purchase price for that supplier part."></i
var bomdata = { var bomdata = {
labels: [{% for line in bom_parts %}'{{ line.name }}',{% endfor %}], labels: [{% for line in bom_parts %}'{{ line.name }}',{% endfor %}],
datasets: [ datasets: [
{% if bom_pie_min %} {
label: 'Price',
data: [{% for line in bom_parts %}{{ line.min_price }},{% endfor %}],
backgroundColor: bom_colors,
},
{% if bom_pie_max %}
{ {
label: 'Max Price', label: 'Max Price',
data: [{% for line in bom_parts %}{{ line.max_price }},{% endfor %}], data: [{% for line in bom_parts %}{{ line.max_price }},{% endfor %}],
backgroundColor: bom_colors, backgroundColor: bom_colors,
}, },
{% endif %} {% endif %}
{
label: 'Price',
data: [{% for line in bom_parts %}{% if bom_pie_min %}{{ line.min_price }}{% else %}{{ line.price }}{% endif%},{% endfor %}],
backgroundColor: bom_colors,
}
] ]
}; };
var BomChart = loadBomChart(document.getElementById('BomChart'), bomdata) var BomChart = loadBomChart(document.getElementById('BomChart'), bomdata)

View File

@ -846,17 +846,26 @@ class PartPricingView(PartDetail):
ctx['price_history'] = ret ctx['price_history'] = ret
# BOM Information for Pie-Chart # BOM Information for Pie-Chart
bom_items = [{'name': str(a.sub_part), 'price': a.sub_part.get_price_range(quantity), 'q': a.quantity} for a in part.bom_items.all()] if part.has_bom:
if [True for a in bom_items if len(set(a['price'])) == 2]: # iterate over all bom-items
ctx['bom_parts'] = [{ ctx_bom_parts = []
'name': a['name'], for item in part.bom_items.all():
'min_price': str((a['price'][0] * a['q']) / quantity), ctx_item = {'name': str(item.sub_part)}
'max_price': str((a['price'][1] * a['q']) / quantity)} for a in bom_items] price, qty = item.sub_part.get_price_range(quantity), item.quantity
ctx['bom_pie_min'] = True
else: price_min, price_max = 0, 0
ctx['bom_parts'] = [{ if price: # check if price available
'name': a['name'], price_min = str((price[0] * qty) / quantity)
'price': str((a['price'][0] * a['q']) / quantity)} for a in bom_items] if len(set(price)) == 2: # min and max-price present
price_max = str((price[1] * qty) / quantity)
ctx['bom_pie_max'] = True # enable showing min prices in bom
ctx_item['max_price'] = price_min
ctx_item['min_price'] = price_max if price_max else price_min
ctx_bom_parts.append(ctx_item)
# add to global context
ctx['bom_parts'] = ctx_bom_parts
return ctx return ctx