diff --git a/application/Espo/Core/ORM/Repositories/RDB.php b/application/Espo/Core/ORM/Repositories/RDB.php index c889b778af..8246970a10 100644 --- a/application/Espo/Core/ORM/Repositories/RDB.php +++ b/application/Espo/Core/ORM/Repositories/RDB.php @@ -26,6 +26,7 @@ use \Espo\ORM\EntityManager; use \Espo\ORM\EntityFactory; use \Espo\ORM\Entity; use \Espo\ORM\IEntity; +use Espo\Core\Utils\Util; use \Espo\Core\Interfaces\Injectable; @@ -63,11 +64,38 @@ class RDB extends \Espo\ORM\Repositories\RDB implements Injectable { $this->handleEmailAddressParams($params); $this->handlePhoneNumberParams($params); + $this->handleCurrencyParams($params); + } + + protected function handleCurrencyParams(&$params) + { + $entityName = $this->entityName; + + $metadata = $this->getMetadata(); + + if (!$metadata) { + return; + } + + $defs = $metadata->get('entityDefs.' . $entityName); + + foreach ($defs['fields'] as $field => $d) { + if ($d['type'] == 'currency') { + if (empty($params['customJoin'])) { + $params['customJoin'] = ''; + } + $alias = Util::toUnderScore($field) . "_currency_alias"; + $params['customJoin'] .= " + LEFT JOIN currency AS `{$alias}` ON {$alias}.id = ".Util::toUnderScore($entityName).".".Util::toUnderScore($field)."_currency + "; + } + } + } protected function handleEmailAddressParams(&$params) { - $entityName = $this->entityName; + $entityName = $this->entityName; $defs = $this->getEntityManager()->getMetadata()->get($entityName); if (!empty($defs['relations']) && array_key_exists('emailAddresses', $defs['relations'])) { diff --git a/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php b/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php new file mode 100644 index 0000000000..b3d677af2d --- /dev/null +++ b/application/Espo/Core/Utils/Database/Orm/Fields/Currency.php @@ -0,0 +1,45 @@ + array( + 'fields' => array( + $fieldName => array( + "type" => "float", + "orderBy" => $converedFieldName . " {direction}" + ), + ), + ), + ); + } + +} diff --git a/application/Espo/Core/Utils/Database/Orm/Fields/CurrencyConverted.php b/application/Espo/Core/Utils/Database/Orm/Fields/CurrencyConverted.php new file mode 100644 index 0000000000..13bae84ea6 --- /dev/null +++ b/application/Espo/Core/Utils/Database/Orm/Fields/CurrencyConverted.php @@ -0,0 +1,58 @@ + array( + 'fields' => array( + $fieldName => array( + 'type' => 'float', + 'select' => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate" , + 'where' => + array ( + "=" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate = {value}", + ">" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate > {value}", + "<" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate < {value}", + ">=" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate >= {value}", + "<=" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate <= {value}", + "<>" => Util::toUnderScore($entityName) . "." . $currencyColumnName . " * {$alias}.rate <> {value}" + ), + 'notStorable' => true + ), + + ), + ), + ); + } + +} diff --git a/application/Espo/Modules/Crm/Repositories/Lead.php b/application/Espo/Modules/Crm/Repositories/Lead.php index e72d67f9ca..401211767b 100644 --- a/application/Espo/Modules/Crm/Repositories/Lead.php +++ b/application/Espo/Modules/Crm/Repositories/Lead.php @@ -26,18 +26,7 @@ use Espo\ORM\Entity; class Lead extends \Espo\Core\ORM\Repositories\RDB { - public function handleSelectParams(&$params) - { - parent::handleSelectParams($params); - - if (empty($params['customJoin'])) { - $params['customJoin'] = ''; - } - - $params['customJoin'] .= " - LEFT JOIN currency ON currency.id = lead.opportunity_amount_currency - "; - } + } diff --git a/application/Espo/Modules/Crm/Repositories/Opportunity.php b/application/Espo/Modules/Crm/Repositories/Opportunity.php index cb2d0e3ccd..70224d1a82 100644 --- a/application/Espo/Modules/Crm/Repositories/Opportunity.php +++ b/application/Espo/Modules/Crm/Repositories/Opportunity.php @@ -26,18 +26,7 @@ use Espo\ORM\Entity; class Opportunity extends \Espo\Core\ORM\Repositories\RDB { - public function handleSelectParams(&$params) - { - parent::handleSelectParams($params); - - if (empty($params['customJoin'])) { - $params['customJoin'] = ''; - } - - $params['customJoin'] .= " - LEFT JOIN currency ON currency.id = opportunity.amount_currency - "; - } + } diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json index d6b22608c9..46750859cc 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Lead.json @@ -39,22 +39,11 @@ }, "opportunityAmount": { "type": "currency", - "audited": true, - "orderBy": "opportunityAmountConverted {direction}" + "audited": true }, "opportunityAmountConverted": { - "type": "float", - "notStorable": true, - "readOnly": true, - "select": "lead.opportunity_amount * currency.rate", - "where": { - "=": "lead.opportunity_amount * currency.rate = {value}", - ">": "lead.opportunity_amount * currency.rate > {value}", - "<": "lead.opportunity_amount * currency.rate < {value}", - ">=": "lead.opportunity_amount * currency.rate >= {value}", - "<=": "lead.opportunity_amount * currency.rate <= {value}", - "<>": "lead.opportunity_amount * currency.rate <> {value}" - } + "type": "currencyConverted", + "readOnly": true }, "website": { "type": "url" diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json index cd23cffb46..e2ceec880f 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/Opportunity.json @@ -7,22 +7,11 @@ "amount": { "type": "currency", "required": true, - "audited": true, - "orderBy": "amountConverted {direction}" + "audited": true }, "amountConverted": { - "type": "float", - "notStorable": true, - "readOnly": true, - "select": "opportunity.amount * currency.rate", - "where": { - "=": "opportunity.amount * currency.rate = {value}", - ">": "opportunity.amount * currency.rate > {value}", - "<": "opportunity.amount * currency.rate < {value}", - ">=": "opportunity.amount * currency.rate >= {value}", - "<=": "opportunity.amount * currency.rate <= {value}", - "<>": "opportunity.amount * currency.rate <> {value}" - } + "type": "currencyConverted", + "readOnly": true }, "account": { "type": "link", diff --git a/application/Espo/Resources/metadata/fields/currency.json b/application/Espo/Resources/metadata/fields/currency.json index f7e2cf57c0..92581559d4 100644 --- a/application/Espo/Resources/metadata/fields/currency.json +++ b/application/Espo/Resources/metadata/fields/currency.json @@ -16,16 +16,18 @@ ], "actualFields":[ "currency", + "converted", "" ], "fields":{ "currency":{ "type":"varchar", "disabled": true + }, + "converted":{ + "type":"currencyConverted", + "readOnly": true } }, - "filter": true, - "fieldDefs":{ - "type":"float" - } + "filter": true } diff --git a/application/Espo/Resources/metadata/fields/currencyConverted.json b/application/Espo/Resources/metadata/fields/currencyConverted.json new file mode 100644 index 0000000000..50d4b0babf --- /dev/null +++ b/application/Espo/Resources/metadata/fields/currencyConverted.json @@ -0,0 +1,9 @@ +{ + "params":[ + ], + "filter": true, + "notCreatable": true, + "fieldDefs":{ + "notStorable":true + } +} diff --git a/frontend/client/src/views/fields/currency-converted.js b/frontend/client/src/views/fields/currency-converted.js new file mode 100644 index 0000000000..25cadf4791 --- /dev/null +++ b/frontend/client/src/views/fields/currency-converted.js @@ -0,0 +1,28 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + ************************************************************************/ + +Espo.define('Views.Fields.CurrencyConverted', 'Views.Fields.Float', function (Dep) { + + return Dep.extend({ + + }); +}); +