This commit is contained in:
Yuri Kuznetsov
2024-10-29 21:29:17 +02:00
parent 95a4ca9524
commit 2c6dd05ec8
7 changed files with 46 additions and 49 deletions
@@ -29,6 +29,7 @@
namespace Espo\Hooks\Common;
use Espo\Core\ORM\Type\FieldType;
use Espo\ORM\Entity;
use Espo\Core\Di;
@@ -44,7 +45,7 @@ class CurrencyConverted implements Di\MetadataAware, Di\ConfigAware
$fieldDefs = $this->metadata->get(['entityDefs', $entity->getEntityType(), 'fields'], []);
foreach ($fieldDefs as $fieldName => $defs) {
if (empty($defs['type']) || $defs['type'] !== 'currencyConverted') {
if (empty($defs['type']) || $defs['type'] !== FieldType::CURRENCY_CONVERTED) {
continue;
}
@@ -84,8 +85,8 @@ class CurrencyConverted implements Di\MetadataAware, Di\ConfigAware
$targetValue = $value;
} else {
$targetValue = $value;
$targetValue = $targetValue / (isset($rates[$baseCurrency]) ? $rates[$baseCurrency] : 1.0);
$targetValue = $targetValue * (isset($rates[$currency]) ? $rates[$currency] : 1.0);
$targetValue = $targetValue / ($rates[$baseCurrency] ?? 1.0);
$targetValue = $targetValue * ($rates[$currency] ?? 1.0);
$targetValue = round($targetValue, 2);
}
@@ -29,30 +29,21 @@
namespace Espo\Hooks\Common;
use Espo\Core\ORM\Type\FieldType;
use Espo\ORM\Entity;
use Espo\Core\Utils\{
Config,
FieldUtil,
};
use Espo\Core\Utils\Config;
use Espo\Core\Utils\FieldUtil;
class CurrencyDefault
{
public static int $order = 200;
private Config $config;
private FieldUtil $fieldUtil;
public function __construct(Config $config, FieldUtil $fieldUtil)
{
$this->config = $config;
$this->fieldUtil = $fieldUtil;
}
public function __construct(private Config $config, private FieldUtil $fieldUtil)
{}
public function beforeSave(Entity $entity): void
{
$fieldList = $this->fieldUtil->getFieldByTypeList($entity->getEntityType(), 'currency');
$fieldList = $this->fieldUtil->getFieldByTypeList($entity->getEntityType(), FieldType::CURRENCY);
$defaultCurrency = $this->config->get('defaultCurrency');