diff --git a/entity/Turnover_entity/recordcontainers/jdito/contentProcess.js b/entity/Turnover_entity/recordcontainers/jdito/contentProcess.js index 58bc65ef52c577062f096bb6e0772453784bd6e9..d10f509a90cef8f610971115751e446a5dd1d139 100644 --- a/entity/Turnover_entity/recordcontainers/jdito/contentProcess.js +++ b/entity/Turnover_entity/recordcontainers/jdito/contentProcess.js @@ -5,7 +5,8 @@ import("system.db"); import("system.result"); import("system.translate"); import("Data_lib"); - +import("Keyword_lib"); +import("Money_lib"); var turnoverCategory = translate.text('Turnover'); var forecastCategory = translate.text('Forecast'); @@ -13,6 +14,13 @@ var forecastCategory = translate.text('Forecast'); // load data var sumOfMonthTurnover = db.table("select year(SALESORDERDATE) yearNum, month(SALESORDERDATE) monthNum, sum(NET + VAT) from SALESORDER group by year(SALESORDERDATE), month(SALESORDERDATE) order by yearNum, monthNum"); var sumOfMonthForecast = db.table("select year(DATE_START) yearNum, month(DATE_START) monthNum, sum(VOLUME * 1000) from SALESPROJECT_FORECAST group by year(DATE_START), month(DATE_START) order by yearNum, monthNum"); +var sumOfMonthProductsTurnover = db.table("select year(SALESORDERDATE) yearNum, month(SALESORDERDATE) monthNum, SALESORDERITEM.DISCOUNT, SALESORDERITEM.VAT, SALESORDERITEM.PRICE, sum(SALESORDERITEM.QUANTITY), SALESORDERITEM.GROUPCODEID, (" + KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.get.ProductGroupcode, "SALESORDERITEM.GROUPCODEID") + ") \n\ + from SALESORDER \n\ + join SALESORDERITEM on SALESORDERITEM.SALESORDER_ID = SALESORDER.SALESORDERID \n\ + where SALESORDERITEM.OPTIONAL <> 1 \n\ + group by year(SALESORDERDATE), month(SALESORDERDATE), SALESORDERITEM.GROUPCODEID, SALESORDERITEM.DISCOUNT, SALESORDERITEM.VAT, SALESORDERITEM.PRICE \n\ + order by yearNum, monthNum"); // V--V--> there is no VAT/Discount in forecasts V-----> forecasts are grouped by grupcode and have always quantity 1 +var sumOfMonthProductsForecast = db.table("select year(DATE_START) yearNum, month(DATE_START) monthNum, 0, 0, sum(VOLUME * 1000), 1, GROUPCODE, (" + KeywordUtils.getResolvedTitleSqlPart($KeywordRegistry.get.ProductGroupcode, "GROUPCODE") + ") from SALESPROJECT_FORECAST group by year(DATE_START), month(DATE_START), GROUPCODE order by yearNum, monthNum"); // build chartData var rootNode = ""; @@ -27,30 +35,33 @@ for (let i = 0; i < yearCountToShow; i++) { var year = i + maxYear - yearCountToShow + 1; - var TurnoverYearSum = 0; - var ForecastYearSum = 0; + var turnoverYearSum = 0; + var forecastYearSum = 0; // filter data by current year - var TurnoverYearData = sumOfMonthTurnover.filter(function(row) + var turnoverYearData = sumOfMonthTurnover.filter(function(row) { - return row[0] == year + return row[0] == year; }); - var ForecastYearData = sumOfMonthForecast.filter(function(row) + var forecastYearData = sumOfMonthForecast.filter(function(row) { - return row[0] == year + return row[0] == year; }); for (let i = 1; i <= 12; i++) { // add months - TurnoverYearSum += _addMonth(year, i, TurnoverYearData, turnoverCategory); - ForecastYearSum += _addMonth(year, i, ForecastYearData, forecastCategory); + turnoverYearSum += _addMonth(year, i, turnoverYearData, turnoverCategory); + forecastYearSum += _addMonth(year, i, forecastYearData, forecastCategory); + + _addProducts(year, i, sumOfMonthProductsTurnover, turnoverCategory); + _addProducts(year, i, sumOfMonthProductsForecast, forecastCategory); } // add year nodes - chartData.add(turnoverCategory + year, rootNode, [turnoverCategory, year.toString(), TurnoverYearSum]); - chartData.add(forecastCategory + year, rootNode, [forecastCategory, year.toString(), ForecastYearSum]); + chartData.add(turnoverCategory + year, rootNode, [turnoverCategory, year.toString(), turnoverYearSum]); + chartData.add(forecastCategory + year, rootNode, [forecastCategory, year.toString(), forecastYearSum]); } @@ -86,4 +97,39 @@ function _addMonth(pYear, pMonth, pData, pCategory) // add month node chartData.add(pCategory + pYear + pMonth, pCategory + pYear, [pCategory, monthDate, monthValue]); return yearSum; +} + +function _addProducts(pYear, pMonth, pData, pCategory) +{ + var groupcodeSums = {}; + + for (let i = 0; i < pData.length; i++) + { + if (pData[i][0] == pYear && pData[i][1] == pMonth) + { + var groupCode = pData[i][6]; + if (groupCode != undefined) + { + if (groupcodeSums[groupCode] == undefined) + { + groupcodeSums[groupCode] = { + sum: 0, + name: pData[i][7] + } + } + + pData[i][2] = pData[i][2] || 0; + pData[i][3] = pData[i][3] || 0; + pData[i][4] = pData[i][4] || 0; + pData[i][5] = pData[i][5] || 0; + + groupcodeSums[groupCode]["sum"] += MoneyUtils.getGross(parseFloat(pData[i][3]), parseFloat(pData[i][4]), parseFloat(pData[i][5]), parseFloat(pData[i][2])); + } + } + } + + for (let groupcode in groupcodeSums) + { + chartData.add(pCategory + pYear + pMonth + groupcode, pCategory + pYear + pMonth, [pCategory, groupcodeSums[groupcode]["name"], groupcodeSums[groupcode]["sum"]]); + } } \ No newline at end of file diff --git a/process/Money_lib/Money_lib.aod b/process/Money_lib/Money_lib.aod new file mode 100644 index 0000000000000000000000000000000000000000..61248fd244213806e7a23ebd44d8353bed49dc4b --- /dev/null +++ b/process/Money_lib/Money_lib.aod @@ -0,0 +1,9 @@ +<?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>Money_lib</name> + <majorModelMode>DISTRIBUTED</majorModelMode> + <process>%aditoprj%/process/Money_lib/process.js</process> + <variants> + <element>LIBRARY</element> + </variants> +</process> diff --git a/process/Money_lib/process.js b/process/Money_lib/process.js new file mode 100644 index 0000000000000000000000000000000000000000..1b59e2d69db667b7904c12747574f8c20ed65ae8 --- /dev/null +++ b/process/Money_lib/process.js @@ -0,0 +1,70 @@ +import("system.logging"); +import("system.eMath"); + +/** + * Class containing static utility functions for working with money + * Do not create an instance of this! + * + * @class + */ +function MoneyUtils() {} + +/** + * round a price with eMath.ROUND_HALF_UP + * @param {Double} pPrice + * + * @return rounded price + */ +MoneyUtils.round = function(pPrice) +{ + return eMath.roundDec(pPrice, 2, eMath.ROUND_HALF_UP); +} + +/** + * get net price + * @param {Double} pPrice + * @param {Double} [pQuantity=0.0] + * @param {Double} [pDiscount=0.0] + * + * @return net of price + */ +MoneyUtils.getNet = function(pPrice, pQuantity, pDiscount) +{ + pQuantity = pQuantity || 0; + pDiscount = pDiscount || 0; + return MoneyUtils.round(pPrice * pQuantity * (100 - pDiscount) / 100); +} + +/** + * get vat of a price + * @param {Double} pVat + * @param {Double} pPrice + * @param {Double} [pQuantity=0.0] + * @param {Double} [pDiscount=0.0] + * + * @return vat of price + */ +MoneyUtils.getVat = function(pVat, pPrice, pQuantity, pDiscount) +{ + pQuantity = pQuantity || 0; + pDiscount = pDiscount || 0; + + return MoneyUtils.round(pPrice * pQuantity * (100 - pDiscount) / 100 * pVat / 100); +} + +/** + * get gross of a price + * @param {Double} pVat + * @param {Double} pPrice + * @param {Double} [pQuantity=0.0] + * @param {Double} [pDiscount=0.0] + * + * @return gross of price + */ +MoneyUtils.getGross = function(pVat, pPrice, pQuantity, pDiscount) { + + pQuantity = pQuantity || 0; + pDiscount = pDiscount || 0; + + return eMath.addDec(MoneyUtils.getNet(pPrice, pQuantity, pDiscount), MoneyUtils.getVat(pVat, pPrice, pQuantity, pDiscount)); +} \ No newline at end of file diff --git a/process/OfferOrder_lib/process.js b/process/OfferOrder_lib/process.js index cb1dc57b42c964b43850f223dfaee60a8aff5789..d96c87ce756caa5319eac5ad3ddba88aa09625e1 100644 --- a/process/OfferOrder_lib/process.js +++ b/process/OfferOrder_lib/process.js @@ -6,6 +6,7 @@ import("system.util"); import("system.eMath"); import("system.db"); import("Product_lib"); +import("Money_lib"); /** * Abstract class that provides methods for dealing with offer / order items. @@ -163,8 +164,8 @@ ItemUtils.prototype.getNetAndVat = function(itemIds) { for (var i = 0; i < orderItems.length; i++) { - sum += this.getItemSum(orderItems[i][0], orderItems[i][1], orderItems[i][2], orderItems[i][4]); - vat += this.getItemVAT(orderItems[i][0], orderItems[i][1], orderItems[i][2], orderItems[i][3], orderItems[i][4]); + sum = eMath.addDec(sum, this.getItemSum(orderItems[i][0], orderItems[i][1], orderItems[i][2], orderItems[i][4])); + vat = eMath.addDec(vat, this.getItemVAT(orderItems[i][0], orderItems[i][1], orderItems[i][2], orderItems[i][3], orderItems[i][4])); } return [sum, vat]; @@ -200,33 +201,33 @@ ItemUtils.prototype.initItemTree = function() { /** * @abstract */ -ItemUtils.prototype.getItemSum = function(quantity, price, discount, optional) { - quantity = quantity || 0; - price = price || 0; - discount = discount || 0; +ItemUtils.prototype.getItemSum = function(pQuantity, pPrice, pDiscount, pOptional) { + pQuantity = pQuantity || 0; + pPrice = pPrice || 0; + pDiscount = pDiscount || 0; - return optional ? (parseFloat(quantity) * parseFloat(price) * ((100 - parseFloat(discount)) / 100)) + return pOptional == "0" ? (MoneyUtils.getNet(parseFloat(pPrice), parseFloat(pQuantity), parseFloat(pDiscount))) : 0; } /** * @abstract */ -ItemUtils.prototype.getItemVAT = function(quantity, price, discount, vat, optional) { - quantity = quantity || 0; - price = price || 0; - discount = discount || 0; - vat = vat || 0; - - return optional ? (parseFloat(quantity) * parseFloat(price) * ((100 - parseFloat(discount)) / 100) * (parseFloat(vat) / 100)) +ItemUtils.prototype.getItemVAT = function(pQuantity, pPrice, pDiscount, pVat, pOptional) { + pQuantity = pQuantity || 0; + pPrice = pPrice || 0; + pDiscount = pDiscount || 0; + pVat = pVat || 0; + logging.log(pOptional.toSource()) + return pOptional == "0" ? (MoneyUtils.getVat(parseFloat(pVat), parseFloat(pPrice), parseFloat(pQuantity), parseFloat(pDiscount))) : 0; } /** * @abstract */ -ItemUtils.prototype.roundPrice = function(price) { - return eMath.roundDec(price, 2, eMath.ROUND_HALF_UP); +ItemUtils.prototype.roundPrice = function(pPrice) { + return MoneyUtils.round(pPrice); } /**