From 533fdb71c45aec63f81e06b202fa47ab42c42ad2 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 25 Apr 2019 22:04:02 +1000 Subject: [PATCH] Javascript function to render stock tracking table - Added extra info to StockItemTracking serializer --- InvenTree/static/script/inventree/stock.js | 91 +++++++++++++++++++++- InvenTree/stock/serializers.py | 55 +++++++++---- InvenTree/stock/templates/stock/item.html | 83 +++----------------- 3 files changed, 138 insertions(+), 91 deletions(-) diff --git a/InvenTree/static/script/inventree/stock.js b/InvenTree/static/script/inventree/stock.js index 9785cc3c3d..91aec8c6d1 100644 --- a/InvenTree/static/script/inventree/stock.js +++ b/InvenTree/static/script/inventree/stock.js @@ -383,4 +383,93 @@ function loadStockTable(table, options) { if (options.buttons) { linkButtonsToSelection(table, options.buttons); } -}; \ No newline at end of file +} + + +function loadStockTrackingTable(table, options) { + + var cols = [ + { + field: 'pk', + visible: false, + }, + { + field: 'date', + title: 'Date', + sortable: true, + formatter: function(value, row, index, field) { + var m = moment(value); + if (m.isValid()) { + var html = m.format('dddd MMMM Do YYYY') + '
' + m.format('h:mm a'); + return html; + } + + return 'N/A'; + } + }, + ]; + + // If enabled, provide a link to the referenced StockItem + if (options.partColumn) { + cols.push({ + field: 'item', + title: 'Stock Item', + sortable: true, + formatter: function(value, row, index, field) { + return renderLink(value.part_name, value.url); + } + }); + } + + // Stock transaction description + cols.push({ + field: 'title', + title: 'Description', + sortable: true, + formatter: function(value, row, index, field) { + var html = "" + value + ""; + + if (row.notes) { + html += "
" + row.notes + ""; + } + + return html; + } + }); + + cols.push({ + field: 'quantity', + title: 'Quantity', + }); + + cols.push({ + sortable: true, + field: 'user', + title: 'User', + formatter: function(value, row, index, field) { + if (value) + { + // TODO - Format the user's first and last names + return value.username; + } + else + { + return "No user information"; + } + } + }); + + table.bootstrapTable({ + sortable: true, + search: true, + method: 'get', + rememberOrder: true, + queryParams: options.params, + columns: cols, + url: options.url, + }); + + if (options.buttons) { + linkButtonsToSelection(table, options.buttons); + } +} \ No newline at end of file diff --git a/InvenTree/stock/serializers.py b/InvenTree/stock/serializers.py index f79ceaa3b1..64baeb5330 100644 --- a/InvenTree/stock/serializers.py +++ b/InvenTree/stock/serializers.py @@ -24,31 +24,22 @@ class LocationBriefSerializer(serializers.ModelSerializer): ] -class StockTrackingSerializer(serializers.ModelSerializer): +class StockItemSerializerBrief(serializers.ModelSerializer): + """ + Provide a brief serializer for StockItem + """ url = serializers.CharField(source='get_absolute_url', read_only=True) - user = UserSerializerBrief(many=False, read_only=True) + part_name = serializers.CharField(source='part.name', read_only=True) class Meta: - model = StockItemTracking + model = StockItem fields = [ 'pk', + 'uuid', 'url', - 'item', - 'date', - 'title', - 'notes', - 'quantity', - 'user', - 'system', - ] - - read_only_fields = [ - 'date', - 'user', - 'system', - 'quantity', + 'part_name', ] @@ -118,3 +109,33 @@ class LocationSerializer(serializers.ModelSerializer): 'parent', 'pathstring' ] + + +class StockTrackingSerializer(serializers.ModelSerializer): + + url = serializers.CharField(source='get_absolute_url', read_only=True) + + user = UserSerializerBrief(many=False, read_only=True) + + item = StockItemSerializerBrief(many=False, read_only=True) + + class Meta: + model = StockItemTracking + fields = [ + 'pk', + 'url', + 'item', + 'date', + 'title', + 'notes', + 'quantity', + 'user', + 'system', + ] + + read_only_fields = [ + 'date', + 'user', + 'system', + 'quantity', + ] diff --git a/InvenTree/stock/templates/stock/item.html b/InvenTree/stock/templates/stock/item.html index aee40d513d..cd2de3bf0d 100644 --- a/InvenTree/stock/templates/stock/item.html +++ b/InvenTree/stock/templates/stock/item.html @@ -121,22 +121,11 @@ {% if item.has_tracking_info %} -
-
-
-
-

- Stock Tracking{{ item.tracking_info.all|length }} -

-
-
-
- -
-
-
-
+
+

Stock Tracking Information

+ +
{% endif %} {% endblock %} {% block js_ready %} @@ -210,66 +199,14 @@ }); }); - $('#track-table').bootstrapTable({ - sortable: true, - search: true, - method: 'get', - queryParams: function(p) { + loadStockTrackingTable($("#track-table"), { + params: function(p) { return { + ordering: '-date', item: {{ item.pk }}, - } + }; }, - columns: [ - { - field: 'date', - title: 'Date', - sortable: true, - formatter: function(value, row, index, field) { - var m = moment(value); - if (m.isValid()) { - var html = m.format('dddd MMMM Do YYYY') + '
' + m.format('h:mm a'); - return html; - } - - return 'N/A'; - } - }, - { - field: 'title', - title: 'Description', - sortable: true, - formatter: function(value, row, index, field) { - var html = "" + value + ""; - - if (row.notes) { - html += "
" + row.notes + ""; - } - - return html; - } - }, - { - field: 'quantity', - title: 'Quantity', - }, - { - sortable: true, - field: 'user', - title: 'User', - formatter: function(value, row, index, field) { - if (value) - { - // TODO - Format the user's first and last names - return value.username; - } - else - { - return "No user information"; - } - } - } - ], - url: "{% url 'api-stock-track' %}", - }) + url: "{% url 'api-stock-track' %}", + }); {% endblock %} \ No newline at end of file