From 962ace205fcdec8d96f829c1135782131be5592e Mon Sep 17 00:00:00 2001
From: juanf <juanf>
Date: Fri, 29 Jul 2016 08:37:24 +0000
Subject: [PATCH] SSDM-2732 : Order summary report.

SVN: 36856
---
 .../core-plugins/eln-lims/1/as/elnTypes.py    |   9 +-
 .../html/js/config/StandardProfile.js         | 156 +++++++++++++-----
 2 files changed, 122 insertions(+), 43 deletions(-)

diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/elnTypes.py b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/elnTypes.py
index a18334c299d..23b9f3388b5 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/elnTypes.py
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/elnTypes.py
@@ -875,7 +875,8 @@ LANGUAGE = [ "LANGUAGE", "Language",
 CURRENCY = [ "CURRENCY", "Currency", 
                                 [ 
                                     ["CHF", "CHF (Default)", None],
-                                    ["EUR", "EUR", None]
+                                    ["EUR", "EUR", None],
+                                    ["USD", "USD", None]
                                 ]];
 
 PREFERRED_ORDER_METHOD = [ "PREFERRED_ORDER_METHOD", "Preferred Order Method", 
@@ -894,7 +895,7 @@ ORDER_STATUS = [ "ORDER_STATUS", "Order Status",
                                 ]];
                                 
 SUPPLIER = ["SUPPLIER", "", [
-        ["COMPANY_NAME",                        "General",            "Company name",           DataType.VARCHAR,                   None,                       "Company name",                 None,       None, True],
+        ["NAME",                     "General",            "Name",                        DataType.VARCHAR,        None,                "Name", None, None, True],
         ["COMPANY_ADDRESS",                     "General",            "Company address",        DataType.VARCHAR,         None,                       "Company address",              None,       None, True],
         ["COMPANY_FAX",                         "General",            "Company fax",            DataType.VARCHAR,                   None,                       "Company fax",                  None,       None],
         ["COMPANY_PHONE",                       "General",            "Company phone",          DataType.VARCHAR,                   None,                       "Company phone",                None,       None],
@@ -908,7 +909,7 @@ SUPPLIER = ["SUPPLIER", "", [
     ]];
     
 PRODUCT = ["PRODUCT", "", [
-        ["PRODUCT_MAIN_NAME",                   "General",            "Product Main Name",              DataType.VARCHAR,                   None,                       "Product Main Name",                  None,       None, True],
+        ["NAME",                     "General",            "Name",                        DataType.VARCHAR,        None,                "Name", None, None, True],
         ["PRODUCT_SECONDARY_NAMES",             "General",            "Product Secondary Names",        DataType.VARCHAR,                   None,                       "Product Secondary Names",            None,       None],
         ["DESCRIPTION",                         "General",            "Description",                    DataType.MULTILINE_VARCHAR,         None,                       "Description",                        None,       None],
         ["COMPANY",                             "General",            "Company",                        DataType.VARCHAR,                   None,                       "Company",                            None,       None],
@@ -922,6 +923,7 @@ PRODUCT = ["PRODUCT", "", [
     ]];
 
 REQUEST = ["REQUEST", "", [
+        ["NAME",                     "General",            "Name",                        DataType.VARCHAR,        None,                "Name", None, None],
         ["PROJECT",                             "General",            "Project",                            DataType.VARCHAR,                   None,                       "Project",                          None,       None],
         ["DEPARTMENT",                          "General",            "Department",                         DataType.VARCHAR,                   None,                       "Department",                       None,       None],
         ["BUYER",                               "General",            "Buyer",                              DataType.VARCHAR,                   None,                       "Buyer",                            None,       None],
@@ -931,6 +933,7 @@ REQUEST = ["REQUEST", "", [
     ]];
 
 ORDER = ["ORDER", "", [
+        ["NAME",                     "General",            "Name",                        DataType.VARCHAR,        None,                "Name", None, None],
         ["ORDER_MANAGER",                             "General",            "Order Manager",                            DataType.VARCHAR,                                None,                       "Order Manager",                          None,       None],
         ["ORDER_MANAGER_CONTACT_DETAILS",             "General",            "Order Manager Contact Details",            DataType.VARCHAR,                                None,                       "Order Manager Contact Details",          None,       None],
         ["ORDER_STATUS",                              "General",            "Order Status",                             DataType.CONTROLLEDVOCABULARY,                   "ORDER_STATUS",             "Order Status",                           None,       None],
diff --git a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js
index e7ddc20dfec..0bfca38a611 100644
--- a/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js
+++ b/openbis_standard_technologies/dist/core-plugins/eln-lims/1/as/webapps/eln-lims/html/js/config/StandardProfile.js
@@ -411,48 +411,56 @@ $.extend(StandardProfile.prototype, DefaultProfile.prototype, {
 			} else if(sampleTypeCode === "ORDER") {
 				var isExisting = mainController.currentView._sampleFormModel.mode === FormMode.VIEW;
 				if(isExisting) {
+					//
+					// Data Structures to Help the reports functionality
+					//
 					var order = mainController.currentView._sampleFormModel.sample;
 					var requests = order.parents;
 					var providerByPermId = {};
 					var productsByProviderPermId = {};
 					var quantityByProductPermId = {};
-					var printOrder = FormUtil.getButtonWithIcon("glyphicon-print",function() {
-						
-						//1. Group products from all requests by provider and obtain their quantities from the annotations
-						for(var rIdx = 0; rIdx < requests.length; rIdx++) {
-							var request = requests[rIdx];
-							var requestProductsAnnotations = FormUtil.getAnnotationsFromSample(request);
-							var requestProducts = request.parents;
-							for(var pIdx = 0; pIdx < requestProducts.length; pIdx++) {
-								var requestProduct = requestProducts[pIdx];
-								var requestProductAnnotations = requestProductsAnnotations[requestProduct.permId];
-								
-								if(requestProduct.parents.length === 0) {
-									Util.showError("Product " + requestProduct.code + " don't have a provider, FIX IT!.");
-									return;
-								}
-								var provider = requestProduct.parents[0];
-								var providerProducts = productsByProviderPermId[provider.permId];
-								if(!providerProducts) {
-									providerProducts = [];
-									productsByProviderPermId[provider.permId] = providerProducts;
-									providerByPermId[provider.permId] = provider;
-								}
-								var quantity = quantityByProductPermId[requestProduct.permId];
-								if(!quantity) {
-									quantity = 0;
-								}
-								quantity += parseInt(requestProductAnnotations["QUANTITY"]);
-								if(!quantity) {
-									Util.showError("Product " + requestProduct.code + " from request " +  request.code + " don't have a quantity, FIX IT!.");
-									return;
-								}
-								quantityByProductPermId[requestProduct.permId] = quantity;
-								providerProducts.push(requestProduct);
+					
+					//
+					// Fills data structures
+					//
+					for(var rIdx = 0; rIdx < requests.length; rIdx++) {
+						var request = requests[rIdx];
+						var requestProductsAnnotations = FormUtil.getAnnotationsFromSample(request);
+						var requestProducts = request.parents;
+						for(var pIdx = 0; pIdx < requestProducts.length; pIdx++) {
+							var requestProduct = requestProducts[pIdx];
+							var requestProductAnnotations = requestProductsAnnotations[requestProduct.permId];
+							
+							if(requestProduct.parents.length === 0) {
+								Util.showError("Product " + requestProduct.code + " don't have a provider, FIX IT!.");
+								return;
+							}
+							var provider = requestProduct.parents[0];
+							var providerProducts = productsByProviderPermId[provider.permId];
+							if(!providerProducts) {
+								providerProducts = [];
+								productsByProviderPermId[provider.permId] = providerProducts;
+								providerByPermId[provider.permId] = provider;
+							}
+							var quantity = quantityByProductPermId[requestProduct.permId];
+							if(!quantity) {
+								quantity = 0;
+							}
+							quantity += parseInt(requestProductAnnotations["QUANTITY"]);
+							if(!quantity) {
+								Util.showError("Product " + requestProduct.code + " from request " +  request.code + " don't have a quantity, FIX IT!.");
+								return;
 							}
+							quantityByProductPermId[requestProduct.permId] = quantity;
+							providerProducts.push(requestProduct);
 						}
-						
-						//2. Create an order page for each provider
+					}
+					
+					//
+					// Button that prints an order report
+					//
+					var printOrder = FormUtil.getButtonWithIcon("glyphicon-print",function() {
+						//Create an order page for each provider
 						var orderPages = [];
 						for(var providerPermId in productsByProviderPermId) {
 							var provider = providerByPermId[providerPermId];
@@ -464,9 +472,9 @@ $.extend(StandardProfile.prototype, DefaultProfile.prototype, {
 							}
 							
 							var providerProducts = productsByProviderPermId[providerPermId];
-							var page = languageLabels["DATE_LABEL"] + ": " + Util.getFormatedDate(new Date());
+							var page = languageLabels["DATE_LABEL"] + ": " + Util.getFormatedDate(new Date(order.registrationDetails.modificationDate));
 								page += "\n";
-								page += languageLabels["SUPPLIER_LABEL"] + ": " + provider.properties["COMPANY_NAME"];
+								page += languageLabels["SUPPLIER_LABEL"] + ": " + provider.properties["NAME"];
 								page += "\n";
 								page += languageLabels["CONTACT_INFO_LABEL"] + ":";
 								page += "\n";
@@ -498,7 +506,7 @@ $.extend(StandardProfile.prototype, DefaultProfile.prototype, {
 									var product = providerProducts[pIdx];
 									var quantity = quantityByProductPermId[product.permId];
 									var unitPrice = parseFloat(product.properties["PRICE_PER_UNIT"]);
-									page += product.properties["PRODUCT_MAIN_NAME"] + "\t" + product.properties["CATALOG_CODE"] + "\t" + quantity + "\t" + product.properties["PRICE_PER_UNIT"] + "\t" + product.properties["CURRENCY"];
+									page += product.properties["NAME"] + "\t" + product.properties["CATALOG_CODE"] + "\t" + quantity + "\t" + product.properties["PRICE_PER_UNIT"] + "\t" + product.properties["CURRENCY"];
 									page += "\n";
 									var totalForCurrency = totalByCurrency[product.properties["CURRENCY"]];
 									if(!totalForCurrency) {
@@ -521,14 +529,82 @@ $.extend(StandardProfile.prototype, DefaultProfile.prototype, {
 							orderPages.push(page);
 						}
 						
-						//3. Print Pages
+						//Print Pages
 						var completeOrder = "";
 						for(var pageIdx = 0; pageIdx < orderPages.length; pageIdx++) {
 							completeOrder += orderPages[pageIdx];
 						}
 						Util.downloadTextFile(completeOrder, "order.txt");
 					}, "Print Order");
-					$("#" + containerId).append(printOrder);
+					
+					//
+					// Order Summary Grid
+					//
+					var columns = [ {
+						label : 'Code',
+						property : 'code',
+						isExportable: true,
+						sortable : true,
+						render : function(data) {
+							return FormUtil.getFormLink(data.code, "SAMPLE", data.permId);
+						}
+					},{
+						label : 'Supplier',
+						property : 'supplier',
+						isExportable: true,
+						sortable : true
+					}, {
+						label : 'Name',
+						property : 'name',
+						isExportable: true,
+						sortable : true
+					}, {
+						label : 'Quantity',
+						property : 'quantity',
+						isExportable: true,
+						sortable : true,
+					}, {
+						label : 'Unit Price',
+						property : 'unitPrice',
+						isExportable: true,
+						sortable : true
+					}, {
+						label : 'Currency',
+						property : 'currency',
+						isExportable: true,
+						sortable : true
+					}];
+					
+					var getDataRows = function(callback) {
+						var rows = [];
+						for(var providerPermId in productsByProviderPermId) {
+							var provider = providerByPermId[providerPermId];
+							var providerProducts = productsByProviderPermId[providerPermId];
+							for(var pIdx = 0; pIdx < providerProducts.length; pIdx++) {
+								var product = providerProducts[pIdx];
+								var quantity = quantityByProductPermId[product.permId];
+								var unitPrice = parseFloat(product.properties["PRICE_PER_UNIT"]);
+								
+								var rowData = {};
+								rowData.permId = product.permId;
+								rowData.supplier = provider.properties["NAME"];
+								rowData.name = product.properties["NAME"];
+								rowData.code =  product.properties["CATALOG_CODE"];
+								rowData.quantity = quantity;
+								rowData.unitPrice = product.properties["PRICE_PER_UNIT"];
+								rowData.currency = product.properties["CURRENCY"];
+								rows.push(rowData);
+							}
+							
+						}
+						callback(rows);
+					};
+					
+					var orderSummaryContainer = $("<div>");
+					var orderSummary = new DataGridController("Order Summary", columns, getDataRows, null, false, "ORDER_SUMMARY");
+					orderSummary.init(orderSummaryContainer);
+					
+					$("#" + containerId).append(orderSummaryContainer).append($("<br>")).append(printOrder);
 				}
 			}
 		}
-- 
GitLab