htmlizer: currency format

This commit is contained in:
Yuri Kuznetsov
2025-11-27 10:34:13 +02:00
parent 5b87d12449
commit 1d1e012991
4 changed files with 123 additions and 2 deletions

View File

@@ -0,0 +1,53 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM Open Source CRM application.
* Copyright (C) 2014-2025 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\Currency;
use Espo\Core\Utils\Metadata;
/**
* @since 9.3.0
*/
class PrecisionProvider
{
private const int DEFAULT_PRECISION = 2;
public function __construct(
private Metadata $metadata,
) {}
public function get(?string $code): int
{
if (!$code) {
return self::DEFAULT_PRECISION;
}
return $this->metadata->get("app.currency.precisionMap.$code") ?? self::DEFAULT_PRECISION;
}
}

View File

@@ -34,6 +34,7 @@ use DOMDocument;
use DOMElement;
use DOMException;
use DOMXPath;
use Espo\Core\Currency\PrecisionProvider;
use Espo\Core\Exceptions\BadRequest;
use Espo\Core\Exceptions\Forbidden;
use Espo\Core\ORM\Entity as CoreEntity;
@@ -73,7 +74,7 @@ use const JSON_PRESERVE_ZERO_FRACTION;
*/
class Htmlizer
{
private const LINK_LIMIT = 100;
private const int LINK_LIMIT = 100;
public function __construct(
private DateTime $dateTime,
@@ -86,6 +87,7 @@ class Htmlizer
private Config $config,
private Log $log,
private InjectableFactory $injectableFactory,
private PrecisionProvider $precisionProvider,
private ?Acl $acl = null,
private ?ServiceFactory $serviceFactory = null,
) {}
@@ -1061,6 +1063,16 @@ class Htmlizer
}
}
if ($fieldType === FieldType::CURRENCY) {
$code = $data[$attribute . 'Currency'] ?? null;
if (!is_string($code)) {
$code = null;
}
$data[$attribute] = $this->formatCurrencyValue($data[$attribute], $code);
}
$data[$attribute] = $this->format($data[$attribute]);
}
}
@@ -1107,4 +1119,19 @@ class Htmlizer
return $data;
}
private function formatCurrencyValue(mixed $value, ?string $code): ?string
{
if (!is_string($value) && !is_int($value) && !is_float($value)) {
return null;
}
if (is_string($value) && !is_numeric($value)) {
return null;
}
$precision = $this->precisionProvider->get($code);
return $this->number->format($value, $precision);
}
}

View File

@@ -265,5 +265,36 @@
"ZAR",
"ZMW",
"ZWL"
]
],
"precisionMap": {
"BHD": 3,
"BIF": 0,
"CLP": 0,
"DJF": 0,
"GNF": 0,
"ISK": 0,
"IQD": 3,
"JOD": 3,
"JPY": 0,
"KMF": 0,
"KRW": 0,
"KWD": 3,
"LYD": 3,
"OMR": 3,
"PYG": 0,
"RWF": 0,
"TND": 3,
"UGX": 0,
"UYI": 0,
"VND": 0,
"VUV": 0,
"XAF": 0,
"XAU": 0,
"XBA": 0,
"XBB": 0,
"XBC": 0,
"XDR": 0,
"XOF": 0,
"XPF": 0
}
}

View File

@@ -13,6 +13,16 @@
"description": "A 3-letter ISO 4217 currency code."
}
},
"precisionMap": {
"type": "object",
"description": "A currency-code => precision mapping.",
"additionalProperties": {
"type": "number",
"description": "Number of decimal places.",
"minimum": 0,
"maximum": 4
}
},
"list": {
"type": "array",
"description": "A list of currencies available in the system. Values defined as 3-letter currency codes in ISO 4217 standard. Use __APPEND__ to add new currencies w/o deleting existing.",