Delete BuildItem objects once a Build has been completed

- Much more complicated template for build allocation page!
- This will require some refactoring at some point ...
This commit is contained in:
Oliver Walters 2020-04-27 20:46:34 +10:00
parent 489dfa1823
commit 35f48ed899
3 changed files with 68 additions and 6 deletions

View File

@ -265,8 +265,9 @@ class Build(MPTTModel):
for build_item in self.allocated_stock.all().prefetch_related('stock_item'): for build_item in self.allocated_stock.all().prefetch_related('stock_item'):
build_item.complete_allocation(user) build_item.complete_allocation(user)
# TODO - Remove the builditem from the database # Check that the stock-item has been assigned to this build, and remove the builditem from the database
# build_item.delete() if build_item.stock_item.build_order == self:
build_item.delete()
notes = 'Built {q} on {now}'.format( notes = 'Built {q} on {now}'.format(
q=self.quantity, q=self.quantity,

View File

@ -155,22 +155,39 @@ InvenTree | Allocate Parts
formatter: function(value, row, index, field) { formatter: function(value, row, index, field) {
var text = ''; var text = '';
var url = '';
if (row.serial && row.quantity == 1) { if (row.serial && row.quantity == 1) {
text = `{% trans "Serial Number" %}: ${row.serial}`; text = `{% trans "Serial Number" %}: ${row.serial}`;
} else { } else {
text = `{% trans "Quantity" %}: ${row.quantity}`; text = `{% trans "Quantity" %}: ${row.quantity}`;
} }
return renderLink(text, `/stock/item/${row.stock_item}/`); {% if build.status == BuildStatus.COMPLETE %}
url = `/stock/item/${row.pk}/`;
{% else %}
url = `/stock/item/${row.stock_item}/`;
{% endif %}
return renderLink(text, url);
}, },
}, },
{ {
field: 'location', field: 'location',
title: '{% trans "Location" %}', title: '{% trans "Location" %}',
formatter: function(value, row, index, field) { formatter: function(value, row, index, field) {
return renderLink(row.stock_item_detail.location_name, `/stock/location/${row.stock_item_detail.location}/`); {% if build.status == BuildStatus.COMPLETE %}
var text = row.location_detail.pathstring;
var url = `/stock/location/${row.location}/`;
{% else %}
var text = row.stock_item_detail.location_name;
var url = `/stock/location/${row.stock_item_detail.location}/`;
{% endif %}
return renderLink(text, url);
} }
}, },
{% if build.status == BuildStatus.PENDING %}
{ {
field: 'buttons', field: 'buttons',
title: 'Actions', title: 'Actions',
@ -190,6 +207,7 @@ InvenTree | Allocate Parts
return html; return html;
}, },
}, },
{% endif %}
] ]
}); });
@ -211,6 +229,42 @@ InvenTree | Allocate Parts
formatNoMatches: function() { return "{% trans 'No BOM items found' %}"; }, formatNoMatches: function() { return "{% trans 'No BOM items found' %}"; },
onLoadSuccess: function(tableData) { onLoadSuccess: function(tableData) {
// Once the BOM data are loaded, request allocation data for the build // Once the BOM data are loaded, request allocation data for the build
{% if build.status == BuildStatus.COMPLETE %}
// Request StockItem which have been assigned to this build
inventreeGet('/api/stock/',
{
build_order: {{ build.id }},
location_detail: true,
},
{
success: function(data) {
// Iterate through the returned data, group by "part",
var allocations = {};
data.forEach(function(item) {
// Group allocations by referenced 'part'
var key = parseInt(item.part);
if (!(key in allocations)) {
allocations[key] = new Array();
}
allocations[key].push(item);
});
for (var key in allocations) {
var tableRow = buildTable.bootstrapTable('getRowByUniqueId', key);
tableRow.allocations = allocations[key];
buildTable.bootstrapTable('updateByUniqueId', key, tableRow, true);
}
},
},
);
{% else %}
inventreeGet('/api/build/item/', inventreeGet('/api/build/item/',
{ {
build: {{ build.id }}, build: {{ build.id }},
@ -249,6 +303,7 @@ InvenTree | Allocate Parts
} }
}, },
); );
{% endif %}
}, },
queryParams: { queryParams: {
part: {{ build.part.id }}, part: {{ build.part.id }},
@ -288,7 +343,11 @@ InvenTree | Allocate Parts
{ {
sortable: true, sortable: true,
field: 'allocated', field: 'allocated',
{% if build.status == BuildStatus.COMPLETE %}
title: '{% trans "Assigned" %}',
{% else %}
title: '{% trans "Allocated" %}', title: '{% trans "Allocated" %}',
{% endif %}
formatter: function(value, row) { formatter: function(value, row) {
var allocated = sumAllocations(row); var allocated = sumAllocations(row);
@ -313,6 +372,7 @@ InvenTree | Allocate Parts
return (progressA < progressB) ? 1 : -1; return (progressA < progressB) ? 1 : -1;
} }
}, },
{% if build.status == BuildStatus.PENDING %}
{ {
field: 'buttons', field: 'buttons',
formatter: function(value, row, index, field) { formatter: function(value, row, index, field) {
@ -337,6 +397,7 @@ InvenTree | Allocate Parts
return html; return html;
}, },
} }
{% endif %}
], ],
}); });

View File

@ -48,8 +48,8 @@
<a href="{% url 'part-sales-orders' part.id %}">{% trans "Sales Orders" %} <span class='badge'>{{ part.sales_orders|length }}</span></a> <a href="{% url 'part-sales-orders' part.id %}">{% trans "Sales Orders" %} <span class='badge'>{{ part.sales_orders|length }}</span></a>
</li> </li>
{% endif %} {% endif %}
{% if part.trackable %} {% if 0 and part.trackable %}
<li{% if 0 and ifequal tab 'track' %} class="active"{% endifequal %}> <li{% ifequal tab 'track' %} class="active"{% endifequal %}>
<a href="{% url 'part-track' part.id %}">{% trans "Tracking" %} <a href="{% url 'part-track' part.id %}">{% trans "Tracking" %}
{% if parts.serials.all|length > 0 %} {% if parts.serials.all|length > 0 %}
<span class="badge">{{ part.serials.all|length }}</span> <span class="badge">{{ part.serials.all|length }}</span>