From 2c6dd05ec82869a93075911f141d7fd313e347cb Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Tue, 29 Oct 2024 21:29:17 +0200 Subject: [PATCH] ref --- application/Espo/Core/ORM/Type/FieldType.php | 1 + .../Database/Orm/FieldConverters/Currency.php | 9 ++-- .../Espo/Hooks/Common/CurrencyConverted.php | 7 +-- .../Espo/Hooks/Common/CurrencyDefault.php | 21 +++----- .../Conversion/DefaultEntityConverter.php | 3 +- .../Export/Format/Xlsx/ParamsHandler.php | 3 +- .../Format/Xlsx/PhpSpreadsheetProcessor.php | 51 ++++++++++--------- 7 files changed, 46 insertions(+), 49 deletions(-) diff --git a/application/Espo/Core/ORM/Type/FieldType.php b/application/Espo/Core/ORM/Type/FieldType.php index a1ed5ae9cf..f7b9545974 100644 --- a/application/Espo/Core/ORM/Type/FieldType.php +++ b/application/Espo/Core/ORM/Type/FieldType.php @@ -44,6 +44,7 @@ class FieldType public const ARRAY = 'array'; public const CHECKLIST = 'checklist'; public const CURRENCY = 'currency'; + public const CURRENCY_CONVERTED = 'currencyConverted'; public const PERSON_NAME = 'personName'; public const ADDRESS = 'address'; public const EMAIL = 'email'; diff --git a/application/Espo/Core/Utils/Database/Orm/FieldConverters/Currency.php b/application/Espo/Core/Utils/Database/Orm/FieldConverters/Currency.php index a16f9ddcb1..c4e1212bb1 100644 --- a/application/Espo/Core/Utils/Database/Orm/FieldConverters/Currency.php +++ b/application/Espo/Core/Utils/Database/Orm/FieldConverters/Currency.php @@ -31,6 +31,7 @@ namespace Espo\Core\Utils\Database\Orm\FieldConverters; use Doctrine\DBAL\Types\Types; use Espo\Core\Currency\ConfigDataProvider; +use Espo\Core\ORM\Type\FieldType; use Espo\Core\Utils\Config; use Espo\Core\Utils\Database\Orm\Defs\AttributeDefs; use Espo\Core\Utils\Database\Orm\Defs\EntityDefs; @@ -57,14 +58,14 @@ class Currency implements FieldConverter ->withType(AttributeType::FLOAT) ->withParamsMerged([ 'attributeRole' => 'value', - 'fieldType' => 'currency', + 'fieldType' => FieldType::CURRENCY, ]); $currencyDefs = AttributeDefs::create($name . 'Currency') ->withType(AttributeType::VARCHAR) ->withParamsMerged([ 'attributeRole' => 'currency', - 'fieldType' => 'currency', + 'fieldType' => FieldType::CURRENCY, ]); $convertedDefs = null; @@ -198,7 +199,7 @@ class Currency implements FieldConverter ], ], 'attributeRole' => 'valueConverted', - 'fieldType' => 'currency', + 'fieldType' => FieldType::CURRENCY, ]); return [$amountDefs, $convertedDefs]; @@ -331,7 +332,7 @@ class Currency implements FieldConverter 'additionalSelect' => ["{$alias}.rate"], ], 'attributeRole' => 'valueConverted', - 'fieldType' => 'currency', + 'fieldType' => FieldType::CURRENCY, ]); return [$amountDefs, $convertedDefs]; diff --git a/application/Espo/Hooks/Common/CurrencyConverted.php b/application/Espo/Hooks/Common/CurrencyConverted.php index e4fecbca59..4d04bbcd5d 100644 --- a/application/Espo/Hooks/Common/CurrencyConverted.php +++ b/application/Espo/Hooks/Common/CurrencyConverted.php @@ -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); } diff --git a/application/Espo/Hooks/Common/CurrencyDefault.php b/application/Espo/Hooks/Common/CurrencyDefault.php index 313318eafd..39e2be7a0c 100644 --- a/application/Espo/Hooks/Common/CurrencyDefault.php +++ b/application/Espo/Hooks/Common/CurrencyDefault.php @@ -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'); diff --git a/application/Espo/Tools/Currency/Conversion/DefaultEntityConverter.php b/application/Espo/Tools/Currency/Conversion/DefaultEntityConverter.php index 66ae70b0bd..e1377ac9e4 100644 --- a/application/Espo/Tools/Currency/Conversion/DefaultEntityConverter.php +++ b/application/Espo/Tools/Currency/Conversion/DefaultEntityConverter.php @@ -35,6 +35,7 @@ use Espo\Core\Currency\Converter; use Espo\Core\Currency\Rates; use Espo\Core\Field\Currency; use Espo\Core\ORM\Entity as CoreEntity; +use Espo\Core\ORM\Type\FieldType; use Espo\Core\Utils\Metadata; use Espo\ORM\Entity; use Espo\ORM\EntityManager; @@ -109,7 +110,7 @@ class DefaultEntityConverter implements EntityConverter $field = $fieldDefs->getName(); $type = $fieldDefs->getType(); - if ($type !== 'currency') { + if ($type !== FieldType::CURRENCY) { continue; } diff --git a/application/Espo/Tools/Export/Format/Xlsx/ParamsHandler.php b/application/Espo/Tools/Export/Format/Xlsx/ParamsHandler.php index ed942f1eb8..4b7e067e2a 100644 --- a/application/Espo/Tools/Export/Format/Xlsx/ParamsHandler.php +++ b/application/Espo/Tools/Export/Format/Xlsx/ParamsHandler.php @@ -29,6 +29,7 @@ namespace Espo\Tools\Export\Format\Xlsx; +use Espo\Core\ORM\Type\FieldType; use Espo\Core\Utils\Metadata; use Espo\ORM\Entity; use Espo\Tools\Export\Params; @@ -122,7 +123,7 @@ class ParamsHandler implements ProcessorParamsHandler foreach ($fieldList as $field) { $type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']); - if ($type === 'currencyConverted') { + if ($type === FieldType::CURRENCY_CONVERTED) { if (!in_array($field, $attributeList)) { $attributeList[] = $field; } diff --git a/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php b/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php index 9ef54514a2..8a0f0298e9 100644 --- a/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php +++ b/application/Espo/Tools/Export/Format/Xlsx/PhpSpreadsheetProcessor.php @@ -32,6 +32,7 @@ namespace Espo\Tools\Export\Format\Xlsx; use Espo\Core\Field\Currency; use Espo\Core\Field\Date; use Espo\Core\Field\DateTime as DateTimeValue; +use Espo\Core\ORM\Type\FieldType; use Espo\Entities\Attachment; use Espo\Core\FileStorage\Manager as FileStorageManager; use Espo\Core\ORM\EntityManager; @@ -190,8 +191,8 @@ class PhpSpreadsheetProcessor implements ProcessorInterface $col = $azRange[$lastIndex]; - $sheet->getStyle("A{$rowNumber}:{$col}{$rowNumber}")->applyFromArray($this->headerStyle); - $sheet->setAutoFilter("A{$rowNumber}:{$col}{$rowNumber}"); + $sheet->getStyle("A$rowNumber:$col$rowNumber")->applyFromArray($this->headerStyle); + $sheet->setAutoFilter("A$rowNumber:$col$rowNumber"); $typesCache = []; @@ -210,7 +211,7 @@ class PhpSpreadsheetProcessor implements ProcessorInterface $rowNumber++; } - $sheet->getStyle("A{$headerRowNumber}:A{$rowNumber}") + $sheet->getStyle("A$headerRowNumber:A$rowNumber") ->getNumberFormat() ->setFormatCode(NumberFormat::FORMAT_TEXT); @@ -229,35 +230,35 @@ class PhpSpreadsheetProcessor implements ProcessorInterface $type = $typesCache[$name]; - $coordinate = $col . $startingRowNumber . ':' . $col . $rowNumber; + $coordinate = "$col$startingRowNumber:$col$rowNumber"; switch ($type) { - case 'currency': - case 'currencyConverted': + case FieldType::CURRENCY: + case FieldType::CURRENCY_CONVERTED: break; - case 'int': + case FieldType::INT: $sheet->getStyle($coordinate) ->getNumberFormat() ->setFormatCode('0'); break; - case 'float': + case FieldType::FLOAT: $sheet->getStyle($coordinate) ->getNumberFormat() ->setFormatCode(NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1); break; - case 'date': + case FieldType::DATE: $sheet->getStyle($coordinate) ->getNumberFormat() ->setFormatCode($this->dateTime->getDateFormat()); break; - case 'datetimeOptional': - case 'datetime': + case FieldType::DATETIME_OPTIONAL: + case FieldType::DATETIME: $sheet->getStyle($coordinate) ->getNumberFormat() ->setFormatCode($this->dateTime->getDateTimeFormat()); @@ -506,22 +507,22 @@ class PhpSpreadsheetProcessor implements ProcessorInterface $foreignField = null; if (strpos($name, '_')) { - list($foreignLink, $foreignField) = explode('_', $name); + [$foreignLink, $foreignField] = explode('_', $name); } $siteUrl = $this->config->getSiteUrl(); if ($name === 'name') { if ($entity->hasId()) { - $link = $siteUrl . '/#' . $entityType . '/view/' . $entity->getId(); + $link = "$siteUrl/#$entityType/view/{$entity->getId()}"; } - } else if ($type === 'url') { + } else if ($type === FieldType::URL) { $value = $entity->get($name); if ($value) { $link = $this->sanitizeUrl($value); } - } else if ($type === 'link') { + } else if ($type === FieldType::LINK) { $idValue = $entity->get($name . 'Id'); if ($idValue && $foreignField) { @@ -536,33 +537,33 @@ class PhpSpreadsheetProcessor implements ProcessorInterface } if ($foreignEntity) { - $link = $siteUrl . '/#' . $foreignEntity . '/view/' . $idValue; + $link = "$siteUrl/#$foreignEntity/view/$idValue"; } } - } else if ($type === 'file') { + } else if ($type === FieldType::FILE) { $idValue = $entity->get($name . 'Id'); if ($idValue) { - $link = $siteUrl . '/?entryPoint=download&id=' . $idValue; + $link = "$siteUrl/?entryPoint=download&id=$idValue"; } - } else if ($type === 'linkParent') { + } else if ($type === FieldType::LINK_PARENT) { $idValue = $entity->get($name . 'Id'); - $typeValue = $entity->get($name . 'Type');; + $typeValue = $entity->get($name . 'Type'); if ($idValue && $typeValue) { - $link = $siteUrl . '/#' . $typeValue . '/view/' . $idValue; + $link = "$siteUrl/#$typeValue/view/$idValue"; } - } else if ($type === 'phone') { + } else if ($type === FieldType::PHONE) { $value = $entity->get($name); if ($value) { - $link = 'tel:' . $value; + $link = "tel:$value"; } - } else if ($type === 'email') { + } else if ($type === FieldType::EMAIL) { $value = $entity->get($name); if ($value) { - $link = 'mailto:' . $value; + $link = "mailto:$value"; } }