mirror of
https://github.com/espocrm/espocrm.git
synced 2026-03-03 02:57:01 +00:00
currency no join mode getting rates dynamically
This commit is contained in:
@@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
/************************************************************************
|
||||||
|
* This file is part of EspoCRM.
|
||||||
|
*
|
||||||
|
* EspoCRM – Open Source CRM application.
|
||||||
|
* Copyright (C) 2014-2026 EspoCRM, Inc.
|
||||||
|
* Website: https://www.espocrm.com
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* The interactive user interfaces in modified source and object code versions
|
||||||
|
* of this program must display Appropriate Legal Notices, as required under
|
||||||
|
* Section 5 of the GNU Affero General Public License version 3.
|
||||||
|
*
|
||||||
|
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||||
|
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||||
|
************************************************************************/
|
||||||
|
|
||||||
|
namespace Espo\Core\ORM\QueryComposer\Part\FunctionConverters;
|
||||||
|
|
||||||
|
use Espo\Core\Currency\ConfigDataProvider;
|
||||||
|
use Espo\ORM\Query\Part\Expression;
|
||||||
|
use Espo\ORM\QueryComposer\Part\FunctionConverter;
|
||||||
|
use Espo\ORM\QueryComposer\Util;
|
||||||
|
use RuntimeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @noinspection PhpUnused
|
||||||
|
*/
|
||||||
|
class CurrencyRate implements FunctionConverter
|
||||||
|
{
|
||||||
|
private const int PRECISION = 5;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private ConfigDataProvider $config,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
public function convert(string ...$argumentList): string
|
||||||
|
{
|
||||||
|
$arg = $argumentList[0] ?? null;
|
||||||
|
|
||||||
|
if (!is_string($arg) || !Util::isArgumentString($arg)) {
|
||||||
|
throw new RuntimeException("CURRENCY_RATE function accepts only literal string argument.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$code = substr($arg, 1, -1);
|
||||||
|
|
||||||
|
if (!in_array($code, $this->config->getCurrencyList())) {
|
||||||
|
return Expression::value(0)->getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
$baseCurrency = $this->config->getBaseCurrency();
|
||||||
|
$defaultCurrency = $this->config->getDefaultCurrency();
|
||||||
|
|
||||||
|
$rates = $this->config->getCurrencyRates()->toAssoc();
|
||||||
|
|
||||||
|
if ($defaultCurrency !== $baseCurrency) {
|
||||||
|
$rates = $this->exchangeRates($baseCurrency, $defaultCurrency, $rates);
|
||||||
|
}
|
||||||
|
|
||||||
|
$rate = $rates[$code] ?? 1.0;
|
||||||
|
|
||||||
|
return Expression::value($rate)->getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, float> $currencyRates
|
||||||
|
* @return array<string, float>
|
||||||
|
*/
|
||||||
|
private function exchangeRates(string $baseCurrency, string $defaultCurrency, array $currencyRates): array
|
||||||
|
{
|
||||||
|
$defaultCurrencyRate = round(1 / $currencyRates[$defaultCurrency], self::PRECISION);
|
||||||
|
|
||||||
|
$exchangedRates = [];
|
||||||
|
$exchangedRates[$baseCurrency] = $defaultCurrencyRate;
|
||||||
|
|
||||||
|
unset($currencyRates[$baseCurrency], $currencyRates[$defaultCurrency]);
|
||||||
|
|
||||||
|
foreach ($currencyRates as $code => $rate) {
|
||||||
|
$exchangedRates[$code] = round($rate * $defaultCurrencyRate, self::PRECISION);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $exchangedRates;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -124,12 +124,8 @@ class Currency implements FieldConverter
|
|||||||
$currencyAttribute = $name . 'Currency';
|
$currencyAttribute = $name . 'Currency';
|
||||||
|
|
||||||
$defaultCurrency = $this->configDataProvider->getDefaultCurrency();
|
$defaultCurrency = $this->configDataProvider->getDefaultCurrency();
|
||||||
$baseCurrency = $this->configDataProvider->getBaseCurrency();
|
|
||||||
$rates = $this->configDataProvider->getCurrencyRates()->toAssoc();
|
|
||||||
|
|
||||||
if ($defaultCurrency !== $baseCurrency) {
|
$rates = $this->configDataProvider->getCurrencyList();
|
||||||
$rates = $this->exchangeRates($baseCurrency, $defaultCurrency, $rates);
|
|
||||||
}
|
|
||||||
|
|
||||||
$expr = Expr::multiply(
|
$expr = Expr::multiply(
|
||||||
Expr::column($name),
|
Expr::column($name),
|
||||||
@@ -216,28 +212,7 @@ class Currency implements FieldConverter
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, float> $currencyRates
|
* @param string[] $rates
|
||||||
* @return array<string, float>
|
|
||||||
*/
|
|
||||||
private function exchangeRates(string $baseCurrency, string $defaultCurrency, array $currencyRates): array
|
|
||||||
{
|
|
||||||
$precision = 5;
|
|
||||||
$defaultCurrencyRate = round(1 / $currencyRates[$defaultCurrency], $precision);
|
|
||||||
|
|
||||||
$exchangedRates = [];
|
|
||||||
$exchangedRates[$baseCurrency] = $defaultCurrencyRate;
|
|
||||||
|
|
||||||
unset($currencyRates[$baseCurrency], $currencyRates[$defaultCurrency]);
|
|
||||||
|
|
||||||
foreach ($currencyRates as $currencyName => $rate) {
|
|
||||||
$exchangedRates[$currencyName] = round($rate * $defaultCurrencyRate, $precision);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $exchangedRates;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array<string, float> $rates
|
|
||||||
*/
|
*/
|
||||||
private function buildExpression(string $currencyAttribute, array $rates): Expr|float
|
private function buildExpression(string $currencyAttribute, array $rates): Expr|float
|
||||||
{
|
{
|
||||||
@@ -245,13 +220,11 @@ class Currency implements FieldConverter
|
|||||||
return 0.0;
|
return 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$currency = array_key_first($rates);
|
$currency = array_shift($rates);
|
||||||
$value = $rates[$currency];
|
|
||||||
unset($rates[$currency]);
|
|
||||||
|
|
||||||
return Expr::if(
|
return Expr::if(
|
||||||
Expr::equal(Expr::column($currencyAttribute), $currency),
|
Expr::equal(Expr::column($currencyAttribute), $currency),
|
||||||
$value,
|
Expr::create("CURRENCY_RATE:('$currency')"),
|
||||||
$this->buildExpression($currencyAttribute, $rates)
|
$this->buildExpression($currencyAttribute, $rates)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,16 @@
|
|||||||
"queryComposerClassName": "Espo\\ORM\\QueryComposer\\MysqlQueryComposer",
|
"queryComposerClassName": "Espo\\ORM\\QueryComposer\\MysqlQueryComposer",
|
||||||
"pdoFactoryClassName": "Espo\\ORM\\PDO\\MysqlPDOFactory",
|
"pdoFactoryClassName": "Espo\\ORM\\PDO\\MysqlPDOFactory",
|
||||||
"functionConverterClassNameMap": {
|
"functionConverterClassNameMap": {
|
||||||
"ABS": "Espo\\Core\\ORM\\QueryComposer\\Part\\FunctionConverters\\Abs"
|
"ABS": "Espo\\Core\\ORM\\QueryComposer\\Part\\FunctionConverters\\Abs",
|
||||||
|
"CURRENCY_RATE": "Espo\\Core\\ORM\\QueryComposer\\Part\\FunctionConverters\\CurrencyRate"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"Postgresql": {
|
"Postgresql": {
|
||||||
"queryComposerClassName": "Espo\\ORM\\QueryComposer\\PostgresqlQueryComposer",
|
"queryComposerClassName": "Espo\\ORM\\QueryComposer\\PostgresqlQueryComposer",
|
||||||
"pdoFactoryClassName": "Espo\\ORM\\PDO\\PostgresqlPDOFactory",
|
"pdoFactoryClassName": "Espo\\ORM\\PDO\\PostgresqlPDOFactory",
|
||||||
"functionConverterClassNameMap": {
|
"functionConverterClassNameMap": {
|
||||||
"ABS": "Espo\\Core\\ORM\\QueryComposer\\Part\\FunctionConverters\\Abs"
|
"ABS": "Espo\\Core\\ORM\\QueryComposer\\Part\\FunctionConverters\\Abs",
|
||||||
|
"CURRENCY_RATE": "Espo\\Core\\ORM\\QueryComposer\\Part\\FunctionConverters\\CurrencyRate"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ use Espo\Core\Exceptions\Error;
|
|||||||
use Espo\Core\Field\Date;
|
use Espo\Core\Field\Date;
|
||||||
use Espo\Core\Formula\Manager as FormulaManager;
|
use Espo\Core\Formula\Manager as FormulaManager;
|
||||||
use Espo\Modules\Crm\Entities\Lead;
|
use Espo\Modules\Crm\Entities\Lead;
|
||||||
|
use Espo\Modules\Crm\Entities\Opportunity;
|
||||||
use Espo\Tools\Currency\RateEntryProvider;
|
use Espo\Tools\Currency\RateEntryProvider;
|
||||||
use Espo\Tools\Currency\RateService;
|
use Espo\Tools\Currency\RateService;
|
||||||
use Espo\Core\Currency\Rates;
|
use Espo\Core\Currency\Rates;
|
||||||
@@ -178,4 +179,88 @@ class CurrencyTest extends BaseTestCase
|
|||||||
$value = $formulaManager->run($script);
|
$value = $formulaManager->run($script);
|
||||||
$this->assertEquals(1.0, (float) $value);
|
$this->assertEquals(1.0, (float) $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @noinspection PhpUnhandledExceptionInspection
|
||||||
|
*/
|
||||||
|
public function testDefaultCurrencyJoinMode(): void
|
||||||
|
{
|
||||||
|
$configWriter = $this->getInjectableFactory()->create(ConfigWriter::class);
|
||||||
|
|
||||||
|
$configWriter->set('currencyNoJoinMode', false);
|
||||||
|
$configWriter->set('currencyList', ['USD', 'EUR']);
|
||||||
|
$configWriter->set('defaultCurrency', 'USD');
|
||||||
|
$configWriter->set('baseCurrency', 'EUR');
|
||||||
|
$configWriter->save();
|
||||||
|
|
||||||
|
$this->getInjectableFactory()->create(SyncManager::class)->sync();
|
||||||
|
|
||||||
|
$rateService = $this->getInjectableFactory()->create(RateService::class);
|
||||||
|
$rateService->set(Rates::fromAssoc([
|
||||||
|
'USD' => 0.5,
|
||||||
|
], 'EUR'));
|
||||||
|
|
||||||
|
$this->getDataManager()->rebuild();
|
||||||
|
$this->reCreateApplication();
|
||||||
|
|
||||||
|
$em = $this->getEntityManager();
|
||||||
|
|
||||||
|
$opp = $em->getRDBRepositoryByClass(Opportunity::class)->getNew();
|
||||||
|
$opp->setAmount(Currency::create('10.0', 'USD'));
|
||||||
|
$em->saveEntity($opp);
|
||||||
|
$em->refreshEntity($opp);
|
||||||
|
|
||||||
|
$this->assertEquals(10, $opp->get('amountConverted'));
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
$opp = $em->getRDBRepositoryByClass(Opportunity::class)->getNew();
|
||||||
|
$opp->setAmount(Currency::create('10.0', 'EUR'));
|
||||||
|
$em->saveEntity($opp);
|
||||||
|
$em->refreshEntity($opp);
|
||||||
|
|
||||||
|
$this->assertEquals(20, $opp->get('amountConverted'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @noinspection PhpUnhandledExceptionInspection
|
||||||
|
*/
|
||||||
|
public function testDefaultCurrencyNoJoinMode(): void
|
||||||
|
{
|
||||||
|
$configWriter = $this->getInjectableFactory()->create(ConfigWriter::class);
|
||||||
|
|
||||||
|
$configWriter->set('currencyNoJoinMode', true);
|
||||||
|
$configWriter->set('currencyList', ['USD', 'EUR']);
|
||||||
|
$configWriter->set('defaultCurrency', 'USD');
|
||||||
|
$configWriter->set('baseCurrency', 'EUR');
|
||||||
|
$configWriter->save();
|
||||||
|
|
||||||
|
$this->getInjectableFactory()->create(SyncManager::class)->sync();
|
||||||
|
|
||||||
|
$rateService = $this->getInjectableFactory()->create(RateService::class);
|
||||||
|
$rateService->set(Rates::fromAssoc([
|
||||||
|
'USD' => 0.5,
|
||||||
|
], 'EUR'));
|
||||||
|
|
||||||
|
$this->getDataManager()->rebuild();
|
||||||
|
$this->reCreateApplication();
|
||||||
|
|
||||||
|
$em = $this->getEntityManager();
|
||||||
|
|
||||||
|
$opp = $em->getRDBRepositoryByClass(Opportunity::class)->getNew();
|
||||||
|
$opp->setAmount(Currency::create('10.0', 'USD'));
|
||||||
|
$em->saveEntity($opp);
|
||||||
|
$em->refreshEntity($opp);
|
||||||
|
|
||||||
|
$this->assertEquals(10, $opp->get('amountConverted'));
|
||||||
|
|
||||||
|
//
|
||||||
|
|
||||||
|
$opp = $em->getRDBRepositoryByClass(Opportunity::class)->getNew();
|
||||||
|
$opp->setAmount(Currency::create('10.0', 'EUR'));
|
||||||
|
$em->saveEntity($opp);
|
||||||
|
$em->refreshEntity($opp);
|
||||||
|
|
||||||
|
$this->assertEquals(20, $opp->get('amountConverted'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user