|
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_REFERRAL: |
|
59
|
|
|
return $this->plan->prices; |
|
60
|
|
|
case Plan::TYPE_DOMAIN: |
|
61
|
|
|
$byType = static function (array $servicePrices) { |
|
62
|
|
|
return ArrayHelper::index($servicePrices, 'type', static function ($servicePrice) { |
|
|
|
|
|
|
63
|
|
|
if (strpos($servicePrice->type, 'premium_dns') !== false) { |
|
64
|
|
|
return 'premium_dns'; |
|
65
|
|
|
} |
|
66
|
|
|
if (strpos($servicePrice->type, 'whois_protect') !== false) { |
|
67
|
|
|
return 'whois_protect'; |
|
68
|
|
|
} |
|
69
|
|
|
}); |
|
70
|
|
|
}; |
|
71
|
|
|
[$zonePrices, $servicePrices] = $this->groupDomainPrices(); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
return [$zonePrices, $byType($servicePrices)]; |
|
74
|
|
|
default: |
|
75
|
|
|
return $this->groupServerPrices(); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return array of two elements: |
|
81
|
|
|
* 0: sales, grouped by sold object |
|
82
|
|
|
* 1: prices, grouped by sold object |
|
83
|
|
|
*/ |
|
84
|
|
|
private function groupServerPrices() |
|
85
|
|
|
{ |
|
86
|
|
|
$model = $this->plan; |
|
87
|
|
|
/** @var Sale[] $salesByObject */ |
|
88
|
|
|
$salesByObject = []; |
|
89
|
|
|
/** @var Price[][] $pricesByMainObject */ |
|
90
|
|
|
$pricesByMainObject = []; |
|
91
|
|
|
|
|
92
|
|
|
foreach ($model->prices as $price) { |
|
93
|
|
|
$pricesByMainObject[$price->main_object_id ?? $price->object_id ?? 0][$price->id] = $price; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (isset($pricesByMainObject[0])) { |
|
97
|
|
|
$salesByObject[0] = new FakeSharedSale([ |
|
98
|
|
|
'object' => Yii::t('hipanel.finance.price', 'For all sold objects'), |
|
99
|
|
|
'tariff_id' => $model->id, |
|
100
|
|
|
'tariff_type' => $model->type, |
|
101
|
|
|
]); |
|
102
|
|
|
} |
|
103
|
|
|
if (isset($pricesByMainObject[$model->id])) { |
|
104
|
|
|
$salesByObject[$model->id] = new FakeGroupingSale([ |
|
105
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Grouping prices'), |
|
106
|
|
|
'object_id' => $model->id, |
|
107
|
|
|
'tariff_id' => $model->id, |
|
108
|
|
|
'tariff_type' => $model->type, |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
foreach ($model->sales as $sale) { |
|
112
|
|
|
$salesByObject[$sale->object_id] = $sale; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
foreach ($pricesByMainObject as $id => $prices) { |
|
116
|
|
|
if (isset($salesByObject[$id])) { |
|
117
|
|
|
continue; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
foreach ($prices as $price) { |
|
|
|
|
|
|
121
|
|
|
if ((int) $price->main_object_id === (int) $id) { |
|
122
|
|
|
$salesByObject[$id] = new FakeSale([ |
|
123
|
|
|
'object' => $price->main_object_name, |
|
124
|
|
|
'tariff_id' => $model->id, |
|
125
|
|
|
'object_id' => $id, |
|
126
|
|
|
'tariff_type' => $model->type, |
|
127
|
|
|
]); |
|
128
|
|
|
continue 2; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if ((int) $price->object_id === (int) $id) { |
|
132
|
|
|
$salesByObject[$id] = new FakeSale([ |
|
133
|
|
|
'object' => $price->object->name, |
|
134
|
|
|
'tariff_id' => $model->id, |
|
135
|
|
|
'object_id' => $price->object_id, |
|
136
|
|
|
'tariff_type' => $model->type, |
|
137
|
|
|
]); |
|
138
|
|
|
continue 2; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$salesByObject[$id] = new FakeSale([ |
|
143
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Unknown object name - neither direct object prices, nor resolvable references exist'), |
|
144
|
|
|
'tariff_id' => $model->id, |
|
145
|
|
|
'object_id' => $id, |
|
146
|
|
|
'tariff_type' => $model->type, |
|
147
|
|
|
]); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this->sortSaleAndPrices($salesByObject, $pricesByMainObject); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
private function groupHardwarePrices(): array |
|
154
|
|
|
{ |
|
155
|
|
|
$model = $this->plan; |
|
156
|
|
|
/** @var Sale[] $salesByObject */ |
|
157
|
|
|
$salesByObject = []; |
|
158
|
|
|
$salesWithId = []; |
|
159
|
|
|
/** @var Price[][] $pricesByMainObject */ |
|
160
|
|
|
$pricesByMainObject = []; |
|
161
|
|
|
|
|
162
|
|
|
foreach ($model->prices as $price) { |
|
163
|
|
|
$pricesByMainObject[$price->main_object_id ?? $price->object_id ?? 0][$price->id] = $price; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
foreach ($model->sales as $sale) { |
|
167
|
|
|
$salesWithId[$sale->object_id] = $sale; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
foreach ($pricesByMainObject as $id => $prices) { |
|
171
|
|
|
foreach ($prices as $price) { |
|
172
|
|
|
if ((int)$price->main_object_id === (int)$id) { |
|
173
|
|
|
$tmpSale = $salesWithId[$price->object_id]; |
|
174
|
|
|
$tmpSale->object = $price->main_object_name; |
|
175
|
|
|
$tmpSale->tariff_id = $model->id; |
|
176
|
|
|
$tmpSale->object_id = $id; |
|
177
|
|
|
$tmpSale->tariff_type = 'model_group'; |
|
178
|
|
|
$salesByObject[$id] = $tmpSale; |
|
179
|
|
|
continue 2; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $this->sortSaleAndPrices($salesByObject, $pricesByMainObject); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
private function sortSaleAndPrices(array $salesByObject, array $pricesByMainObject): array |
|
188
|
|
|
{ |
|
189
|
|
|
foreach ($pricesByMainObject as &$objPrices) { |
|
190
|
|
|
$objPrices = PriceSort::anyPrices()->values($objPrices, true); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
$salesByObject = SaleSort::toDisplayInPlan()->values($salesByObject, true); |
|
194
|
|
|
|
|
195
|
|
|
return [$salesByObject, $pricesByMainObject]; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @return array of certificate prices |
|
200
|
|
|
* every element of array consist of two elements: |
|
201
|
|
|
* certificate,certificate_purchase: CertificatePrice |
|
202
|
|
|
* certificate,certificate_renewal: CertificatePrice |
|
203
|
|
|
*/ |
|
204
|
|
|
private function groupCertificatePrices(): array |
|
205
|
|
|
{ |
|
206
|
|
|
$result = []; |
|
207
|
|
|
|
|
208
|
|
|
foreach ($this->plan->prices as $price) { |
|
209
|
|
|
/** @var CertificatePrice $price */ |
|
210
|
|
|
$result[$price->object_id][$price->type] = $price; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $result; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return array[] of domain prices |
|
218
|
|
|
*/ |
|
219
|
|
|
private function groupDomainPrices(): array |
|
220
|
|
|
{ |
|
221
|
|
|
$zonePrices = []; |
|
222
|
|
|
$servicePrices = []; |
|
223
|
|
|
|
|
224
|
|
|
foreach ($this->plan->prices as $price) { |
|
225
|
|
|
if ($price instanceof DomainZonePrice) { |
|
226
|
|
|
/** @var DomainZonePrice $price */ |
|
227
|
|
|
$zonePrices[$price->object_id][$price->type] = $price; |
|
228
|
|
|
} elseif ($price instanceof DomainServicePrice) { |
|
229
|
|
|
/** @var DomainServicePrice $price */ |
|
230
|
|
|
$servicePrices[$price->type] = $price; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
$zonePrices = PriceSort::zonePrices()->values($zonePrices, true); |
|
234
|
|
|
|
|
235
|
|
|
return [$zonePrices, $servicePrices]; |
|
236
|
|
|
} |
|
237
|
|
|
} |
|
238
|
|
|
|
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: