From c69ac3e4e6c6d4543b2b77590eb91b60e7e08818 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Maximilian=20Schr=C3=B6ger?= <m.schroeger@adito.de>
Date: Tue, 16 Oct 2018 13:56:28 +0200
Subject: [PATCH] Produkt - Anzeigefelder, Product_lib.aod

---
 entity/Product_entity/Product_entity.aod      | 10 +++
 .../currentpurchaseprice/valueProcess.js      |  7 ++
 .../currentsalesprice/valueProcess.js         |  7 ++
 .../entityfields/stockcount/valueProcess.js   |  7 ++
 .../_____PREFERENCES_PROJECT.aod              |  2 +-
 process/Product_lib/Product_lib.aod           |  6 ++
 process/Product_lib/process.js                | 67 +++++++++++++++++++
 7 files changed, 105 insertions(+), 1 deletion(-)
 create mode 100644 entity/Product_entity/entityfields/currentpurchaseprice/valueProcess.js
 create mode 100644 entity/Product_entity/entityfields/currentsalesprice/valueProcess.js
 create mode 100644 entity/Product_entity/entityfields/stockcount/valueProcess.js
 create mode 100644 process/Product_lib/Product_lib.aod
 create mode 100644 process/Product_lib/process.js

diff --git a/entity/Product_entity/Product_entity.aod b/entity/Product_entity/Product_entity.aod
index aa87c91ce0..1d63cd564e 100644
--- a/entity/Product_entity/Product_entity.aod
+++ b/entity/Product_entity/Product_entity.aod
@@ -134,6 +134,10 @@
     <entityField>
       <name>STOCKCOUNT</name>
       <caption>Stock</caption>
+      <contentType>NUMBER</contentType>
+      <outputFormat>#.##0</outputFormat>
+      <state>READONLY</state>
+      <valueProcess>%aditoprj%/entity/Product_entity/entityfields/stockcount/valueProcess.js</valueProcess>
     </entityField>
     <entityParameter>
       <name>OrgId_param</name>
@@ -145,10 +149,16 @@
     <entityField>
       <name>currentPurchasePrice</name>
       <caption>Curr. purchase price</caption>
+      <contentType>NUMBER</contentType>
+      <outputFormat>#,##0.00 €</outputFormat>
+      <state>READONLY</state>
+      <valueProcess>%aditoprj%/entity/Product_entity/entityfields/currentpurchaseprice/valueProcess.js</valueProcess>
     </entityField>
     <entityField>
       <name>currentSalesPrice</name>
       <caption>Curr. sales price</caption>
+      <state>READONLY</state>
+      <valueProcess>%aditoprj%/entity/Product_entity/entityfields/currentsalesprice/valueProcess.js</valueProcess>
     </entityField>
     <entityOutgoingField>
       <name>ProductProductprice_dfo</name>
diff --git a/entity/Product_entity/entityfields/currentpurchaseprice/valueProcess.js b/entity/Product_entity/entityfields/currentpurchaseprice/valueProcess.js
new file mode 100644
index 0000000000..b026f1d3da
--- /dev/null
+++ b/entity/Product_entity/entityfields/currentpurchaseprice/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.result");
+import("system.vars");
+import("Product_lib");
+
+var ProductUtils = new ProductUtils();
+
+result.string( ProductUtils.getCurrentProductPrice(vars.get("$field.PRODUCTID"), "PP") );
\ No newline at end of file
diff --git a/entity/Product_entity/entityfields/currentsalesprice/valueProcess.js b/entity/Product_entity/entityfields/currentsalesprice/valueProcess.js
new file mode 100644
index 0000000000..156641616b
--- /dev/null
+++ b/entity/Product_entity/entityfields/currentsalesprice/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.result");
+import("system.vars");
+import("Product_lib");
+
+var productUtils = new ProductUtils();
+
+result.string( productUtils.getCurrentProductPrice(vars.get("$field.PRODUCTID"), "SP") );
\ No newline at end of file
diff --git a/entity/Product_entity/entityfields/stockcount/valueProcess.js b/entity/Product_entity/entityfields/stockcount/valueProcess.js
new file mode 100644
index 0000000000..1f4a638ea8
--- /dev/null
+++ b/entity/Product_entity/entityfields/stockcount/valueProcess.js
@@ -0,0 +1,7 @@
+import("system.result");
+import("system.vars");
+import("Product_lib");
+
+var ProductUtils = new ProductUtils();
+
+result.string( ProductUtils.getStockCount(vars.get("$field.PRODUCTID")) );
\ No newline at end of file
diff --git a/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod b/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod
index 9fb33b2728..6342decac5 100644
--- a/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod
+++ b/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod
@@ -2,7 +2,7 @@
 <preferences xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="3.0.1" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/preferences/3.0.1">
   <name>_____PREFERENCES_PROJECT</name>
   <majorModelMode>DISTRIBUTED</majorModelMode>
-  <projectName>xRM-Basic 5</projectName>
+  <projectName>xRM-Basic</projectName>
   <jditoMaxContentSize v="57671680" />
   <calendarCategoriesEvent>
     <entry>
diff --git a/process/Product_lib/Product_lib.aod b/process/Product_lib/Product_lib.aod
new file mode 100644
index 0000000000..0940dbd301
--- /dev/null
+++ b/process/Product_lib/Product_lib.aod
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<process xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="1.1.7" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/process/1.1.7">
+  <name>Product_lib</name>
+  <majorModelMode>DISTRIBUTED</majorModelMode>
+  <process>%aditoprj%/process/Product_lib/process.js</process>
+</process>
diff --git a/process/Product_lib/process.js b/process/Product_lib/process.js
new file mode 100644
index 0000000000..f4bc88ee88
--- /dev/null
+++ b/process/Product_lib/process.js
@@ -0,0 +1,67 @@
+import("system.logging");
+import("system.SQLTYPES");
+import("system.datetime");
+import("system.db");
+import("system.vars");
+
+function ProductUtils()
+{
+    /**
+     * Delivers the currently valid product price 
+     * 
+     * @param pPid {String} ProductID
+     * @param pBuySell {String} possible values: PP, SP
+     * 
+     * @example productUtils.getCurrentProductPrice(vars.get("$field.PRODUCTID"), "PP")
+     * 
+     * @result {String} currently valid product price
+     */
+    this.getCurrentProductPrice = function( pPid, pBuySell )
+    {
+        if(pPid != undefined && pPid != "" && pBuySell != undefined && pBuySell != "")
+        {
+            var actualpriceSelect = "select PRICE from PRODUCTPRICE \n\
+                                    where BUYSELL = '" + pBuySell + "' and PRODUCT_ID = '" + pPid + "' and CURRENCY = 1 \n\
+                                    and VALID_FROM <= ? and (VALID_TO >= ? or VALID_TO is null) \n\
+                                    order by VALID_FROM desc";
+
+            var today = datetime.today();
+            var sqltypes = [ [today.toString(), SQLTYPES.TIMESTAMP], [today.toString(), SQLTYPES.TIMESTAMP] ];
+            var actualprice = db.cell([actualpriceSelect, sqltypes]);
+
+            if(actualprice != "")
+                return actualprice;
+            else
+                return "0.00";
+        }
+        else
+        {
+            throw new Error();//TODO: add message
+        }
+    }
+    
+    /**
+     * Delivers the stock
+     * 
+     * @param pPid {String} ProductID
+     * 
+     * @example productUtils.getStockCount(vars.get("$field.PRODUCTID"))
+     * 
+     * @result {String} stock count
+     */
+    this.getStockCount = function( pPid )
+    {
+        if(pPid != undefined && pPid != "")//TODO: could this not be solved with dfo's ? 
+        {
+            var sum = db.cell("select sum(QUANTITY * IN_OUT) from STOCK where PRODUCT_ID = '" + pPid + "'");
+            if(sum == "")
+                sum = "0";
+            
+            return sum;
+        }
+        else
+        {
+            throw new Error();//TODO: add message
+        }
+    }
+}
\ No newline at end of file
-- 
GitLab