1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Finance module for HiPanel |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/hipanel-module-finance |
6
|
|
|
* @package hipanel-module-finance |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\helpers; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\models\CertificatePrice; |
14
|
|
|
use hipanel\modules\finance\models\DomainServicePrice; |
15
|
|
|
use hipanel\modules\finance\models\DomainZonePrice; |
16
|
|
|
use hipanel\modules\finance\models\FakeGroupingSale; |
17
|
|
|
use hipanel\modules\finance\models\FakeSale; |
18
|
|
|
use hipanel\modules\finance\models\FakeSharedSale; |
19
|
|
|
use hipanel\modules\finance\models\Plan; |
20
|
|
|
use hipanel\modules\finance\models\Price; |
21
|
|
|
use hipanel\modules\finance\models\Sale; |
22
|
|
|
use Yii; |
23
|
|
|
use yii\helpers\ArrayHelper; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class PlanInternalsGrouper can be used to group prices inside $plan depending on |
27
|
|
|
* different factors. |
28
|
|
|
* |
29
|
|
|
* @author Dmytro Naumenko <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class PlanInternalsGrouper |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var Plan |
35
|
|
|
*/ |
36
|
|
|
private $plan; |
37
|
|
|
|
38
|
|
|
public function __construct(Plan $plan) |
39
|
|
|
{ |
40
|
|
|
$this->plan = $plan; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Should be used to group prices of [[Plan]] with the following types: |
45
|
|
|
* - server |
46
|
|
|
* - sVDS |
47
|
|
|
* - oVDS |
48
|
|
|
* - certificate. |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function group() |
52
|
|
|
{ |
53
|
|
|
switch ($this->plan->type) { |
54
|
|
|
case Plan::TYPE_CERTIFICATE: |
55
|
|
|
return $this->groupCertificatePrices(); |
56
|
|
|
case Plan::TYPE_HARDWARE: |
57
|
|
|
return $this->groupHardwarePrices(); |
58
|
|
|
case Plan::TYPE_DOMAIN: |
59
|
|
|
$byType = static function (array $servicePrices) { |
60
|
|
|
return ArrayHelper::index($servicePrices, 'type', static function ($servicePrice) { |
|
|
|
|
61
|
|
|
if (strpos($servicePrice->type, 'premium_dns') !== false) { |
62
|
|
|
return 'premium_dns'; |
63
|
|
|
} |
64
|
|
|
if (strpos($servicePrice->type, 'whois_protect') !== false) { |
65
|
|
|
return 'whois_protect'; |
66
|
|
|
} |
67
|
|
|
}); |
68
|
|
|
}; |
69
|
|
|
[$zonePrices, $servicePrices] = $this->groupDomainPrices(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return [$zonePrices, $byType($servicePrices)]; |
72
|
|
|
default: |
73
|
|
|
return $this->groupServerPrices(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array of two elements: |
79
|
|
|
* 0: sales, grouped by sold object |
80
|
|
|
* 1: prices, grouped by sold object |
81
|
|
|
*/ |
82
|
|
|
private function groupServerPrices() |
83
|
|
|
{ |
84
|
|
|
$model = $this->plan; |
85
|
|
|
/** @var Sale[] $salesByObject */ |
86
|
|
|
$salesByObject = []; |
87
|
|
|
/** @var Price[][] $pricesByMainObject */ |
88
|
|
|
$pricesByMainObject = []; |
89
|
|
|
|
90
|
|
|
foreach ($model->prices as $price) { |
91
|
|
|
$pricesByMainObject[$price->main_object_id ?? $price->object_id ?? 0][$price->id] = $price; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (isset($pricesByMainObject[0])) { |
95
|
|
|
$salesByObject[0] = new FakeSharedSale([ |
96
|
|
|
'object' => Yii::t('hipanel.finance.price', 'For all sold objects'), |
97
|
|
|
'tariff_id' => $model->id, |
98
|
|
|
'tariff_type' => $model->type, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
if (isset($pricesByMainObject[$model->id])) { |
102
|
|
|
$salesByObject[$model->id] = new FakeGroupingSale([ |
103
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Grouping prices'), |
104
|
|
|
'object_id' => $model->id, |
105
|
|
|
'tariff_id' => $model->id, |
106
|
|
|
'tariff_type' => $model->type, |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
foreach ($model->sales as $sale) { |
110
|
|
|
$salesByObject[$sale->object_id] = $sale; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
foreach ($pricesByMainObject as $id => $prices) { |
114
|
|
|
if (isset($salesByObject[$id])) { |
115
|
|
|
continue; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($prices as $price) { |
|
|
|
|
119
|
|
|
if ((int) $price->main_object_id === (int) $id) { |
120
|
|
|
$salesByObject[$id] = new FakeSale([ |
121
|
|
|
'object' => $price->main_object_name, |
122
|
|
|
'tariff_id' => $model->id, |
123
|
|
|
'object_id' => $id, |
124
|
|
|
'tariff_type' => $model->type, |
125
|
|
|
]); |
126
|
|
|
continue 2; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ((int) $price->object_id === (int) $id) { |
130
|
|
|
$salesByObject[$id] = new FakeSale([ |
131
|
|
|
'object' => $price->object->name, |
132
|
|
|
'tariff_id' => $model->id, |
133
|
|
|
'object_id' => $price->object_id, |
134
|
|
|
'tariff_type' => $model->type, |
135
|
|
|
]); |
136
|
|
|
continue 2; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$salesByObject[$id] = new FakeSale([ |
141
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Unknown object name - neither direct object prices, nor resolvable references exist'), |
142
|
|
|
'tariff_id' => $model->id, |
143
|
|
|
'object_id' => $id, |
144
|
|
|
'tariff_type' => $model->type, |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->sortSaleAndPrices($salesByObject, $pricesByMainObject); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
private function groupHardwarePrices(): array |
152
|
|
|
{ |
153
|
|
|
$model = $this->plan; |
154
|
|
|
/** @var Sale[] $salesByObject */ |
155
|
|
|
$salesByObject = []; |
156
|
|
|
$salesWithId = []; |
157
|
|
|
/** @var Price[][] $pricesByMainObject */ |
158
|
|
|
$pricesByMainObject = []; |
159
|
|
|
|
160
|
|
|
foreach ($model->prices as $price) { |
161
|
|
|
$pricesByMainObject[$price->main_object_id ?? $price->object_id ?? 0][$price->id] = $price; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
foreach ($model->sales as $sale) { |
165
|
|
|
$salesWithId[$sale->object_id] = $sale; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
foreach ($pricesByMainObject as $id => $prices) { |
169
|
|
|
foreach ($prices as $price) { |
170
|
|
|
if ((int)$price->main_object_id === (int)$id) { |
171
|
|
|
$tmpSale = $salesWithId[$price->object_id]; |
172
|
|
|
$tmpSale->object = $price->main_object_name; |
173
|
|
|
$tmpSale->tariff_id = $model->id; |
174
|
|
|
$tmpSale->object_id = $id; |
175
|
|
|
$tmpSale->tariff_type = 'model_group'; |
176
|
|
|
$salesByObject[$id] = $tmpSale; |
177
|
|
|
continue 2; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->sortSaleAndPrices($salesByObject, $pricesByMainObject); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
private function sortSaleAndPrices(array $salesByObject, array $pricesByMainObject): array |
186
|
|
|
{ |
187
|
|
|
foreach ($pricesByMainObject as &$objPrices) { |
188
|
|
|
$objPrices = PriceSort::anyPrices()->values($objPrices, true); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$salesByObject = SaleSort::toDisplayInPlan()->values($salesByObject, true); |
192
|
|
|
|
193
|
|
|
return [$salesByObject, $pricesByMainObject]; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return array of certificate prices |
198
|
|
|
* every element of array consist of two elements: |
199
|
|
|
* certificate,certificate_purchase: CertificatePrice |
200
|
|
|
* certificate,certificate_renewal: CertificatePrice |
201
|
|
|
*/ |
202
|
|
|
private function groupCertificatePrices(): array |
203
|
|
|
{ |
204
|
|
|
$result = []; |
205
|
|
|
|
206
|
|
|
foreach ($this->plan->prices as $price) { |
207
|
|
|
/** @var CertificatePrice $price */ |
208
|
|
|
$result[$price->object_id][$price->type] = $price; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $result; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @return array[] of domain prices |
216
|
|
|
*/ |
217
|
|
|
private function groupDomainPrices(): array |
218
|
|
|
{ |
219
|
|
|
$zonePrices = []; |
220
|
|
|
$servicePrices = []; |
221
|
|
|
|
222
|
|
|
foreach ($this->plan->prices as $price) { |
223
|
|
|
if ($price instanceof DomainZonePrice) { |
224
|
|
|
/** @var DomainZonePrice $price */ |
225
|
|
|
$zonePrices[$price->object_id][$price->type] = $price; |
226
|
|
|
} elseif ($price instanceof DomainServicePrice) { |
227
|
|
|
/** @var DomainServicePrice $price */ |
228
|
|
|
$servicePrices[$price->type] = $price; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
return [$zonePrices, $servicePrices]; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: