From 4694e125115122b8fb7603ab2560f6150b5c0c72 Mon Sep 17 00:00:00 2001
From: Sebastien Robin <seb@nexedi.com>
Date: Thu, 4 Jan 2018 17:35:41 +0000
Subject: [PATCH] mrp: show production planning also on ressources

---
 .../resource_view_manufacturing_gantt.xml     | 81 +++++++++++++++++++
 .../resource_view_manufacturing_gantt.xml     | 81 +++++++++++++++++++
 ...ngOrderModule_getGanttConfigurationDict.py | 15 ++++
 ...gOrderModule_getGanttConfigurationDict.xml | 62 ++++++++++++++
 .../manufacturing_gantt.xml                   |  2 +-
 bt5/erp5_mrp/bt/template_action_path_list     |  2 +
 .../gadget_officejs_manufacturing_gantt_js.js | 16 +++-
 7 files changed, 256 insertions(+), 3 deletions(-)
 create mode 100644 bt5/erp5_mrp/ActionTemplateItem/portal_types/Component/resource_view_manufacturing_gantt.xml
 create mode 100644 bt5/erp5_mrp/ActionTemplateItem/portal_types/Product/resource_view_manufacturing_gantt.xml
 create mode 100644 bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.py
 create mode 100644 bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.xml

diff --git a/bt5/erp5_mrp/ActionTemplateItem/portal_types/Component/resource_view_manufacturing_gantt.xml b/bt5/erp5_mrp/ActionTemplateItem/portal_types/Component/resource_view_manufacturing_gantt.xml
new file mode 100644
index 0000000000..16058cc098
--- /dev/null
+++ b/bt5/erp5_mrp/ActionTemplateItem/portal_types/Component/resource_view_manufacturing_gantt.xml
@@ -0,0 +1,81 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ActionInformation" module="Products.CMFCore.ActionInformation"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>action</string> </key>
+            <value>
+              <persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
+            </value>
+        </item>
+        <item>
+            <key> <string>categories</string> </key>
+            <value>
+              <tuple>
+                <string>action_type/object_jio_action</string>
+              </tuple>
+            </value>
+        </item>
+        <item>
+            <key> <string>category</string> </key>
+            <value> <string>object_jio_action</string> </value>
+        </item>
+        <item>
+            <key> <string>condition</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>description</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>icon</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>resource_view_manufacturing_gantt</string> </value>
+        </item>
+        <item>
+            <key> <string>permissions</string> </key>
+            <value>
+              <tuple>
+                <string>View</string>
+              </tuple>
+            </value>
+        </item>
+        <item>
+            <key> <string>priority</string> </key>
+            <value> <float>2.0</float> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <string>Manufacturing Gantt</string> </value>
+        </item>
+        <item>
+            <key> <string>visible</string> </key>
+            <value> <int>1</int> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+  <record id="2" aka="AAAAAAAAAAI=">
+    <pickle>
+      <global name="Expression" module="Products.CMFCore.Expression"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>text</string> </key>
+            <value> <string>string:${object_url}/ManufacturingOrderModule_viewGantt</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_mrp/ActionTemplateItem/portal_types/Product/resource_view_manufacturing_gantt.xml b/bt5/erp5_mrp/ActionTemplateItem/portal_types/Product/resource_view_manufacturing_gantt.xml
new file mode 100644
index 0000000000..16058cc098
--- /dev/null
+++ b/bt5/erp5_mrp/ActionTemplateItem/portal_types/Product/resource_view_manufacturing_gantt.xml
@@ -0,0 +1,81 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="ActionInformation" module="Products.CMFCore.ActionInformation"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>action</string> </key>
+            <value>
+              <persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
+            </value>
+        </item>
+        <item>
+            <key> <string>categories</string> </key>
+            <value>
+              <tuple>
+                <string>action_type/object_jio_action</string>
+              </tuple>
+            </value>
+        </item>
+        <item>
+            <key> <string>category</string> </key>
+            <value> <string>object_jio_action</string> </value>
+        </item>
+        <item>
+            <key> <string>condition</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>description</string> </key>
+            <value>
+              <none/>
+            </value>
+        </item>
+        <item>
+            <key> <string>icon</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>resource_view_manufacturing_gantt</string> </value>
+        </item>
+        <item>
+            <key> <string>permissions</string> </key>
+            <value>
+              <tuple>
+                <string>View</string>
+              </tuple>
+            </value>
+        </item>
+        <item>
+            <key> <string>priority</string> </key>
+            <value> <float>2.0</float> </value>
+        </item>
+        <item>
+            <key> <string>title</string> </key>
+            <value> <string>Manufacturing Gantt</string> </value>
+        </item>
+        <item>
+            <key> <string>visible</string> </key>
+            <value> <int>1</int> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+  <record id="2" aka="AAAAAAAAAAI=">
+    <pickle>
+      <global name="Expression" module="Products.CMFCore.Expression"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>text</string> </key>
+            <value> <string>string:${object_url}/ManufacturingOrderModule_viewGantt</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.py b/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.py
new file mode 100644
index 0000000000..01d94c5d75
--- /dev/null
+++ b/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.py
@@ -0,0 +1,15 @@
+from Products.ZSQLCatalog.SQLCatalog import Query, NegatedQuery
+configuration_dict = {'portal_type': ["Manufacturing Order", "Manufacturing Execution"]}
+portal = context.getPortalObject()
+if context.getPortalType() in portal.getPortalResourceTypeList():
+  # only show production related to current resource
+  delivery_uid_set = set()
+  for line in portal.portal_catalog(portal_type="Manufacturing Execution Line",
+                                    query=NegatedQuery(Query(simulation_state=["draft", "cancelled", "delivered"])),
+                                    default_resource_uid=context.getUid(),
+                                    select_dict={"parent_uid": None}):
+    delivery_uid_set.add(line.parent_uid)
+  # to make sure to filter even if nothing match
+  delivery_uid_set.add(0)
+  configuration_dict["delivery_uid_list"] = [x for x in delivery_uid_set]
+return configuration_dict
diff --git a/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.xml b/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.xml
new file mode 100644
index 0000000000..29b5a98f39
--- /dev/null
+++ b/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_getGanttConfigurationDict.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>Script_magic</string> </key>
+            <value> <int>3</int> </value>
+        </item>
+        <item>
+            <key> <string>_bind_names</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>_asgns</string> </key>
+                        <value>
+                          <dictionary>
+                            <item>
+                                <key> <string>name_container</string> </key>
+                                <value> <string>container</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_context</string> </key>
+                                <value> <string>context</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_m_self</string> </key>
+                                <value> <string>script</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>ManufacturingOrderModule_getGanttConfigurationDict</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
diff --git a/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_viewGantt/manufacturing_gantt.xml b/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_viewGantt/manufacturing_gantt.xml
index e1944ca453..4d70d57f96 100644
--- a/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_viewGantt/manufacturing_gantt.xml
+++ b/bt5/erp5_mrp/SkinTemplateItem/portal_skins/erp5_mrp/ManufacturingOrderModule_viewGantt/manufacturing_gantt.xml
@@ -115,7 +115,7 @@
       <dictionary>
         <item>
             <key> <string>_text</string> </key>
-            <value> <string>python: {\'portal_type\': ["Manufacturing Order", "Manufacturing Execution"],}</string> </value>
+            <value> <string>python: here.ManufacturingOrderModule_getGanttConfigurationDict()</string> </value>
         </item>
       </dictionary>
     </pickle>
diff --git a/bt5/erp5_mrp/bt/template_action_path_list b/bt5/erp5_mrp/bt/template_action_path_list
index 470b463d33..7ec9d6553c 100644
--- a/bt5/erp5_mrp/bt/template_action_path_list
+++ b/bt5/erp5_mrp/bt/template_action_path_list
@@ -1,3 +1,4 @@
+Component | resource_view_manufacturing_gantt
 Manufacturing Execution Cell | view
 Manufacturing Execution Line | option_view
 Manufacturing Execution Line | quantity_view
@@ -22,6 +23,7 @@ Manufacturing Order | production_order_operation_consumption
 Manufacturing Order | profile_view
 Manufacturing Order | view
 Manufacturing Order | view_stock
+Product | resource_view_manufacturing_gantt
 Production Order Cell | view
 Production Order Line | jump_to_transformation
 Production Order Line | price_view
diff --git a/bt5/erp5_officejs/PathTemplateItem/web_page_module/gadget_officejs_manufacturing_gantt_js.js b/bt5/erp5_officejs/PathTemplateItem/web_page_module/gadget_officejs_manufacturing_gantt_js.js
index 4db2de7df6..510de384fd 100644
--- a/bt5/erp5_officejs/PathTemplateItem/web_page_module/gadget_officejs_manufacturing_gantt_js.js
+++ b/bt5/erp5_officejs/PathTemplateItem/web_page_module/gadget_officejs_manufacturing_gantt_js.js
@@ -57,10 +57,14 @@
       .push(function (gantt_widget) {
         // First search all production report not finished to find out the
         // list of production orders still having work on them
-        var query;
+        var query, delivery_uid_list;
         gadget.property_dict.gantt_widget = gantt_widget;
         console.log("gantt_widget", gantt_widget);
         query = 'portal_type:="Manufacturing Execution" AND NOT simulation_state: ("draft", "cancelled", "delivered")';
+        delivery_uid_list = option_dict.delivery_uid_list;
+        if ((delivery_uid_list !== undefined) && (delivery_uid_list.length > 0)) {
+          query = query + ' AND uid: (' + delivery_uid_list.join(', ') + ')';
+        }
         return gadget.jio_allDocs({
           query: query,
           limit: 10000,
@@ -69,8 +73,11 @@
         });
       })
       .push(function (delivery_list) {
+        // try to search for other manufacturing execution having same causality as
+        // other manufacturing execution already found
         var causality_uid_list = [0], // Initiliaze with 0 to make sure to have at least one uid to search for
-            i, delivery, query, empty_causality_delivery_list = [];
+            i, delivery, query, empty_causality_delivery_list = [],
+            delivery_uid_list;
 
         delivery_list = delivery_list.data.rows;
         for (i = 0; i < delivery_list.length; i = i + 1) {
@@ -86,6 +93,11 @@
         query = 'portal_type:="Manufacturing Execution" AND causality_uid: (' + causality_uid_list.join(', ') + ') AND NOT simulation_state: ("draft", "cancelled")';
         console.log("QUERY", query);
         gadget.property_dict.empty_causality_delivery_list = empty_causality_delivery_list;
+        delivery_uid_list = option_dict.delivery_uid_list;
+        // No need to get more
+        if ((delivery_uid_list !== undefined) && (delivery_uid_list.length > 0)) {
+          query = query + ' AND uid: (' + delivery_uid_list.join(', ') + ')';
+        }
         return gadget.jio_allDocs({
           query: query,
           limit: 10000,
-- 
2.30.9