From cae40465f544dffc3fbd069bf7b2481a4a647a48 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 8 Oct 2014 16:44:58 +0300 Subject: [PATCH 1/5] query aggregation and distrinct --- application/Espo/ORM/DB/Query.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/application/Espo/ORM/DB/Query.php b/application/Espo/ORM/DB/Query.php index 4cd463e90d..4898ee2689 100644 --- a/application/Espo/ORM/DB/Query.php +++ b/application/Espo/ORM/DB/Query.php @@ -104,7 +104,7 @@ class Query } if (empty($params['aggregation'])) { - $selectPart = $this->getSelect($entity, $params['select']); + $selectPart = $this->getSelect($entity, $params['select'], $params['distinct']); $orderPart = $this->getOrder($entity, $params['orderBy'], $params['order']); if (!empty($params['additionalColumns']) && is_array($params['additionalColumns']) && !empty($params['relationName'])) { @@ -179,7 +179,7 @@ class Query } } - protected function getFunctionPart($function, $part) + protected function getFunctionPart($function, $part, $entityName, $distinct = false) { switch ($function) { case 'MONTH': @@ -187,11 +187,19 @@ class Query case 'DAY': return "DATE_FORMAT({$part}, '%Y-%m-%d')"; } + if ($distinct) { + $idPart = $this->toDb($entityName) . ".id"; + switch ($function) { + case 'SUM': + case 'COUNT': + return $function . "({$part}) * COUNT(DISTINCT {$idPart}) / COUNT({$idPart})"; + } + } return $function . '(' . $part . ')'; } - protected function convertComplexExpression($entity, $field, $entityName = null) + protected function convertComplexExpression($entity, $field, $entityName = null, $distinct = false) { $function = null; $relName = null; @@ -216,12 +224,12 @@ class Query } } if ($function) { - $part = $this->getFunctionPart(strtoupper($function), $part); + $part = $this->getFunctionPart(strtoupper($function), $part, $entityName, $distinct); } return $part; } - protected function getSelect(IEntity $entity, $fields = null) + protected function getSelect(IEntity $entity, $fields = null, $distinct = false) { $select = ""; $arr = array(); @@ -237,7 +245,7 @@ class Query if (array_key_exists($field, $entity->fields)) { $fieldDefs = $entity->fields[$field]; } else { - $part = $this->convertComplexExpression($entity, $field, $entity->getEntityName()); + $part = $this->convertComplexExpression($entity, $field, $entity->getEntityName(), $distinct); $arr[] = $part . ' AS `' . $field . '`'; continue; } From 016992573f0a97247228573e2ad6ce81b2423897 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Wed, 8 Oct 2014 17:55:12 +0300 Subject: [PATCH 2/5] change version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c099a85d7c..3b6bb493f5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "espocrm", - "version": "2.5.2", + "version": "2.6.0", "description": "", "main": "index.php", "repository": "", From 6379198019135eba0564ba058be76ab3748565da Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 9 Oct 2014 11:32:22 +0300 Subject: [PATCH 3/5] export delimited ; --- application/Espo/Core/defaults/config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/Espo/Core/defaults/config.php b/application/Espo/Core/defaults/config.php index 4bc28684cc..31e6e67053 100644 --- a/application/Espo/Core/defaults/config.php +++ b/application/Espo/Core/defaults/config.php @@ -41,7 +41,7 @@ return array ( 'weekStart' => 0, 'thousandSeparator' => ',', 'decimalMark' => '.', - 'exportDelimiter' => ',', + 'exportDelimiter' => ';', 'currencyList' => array ( ), From 3ba308e513dab965f9ed48509434fd566a6c05c0 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 9 Oct 2014 11:33:26 +0300 Subject: [PATCH 4/5] weighted amount --- .../Utils/Database/Orm/Fields/Currency.php | 3 ++- .../Crm/Resources/i18n/en_US/Opportunity.json | 3 ++- .../metadata/entityDefs/Opportunity.json | 18 +++++++++++++++++- .../src/views/fields/currency-converted.js | 10 ++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php b/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php index 1febef7540..f3d56094a2 100644 --- a/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php +++ b/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php @@ -53,7 +53,8 @@ class Currency extends \Espo\Core\Utils\Database\Orm\Base "<=" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate <= {value}", "<>" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate <> {value}" ), - 'notStorable' => true + 'notStorable' => true, + 'orderBy' => $converedFieldName . " {direction}" ), ), ), diff --git a/application/Espo/Modules/Crm/Resources/i18n/en_US/Opportunity.json b/application/Espo/Modules/Crm/Resources/i18n/en_US/Opportunity.json index 76ae378181..fd1953f729 100644 --- a/application/Espo/Modules/Crm/Resources/i18n/en_US/Opportunity.json +++ b/application/Espo/Modules/Crm/Resources/i18n/en_US/Opportunity.json @@ -10,7 +10,8 @@ "closeDate": "Close Date", "contacts": "Contacts", "description": "Description", - "amountConverted": "Amount (converted)" + "amountConverted": "Amount (converted)", + "amountWeightedConverted": "Amount Weighted" }, "links": { "contacts": "Contacts", diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json index e2ceec880f..ec966bc7fd 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json @@ -11,7 +11,23 @@ }, "amountConverted": { "type": "currencyConverted", - "readOnly": true + "readOnly": true + }, + "amountWeightedConverted": { + "type": "float", + "readOnly": true, + "notStorable": true, + "select": "opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100", + "where": { + "=": "(opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100) = {value}", + "<": "(opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100) < {value}", + ">": "(opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100) > {value}", + "<=": "(opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100) <= {value}", + ">=": "(opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100) >= {value}", + "<>": "(opportunity.amount * amount_currency_alias.rate * opportunity.probability / 100) <> {value}" + }, + "orderBy": "amountWeightedConverted {direction}", + "view": "Fields.CurrencyConverted" }, "account": { "type": "link", diff --git a/frontend/client/src/views/fields/currency-converted.js b/frontend/client/src/views/fields/currency-converted.js index 25cadf4791..20fac901f3 100644 --- a/frontend/client/src/views/fields/currency-converted.js +++ b/frontend/client/src/views/fields/currency-converted.js @@ -22,6 +22,16 @@ Espo.define('Views.Fields.CurrencyConverted', 'Views.Fields.Float', function (Dep) { return Dep.extend({ + + detailTemplate: 'fields.currency.detail', + + listTemplate: 'fields.currency.detail', + + data: function () { + return _.extend({ + currencyValue: this.getConfig().get('baseCurrency'), + }, Dep.prototype.data.call(this)); + }, }); }); From b65e23edfe8d34c972852fba5b9f0ad4788a76e0 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Thu, 9 Oct 2014 12:12:57 +0300 Subject: [PATCH 5/5] change main.html --- frontend/html/main.html | 2 ++ frontend/index.html | 1 + 2 files changed, 3 insertions(+) diff --git a/frontend/html/main.html b/frontend/html/main.html index 4abbc32cf0..1150e901cd 100644 --- a/frontend/html/main.html +++ b/frontend/html/main.html @@ -7,6 +7,8 @@ + +