Merge pull request #131 from SchrodingersGat/stock-stuff

Stock stuff
This commit is contained in:
Oliver 2019-04-17 23:35:44 +10:00 committed by GitHub
commit d315b7c1d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 133 additions and 130 deletions

View File

@ -9,6 +9,7 @@ from .models import Build
class BuildSerializer(serializers.ModelSerializer): class BuildSerializer(serializers.ModelSerializer):
url = serializers.CharField(source='get_absolute_url', read_only=True) url = serializers.CharField(source='get_absolute_url', read_only=True)
status_text = serializers.CharField(source='get_status_display', read_only=True)
class Meta: class Meta:
model = Build model = Build
@ -20,4 +21,6 @@ class BuildSerializer(serializers.ModelSerializer):
'completion_date', 'completion_date',
'part', 'part',
'quantity', 'quantity',
'status',
'status_text',
'notes'] 'notes']

View File

@ -45,50 +45,11 @@
}); });
}); });
$("#stock-table").bootstrapTable({ loadStockTable($("#stock-table"), {
sortable: true, params: {
search: true,
pagination: true,
queryParams: function(p) {
return {
part: {{ part.id }}, part: {{ part.id }},
in_stock: true,
}
}, },
columns: [ url: "{% url 'api-stock-list' %}",
{
field: 'pk',
title: 'ID',
visible: false,
},
{
checkbox: true,
},
{
field: 'location',
title: 'Location',
sortable: true,
formatter: function(value, row, index, field){
return renderLink(value.pathstring, value.url);
}
},
{
field: 'quantity',
title: 'Stock',
searchable: false,
sortable: true,
formatter: function(value, row, index, field) {
var text = renderLink(value, row.url)
text = text + "<span class='badge'>" + row.status + "</span>";
return text;
}
},
{
field: 'notes',
title: 'Notes',
},
],
url: "{% url 'api-stock-list' %}"
}); });
function selectedStock() { function selectedStock() {
@ -109,28 +70,24 @@
return false; return false;
}); });
function _stock(action) {
adjustStock({
action: action,
items: selectedStock(),
success: function() {
$('#stock-table').bootstrapTable('refresh');
}
});
}
$("#multi-item-stocktake").click(function() { $("#multi-item-stocktake").click(function() {
_stock('stocktake'); updateStockItems({
action: 'stocktake'
});
return false; return false;
}); });
$("#multi-item-take").click(function() { $("#multi-item-take").click(function() {
_stock('remove'); updateStockItems({
action: 'remove',
});
return false; return false;
}); });
$("#multi-item-give").click(function() { $("#multi-item-give").click(function() {
_stock('add'); updateStockItems({
action: 'add',
});
return false; return false;
}) })

View File

@ -153,6 +153,19 @@ function updateStock(items, options={}) {
}); });
} }
function selectStockItems(options) {
/* Return list of selections from stock table
* If options.table not provided, assumed to be '#stock-table'
*/
var table_name = options.table || '#stock-table';
// Return list of selected items from the bootstrap table
return $(table_name).bootstrapTable('getSelections');
}
function adjustStock(options) { function adjustStock(options) {
if (options.items) { if (options.items) {
updateStock(options.items, options); updateStock(options.items, options);
@ -172,6 +185,32 @@ function adjustStock(options) {
} }
} }
function updateStockItems(options) {
/* Update one or more stock items selected from a stock-table
* Options available:
* 'action' - Action to perform - 'add' / 'remove' / 'stocktake'
* 'table' - ID of the stock table (default = '#stock-table'
*/
var table = options.table || '#stock-table';
var items = selectStockItems({
table: table,
});
// Pass items through
options.items = items;
options.table = table;
// On success, reload the table
options.success = function() {
$(table).bootstrapTable('refresh');
};
adjustStock(options);
}
function moveStockItems(items, options) { function moveStockItems(items, options) {
var modal = options.modal || '#modal-form'; var modal = options.modal || '#modal-form';
@ -278,3 +317,63 @@ function deleteStockItems(items, options) {
title: 'Delete ' + items.length + ' stock items' title: 'Delete ' + items.length + ' stock items'
}); });
} }
function loadStockTable(modal, options) {
modal.bootstrapTable({
sortable: true,
search: true,
method: 'get',
pagination: true,
rememberOrder: true,
queryParams: options.params,
columns: [
{
checkbox: true,
title: 'Select',
searchable: false,
},
{
field: 'pk',
title: 'ID',
visible: false,
},
{
field: 'part.name',
title: 'Part',
sortable: true,
formatter: function(value, row, index, field) {
return renderLink(value, row.part.url);
}
},
{
field: 'location',
title: 'Location',
sortable: true,
formatter: function(value, row, index, field) {
if (row.location) {
return renderLink(row.location.pathstring, row.location.url);
}
else {
return '';
}
}
},
{
field: 'quantity',
title: 'Quantity',
sortable: true,
formatter: function(value, row, index, field) {
var text = renderLink(value, row.url);
text = text + "<span class='badge'>" + row.status_text + "</span>";
return text;
}
},
{
field: 'notes',
title: 'Notes',
}
],
url: options.url,
});
};

View File

@ -63,6 +63,7 @@ class StockItemSerializer(serializers.ModelSerializer):
part = PartBriefSerializer(many=False, read_only=True) part = PartBriefSerializer(many=False, read_only=True)
location = LocationBriefSerializer(many=False, read_only=True) location = LocationBriefSerializer(many=False, read_only=True)
status_text = serializers.CharField(source='get_status_display', read_only=True)
class Meta: class Meta:
model = StockItem model = StockItem
@ -78,6 +79,7 @@ class StockItemSerializer(serializers.ModelSerializer):
'serial', 'serial',
'batch', 'batch',
'status', 'status',
'status_text',
'notes', 'notes',
] ]

View File

@ -9,6 +9,7 @@
<p>{{ location.description }}</p> <p>{{ location.description }}</p>
{% else %} {% else %}
<h3>Stock</h3> <h3>Stock</h3>
<p>All stock items</p>
{% endif %} {% endif %}
</div> </div>
<div class='col-sm-6'> <div class='col-sm-6'>
@ -135,92 +136,33 @@
return false; return false;
}); });
function _stock(action) {
adjustStock({
action: action,
items: selectedStock(),
success: function() {
$('#stock-table').bootstrapTable('refresh');
}
})
}
$('#multi-item-stocktake').click(function() { $('#multi-item-stocktake').click(function() {
_stock('stocktake'); updateStockItems({
action: 'stocktake',
});
return false; return false;
}); });
$('#multi-item-remove').click(function() { $('#multi-item-remove').click(function() {
_stock('remove'); updateStockItems({
action: 'remove',
});
return false; return false;
}); });
$('#multi-item-add').click(function() { $('#multi-item-add').click(function() {
_stock('add'); updateStockItems({
action: 'add',
});
return false; return false;
}); });
$("#stock-table").bootstrapTable({ loadStockTable($("#stock-table"), {
sortable: true, params: {
search: true,
method: 'get',
pagination: true,
rememberOrder: true,
{% if location %} {% if location %}
queryParams: function(p) {
return {
location: {{ location.id }} location: {{ location.id }}
}
},
{% endif %} {% endif %}
columns: [
{
checkbox: true,
title: 'Select',
searchable: false,
}, },
{
field: 'pk',
title: 'ID',
visible: false,
},
{
field: 'part.name',
title: 'Part',
sortable: true,
formatter: function(value, row, index, field) {
return renderLink(value, row.part.url);
}
},
{% if location == None %}
{
field: 'location',
title: 'Location',
sortable: true,
formatter: function(value, row, index, field) {
if (row.location) {
return renderLink(row.location.name, row.location.url);
}
else {
return '';
}
}
},
{% endif %}
{
field: 'quantity',
title: 'Stock',
sortable: true,
formatter: function(value, row, index, field) {
return renderLink(value, row.url);
}
},
{
field: 'status',
title: 'Status',
sortable: true,
}
],
url: "{% url 'api-stock-list' %}", url: "{% url 'api-stock-list' %}",
}); });