From 145f4f07a3b40092ff05f32411887de162c2e110 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 17 Dec 2024 09:35:45 +0200 Subject: [PATCH 01/25] HP-1751 Create configurations for billing types --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1c690397..da19ace1 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,8 @@ "hiqdev/hipanel-module-client": "dev-master", "tuck/sort": "^0.1.0", "moneyphp/money": "*", - "hiqdev/php-units": "*" + "hiqdev/php-units": "*", + "advancedhosters/billing-registry": "dev-master" }, "require-dev": { "hiqdev/hidev-php": "dev-master", From 153f37d13224e6d13490e689236696c7d3a0cbcd Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 17 Dec 2024 10:25:40 +0200 Subject: [PATCH 02/25] HP-1751 Create configurations for billing types --- config/web.php | 3 +++ src/logic/bill/QuantityFormatterFactory.php | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config/web.php b/config/web.php index ddbd0c71..78b972f0 100644 --- a/config/web.php +++ b/config/web.php @@ -290,6 +290,9 @@ }, \Money\MoneyParser::class => \Money\Parser\DecimalMoneyParser::class, // \hipanel\modules\finance\helpers\ConsumptionConfigurator::class => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class, + \hiqdev\php\billing\product\BillingRegistryInterface::class => static function () { + return \hiqdev\billing\registry\TariffConfiguration::buildRegistry(); + }, ], ], ]; diff --git a/src/logic/bill/QuantityFormatterFactory.php b/src/logic/bill/QuantityFormatterFactory.php index 1a124f0b..49641b39 100644 --- a/src/logic/bill/QuantityFormatterFactory.php +++ b/src/logic/bill/QuantityFormatterFactory.php @@ -64,7 +64,11 @@ final class QuantityFormatterFactory implements QuantityFormatterFactoryInterfac 'overuse,snapshot_du' => [FractionQuantityFormatter::class, FractionUnit::SIZE], ]; - public function __construct(private readonly IntlFormatter $intlFormatter, private readonly BillTypesProvider $billTypesProvider) + public function __construct( + private readonly IntlFormatter $intlFormatter, + private readonly BillTypesProvider $billTypesProvider, + private readonly BillingRegistryInterface $BillingRegistry, + ) { } From 84f3be6b11f9dbe19d9f03bd9c580a5e9371a829 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 20 Dec 2024 12:52:25 +0200 Subject: [PATCH 03/25] HP-1751 cleaned up ConsumptionConfigurator from duplicates and thus simplified it --- config/web.php | 20 -------------------- src/helpers/ConsumptionConfigurator.php | 12 ++++++------ 2 files changed, 6 insertions(+), 26 deletions(-) diff --git a/config/web.php b/config/web.php index 78b972f0..99630734 100644 --- a/config/web.php +++ b/config/web.php @@ -154,8 +154,6 @@ 'cdn_cache95', ], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'videocdn' => [ 'label' => ['hipanel:finance', 'VideoCDN'], @@ -170,64 +168,46 @@ 'server_sata', ], 'groups' => [['server_traf', 'server_traf_in'], ['server_traf95', 'server_traf95_in']], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'vps' => [ 'label' => ['hipanel:finance', 'VPS'], 'columns' => ['vps_traf', 'vps_traf_in', 'vps_traf_max', 'cloud_ip_anycast', 'cloud_ip_public'], 'groups' => [['vps_traf', 'vps_traf_in', 'cloud_ip_anycast', 'cloud_ip_public']], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'snapshot' => [ 'label' => ['hipanel:finance', 'Snapshot'], 'columns' => ['snapshot_du'], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'volume' => [ 'label' => ['hipanel:finance', 'Volume'], 'columns' => ['volume_du'], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'storage' => [ 'label' => ['hipanel:finance', 'Storage'], 'columns' => ['storage_du', 'storage_du95'], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'private_cloud' => [ 'label' => ['hipanel:finance', 'Private cloud'], 'columns' => [], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'private_cloud_backup' => [ 'label' => ['hipanel:finance', 'Private cloud backup'], 'columns' => ['private_cloud_backup_du'], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'load_balancer' => [ 'label' => ['hipanel:finance', 'Load balancer'], 'columns' => ['lb_capacity_unit', 'lb_ha_capacity_unit'], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], 'tariff' => [ 'label' => ['hipanel:finance', 'Tariff resources'], 'columns' => ['server_traf95_max', 'server_traf95', 'server_traf95_in'], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, ], ] ], diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index 47a67135..662048a5 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -21,7 +21,7 @@ public function getColumns(string $class): array return $this->getConfigurationByClass($class)['columns']; } - public function getGroups(string $class): array + private function getGroups(string $class): array { $groups = []; $columns = $this->getColumns($class); @@ -97,7 +97,7 @@ public function getAllPossibleColumnsWithLabels(): array return $result; } - public function getDecorator(string $class, string $type): ResourceDecoratorInterface + private function getDecorator(string $class, string $type): ResourceDecoratorInterface { $config = $this->getConfigurationByClass($class); $config['resourceModel']->type = $type; @@ -144,8 +144,8 @@ private function getConfigurationByClass(string $class): array 'label' => ['hipanel:finance', $class], 'columns' => [], 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, + 'model' => $this->createObject(Target::class), + 'resourceModel' => $this->createObject(TargetResource::class), ]; return $this->getConfigurations()[$class] ?? $fallback; @@ -156,8 +156,8 @@ public function getConfigurations(): array return array_map(function (array $config): array { [$dictionary, $label] = $config['label']; $config['label'] = Yii::t($dictionary, $label); - $config['model'] = $this->createObject($config['model']); - $config['resourceModel'] = $this->createObject($config['resourceModel']); + $config['model'] = $this->createObject(Target::class); + $config['resourceModel'] = $this->createObject(TargetResource::class); return $config; }, $this->configurations); From 35e187c84942f4b55dae50889d2fd76bd3640d12 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 27 Dec 2024 01:54:42 +0200 Subject: [PATCH 04/25] HP-1751 removed part of Consumption configuration because it was implemented in Registry --- config/web.php | 65 ++------------------------------------------------ 1 file changed, 2 insertions(+), 63 deletions(-) diff --git a/config/web.php b/config/web.php index 99630734..62365365 100644 --- a/config/web.php +++ b/config/web.php @@ -141,73 +141,12 @@ \hipanel\modules\finance\helpers\ConsumptionConfigurator::class => [ 'class' => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class, 'configurations' => [ - 'anycastcdn' => [ - 'label' => ['hipanel:finance', 'Anycast CDN'], - 'columns' => [ - 'cdn_traf_plain', - 'cdn_traf_ssl', - 'cdn_traf', - 'cdn_traf_max', - 'cdn_traf95', - 'cdn_traf95_max', - 'cdn_cache', - 'cdn_cache95', - ], - 'groups' => [], - ], - 'videocdn' => [ - 'label' => ['hipanel:finance', 'VideoCDN'], - 'columns' => [ - 'server_traf', - 'server_traf_in', - 'server_traf95', - 'server_traf95_in', - 'server_du', - 'server_files', - 'server_ssd', - 'server_sata', - ], - 'groups' => [['server_traf', 'server_traf_in'], ['server_traf95', 'server_traf95_in']], - ], - 'vps' => [ - 'label' => ['hipanel:finance', 'VPS'], - 'columns' => ['vps_traf', 'vps_traf_in', 'vps_traf_max', 'cloud_ip_anycast', 'cloud_ip_public'], - 'groups' => [['vps_traf', 'vps_traf_in', 'cloud_ip_anycast', 'cloud_ip_public']], - ], - 'snapshot' => [ - 'label' => ['hipanel:finance', 'Snapshot'], - 'columns' => ['snapshot_du'], - 'groups' => [], - ], - 'volume' => [ - 'label' => ['hipanel:finance', 'Volume'], - 'columns' => ['volume_du'], - 'groups' => [], - ], - 'storage' => [ - 'label' => ['hipanel:finance', 'Storage'], - 'columns' => ['storage_du', 'storage_du95'], - 'groups' => [], - ], - 'private_cloud' => [ - 'label' => ['hipanel:finance', 'Private cloud'], - 'columns' => [], - 'groups' => [], - ], - 'private_cloud_backup' => [ - 'label' => ['hipanel:finance', 'Private cloud backup'], - 'columns' => ['private_cloud_backup_du'], - 'groups' => [], - ], - 'load_balancer' => [ - 'label' => ['hipanel:finance', 'Load balancer'], - 'columns' => ['lb_capacity_unit', 'lb_ha_capacity_unit'], - 'groups' => [], - ], 'tariff' => [ 'label' => ['hipanel:finance', 'Tariff resources'], 'columns' => ['server_traf95_max', 'server_traf95', 'server_traf95_in'], 'groups' => [], + 'model' => Target::class, + 'resourceModel' => TargetResource::class, ], ] ], From c848e294323aef952e50f770f0623b0e5435c6d1 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 27 Dec 2024 02:20:38 +0200 Subject: [PATCH 05/25] HP-1751 overwrote ConsumptionConfigurator::getConfigurations() method to use Billing Registry --- src/helpers/ConsumptionConfigurator.php | 31 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index 662048a5..e8e94e7d 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -9,6 +9,8 @@ use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; use hipanel\modules\finance\models\Target; use hipanel\modules\finance\models\TargetResource; +use hiqdev\billing\registry\behavior\ConsumptionConfigurationBehaviour; +use hiqdev\php\billing\product\BillingRegistry; use yii\db\ActiveRecordInterface; use Yii; @@ -16,6 +18,10 @@ final class ConsumptionConfigurator { public array $configurations = []; + public function __construct(private readonly BillingRegistry $billingRegistry) + { + } + public function getColumns(string $class): array { return $this->getConfigurationByClass($class)['columns']; @@ -138,7 +144,7 @@ public function getFirstAvailableClass(): string * @param string $class * @return array{label: string, columns: array, group: array, model: ActiveRecordInterface, resourceModel: ActiveRecordInterface} */ - private function getConfigurationByClass(string $class): array + public function getConfigurationByClass(string $class): array { $fallback = [ 'label' => ['hipanel:finance', $class], @@ -153,14 +159,21 @@ private function getConfigurationByClass(string $class): array public function getConfigurations(): array { - return array_map(function (array $config): array { - [$dictionary, $label] = $config['label']; - $config['label'] = Yii::t($dictionary, $label); - $config['model'] = $this->createObject(Target::class); - $config['resourceModel'] = $this->createObject(TargetResource::class); - - return $config; - }, $this->configurations); + $configurations = []; + /** @var ConsumptionConfigurationBehaviour $behavior */ + foreach ($this->billingRegistry->getBehaviors(ConsumptionConfigurationBehaviour::class) as $behavior) { + $tariffType = $behavior->getTariffType(); + + $configurations[$tariffType->name()] = [ + 'label' => $behavior->getLabel(), + 'columns' => $behavior->columns, + 'groups' => $behavior->groups, + 'model' => $this->createObject($behavior->getModel() ?? Target::class), + 'resourceModel' => $this->createObject($behavior->getResourceModel() ?? TargetResource::class), + ]; + } + + return $configurations; } private function createObject(string $className, array $params = []): object From 547de8136d75681d9f299f7d6244e7393a6862ea Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Fri, 27 Dec 2024 12:43:53 +0200 Subject: [PATCH 06/25] HP-1751 Created HasDecorator trait and removed ServerResourceDecoratorFactory, TargetResourceDecoratorFactory and ClientResourceDecoratorFactory classes because ResourceDecoratorFactory in enough --- src/models/ClientResource.php | 13 +--- src/models/HasDecorator.php | 20 ++++++ src/models/ServerResource.php | 15 +---- src/models/TargetResource.php | 13 +--- .../decorators/ResourceDecoratorFactory.php | 29 ++++++--- .../client/ClientResourceDecoratorFactory.php | 34 ----------- .../server/ServerResourceDecoratorFactory.php | 61 ------------------- .../target/TargetResourceDecoratorFactory.php | 41 ------------- src/models/stubs/ServerResourceStub.php | 15 +---- 9 files changed, 47 insertions(+), 194 deletions(-) create mode 100644 src/models/HasDecorator.php delete mode 100644 src/models/decorators/client/ClientResourceDecoratorFactory.php delete mode 100644 src/models/decorators/server/ServerResourceDecoratorFactory.php delete mode 100644 src/models/decorators/target/TargetResourceDecoratorFactory.php diff --git a/src/models/ClientResource.php b/src/models/ClientResource.php index 44fec6d2..84986b43 100644 --- a/src/models/ClientResource.php +++ b/src/models/ClientResource.php @@ -1,19 +1,10 @@ -decorator)) { - $this->decorator = ClientResourceDecoratorFactory::createFromResource($this); - } - - return $this->decorator; - } + use HasDecorator; } diff --git a/src/models/HasDecorator.php b/src/models/HasDecorator.php new file mode 100644 index 00000000..4045b9a5 --- /dev/null +++ b/src/models/HasDecorator.php @@ -0,0 +1,20 @@ +decorator)) { + $this->decorator = ResourceDecoratorFactory::createFromResource($this); + } + + return $this->decorator; + } +} diff --git a/src/models/ServerResource.php b/src/models/ServerResource.php index c514765f..224d5758 100644 --- a/src/models/ServerResource.php +++ b/src/models/ServerResource.php @@ -12,11 +12,7 @@ use hipanel\base\ModelTrait; use hipanel\modules\finance\models\decorators\DecoratedInterface; -use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; -use hipanel\modules\finance\models\decorators\server\AbstractServerResourceDecorator; -use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory; use Yii; -use yii\base\InvalidConfigException; /** * Class ServerResource. @@ -29,6 +25,8 @@ class ServerResource extends Resource implements DecoratedInterface { use ModelTrait; + use HasDecorator; + public static function tableName() { return 'resource'; @@ -116,15 +114,6 @@ public function getMinimumQuantity() return isset($types[$this->type]) ? $types[$this->type] : 0.01; } - public function decorator(): ResourceDecoratorInterface - { - if (empty($this->decorator)) { - $this->decorator = ServerResourceDecoratorFactory::createFromResource($this); - } - - return $this->decorator; - } - public function realObjectId() { if (!$this->isPeriodic()) { diff --git a/src/models/TargetResource.php b/src/models/TargetResource.php index b19317b5..86d2eafe 100644 --- a/src/models/TargetResource.php +++ b/src/models/TargetResource.php @@ -1,19 +1,10 @@ -decorator)) { - $this->decorator = TargetResourceDecoratorFactory::createFromResource($this); - } - - return $this->decorator; - } + use HasDecorator; } diff --git a/src/models/decorators/ResourceDecoratorFactory.php b/src/models/decorators/ResourceDecoratorFactory.php index b16923bb..87e33f33 100644 --- a/src/models/decorators/ResourceDecoratorFactory.php +++ b/src/models/decorators/ResourceDecoratorFactory.php @@ -1,16 +1,15 @@ -model_type ?? $resource->type; - $map = static::typeMap(); - if (!isset($map[$type])) { + $registry = TariffConfiguration::buildRegistry(); + + try { + /** @var ResourceDecoratorBehavior $behavior */ + $behavior = $registry->getBehavior($type, ResourceDecoratorBehavior::class); + + return $behavior->createDecorator(new ResourceDecoratorData( + $resource->quantity, + $resource->price, + $resource->unit, + $resource->currency, + $resource->partno, + )); + } catch (BehaviorNotFoundException) { throw new InvalidConfigException('No representative decoration class found for type "' . $type . '"'); } - - return new $map[$type]($resource); } } diff --git a/src/models/decorators/client/ClientResourceDecoratorFactory.php b/src/models/decorators/client/ClientResourceDecoratorFactory.php deleted file mode 100644 index 778fb02b..00000000 --- a/src/models/decorators/client/ClientResourceDecoratorFactory.php +++ /dev/null @@ -1,34 +0,0 @@ - ReferralResourceDecorator::class, - 'support_time' => SupportResourceDecorator::class, - 'cloud_ip_regular' => IpRegularResourceDecorator::class, - 'cloud_ip_public' => IpPublicResourceDecorator::class, - 'cloud_ip_anycast' => IpAnycastResourceDecorator::class, - 'cloud_ip_regular_max' => IpRegularResourceDecorator::class, - 'cloud_ip_public_max' => IpPublicResourceDecorator::class, - 'cloud_ip_anycast_max' => IpAnycastResourceDecorator::class, - ]; - } -} diff --git a/src/models/decorators/server/ServerResourceDecoratorFactory.php b/src/models/decorators/server/ServerResourceDecoratorFactory.php deleted file mode 100644 index 010cbcf6..00000000 --- a/src/models/decorators/server/ServerResourceDecoratorFactory.php +++ /dev/null @@ -1,61 +0,0 @@ - BackupResourceDecorator::class, - 'chassis' => ChassisResourceDecorator::class, - 'cpu' => CpuResourceDecorator::class, - 'hdd' => HddResourceDecorator::class, - 'ip_num' => IpResourceDecorator::class, - 'isp5' => Isp5ResourceDecorator::class, - 'isp' => IspResourceDecorator::class, - 'ram' => RamResourceDecorator::class, - 'speed' => SpeedResourceDecorator::class, - 'panel' => PanelResourceDecorator::class, - 'support_time' => SupportResourceDecorator::class, - 'server_traf95_max' => Traffic95ResourceDecorator::class, - 'server_traf95_in' => Traffic95ResourceDecorator::class, - 'server_traf95' => Traffic95ResourceDecorator::class, - 'server_traf_max' => TrafficResourceDecorator::class, - 'server_traf_in' => TrafficResourceDecorator::class, - 'server_traf' => TrafficResourceDecorator::class, - 'server_du' => ServerDUResourceDecorator::class, - 'storage_du' => StorageDUResourceDecorator::class, - 'server_ssd' => ServerSSDResourceDecorator::class, - 'server_sata' => ServerSataDUResourceDecorator::class, - 'server_files' => ServerFilesResourceDecorator::class, - 'rack_unit' => RackUnitResourceDecorator::class, - 'location' => LocationResourceDecorator::class, - 'power' => PowerResourceDecorator::class, - 'monthly' => MonthlyResourceDecorator::class, - 'lb_capacity_unit' => LoadBalancerResourceDecorator::class, - 'lb_ha_capacity_unit' => HALoadBalancerResourceDecorator::class, - 'cdn_traf' => CdnTrafficResourceDecorator::class, - 'cdn_traf_plain' => CdnTrafficPlainResourceDecorator::class, - 'cdn_traf_ssl' => CdnTrafficSSLResourceDecorator::class, - 'cdn_traf_max' => CdnTrafficMaxResourceDecorator::class, - 'cloud_ip_regular' => IpRegularResourceDecorator::class, - 'cloud_ip_public' => IpPublicResourceDecorator::class, - 'cloud_ip_anycast' => IpAnycastResourceDecorator::class, - ]; - } -} diff --git a/src/models/decorators/target/TargetResourceDecoratorFactory.php b/src/models/decorators/target/TargetResourceDecoratorFactory.php deleted file mode 100644 index adbf2617..00000000 --- a/src/models/decorators/target/TargetResourceDecoratorFactory.php +++ /dev/null @@ -1,41 +0,0 @@ - CdnTrafficResourceDecorator::class, - 'cdn_traf_plain' => CdnTrafficPlainResourceDecorator::class, - 'cdn_traf_ssl' => CdnTrafficSSLResourceDecorator::class, - 'cdn_traf_max' => CdnTrafficMaxResourceDecorator::class, - 'snapshot_du' => SnapshotDuResourceDecorator::class, - 'volume_du' => VolumeDuResourceDecorator::class, - 'storage_du' => StorageDuResourceDecorator::class, - 'storage_du95' => StorageDu95ResourceDecorator::class, - 'private_cloud_backup_du' => PrivateCloudBackupDuResourceDecorator::class, - 'cdn_cache' => CdnCacheResourceDecorator::class, - 'cdn_cache95' => CdnCache95ResourceDecorator::class, - 'cdn_traf95' => CdnTraffic95ResourceDecorator::class, - 'cdn_traf95_max' => CdnTraffic95MaxResourceDecorator::class, - 'vps_traf' => VpsTrafficResourceDecorator::class, - 'vps_traf_in' => VpsTrafficInResourceDecorator::class, - 'vps_traf_max' => VpsTrafficMaxResourceDecorator::class, - 'cloud_ip_regular' => IpRegularResourceDecorator::class, - 'cloud_ip_public' => IpPublicResourceDecorator::class, - 'cloud_ip_anycast' => IpAnycastResourceDecorator::class, - ]); - } -} diff --git a/src/models/stubs/ServerResourceStub.php b/src/models/stubs/ServerResourceStub.php index 4cb58f1e..8acf9945 100644 --- a/src/models/stubs/ServerResourceStub.php +++ b/src/models/stubs/ServerResourceStub.php @@ -10,22 +10,11 @@ namespace hipanel\modules\finance\models\stubs; -use hipanel\modules\finance\models\decorators\server\AbstractServerResourceDecorator; -use hipanel\modules\finance\models\decorators\server\ServerResourceDecoratorFactory; +use hipanel\modules\finance\models\HasDecorator; class ServerResourceStub extends AbstractResourceStub { - /** - * @return AbstractServerResourceDecorator - */ - public function decorator() - { - if (empty($this->decorator)) { - $this->decorator = ServerResourceDecoratorFactory::createFromResource($this); - } - - return $this->decorator; - } + use HasDecorator; public function __get($value) { From 3c477b08752d176bddff09e26a210c5641c946b5 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 30 Dec 2024 10:37:15 +0200 Subject: [PATCH 07/25] HP-1751 fixed autoload BillingRegistry in ConsumptionConfigurator --- src/helpers/ConsumptionConfigurator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index e8e94e7d..2e7a7dc4 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -10,7 +10,7 @@ use hipanel\modules\finance\models\Target; use hipanel\modules\finance\models\TargetResource; use hiqdev\billing\registry\behavior\ConsumptionConfigurationBehaviour; -use hiqdev\php\billing\product\BillingRegistry; +use hiqdev\php\billing\product\BillingRegistryInterface; use yii\db\ActiveRecordInterface; use Yii; @@ -18,7 +18,7 @@ final class ConsumptionConfigurator { public array $configurations = []; - public function __construct(private readonly BillingRegistry $billingRegistry) + public function __construct(private readonly BillingRegistryInterface $billingRegistry) { } From e39fa0aee204d284f5e295194f16843c43a8fea8 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 30 Dec 2024 15:16:56 +0200 Subject: [PATCH 08/25] HP-1751 use DecoratedInterface from Billing Registry --- src/models/ClientResource.php | 2 +- src/models/HasDecorator.php | 2 +- src/models/Resource.php | 3 --- src/models/ServerResource.php | 2 +- src/models/TargetResource.php | 2 +- src/models/decorators/DecoratedInterface.php | 8 -------- .../decorators/ResourceDecoratorFactory.php | 6 +----- src/models/proxy/Resource.php | 4 ++-- src/models/stubs/AbstractResourceStub.php | 19 ++----------------- 9 files changed, 9 insertions(+), 39 deletions(-) delete mode 100644 src/models/decorators/DecoratedInterface.php diff --git a/src/models/ClientResource.php b/src/models/ClientResource.php index 84986b43..484a7b41 100644 --- a/src/models/ClientResource.php +++ b/src/models/ClientResource.php @@ -2,7 +2,7 @@ namespace hipanel\modules\finance\models; -use hipanel\modules\finance\models\decorators\DecoratedInterface; +use hiqdev\billing\registry\ResourceDecorator\DecoratedInterface; class ClientResource extends Resource implements DecoratedInterface { diff --git a/src/models/HasDecorator.php b/src/models/HasDecorator.php index 4045b9a5..25862b47 100644 --- a/src/models/HasDecorator.php +++ b/src/models/HasDecorator.php @@ -3,7 +3,7 @@ namespace hipanel\modules\finance\models; use hipanel\modules\finance\models\decorators\ResourceDecoratorFactory; -use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; +use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; trait HasDecorator { diff --git a/src/models/Resource.php b/src/models/Resource.php index 0d98d098..c62c90a7 100644 --- a/src/models/Resource.php +++ b/src/models/Resource.php @@ -12,7 +12,6 @@ use hipanel\base\Model; use hipanel\base\ModelTrait; -use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; use hipanel\modules\stock\models\Part; use Money\Money; use Money\MoneyParser; @@ -27,8 +26,6 @@ class Resource extends Model { use ModelTrait; - protected ResourceDecoratorInterface $decorator; - /** {@inheritdoc} */ public static $i18nDictionary = 'hipanel:finance:tariff'; diff --git a/src/models/ServerResource.php b/src/models/ServerResource.php index 224d5758..a65d3eb8 100644 --- a/src/models/ServerResource.php +++ b/src/models/ServerResource.php @@ -11,7 +11,7 @@ namespace hipanel\modules\finance\models; use hipanel\base\ModelTrait; -use hipanel\modules\finance\models\decorators\DecoratedInterface; +use hiqdev\billing\registry\ResourceDecorator\DecoratedInterface; use Yii; /** diff --git a/src/models/TargetResource.php b/src/models/TargetResource.php index 86d2eafe..c6fcefee 100644 --- a/src/models/TargetResource.php +++ b/src/models/TargetResource.php @@ -2,7 +2,7 @@ namespace hipanel\modules\finance\models; -use hipanel\modules\finance\models\decorators\DecoratedInterface; +use hiqdev\billing\registry\ResourceDecorator\DecoratedInterface; class TargetResource extends Resource implements DecoratedInterface { diff --git a/src/models/decorators/DecoratedInterface.php b/src/models/decorators/DecoratedInterface.php deleted file mode 100644 index 57b6f7e8..00000000 --- a/src/models/decorators/DecoratedInterface.php +++ /dev/null @@ -1,8 +0,0 @@ -model_type ?? $resource->type; diff --git a/src/models/proxy/Resource.php b/src/models/proxy/Resource.php index e22a872c..c66d73a0 100644 --- a/src/models/proxy/Resource.php +++ b/src/models/proxy/Resource.php @@ -5,8 +5,8 @@ use hipanel\base\Model; use hipanel\base\ModelTrait; use hipanel\modules\finance\helpers\ConsumptionConfigurator; -use hipanel\modules\finance\models\decorators\DecoratedInterface; -use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; +use hiqdev\billing\registry\ResourceDecorator\DecoratedInterface; +use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; use hiqdev\php\units\Quantity; use hiqdev\php\units\Unit; use yii\db\QueryInterface; diff --git a/src/models/stubs/AbstractResourceStub.php b/src/models/stubs/AbstractResourceStub.php index 7a841609..65339cd9 100644 --- a/src/models/stubs/AbstractResourceStub.php +++ b/src/models/stubs/AbstractResourceStub.php @@ -10,12 +10,11 @@ namespace hipanel\modules\finance\models\stubs; -use hipanel\modules\finance\models\decorators\AbstractResourceDecorator; use hipanel\modules\finance\models\Tariff; -use yii\base\InvalidConfigException; +use hiqdev\billing\registry\ResourceDecorator\DecoratedInterface; use yii\base\Model; -abstract class AbstractResourceStub extends Model +abstract class AbstractResourceStub extends Model implements DecoratedInterface { /** * @var Tariff @@ -26,18 +25,4 @@ abstract class AbstractResourceStub extends Model * @var string */ public $type; - - /** - * @var AbstractResourceDecorator - */ - protected $decorator; - - /** - * @throws InvalidConfigException - * @return AbstractResourceDecorator - */ - public function decorator() - { - throw new InvalidConfigException('Method "decorator" is not available for class Resource'); - } } From ac6d3951513c658e8766b65f6f118675018112ea Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Mon, 30 Dec 2024 15:29:06 +0200 Subject: [PATCH 09/25] HP-1751 tiny --- src/helpers/ConsumptionConfigurator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index 2e7a7dc4..a2b48438 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -6,10 +6,10 @@ use hipanel\helpers\ArrayHelper; use hipanel\modules\finance\models\Consumption; -use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; use hipanel\modules\finance\models\Target; use hipanel\modules\finance\models\TargetResource; use hiqdev\billing\registry\behavior\ConsumptionConfigurationBehaviour; +use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; use hiqdev\php\billing\product\BillingRegistryInterface; use yii\db\ActiveRecordInterface; use Yii; From f1aa88157be00d24a17f55ebd394397a41226210 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 31 Dec 2024 11:14:27 +0200 Subject: [PATCH 10/25] HP-1751 added annotation for ResourceDecoratorFactory::createFromResource() method to understand what parameter types can be passed to the function --- src/models/decorators/ResourceDecoratorFactory.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/models/decorators/ResourceDecoratorFactory.php b/src/models/decorators/ResourceDecoratorFactory.php index ad73a51d..e659de9f 100644 --- a/src/models/decorators/ResourceDecoratorFactory.php +++ b/src/models/decorators/ResourceDecoratorFactory.php @@ -2,6 +2,8 @@ namespace hipanel\modules\finance\models\decorators; +use hipanel\modules\finance\models\Resource; +use hipanel\modules\finance\models\stubs\AbstractResourceStub; use hiqdev\billing\registry\behavior\ResourceDecoratorBehavior; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorData; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; @@ -11,6 +13,11 @@ class ResourceDecoratorFactory { + /** + * @param Resource|AbstractResourceStub $resource + * @return ResourceDecoratorInterface + * @throws InvalidConfigException + */ public static function createFromResource($resource): ResourceDecoratorInterface { $type = $resource->model_type ?? $resource->type; From f43333bf0becf457f0d792a1ec1b76406c257057 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 31 Dec 2024 17:57:10 +0200 Subject: [PATCH 11/25] HP-1751 tiny --- src/models/decorators/ResourceDecoratorFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/models/decorators/ResourceDecoratorFactory.php b/src/models/decorators/ResourceDecoratorFactory.php index e659de9f..74425849 100644 --- a/src/models/decorators/ResourceDecoratorFactory.php +++ b/src/models/decorators/ResourceDecoratorFactory.php @@ -33,7 +33,8 @@ public static function createFromResource($resource): ResourceDecoratorInterface $resource->price, $resource->unit, $resource->currency, - $resource->partno, + $resource->type, + $resource->part->partno, )); } catch (BehaviorNotFoundException) { throw new InvalidConfigException('No representative decoration class found for type "' . $type . '"'); From 08f66cb5024355aa2f57484d8c56305b8b2abf67 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 1 Jan 2025 21:25:43 +0200 Subject: [PATCH 12/25] HP-1751 removed the last mention of ConsumptionConfigurator from configuration --- config/web.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/config/web.php b/config/web.php index 62365365..6d89e73e 100644 --- a/config/web.php +++ b/config/web.php @@ -138,18 +138,6 @@ ], 'container' => [ 'definitions' => [ - \hipanel\modules\finance\helpers\ConsumptionConfigurator::class => [ - 'class' => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class, - 'configurations' => [ - 'tariff' => [ - 'label' => ['hipanel:finance', 'Tariff resources'], - 'columns' => ['server_traf95_max', 'server_traf95', 'server_traf95_in'], - 'groups' => [], - 'model' => Target::class, - 'resourceModel' => TargetResource::class, - ], - ] - ], \hipanel\modules\dashboard\menus\DashboardMenu::class => [ 'add' => [ 'finance' => [ From 9535ff5924d809efd00fd0659920e626508698d5 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 1 Jan 2025 21:33:38 +0200 Subject: [PATCH 13/25] HP-1751 added tariff into ConsumptionConfigurator because it can't be added in Billing Registry --- src/helpers/ConsumptionConfigurator.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index a2b48438..ad9bc54a 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -9,6 +9,7 @@ use hipanel\modules\finance\models\Target; use hipanel\modules\finance\models\TargetResource; use hiqdev\billing\registry\behavior\ConsumptionConfigurationBehaviour; +use hiqdev\billing\registry\product\PriceType; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; use hiqdev\php\billing\product\BillingRegistryInterface; use yii\db\ActiveRecordInterface; @@ -173,6 +174,19 @@ public function getConfigurations(): array ]; } + // Can't be added to Billing Registry, so left as it is + $configurations['tariff'] = [ + 'label' => Yii::t('hipanel:finance', 'Tariff resources'), + 'columns' => [ + PriceType::server_traf95_max->name(), + //'server_traf95', // TODO: Didn't find it in the database for tariff. Commented for now because it is causing fatal error + //'server_traf95_in', // TODO: Didn't find it in the database for tariff. Commented for now because it is causing fatal error + ], + 'groups' => [], + 'model' => $this->createObject(Target::class), + 'resourceModel' => $this->createObject(TargetResource::class), + ]; + return $configurations; } From 041a29476f5fb3a8944298ad2b2bb36c718b49d3 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Wed, 1 Jan 2025 21:39:12 +0200 Subject: [PATCH 14/25] HP-1751 removed decorators because they are moved to Billing Registry --- .../decorators/AbstractResourceDecorator.php | 24 ----- .../decorators/ResourceDecoratorInterface.php | 32 ------- .../AbstractClientResourceDecorator.php | 63 ------------- .../client/ReferralResourceDecorator.php | 23 ----- .../AbstractServerResourceDecorator.php | 94 ------------------- .../server/BackupResourceDecorator.php | 36 ------- .../server/ChassisResourceDecorator.php | 26 ----- .../server/CpuResourceDecorator.php | 54 ----------- .../HALoadBalancerResourceDecorator.php | 30 ------ .../server/HddResourceDecorator.php | 49 ---------- .../decorators/server/IpResourceDecorator.php | 36 ------- .../server/Isp5ResourceDecorator.php | 21 ----- .../server/IspResourceDecorator.php | 37 -------- .../server/LoadBalancerResourceDecorator.php | 30 ------ .../server/LocationResourceDecorator.php | 82 ---------------- .../server/MonthlyResourceDecorator.php | 31 ------ .../server/PanelResourceDecorator.php | 56 ----------- .../server/PowerResourceDecorator.php | 46 --------- .../server/RackUnitResourceDecorator.php | 21 ----- .../server/RamResourceDecorator.php | 49 ---------- .../server/ServerDUResourceDecorator.php | 21 ----- .../server/ServerFilesResourceDecorator.php | 31 ------ .../server/ServerSSDResourceDecorator.php | 21 ----- .../server/ServerSataDUResourceDecorator.php | 21 ----- .../server/SpeedResourceDecorator.php | 51 ---------- .../server/StorageDUResourceDecorator.php | 21 ----- .../server/SupportResourceDecorator.php | 44 --------- .../server/Traffic95ResourceDecorator.php | 36 ------- .../server/TrafficResourceDecorator.php | 36 ------- .../AbstractTargetResourceDecorator.php | 17 ---- .../target/CdnCache95ResourceDecorator.php | 13 --- .../target/CdnCacheResourceDecorator.php | 13 --- .../CdnTraffic95MaxResourceDecorator.php | 21 ----- .../target/CdnTraffic95ResourceDecorator.php | 22 ----- .../target/CdnTrafficMaxResourceDecorator.php | 21 ----- .../CdnTrafficPlainResourceDecorator.php | 21 ----- .../target/CdnTrafficResourceDecorator.php | 22 ----- .../target/CdnTrafficSSLResourceDecorator.php | 22 ----- .../target/IpAnycastResourceDecorator.php | 23 ----- .../target/IpPublicResourceDecorator.php | 23 ----- .../target/IpRegularResourceDecorator.php | 34 ------- .../PrivateCloudBackupDuResourceDecorator.php | 22 ----- .../target/SnapshotDuResourceDecorator.php | 22 ----- .../target/StorageDu95ResourceDecorator.php | 21 ----- .../target/StorageDuResourceDecorator.php | 22 ----- .../target/VolumeDuResourceDecorator.php | 22 ----- .../target/VpsTrafficInResourceDecorator.php | 22 ----- .../target/VpsTrafficMaxResourceDecorator.php | 22 ----- .../target/VpsTrafficResourceDecorator.php | 23 ----- 49 files changed, 1550 deletions(-) delete mode 100644 src/models/decorators/AbstractResourceDecorator.php delete mode 100644 src/models/decorators/ResourceDecoratorInterface.php delete mode 100644 src/models/decorators/client/AbstractClientResourceDecorator.php delete mode 100644 src/models/decorators/client/ReferralResourceDecorator.php delete mode 100644 src/models/decorators/server/AbstractServerResourceDecorator.php delete mode 100644 src/models/decorators/server/BackupResourceDecorator.php delete mode 100644 src/models/decorators/server/ChassisResourceDecorator.php delete mode 100644 src/models/decorators/server/CpuResourceDecorator.php delete mode 100644 src/models/decorators/server/HALoadBalancerResourceDecorator.php delete mode 100644 src/models/decorators/server/HddResourceDecorator.php delete mode 100644 src/models/decorators/server/IpResourceDecorator.php delete mode 100644 src/models/decorators/server/Isp5ResourceDecorator.php delete mode 100644 src/models/decorators/server/IspResourceDecorator.php delete mode 100644 src/models/decorators/server/LoadBalancerResourceDecorator.php delete mode 100644 src/models/decorators/server/LocationResourceDecorator.php delete mode 100644 src/models/decorators/server/MonthlyResourceDecorator.php delete mode 100644 src/models/decorators/server/PanelResourceDecorator.php delete mode 100644 src/models/decorators/server/PowerResourceDecorator.php delete mode 100644 src/models/decorators/server/RackUnitResourceDecorator.php delete mode 100644 src/models/decorators/server/RamResourceDecorator.php delete mode 100644 src/models/decorators/server/ServerDUResourceDecorator.php delete mode 100644 src/models/decorators/server/ServerFilesResourceDecorator.php delete mode 100644 src/models/decorators/server/ServerSSDResourceDecorator.php delete mode 100644 src/models/decorators/server/ServerSataDUResourceDecorator.php delete mode 100644 src/models/decorators/server/SpeedResourceDecorator.php delete mode 100644 src/models/decorators/server/StorageDUResourceDecorator.php delete mode 100644 src/models/decorators/server/SupportResourceDecorator.php delete mode 100644 src/models/decorators/server/Traffic95ResourceDecorator.php delete mode 100644 src/models/decorators/server/TrafficResourceDecorator.php delete mode 100644 src/models/decorators/target/AbstractTargetResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnCache95ResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnCacheResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnTraffic95MaxResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnTraffic95ResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnTrafficMaxResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnTrafficPlainResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnTrafficResourceDecorator.php delete mode 100644 src/models/decorators/target/CdnTrafficSSLResourceDecorator.php delete mode 100644 src/models/decorators/target/IpAnycastResourceDecorator.php delete mode 100644 src/models/decorators/target/IpPublicResourceDecorator.php delete mode 100644 src/models/decorators/target/IpRegularResourceDecorator.php delete mode 100644 src/models/decorators/target/PrivateCloudBackupDuResourceDecorator.php delete mode 100644 src/models/decorators/target/SnapshotDuResourceDecorator.php delete mode 100644 src/models/decorators/target/StorageDu95ResourceDecorator.php delete mode 100644 src/models/decorators/target/StorageDuResourceDecorator.php delete mode 100644 src/models/decorators/target/VolumeDuResourceDecorator.php delete mode 100644 src/models/decorators/target/VpsTrafficInResourceDecorator.php delete mode 100644 src/models/decorators/target/VpsTrafficMaxResourceDecorator.php delete mode 100644 src/models/decorators/target/VpsTrafficResourceDecorator.php diff --git a/src/models/decorators/AbstractResourceDecorator.php b/src/models/decorators/AbstractResourceDecorator.php deleted file mode 100644 index d5658c72..00000000 --- a/src/models/decorators/AbstractResourceDecorator.php +++ /dev/null @@ -1,24 +0,0 @@ -resource = $resource; - } -} diff --git a/src/models/decorators/ResourceDecoratorInterface.php b/src/models/decorators/ResourceDecoratorInterface.php deleted file mode 100644 index 35b14252..00000000 --- a/src/models/decorators/ResourceDecoratorInterface.php +++ /dev/null @@ -1,32 +0,0 @@ -resource->getTypes()[$this->resource->type]; - } - - public function getPrepaidQuantity() - { - return $this->resource->quantity; - } - - public function getOverusePrice() - { - return $this->resource->price; - } - - public function displayUnit() - { - return $this->resource->unit; - } - - public function toUnit(): string - { - return $this->displayUnit(); - } - - public function displayOverusePrice() - { - return Yii::$app->formatter->asCurrency($this->getOverusePrice(), $this->resource->currency); - } - - public function displayPrepaidAmount() - { - return \Yii::t('hipanel:finance:tariff', '{amount} {unit}', [ - 'amount' => $this->getPrepaidQuantity(), - 'unit' => $this->displayUnit(), - ]); - } - - public function prepaidAmountType() - { - return new TextInput(); - } -} diff --git a/src/models/decorators/client/ReferralResourceDecorator.php b/src/models/decorators/client/ReferralResourceDecorator.php deleted file mode 100644 index 6c241e71..00000000 --- a/src/models/decorators/client/ReferralResourceDecorator.php +++ /dev/null @@ -1,23 +0,0 @@ - $this->getPrepaidQuantity()]); - } - - public function displayAmountWithUnit(): string - { - return Yii::$app->formatter->asCurrency($this->getPrepaidQuantity(), $this->displayUnit()); - } -} diff --git a/src/models/decorators/server/AbstractServerResourceDecorator.php b/src/models/decorators/server/AbstractServerResourceDecorator.php deleted file mode 100644 index ec7bd170..00000000 --- a/src/models/decorators/server/AbstractServerResourceDecorator.php +++ /dev/null @@ -1,94 +0,0 @@ -resource->getTypes()[$this->resource->type]; - } - - public function displayTitleWithDirection(string $title): string - { - $direction = Yii::t('hipanel', 'OUT'); - if (str_contains($this->resource->type, '_in')) { - $direction = Yii::t('hipanel', 'IN'); - } - if (str_contains($this->resource->type, '_max')) { - $direction = ''; - } - - return Yii::t('hipanel:finance', '{title} {direction}', ['title' => $title, 'direction' => $direction]); - } - - public function getPrepaidQuantity() - { - return $this->resource->quantity; - } - - public function getOverusePrice() - { - return $this->resource->price; - } - - public function displayUnit() - { - return $this->resource->unit; - } - - public function toUnit(): string - { - return $this->displayUnit(); - } - - public function displayOverusePrice() - { - return Yii::$app->formatter->asCurrency($this->getOverusePrice(), $this->resource->currency); - } - - public function displayPrepaidAmount() - { - return Yii::t('hipanel:finance:tariff', '{amount} {unit}', [ - 'amount' => $this->getPrepaidQuantity(), - 'unit' => $this->displayUnit(), - ]); - } - - public function prepaidAmountType() - { - return new TextInput(); - } - - public function displayAmountWithUnit(): string - { - return Yii::t('hipanel:finance:tariff', '{amount} {unit}', [ - 'amount' => number_format(ResourceHelper::convertAmount($this), 3), - 'unit' => $this->displayUnit(), - ]); - } -} diff --git a/src/models/decorators/server/BackupResourceDecorator.php b/src/models/decorators/server/BackupResourceDecorator.php deleted file mode 100644 index 5575d03d..00000000 --- a/src/models/decorators/server/BackupResourceDecorator.php +++ /dev/null @@ -1,36 +0,0 @@ - $this->getPrepaidQuantity()]); - } - - public function toUnit(): string - { - return 'gb'; - } -} diff --git a/src/models/decorators/server/ChassisResourceDecorator.php b/src/models/decorators/server/ChassisResourceDecorator.php deleted file mode 100644 index 91822331..00000000 --- a/src/models/decorators/server/ChassisResourceDecorator.php +++ /dev/null @@ -1,26 +0,0 @@ -resource->part->partno; - } -} diff --git a/src/models/decorators/server/CpuResourceDecorator.php b/src/models/decorators/server/CpuResourceDecorator.php deleted file mode 100644 index b9a0a861..00000000 --- a/src/models/decorators/server/CpuResourceDecorator.php +++ /dev/null @@ -1,54 +0,0 @@ -getCpuUnit() === self::UNIT_CORE) { - return Yii::t('hipanel.finance.resource', '{0, plural, one{# core} other{# cores}}', - $this->getPrepaidQuantity()); - } - - return Yii::t('hipanel.finance.resource', '{0} MHz', Yii::$app->formatter->asInteger($this->getPrepaidQuantity())); - } - - public function getPrepaidQuantity() - { - if ($this->getCpuUnit() === self::UNIT_CORE) { - preg_match('/((\d+) cores?)$/i', $this->resource->part->partno, $matches); - } else { - preg_match('/((\d+) MHz)$/i', $this->resource->part->partno, $matches); - } - - return $matches[2] === null ? 0 : $matches[2]; - } - - private function getCpuUnit() - { - if (strpos($this->resource->part->partno, 'core') !== false) { - return self::UNIT_CORE; - } - - return self::UNIT_MHZ; - } -} diff --git a/src/models/decorators/server/HALoadBalancerResourceDecorator.php b/src/models/decorators/server/HALoadBalancerResourceDecorator.php deleted file mode 100644 index e78e0a0a..00000000 --- a/src/models/decorators/server/HALoadBalancerResourceDecorator.php +++ /dev/null @@ -1,30 +0,0 @@ - $this->getPrepaidQuantity()]); - } - - public function toUnit(): string - { - return 'items'; - } -} diff --git a/src/models/decorators/server/HddResourceDecorator.php b/src/models/decorators/server/HddResourceDecorator.php deleted file mode 100644 index 49baa935..00000000 --- a/src/models/decorators/server/HddResourceDecorator.php +++ /dev/null @@ -1,49 +0,0 @@ - $this->getPrepaidQuantity()]); // Gb - } - - public function getOverusePrice() - { - return 0.2; // TODO: move to config - } - - public function getPrepaidQuantity() - { - $part = $this->resource->part; - preg_match('/((\d{1,5}) GB)$/i', $part->partno, $matches); - - return (int)$matches[2]; - } - - public function displayUnit() - { - return Yii::t('hipanel', 'GB'); - } - - public function toUnit(): string - { - return 'gb'; - } -} diff --git a/src/models/decorators/server/IpResourceDecorator.php b/src/models/decorators/server/IpResourceDecorator.php deleted file mode 100644 index 4a0835be..00000000 --- a/src/models/decorators/server/IpResourceDecorator.php +++ /dev/null @@ -1,36 +0,0 @@ -resource->quantity; - } -} diff --git a/src/models/decorators/server/Isp5ResourceDecorator.php b/src/models/decorators/server/Isp5ResourceDecorator.php deleted file mode 100644 index d1a06443..00000000 --- a/src/models/decorators/server/Isp5ResourceDecorator.php +++ /dev/null @@ -1,21 +0,0 @@ -getPrepaidQuantity() > 0 ? $this->amountOptions()[1] : $this->amountOptions()[0]; - } - - public function prepaidAmountType() - { - return new OptionsInput($this->amountOptions()); - } - - private function amountOptions() - { - return [0 => Yii::t('hipanel', 'Disabled'), 1 => Yii::t('hipanel', 'Enabled')]; - } -} diff --git a/src/models/decorators/server/LoadBalancerResourceDecorator.php b/src/models/decorators/server/LoadBalancerResourceDecorator.php deleted file mode 100644 index b22c3cb9..00000000 --- a/src/models/decorators/server/LoadBalancerResourceDecorator.php +++ /dev/null @@ -1,30 +0,0 @@ - $this->getPrepaidQuantity()]); - } - - public function toUnit(): string - { - return 'items'; - } -} diff --git a/src/models/decorators/server/LocationResourceDecorator.php b/src/models/decorators/server/LocationResourceDecorator.php deleted file mode 100644 index 75cb7ae9..00000000 --- a/src/models/decorators/server/LocationResourceDecorator.php +++ /dev/null @@ -1,82 +0,0 @@ -amountOptions()); - } - - public function displayShortenLocations() - { - $data = $this->amountOptions(); - $result = []; - foreach ($data as $item) { - $result[] = substr($item, 0, strpos($item, ',')); - } - $result = array_unique($result, SORT_STRING); - - return rtrim(implode(', ', $result), ', '); - } - - private function amountOptions() - { - $data = Yii::$app->cache->getOrSet([__METHOD__, 'serversGetLocations'], function () { - return \hipanel\modules\server\models\Server::Perform('getLocations',[ - Tariff::TYPE_XEN => ['type' => Tariff::TYPE_XEN], - Tariff::TYPE_OPENVZ => ['type' => Tariff::TYPE_OPENVZ], - ], ['batch' => true]); - }, 3600); - - return $data[$this->resource->tariff->type]; - } -} diff --git a/src/models/decorators/server/MonthlyResourceDecorator.php b/src/models/decorators/server/MonthlyResourceDecorator.php deleted file mode 100644 index 0d019414..00000000 --- a/src/models/decorators/server/MonthlyResourceDecorator.php +++ /dev/null @@ -1,31 +0,0 @@ - - */ -class MonthlyResourceDecorator extends AbstractServerResourceDecorator -{ - public function displayTitle() - { - return Yii::t('hipanel:finance:tariff', 'Monthly fee'); - } - - public function displayValue() - { - return $this->displayOverusePrice(); - } -} diff --git a/src/models/decorators/server/PanelResourceDecorator.php b/src/models/decorators/server/PanelResourceDecorator.php deleted file mode 100644 index 887a18aa..00000000 --- a/src/models/decorators/server/PanelResourceDecorator.php +++ /dev/null @@ -1,56 +0,0 @@ - 'HiPanel']); // todo: add faq link - if ($this->resource->tariff->getResourceByType('isp5')->quantity > 0) { - $result .= ' / ' . Yii::t('hipanel:finance:tariff', 'ISP manager'); - } - - return $result; - } - - public function getPrepaidQuantity() - { - return 1; - } - - public function getOverusePrice() - { - return null; - } - - public function displayOverusePrice() - { - return null; - } -} diff --git a/src/models/decorators/server/PowerResourceDecorator.php b/src/models/decorators/server/PowerResourceDecorator.php deleted file mode 100644 index 579cc03d..00000000 --- a/src/models/decorators/server/PowerResourceDecorator.php +++ /dev/null @@ -1,46 +0,0 @@ - $this->resource->quantity]); - } - - public function displayUnit() - { - return Yii::t('hipanel.finance.units', 'W'); - } - - public function toUnit(): string - { - return 'W'; - } - - public function getOverusePrice() - { - return null; - } - - public function displayOverusePrice() - { - return null; - } -} diff --git a/src/models/decorators/server/RackUnitResourceDecorator.php b/src/models/decorators/server/RackUnitResourceDecorator.php deleted file mode 100644 index 54b180a7..00000000 --- a/src/models/decorators/server/RackUnitResourceDecorator.php +++ /dev/null @@ -1,21 +0,0 @@ -resource->part; - preg_match('/((\d{1,5}) MB)$/i', $part->partno, $matches); - - return $matches[2] / 1024; - } - - public function getOverusePrice() - { - return 4; // TODO: move to config - } - - public function displayUnit() - { - return Yii::t('hipanel', 'GB'); - } - - public function toUnit(): string - { - return 'gb'; - } - - public function displayPrepaidAmount() - { - return Yii::t('yii', '{nFormatted} GB', ['nFormatted' => $this->getPrepaidQuantity()]); - } -} diff --git a/src/models/decorators/server/ServerDUResourceDecorator.php b/src/models/decorators/server/ServerDUResourceDecorator.php deleted file mode 100644 index 04a3da9f..00000000 --- a/src/models/decorators/server/ServerDUResourceDecorator.php +++ /dev/null @@ -1,21 +0,0 @@ -resource->unit; - } - - public function displayValue() - { - return $this->resource->quantity; - } -} diff --git a/src/models/decorators/server/ServerSSDResourceDecorator.php b/src/models/decorators/server/ServerSSDResourceDecorator.php deleted file mode 100644 index 50d03e52..00000000 --- a/src/models/decorators/server/ServerSSDResourceDecorator.php +++ /dev/null @@ -1,21 +0,0 @@ - $this->getPrepaidQuantity()]); - } - - public function displayUnit() - { - return Yii::t('hipanel:finance:tariff', 'Gbit/s'); - } - - public function toUnit(): string - { - return 'Gbps'; - } - - public function getPrepaidQuantity() - { - return 1; - } - - public function getOverusePrice() - { - return null; - } - - public function displayOverusePrice() - { - return null; - } -} diff --git a/src/models/decorators/server/StorageDUResourceDecorator.php b/src/models/decorators/server/StorageDUResourceDecorator.php deleted file mode 100644 index d9d568fd..00000000 --- a/src/models/decorators/server/StorageDUResourceDecorator.php +++ /dev/null @@ -1,21 +0,0 @@ -getPrepaidQuantity(); - - // todo: uncomment after adding commerce pages -// if ($quantity == 1) { -// return Yii::t('hipanel:finance:tariff', 'Bronze'); -// } elseif ($quantity == 1.5) { -// return Yii::t('hipanel:finance:tariff', 'Silver'); -// } elseif ($quantity == 2) { -// return Yii::t('hipanel:finance:tariff', 'Gold'); -// } elseif ($quantity == 3) { -// return Yii::t('hipanel:finance:tariff', 'Platinum'); -// } - - return Yii::t('hipanel:finance:tariff', '{n, plural, one{# hour} other{# hours}}', ['n' => $quantity]); - } -} diff --git a/src/models/decorators/server/Traffic95ResourceDecorator.php b/src/models/decorators/server/Traffic95ResourceDecorator.php deleted file mode 100644 index 726be0fc..00000000 --- a/src/models/decorators/server/Traffic95ResourceDecorator.php +++ /dev/null @@ -1,36 +0,0 @@ -displayTitleWithDirection(Yii::t('hipanel.finance.resource', '95 percentile traffic')); - } - - public function displayValue() - { - return Yii::t('yii', '{nFormatted} Mbps', ['nFormatted' => $this->getPrepaidQuantity()]); - } - - public function displayUnit() - { - return Yii::t('hipanel', 'Mbps'); - } - - public function toUnit(): string - { - return 'Mbps'; - } -} diff --git a/src/models/decorators/server/TrafficResourceDecorator.php b/src/models/decorators/server/TrafficResourceDecorator.php deleted file mode 100644 index 69b33034..00000000 --- a/src/models/decorators/server/TrafficResourceDecorator.php +++ /dev/null @@ -1,36 +0,0 @@ -displayTitleWithDirection(Yii::t('hipanel.finance.resource', 'Traffic')); - } - - public function displayValue() - { - return Yii::t('yii', '{nFormatted} GB', ['nFormatted' => $this->getPrepaidQuantity()]); - } - - public function displayUnit() - { - return Yii::t('hipanel', 'GB'); - } - - public function toUnit(): string - { - return 'gb'; - } -} diff --git a/src/models/decorators/target/AbstractTargetResourceDecorator.php b/src/models/decorators/target/AbstractTargetResourceDecorator.php deleted file mode 100644 index ca890fc3..00000000 --- a/src/models/decorators/target/AbstractTargetResourceDecorator.php +++ /dev/null @@ -1,17 +0,0 @@ -resource->quantity; - } -} diff --git a/src/models/decorators/target/PrivateCloudBackupDuResourceDecorator.php b/src/models/decorators/target/PrivateCloudBackupDuResourceDecorator.php deleted file mode 100644 index 182c4e86..00000000 --- a/src/models/decorators/target/PrivateCloudBackupDuResourceDecorator.php +++ /dev/null @@ -1,22 +0,0 @@ - Date: Thu, 2 Jan 2025 12:30:09 +0200 Subject: [PATCH 15/25] HP-1751 overwrote ResourceHelper::convertAmount() --- src/helpers/ResourceHelper.php | 55 ++-------------------------------- 1 file changed, 3 insertions(+), 52 deletions(-) diff --git a/src/helpers/ResourceHelper.php b/src/helpers/ResourceHelper.php index 80473005..85bbed08 100644 --- a/src/helpers/ResourceHelper.php +++ b/src/helpers/ResourceHelper.php @@ -4,13 +4,11 @@ use hipanel\helpers\ArrayHelper; use hipanel\modules\finance\models\Consumption; -use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface; use hipanel\modules\finance\models\proxy\Resource; use hipanel\modules\server\models\Hub; use hipanel\modules\server\models\Server; +use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; use hiqdev\hiart\ActiveRecord; -use hiqdev\php\units\Quantity; -use hiqdev\php\units\Unit; use hiqdev\yii\compat\yii; use Yii as BaseYii; use yii\db\ActiveRecordInterface; @@ -19,56 +17,9 @@ class ResourceHelper { - public static function convertAmount(ResourceDecoratorInterface $decorator) + private static function convertAmount(ResourceDecoratorInterface $decorator) { - $configurator = yii::getContainer()->get(ConsumptionConfigurator::class); - $amount = $decorator->getPrepaidQuantity(); - $targetTypes = $configurator->getAllPossibleColumns(); - unset( - $targetTypes[array_search('referral', $targetTypes, true)], - $targetTypes[array_search('ip_num', $targetTypes, true)], - $targetTypes[array_search('server_files', $targetTypes, true)], - $targetTypes[array_search('power', $targetTypes, true)], - $targetTypes[array_search('cloud_ip_regular', $targetTypes, true)], - $targetTypes[array_search('cloud_ip_anycast', $targetTypes, true)], - $targetTypes[array_search('cloud_ip_public', $targetTypes, true)], - $targetTypes[array_search('cloud_ip_regular_max', $targetTypes, true)], - $targetTypes[array_search('cloud_ip_anycast_max', $targetTypes, true)], - $targetTypes[array_search('cloud_ip_public_max', $targetTypes, true)], - ); - $convertibleTypes = array_merge([ - 'backup_du', - 'cdn_cache', - 'cdn_cache95', - 'cdn_traf', - 'cdn_traf_plain', - 'cdn_traf_ssl', - 'cdn_traf_max', - 'hdd', - 'ram', - 'speed', - 'server_du', - 'server_sata', - 'server_ssd', - 'server_traf95', - 'server_traf95_in', - 'server_traf95_max', - 'server_traf', - 'server_traf_in', - 'server_traf_max', - 'vps_traf', - 'vps_traf_in', - 'vps_traf_max', - 'storage_du', - 'storage_du95', - ], $targetTypes); - if (in_array($decorator->resource->type, $convertibleTypes, true)) { - $from = Unit::create($decorator->resource->unit)->getName(); - $to = Unit::create($decorator->toUnit()); - $amount = sprintf('%.3F', Quantity::create($from, $amount)->convert($to)->getQuantity()); - } - - return $amount; + return \hiqdev\billing\registry\helper\ResourceHelper::convertAmount($decorator); } public static function prepareDetailView(array $resources): array From 07c37beafda6bfb3184215574b231bb81f22b443 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 2 Jan 2025 14:48:10 +0200 Subject: [PATCH 16/25] HP-1751 tiny --- src/helpers/ResourceHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/helpers/ResourceHelper.php b/src/helpers/ResourceHelper.php index 85bbed08..3e02f93c 100644 --- a/src/helpers/ResourceHelper.php +++ b/src/helpers/ResourceHelper.php @@ -61,6 +61,7 @@ public static function normalizeAmount(Resource $resource): string public static function calculateTotal(array $resources): array { $totals = []; + // TODO: overwrite to BillingRegistry $totalsOverMax = [ 'cdn_cache', 'cdn_cache95', From 1ff546286d1d9813e344f6448c433703e2bbe2f6 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 2 Jan 2025 14:58:44 +0200 Subject: [PATCH 17/25] HP-1751 Implemented BillingRegistry::getAggregate() method --- src/helpers/ResourceHelper.php | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/helpers/ResourceHelper.php b/src/helpers/ResourceHelper.php index 3e02f93c..ce14b998 100644 --- a/src/helpers/ResourceHelper.php +++ b/src/helpers/ResourceHelper.php @@ -7,7 +7,9 @@ use hipanel\modules\finance\models\proxy\Resource; use hipanel\modules\server\models\Hub; use hipanel\modules\server\models\Server; +use hiqdev\billing\registry\product\Aggregate; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; +use hiqdev\billing\registry\TariffConfiguration; use hiqdev\hiart\ActiveRecord; use hiqdev\yii\compat\yii; use Yii as BaseYii; @@ -60,7 +62,8 @@ public static function normalizeAmount(Resource $resource): string public static function calculateTotal(array $resources): array { - $totals = []; + $billingRegistry = TariffConfiguration::buildRegistry(); + // TODO: overwrite to BillingRegistry $totalsOverMax = [ 'cdn_cache', @@ -77,22 +80,32 @@ public static function calculateTotal(array $resources): array 'server_ssd', 'server_files', ]; + $totals = []; foreach (self::filterByAvailableTypes($resources) as $resource) { $decorator = $resource->buildResourceModel()->decorator(); - if (in_array($resource->type, $totalsOverMax, true)) { - $totals[$resource->type]['amount'] = max(($totals[$resource->type]['amount'] ?? 0), - self::convertAmount($decorator)); - } else { - $totals[$resource->type]['amount'] = bcadd($totals[$resource->type]['amount'] ?? 0, - self::convertAmount($decorator), - 3); - } + $type = $resource->type; + $aggregate = $billingRegistry->getAggregate($type); + + $totals[$type]['amount'] = self::calculateAmount( + $aggregate, + $totals[$type]['amount'] ?? 0, + $decorator, + ); $totals[$resource->type]['unit'] = $decorator->displayUnit(); } return $totals; } + private static function calculateAmount(Aggregate $aggregate, $amount, ResourceDecoratorInterface $decorator) + { + if ($aggregate->isMax()) { + return max($amount, self::convertAmount($decorator)); + } else { + return bcadd($amount, self::convertAmount($decorator), 3); + } + } + public static function filterByAvailableTypes(array $resources): array { $configurator = yii::getContainer()->get(ConsumptionConfigurator::class); From 5dc957ebeb6741daa48680c781fdaa587c65e9ba Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 2 Jan 2025 15:03:03 +0200 Subject: [PATCH 18/25] HP-1751 removed old code --- src/helpers/ResourceHelper.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/helpers/ResourceHelper.php b/src/helpers/ResourceHelper.php index ce14b998..edb21d20 100644 --- a/src/helpers/ResourceHelper.php +++ b/src/helpers/ResourceHelper.php @@ -64,22 +64,6 @@ public static function calculateTotal(array $resources): array { $billingRegistry = TariffConfiguration::buildRegistry(); - // TODO: overwrite to BillingRegistry - $totalsOverMax = [ - 'cdn_cache', - 'cdn_cache95', - 'cdn_traf95', - 'cdn_traf95_max', - 'server_traf95', - 'server_traf95_in', - 'server_traf95_max', - 'storage_du', - 'storage_du95', - 'server_du', - 'server_sata', - 'server_ssd', - 'server_files', - ]; $totals = []; foreach (self::filterByAvailableTypes($resources) as $resource) { $decorator = $resource->buildResourceModel()->decorator(); From bfcdefc2b7eb5479ff928143b415b74abd70443a Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Thu, 2 Jan 2025 15:05:52 +0200 Subject: [PATCH 19/25] HP-1751 add overuse to all types --- src/helpers/ResourceHelper.php | 13 ++++++++++++- src/models/decorators/ResourceDecoratorFactory.php | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/helpers/ResourceHelper.php b/src/helpers/ResourceHelper.php index edb21d20..a7ddadd8 100644 --- a/src/helpers/ResourceHelper.php +++ b/src/helpers/ResourceHelper.php @@ -8,6 +8,7 @@ use hipanel\modules\server\models\Hub; use hipanel\modules\server\models\Server; use hiqdev\billing\registry\product\Aggregate; +use hiqdev\billing\registry\product\GType; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; use hiqdev\billing\registry\TariffConfiguration; use hiqdev\hiart\ActiveRecord; @@ -68,7 +69,7 @@ public static function calculateTotal(array $resources): array foreach (self::filterByAvailableTypes($resources) as $resource) { $decorator = $resource->buildResourceModel()->decorator(); $type = $resource->type; - $aggregate = $billingRegistry->getAggregate($type); + $aggregate = $billingRegistry->getAggregate(self::addOveruseToTypeIfNeeded($type)); $totals[$type]['amount'] = self::calculateAmount( $aggregate, @@ -81,6 +82,16 @@ public static function calculateTotal(array $resources): array return $totals; } + public static function addOveruseToTypeIfNeeded(string $type): string + { + // TODO: I can't add overuse to all types. For example: + if (str_starts_with($type, GType::overuse->name()) === false) { + return GType::overuse->name() . ',' . $type; + } + + return $type; + } + private static function calculateAmount(Aggregate $aggregate, $amount, ResourceDecoratorInterface $decorator) { if ($aggregate->isMax()) { diff --git a/src/models/decorators/ResourceDecoratorFactory.php b/src/models/decorators/ResourceDecoratorFactory.php index 74425849..fa600724 100644 --- a/src/models/decorators/ResourceDecoratorFactory.php +++ b/src/models/decorators/ResourceDecoratorFactory.php @@ -2,9 +2,11 @@ namespace hipanel\modules\finance\models\decorators; +use hipanel\modules\finance\helpers\ResourceHelper; use hipanel\modules\finance\models\Resource; use hipanel\modules\finance\models\stubs\AbstractResourceStub; use hiqdev\billing\registry\behavior\ResourceDecoratorBehavior; +use hiqdev\billing\registry\product\GType; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorData; use hiqdev\billing\registry\ResourceDecorator\ResourceDecoratorInterface; use hiqdev\billing\registry\TariffConfiguration; @@ -26,7 +28,10 @@ public static function createFromResource($resource): ResourceDecoratorInterface try { /** @var ResourceDecoratorBehavior $behavior */ - $behavior = $registry->getBehavior($type, ResourceDecoratorBehavior::class); + $behavior = $registry->getBehavior( + ResourceHelper::addOveruseToTypeIfNeeded($type), + ResourceDecoratorBehavior::class, + ); return $behavior->createDecorator(new ResourceDecoratorData( $resource->quantity, From 1658f369b403aa224664fa672c60876ff9e28300 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 7 Jan 2025 10:22:54 +0200 Subject: [PATCH 20/25] HP-1751 tiny --- src/helpers/ConsumptionConfigurator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index ad9bc54a..9040e46f 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -179,8 +179,8 @@ public function getConfigurations(): array 'label' => Yii::t('hipanel:finance', 'Tariff resources'), 'columns' => [ PriceType::server_traf95_max->name(), - //'server_traf95', // TODO: Didn't find it in the database for tariff. Commented for now because it is causing fatal error - //'server_traf95_in', // TODO: Didn't find it in the database for tariff. Commented for now because it is causing fatal error + 'server_traf95', + 'server_traf95_in', ], 'groups' => [], 'model' => $this->createObject(Target::class), From d71bbd340dc34d2f5d197ac43df53c4afbe7e362 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 7 Jan 2025 13:31:54 +0200 Subject: [PATCH 21/25] HP-1751 optimized Consumption page --- src/helpers/ConsumptionConfigurator.php | 169 ++++++++++-------- src/helpers/ResourceHelper.php | 2 +- .../decorators/ResourceDecoratorFactory.php | 6 +- 3 files changed, 97 insertions(+), 80 deletions(-) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index 9040e46f..b0e60417 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -1,6 +1,4 @@ -getConfigurationByClass($class)['columns']; } - private function getGroups(string $class): array + /** + * @param string $class + * @return array{label: string, columns: array, group: array, model: ActiveRecordInterface, resourceModel: ActiveRecordInterface} + */ + public function getConfigurationByClass(string $class): array { - $groups = []; - $columns = $this->getColumns($class); - foreach ($this->getConfigurationByClass($class)['groups'] as $group) { - $groups[] = $group; - foreach ($group as $item) { - $columns = array_diff($columns, [$item]); - } + $fallback = [ + 'label' => ['hipanel:finance', $class], + 'columns' => [], + 'groups' => [], + 'model' => $this->createObject(Target::class), + 'resourceModel' => $this->createObject(TargetResource::class), + ]; + + return $this->getConfigurations()[$class] ?? $fallback; + } + + private function createObject(string $className, array $params = []): object + { + return Yii::createObject(array_merge(['class' => $className], $params)); + } + + public function getConfigurations(): array + { + if ($this->configurations === null) { + $this->configurations = $this->buildConfigurations(); } - foreach ($columns as $column) { - $groups[] = [$column]; + + return $this->configurations; + } + + private function buildConfigurations(): array + { + $configurations = []; + /** @var ConsumptionConfigurationBehaviour $behavior */ + foreach ($this->billingRegistry->getBehaviors(ConsumptionConfigurationBehaviour::class) as $behavior) { + $tariffType = $behavior->getTariffType(); + + $configurations[$tariffType->name()] = [ + 'label' => $behavior->getLabel(), + 'columns' => $behavior->columns, + 'groups' => $behavior->groups, + 'model' => $this->createObject($behavior->getModel() ?? Target::class), + 'resourceModel' => $this->createObject($behavior->getResourceModel() ?? TargetResource::class), + ]; } - return $groups; + // Can't be added to Billing Registry, so left as it is + $configurations['tariff'] = [ + 'label' => Yii::t('hipanel:finance', 'Tariff resources'), + 'columns' => [ + PriceType::server_traf95_max->name(), + 'server_traf95', + 'server_traf95_in', + ], + 'groups' => [], + 'model' => $this->createObject(Target::class), + 'resourceModel' => $this->createObject(TargetResource::class), + ]; + + return $configurations; } public function getGroupsWithLabels(string $class): array @@ -58,11 +104,29 @@ public function getGroupsWithLabels(string $class): array return $groups; } + private function getGroups(string $class): array + { + $groups = []; + $columns = $this->getColumns($class); + foreach ($this->getConfigurationByClass($class)['groups'] as $group) { + $groups[] = $group; + foreach ($group as $item) { + $columns = array_diff($columns, [$item]); + } + } + foreach ($columns as $column) { + $groups[] = [$column]; + } + + return $groups; + } + public function getColumnsWithLabels(string $class): array { $result = []; foreach ($this->getColumns($class) as $column) { $decorator = $this->getDecorator($class, $column); + $result[$column] = $decorator->displayTitle(); } @@ -92,22 +156,27 @@ public function getAllPossibleColumns(): array public function getAllPossibleColumnsWithLabels(): array { - $result = []; - foreach ($this->getConfigurations() as $class => $configuration) { - $columns = $configuration['columns']; - foreach ($columns as $column) { - $decorator = $this->getDecorator($class, $column); - $result[$column] = $decorator->displayTitle(); + if ($this->allPossibleColumnsWithLabels === null) { + $this->allPossibleColumnsWithLabels = []; + foreach ($this->getConfigurations() as $class => $configuration) { + $columns = $configuration['columns']; + foreach ($columns as $column) { + $decorator = $this->getDecorator($class, $column); + + $this->allPossibleColumnsWithLabels[$column] = $decorator->displayTitle(); + } } } - return $result; + return $this->allPossibleColumnsWithLabels; } private function getDecorator(string $class, string $type): ResourceDecoratorInterface { $config = $this->getConfigurationByClass($class); + $config['resourceModel']->type = $type; + /** @var ResourceDecoratorInterface $decorator */ $decorator = $config['resourceModel']->decorator(); @@ -117,6 +186,7 @@ private function getDecorator(string $class, string $type): ResourceDecoratorInt public function buildResourceModel(ActiveRecordInterface $resource) { $config = $this->getConfigurationByClass($resource->class); + $config['resourceModel']->setAttributes([ 'type' => $resource->type, 'unit' => $resource->unit, @@ -129,6 +199,7 @@ public function buildResourceModel(ActiveRecordInterface $resource) public function fillTheOriginalModel(Consumption $consumption) { $configuration = $this->getConfigurationByClass($consumption->class); + $configuration['model']->setAttributes($consumption->mainObject, false); return $configuration['model']; @@ -140,58 +211,4 @@ public function getFirstAvailableClass(): string return array_key_first($configurations); } - - /** - * @param string $class - * @return array{label: string, columns: array, group: array, model: ActiveRecordInterface, resourceModel: ActiveRecordInterface} - */ - public function getConfigurationByClass(string $class): array - { - $fallback = [ - 'label' => ['hipanel:finance', $class], - 'columns' => [], - 'groups' => [], - 'model' => $this->createObject(Target::class), - 'resourceModel' => $this->createObject(TargetResource::class), - ]; - - return $this->getConfigurations()[$class] ?? $fallback; - } - - public function getConfigurations(): array - { - $configurations = []; - /** @var ConsumptionConfigurationBehaviour $behavior */ - foreach ($this->billingRegistry->getBehaviors(ConsumptionConfigurationBehaviour::class) as $behavior) { - $tariffType = $behavior->getTariffType(); - - $configurations[$tariffType->name()] = [ - 'label' => $behavior->getLabel(), - 'columns' => $behavior->columns, - 'groups' => $behavior->groups, - 'model' => $this->createObject($behavior->getModel() ?? Target::class), - 'resourceModel' => $this->createObject($behavior->getResourceModel() ?? TargetResource::class), - ]; - } - - // Can't be added to Billing Registry, so left as it is - $configurations['tariff'] = [ - 'label' => Yii::t('hipanel:finance', 'Tariff resources'), - 'columns' => [ - PriceType::server_traf95_max->name(), - 'server_traf95', - 'server_traf95_in', - ], - 'groups' => [], - 'model' => $this->createObject(Target::class), - 'resourceModel' => $this->createObject(TargetResource::class), - ]; - - return $configurations; - } - - private function createObject(string $className, array $params = []): object - { - return Yii::createObject(array_merge(['class' => $className], $params)); - } } diff --git a/src/helpers/ResourceHelper.php b/src/helpers/ResourceHelper.php index a7ddadd8..cb5a78c9 100644 --- a/src/helpers/ResourceHelper.php +++ b/src/helpers/ResourceHelper.php @@ -1,4 +1,4 @@ -model_type ?? $resource->type; - $registry = TariffConfiguration::buildRegistry(); + $registry = Yii::createObject(BillingRegistryInterface::class); try { /** @var ResourceDecoratorBehavior $behavior */ From c882709e0c67119af717cbf0811eb80170ca53d2 Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 7 Jan 2025 16:58:38 +0200 Subject: [PATCH 22/25] HP-1751 created ConsumptionConfiguratorData for easier understanding with which data we are working --- src/helpers/ConsumptionConfigurator.php | 94 ++++++++++--------- src/helpers/ConsumptionConfiguratorData.php | 28 ++++++ src/models/Target.php | 7 +- .../decorators/ResourceDecoratorFactory.php | 21 +++-- 4 files changed, 97 insertions(+), 53 deletions(-) create mode 100644 src/helpers/ConsumptionConfiguratorData.php diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index b0e60417..e265fef2 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -2,6 +2,7 @@ namespace hipanel\modules\finance\helpers; +use hipanel\base\Model; use hipanel\helpers\ArrayHelper; use hipanel\modules\finance\models\Consumption; use hipanel\modules\finance\models\Target; @@ -15,6 +16,7 @@ final class ConsumptionConfigurator { + /** @var ConsumptionConfiguratorData[]|null */ private ?array $configurations = null; private ?array $allPossibleColumnsWithLabels = null; @@ -25,27 +27,26 @@ public function __construct(private readonly BillingRegistryInterface $billingRe public function getColumns(string $class): array { - return $this->getConfigurationByClass($class)['columns']; + return $this->getConfigurationByClass($class)->columns; } /** - * @param string $class - * @return array{label: string, columns: array, group: array, model: ActiveRecordInterface, resourceModel: ActiveRecordInterface} + * @param string $class - for example: load_balancer */ - public function getConfigurationByClass(string $class): array + public function getConfigurationByClass(string $class): ConsumptionConfiguratorData { - $fallback = [ - 'label' => ['hipanel:finance', $class], - 'columns' => [], - 'groups' => [], - 'model' => $this->createObject(Target::class), - 'resourceModel' => $this->createObject(TargetResource::class), - ]; + $fallback = new ConsumptionConfiguratorData( + $class, + [], + [], + $this->createObject(Target::class), + $this->createObject(TargetResource::class), + ); return $this->getConfigurations()[$class] ?? $fallback; } - private function createObject(string $className, array $params = []): object + private function createObject(string $className, array $params = []): Model { return Yii::createObject(array_merge(['class' => $className], $params)); } @@ -59,6 +60,9 @@ public function getConfigurations(): array return $this->configurations; } + /** + * @return ConsumptionConfiguratorData[] + */ private function buildConfigurations(): array { $configurations = []; @@ -66,27 +70,27 @@ private function buildConfigurations(): array foreach ($this->billingRegistry->getBehaviors(ConsumptionConfigurationBehaviour::class) as $behavior) { $tariffType = $behavior->getTariffType(); - $configurations[$tariffType->name()] = [ - 'label' => $behavior->getLabel(), - 'columns' => $behavior->columns, - 'groups' => $behavior->groups, - 'model' => $this->createObject($behavior->getModel() ?? Target::class), - 'resourceModel' => $this->createObject($behavior->getResourceModel() ?? TargetResource::class), - ]; + $configurations[$tariffType->name()] = new ConsumptionConfiguratorData( + $behavior->getLabel(), + $behavior->columns, + $behavior->groups, + $this->createObject($behavior->getModel() ?? Target::class), + $this->createObject($behavior->getResourceModel() ?? TargetResource::class), + ); } // Can't be added to Billing Registry, so left as it is - $configurations['tariff'] = [ - 'label' => Yii::t('hipanel:finance', 'Tariff resources'), - 'columns' => [ + $configurations['tariff'] = new ConsumptionConfiguratorData( + 'Tariff resources', + [ PriceType::server_traf95_max->name(), 'server_traf95', 'server_traf95_in', ], - 'groups' => [], - 'model' => $this->createObject(Target::class), - 'resourceModel' => $this->createObject(TargetResource::class), - ]; + [], + $this->createObject(Target::class), + $this->createObject(TargetResource::class), + ); return $configurations; } @@ -108,7 +112,7 @@ private function getGroups(string $class): array { $groups = []; $columns = $this->getColumns($class); - foreach ($this->getConfigurationByClass($class)['groups'] as $group) { + foreach ($this->getConfigurationByClass($class)->groups as $group) { $groups[] = $group; foreach ($group as $item) { $columns = array_diff($columns, [$item]); @@ -135,20 +139,23 @@ public function getColumnsWithLabels(string $class): array public function getClassesDropDownOptions(): array { - return array_filter(ArrayHelper::getColumn($this->getConfigurations(), static function (array $config): ?string { - if (isset($config['columns']) && !empty($config['columns'])) { - return $config['label']; - } + return array_filter(ArrayHelper::getColumn( + $this->getConfigurations(), + static function (ConsumptionConfiguratorData $config): ?string { + if (!empty($config->columns)) { + return $config->getLabel(); + } - return null; - })); + return null; + } + )); } public function getAllPossibleColumns(): array { $columns = []; foreach ($this->getConfigurations() as $configuration) { - $columns = array_merge($configuration['columns'], $columns); + $columns = array_merge($configuration->columns, $columns); } return array_unique($columns); @@ -159,7 +166,8 @@ public function getAllPossibleColumnsWithLabels(): array if ($this->allPossibleColumnsWithLabels === null) { $this->allPossibleColumnsWithLabels = []; foreach ($this->getConfigurations() as $class => $configuration) { - $columns = $configuration['columns']; + $columns = $configuration->columns; + foreach ($columns as $column) { $decorator = $this->getDecorator($class, $column); @@ -175,34 +183,34 @@ private function getDecorator(string $class, string $type): ResourceDecoratorInt { $config = $this->getConfigurationByClass($class); - $config['resourceModel']->type = $type; + $config->resourceModel->type = $type; /** @var ResourceDecoratorInterface $decorator */ - $decorator = $config['resourceModel']->decorator(); + $decorator = $config->resourceModel->decorator(); return $decorator; } - public function buildResourceModel(ActiveRecordInterface $resource) + public function buildResourceModel(ActiveRecordInterface $resource): object { $config = $this->getConfigurationByClass($resource->class); - $config['resourceModel']->setAttributes([ + $config->resourceModel->setAttributes([ 'type' => $resource->type, 'unit' => $resource->unit, 'quantity' => $resource->getAmount(), ]); - return $config['resourceModel']; + return $config->resourceModel; } - public function fillTheOriginalModel(Consumption $consumption) + public function fillTheOriginalModel(Consumption $consumption): object { $configuration = $this->getConfigurationByClass($consumption->class); - $configuration['model']->setAttributes($consumption->mainObject, false); + $configuration->model->setAttributes($consumption->mainObject, false); - return $configuration['model']; + return $configuration->model; } public function getFirstAvailableClass(): string diff --git a/src/helpers/ConsumptionConfiguratorData.php b/src/helpers/ConsumptionConfiguratorData.php new file mode 100644 index 00000000..fbe7fd75 --- /dev/null +++ b/src/helpers/ConsumptionConfiguratorData.php @@ -0,0 +1,28 @@ +label); + } +} diff --git a/src/models/Target.php b/src/models/Target.php index 09ab3484..7f8dcb65 100644 --- a/src/models/Target.php +++ b/src/models/Target.php @@ -6,6 +6,7 @@ use hipanel\base\ModelTrait; use hipanel\behaviors\TaggableBehavior; use hipanel\models\TaggableInterface; +use hipanel\modules\finance\helpers\ConsumptionConfiguratorData; use hipanel\modules\finance\helpers\ConsumptionConfigurator; use hipanel\modules\finance\models\query\TargetQuery; use Yii; @@ -41,13 +42,15 @@ public function rules() public function getTypes(): array { $configurator = Yii::$container->get(ConsumptionConfigurator::class); + $configurations = array_filter( $configurator->getConfigurations(), - static fn(array $configuration): bool => $configuration['model'] instanceof self + static fn(ConsumptionConfiguratorData $configuration): bool => $configuration->model instanceof self ); + $types = []; foreach ($configurations as $type => $configuration) { - $types[$type] = $configuration['label']; + $types[$type] = $configuration->getLabel(); } return $types; diff --git a/src/models/decorators/ResourceDecoratorFactory.php b/src/models/decorators/ResourceDecoratorFactory.php index 9955315c..2645aea5 100644 --- a/src/models/decorators/ResourceDecoratorFactory.php +++ b/src/models/decorators/ResourceDecoratorFactory.php @@ -33,16 +33,21 @@ public static function createFromResource($resource): ResourceDecoratorInterface ResourceDecoratorBehavior::class, ); - return $behavior->createDecorator(new ResourceDecoratorData( - $resource->quantity, - $resource->price, - $resource->unit, - $resource->currency, - $resource->type, - $resource->part->partno, - )); + return $behavior->createDecorator(self::createResourceDecoratorData($resource)); } catch (BehaviorNotFoundException) { throw new InvalidConfigException('No representative decoration class found for type "' . $type . '"'); } } + + private static function createResourceDecoratorData(Resource|AbstractResourceStub $resource): ResourceDecoratorData + { + return new ResourceDecoratorData( + $resource->quantity, + $resource->price, + $resource->unit, + $resource->currency, + $resource->type, + $resource->part->partno, + ); + } } From ba3b4625ae21b041947af43ca53574cddc779e9b Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 7 Jan 2025 17:43:51 +0200 Subject: [PATCH 23/25] HP-1751 tiny --- src/models/decorators/ResourceDecoratorFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/models/decorators/ResourceDecoratorFactory.php b/src/models/decorators/ResourceDecoratorFactory.php index 2645aea5..f858de5a 100644 --- a/src/models/decorators/ResourceDecoratorFactory.php +++ b/src/models/decorators/ResourceDecoratorFactory.php @@ -20,7 +20,7 @@ class ResourceDecoratorFactory * @return ResourceDecoratorInterface * @throws InvalidConfigException */ - public static function createFromResource($resource): ResourceDecoratorInterface + public static function createFromResource(Resource|AbstractResourceStub $resource): ResourceDecoratorInterface { $type = $resource->model_type ?? $resource->type; From 0582250a7369712c9179bbe981ab1f81b9f2182c Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 7 Jan 2025 18:15:16 +0200 Subject: [PATCH 24/25] HP-1751 increasing speed of finance/consumption page --- src/helpers/ConsumptionConfigurator.php | 53 +++++++++++++++++-------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/helpers/ConsumptionConfigurator.php b/src/helpers/ConsumptionConfigurator.php index e265fef2..f9ec4c42 100644 --- a/src/helpers/ConsumptionConfigurator.php +++ b/src/helpers/ConsumptionConfigurator.php @@ -19,7 +19,7 @@ final class ConsumptionConfigurator /** @var ConsumptionConfiguratorData[]|null */ private ?array $configurations = null; - private ?array $allPossibleColumnsWithLabels = null; + private ?array $columnsWithLabelsGroupedByClass = null; public function __construct(private readonly BillingRegistryInterface $billingRegistry) { @@ -125,18 +125,43 @@ private function getGroups(string $class): array return $groups; } - public function getColumnsWithLabels(string $class): array + public function getColumnsWithLabels(string $searchClass): array { $result = []; - foreach ($this->getColumns($class) as $column) { - $decorator = $this->getDecorator($class, $column); - - $result[$column] = $decorator->displayTitle(); + foreach ($this->getCachedColumnsWithLabelsGroupedByClass() as $class => $columns) { + if ($class === $searchClass) { + foreach ($columns as $column => $label) { + $result[$column] = $label; + } + } } return $result; } + /** + * Please use this method to avoid call heavy getDecorator() method only for retrieve title + * + * @return array + */ + private function getCachedColumnsWithLabelsGroupedByClass(): array + { + if ($this->columnsWithLabelsGroupedByClass === null) { + foreach ($this->getConfigurations() as $class => $configuration) { + $columns = $configuration->columns; + + $this->columnsWithLabelsGroupedByClass[$class] = []; + foreach ($columns as $column) { + $decorator = $this->getDecorator($class, $column); + + $this->columnsWithLabelsGroupedByClass[$class][$column] = $decorator->displayTitle(); + } + } + } + + return $this->columnsWithLabelsGroupedByClass; + } + public function getClassesDropDownOptions(): array { return array_filter(ArrayHelper::getColumn( @@ -163,20 +188,14 @@ public function getAllPossibleColumns(): array public function getAllPossibleColumnsWithLabels(): array { - if ($this->allPossibleColumnsWithLabels === null) { - $this->allPossibleColumnsWithLabels = []; - foreach ($this->getConfigurations() as $class => $configuration) { - $columns = $configuration->columns; - - foreach ($columns as $column) { - $decorator = $this->getDecorator($class, $column); - - $this->allPossibleColumnsWithLabels[$column] = $decorator->displayTitle(); - } + $allPossibleColumnsWithLabels = []; + foreach ($this->getCachedColumnsWithLabelsGroupedByClass() as $class => $columns) { + foreach ($columns as $column => $label) { + $allPossibleColumnsWithLabels[$column] = $label; } } - return $this->allPossibleColumnsWithLabels; + return $allPossibleColumnsWithLabels; } private function getDecorator(string $class, string $type): ResourceDecoratorInterface From 14678741f99b31df20fa1e028bd38a81b6b6381d Mon Sep 17 00:00:00 2001 From: Vadym Hrechukha Date: Tue, 7 Jan 2025 21:51:06 +0200 Subject: [PATCH 25/25] HP-1751 increasing speed of finance/consumption page --- config/web.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/web.php b/config/web.php index 6d89e73e..83014368 100644 --- a/config/web.php +++ b/config/web.php @@ -196,7 +196,7 @@ ); }, \Money\MoneyParser::class => \Money\Parser\DecimalMoneyParser::class, -// \hipanel\modules\finance\helpers\ConsumptionConfigurator::class => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class, + \hipanel\modules\finance\helpers\ConsumptionConfigurator::class => \hipanel\modules\finance\helpers\ConsumptionConfigurator::class, \hiqdev\php\billing\product\BillingRegistryInterface::class => static function () { return \hiqdev\billing\registry\TariffConfiguration::buildRegistry(); },