From a305301b9531428ae42d3153a4d869a1c86cc500 Mon Sep 17 00:00:00 2001
From: Oliver Walters <oliver.henry.walters@gmail.com>
Date: Mon, 27 May 2019 08:07:38 +1000
Subject: [PATCH] Switch between display and edit mode for build allocations

---
 InvenTree/build/api.py                        |  4 +
 InvenTree/build/serializers.py                |  2 +
 InvenTree/build/templates/build/allocate.html | 73 ++++++++++---------
 .../build/templates/build/allocate_edit.html  | 32 ++++++++
 .../build/templates/build/allocate_view.html  | 10 +++
 InvenTree/build/templates/build/required.html |  8 ++
 InvenTree/build/views.py                      |  3 +
 .../templates/company/detail_part.html        |  2 +-
 8 files changed, 100 insertions(+), 34 deletions(-)
 create mode 100644 InvenTree/build/templates/build/allocate_edit.html
 create mode 100644 InvenTree/build/templates/build/allocate_view.html

diff --git a/InvenTree/build/api.py b/InvenTree/build/api.py
index 0ea90a00ba..699671642f 100644
--- a/InvenTree/build/api.py
+++ b/InvenTree/build/api.py
@@ -70,6 +70,10 @@ class BuildItemList(generics.ListCreateAPIView):
 
         query = BuildItem.objects.all()
 
+        query = query.select_related('stock_item')
+        query = query.prefetch_related('stock_item__part')
+        query = query.prefetch_related('stock_item__part__category')
+
         if part_pk:
             query = query.filter(stock_item__part=part_pk)
 
diff --git a/InvenTree/build/serializers.py b/InvenTree/build/serializers.py
index d644cbdc35..dd4a568187 100644
--- a/InvenTree/build/serializers.py
+++ b/InvenTree/build/serializers.py
@@ -45,6 +45,7 @@ class BuildItemSerializer(InvenTreeModelSerializer):
 
     part = serializers.IntegerField(source='stock_item.part.pk', read_only=True)
     part_name = serializers.CharField(source='stock_item.part.full_name', read_only=True)
+    part_image = serializers.CharField(source='stock_item.part.image.url', read_only=True)
     stock_item_detail = StockItemSerializerBrief(source='stock_item', read_only=True)
 
     class Meta:
@@ -54,6 +55,7 @@ class BuildItemSerializer(InvenTreeModelSerializer):
             'build',
             'part',
             'part_name',
+            'part_image',
             'stock_item',
             'stock_item_detail',
             'quantity'
diff --git a/InvenTree/build/templates/build/allocate.html b/InvenTree/build/templates/build/allocate.html
index 6d7017bcf2..3ad123da7f 100644
--- a/InvenTree/build/templates/build/allocate.html
+++ b/InvenTree/build/templates/build/allocate.html
@@ -10,39 +10,11 @@ InvenTree | Allocate Parts
 
 {% include "build/tabs.html" with tab='allocate' %}
 
-<h4>Allocate Parts for Build</h4>
-
-<div class='row'>
-    <div class='col-sm-6'>
-    </div>
-    <div class='col-sm-6'>
-        <div class='btn-group' style='float: right;'>
-                <button class='btn btn-primary' type='button' title='Automatic allocation' id='auto-allocate-build'>Auto Allocate</button>
-                <button class='btn btn-warning' type='button' title='Unallocate build stock' id='unallocate-build'>Unallocate</button>
-        </div>
-    </div>
-</div>
-<hr>
-
-<div class='row'>
-    <div class='col-sm-6'>
-        <h4>Part</h4>
-    </div>
-    <div class='col-sm-2'>
-        <h4>Available</h4>
-    </div>
-    <div class='col-sm-2'>
-        <h4>Required</h4>
-    </div>
-    <div class='col-sm-2'>
-        <h4>Allocated</h4>
-    </div>
-</div>
-
-{% for bom_item in bom_items.all %}
-{% include "build/allocation_item.html" with item=bom_item build=build collapse_id=bom_item.id %}
-{% endfor %}
-
+{% if editing %}
+{% include "build/allocate_edit.html" %}
+{% else %}
+{% include "build/allocate_view.html" %}
+{% endif %}
 
 {% endblock %}
 
@@ -55,6 +27,8 @@ InvenTree | Allocate Parts
 {% block js_ready %}
 {{ block.super }}
 
+    {% if editing %}
+
     {% for bom_item in bom_items.all %}
 
     loadAllocationTable(
@@ -86,4 +60,37 @@ InvenTree | Allocate Parts
         );
     });
 
+    {% else %}
+
+    $("#build-item-table").bootstrapTable({
+        url: "{% url 'api-build-item-list' %}",
+        queryParams: {
+            build: {{ build.id }},
+        },
+        search: true,
+        columns: [
+            {
+                field: 'part_name',
+                title: 'Part',
+                formatter: function(value, row, index, field) {
+                    return imageHoverIcon(row.part_image) + value;
+                }
+            },
+            {
+                field: 'stock_item_detail.location_name',
+                title: 'Location',
+            },
+            {
+                field: 'quantity',
+                title: 'Quantity Allocated',
+            },
+        ]
+    });
+
+    $("#btn-allocate").click(function() {
+        location.href = "{% url 'build-allocate' build.id %}?edit=1";
+    });
+
+    {% endif %}
+
 {% endblock %}
diff --git a/InvenTree/build/templates/build/allocate_edit.html b/InvenTree/build/templates/build/allocate_edit.html
new file mode 100644
index 0000000000..3040997fb9
--- /dev/null
+++ b/InvenTree/build/templates/build/allocate_edit.html
@@ -0,0 +1,32 @@
+<h4>Allocate Stock to Build</h4>
+
+<div class='row'>
+    <div class='col-sm-6'>
+    </div>
+    <div class='col-sm-6'>
+        <div class='btn-group' style='float: right;'>
+                <button class='btn btn-primary' type='button' title='Automatic allocation' id='auto-allocate-build'>Auto Allocate</button>
+                <button class='btn btn-warning' type='button' title='Unallocate build stock' id='unallocate-build'>Unallocate</button>
+        </div>
+    </div>
+</div>
+<hr>
+
+<div class='row'>
+    <div class='col-sm-6'>
+        <h4>Part</h4>
+    </div>
+    <div class='col-sm-2'>
+        <h4>Available</h4>
+    </div>
+    <div class='col-sm-2'>
+        <h4>Required</h4>
+    </div>
+    <div class='col-sm-2'>
+        <h4>Allocated</h4>
+    </div>
+</div>
+
+{% for bom_item in bom_items.all %}
+{% include "build/allocation_item.html" with item=bom_item build=build collapse_id=bom_item.id %}
+{% endfor %}
diff --git a/InvenTree/build/templates/build/allocate_view.html b/InvenTree/build/templates/build/allocate_view.html
new file mode 100644
index 0000000000..9cef45bbbc
--- /dev/null
+++ b/InvenTree/build/templates/build/allocate_view.html
@@ -0,0 +1,10 @@
+<h4>Stock Allocated to Build</h4>
+
+<div id='#build-item-toolbar'>
+    <div class='btn-group'>
+        <button class='btn btn-primary' type='button' id='btn-allocate' title='Allocate Stock'>Allocate</button>
+    </div>
+</div>
+
+<table class='table table-condensed table-striped' id='build-item-table' data-toolbar='#build-item-toolbar'>
+</table>
\ No newline at end of file
diff --git a/InvenTree/build/templates/build/required.html b/InvenTree/build/templates/build/required.html
index 831d8d03b3..6ab16032ab 100644
--- a/InvenTree/build/templates/build/required.html
+++ b/InvenTree/build/templates/build/required.html
@@ -33,4 +33,12 @@
 </tbody>
 </table>
 
+{% endblock %}
+
+{% block js_ready %}
+
+{{ block.super }}
+
+    $("#build-list").bootstrapTable({});
+
 {% endblock %}
\ No newline at end of file
diff --git a/InvenTree/build/views.py b/InvenTree/build/views.py
index 14e22a3fe8..e325d3cc5b 100644
--- a/InvenTree/build/views.py
+++ b/InvenTree/build/views.py
@@ -294,6 +294,9 @@ class BuildAllocate(DetailView):
         context['part'] = part
         context['bom_items'] = bom_items
 
+        if str2bool(self.request.GET.get('edit', None)):
+            context['editing'] = True
+
         return context
 
 
diff --git a/InvenTree/company/templates/company/detail_part.html b/InvenTree/company/templates/company/detail_part.html
index 48e9cb1a77..ec6f80c652 100644
--- a/InvenTree/company/templates/company/detail_part.html
+++ b/InvenTree/company/templates/company/detail_part.html
@@ -18,7 +18,7 @@
 
 <hr>
 
-<table clas='table table-striped table-condensed' id='part-table' data-toolbar='#button-toolbar'>
+<table class='table table-striped table-condensed' id='part-table' data-toolbar='#button-toolbar'>
 </table>
 
 {% endblock %}