1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\helpers; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\CertificatePrice; |
6
|
|
|
use hipanel\modules\finance\models\FakeSale; |
7
|
|
|
use hipanel\modules\finance\models\Plan; |
8
|
|
|
use hipanel\modules\finance\models\Price; |
9
|
|
|
use hipanel\modules\finance\models\Sale; |
10
|
|
|
use Tuck\Sort\Sort; |
11
|
|
|
use Yii; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PlanInternalsGrouper can be used to group prices inside $plan depending on |
15
|
|
|
* different factors. |
16
|
|
|
* |
17
|
|
|
* @author Dmytro Naumenko <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class PlanInternalsGrouper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var Plan |
23
|
|
|
*/ |
24
|
|
|
private $plan; |
25
|
|
|
|
26
|
|
|
public function __construct(Plan $plan) |
27
|
|
|
{ |
28
|
|
|
$this->plan = $plan; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Should be used to group prices of [[Plan]] with the following types: |
33
|
|
|
* - server |
34
|
|
|
* - sVDS |
35
|
|
|
* - oVDS |
36
|
|
|
* - certificate |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function group() |
40
|
|
|
{ |
41
|
|
|
switch ($this->plan->type) { |
42
|
|
|
case Plan::TYPE_CERTIFICATE: |
43
|
|
|
return $this->groupCertificatePrices(); |
44
|
|
|
default: |
45
|
|
|
return $this->groupServerPrices(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return array of two elements: |
51
|
|
|
* 0: sales, grouped by sold object |
52
|
|
|
* 1: prices, grouped by sold object |
53
|
|
|
*/ |
54
|
|
|
private function groupServerPrices() |
55
|
|
|
{ |
56
|
|
|
$model = $this->plan; |
57
|
|
|
/** @var Sale[] $salesByObject */ |
58
|
|
|
$salesByObject = []; |
59
|
|
|
/** @var Price[][] $pricesByMainObject */ |
60
|
|
|
$pricesByMainObject = []; |
61
|
|
|
|
62
|
|
|
foreach ($model->prices as $price) { |
63
|
|
|
$pricesByMainObject[$price->main_object_id ?? $model->id][$price->id] = $price; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (isset($pricesByMainObject[null])) { |
67
|
|
|
$salesByObject[null] = new FakeSale([ |
68
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Applicable for all objects'), |
69
|
|
|
'tariff_id' => $model->id, |
70
|
|
|
]); |
71
|
|
|
} |
72
|
|
|
if (isset($pricesByMainObject[$model->id])) { |
73
|
|
|
$salesByObject[$model->id] = new FakeSale([ |
74
|
|
|
'object' => Yii::t('hipanel.finance.price', 'For the whole tariff'), |
75
|
|
|
'tariff_id' => $model->id, |
76
|
|
|
'object_id' => $model->id, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
foreach ($model->sales as $sale) { |
80
|
|
|
$salesByObject[$sale->object_id] = $sale; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($pricesByMainObject as $id => $prices) { |
84
|
|
|
if (isset($salesByObject[$id])) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($prices as $price) { |
89
|
|
|
if ((int)$price->object_id === (int)$id) { |
90
|
|
|
$salesByObject[$id] = new FakeSale([ |
91
|
|
|
'object' => $price->object->name, |
92
|
|
|
'tariff_id' => $model->id, |
93
|
|
|
'object_id' => $price->object_id, |
94
|
|
|
'tariff_type' => $model->type, |
95
|
|
|
]); |
96
|
|
|
continue 2; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$salesByObject[$id] = new FakeSale([ |
101
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Unknown object name - no direct object prices exist'), |
102
|
|
|
'tariff_id' => $model->id, |
103
|
|
|
'object_id' => $id, |
104
|
|
|
'tariff_type' => $model->type, |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
foreach ($pricesByMainObject as &$objPrices) { |
109
|
|
|
$objPrices = PriceSort::anyPrices()->values($objPrices, true); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return [$salesByObject, $pricesByMainObject]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return array of certificate prices |
117
|
|
|
* every element of array consist of two elements: |
118
|
|
|
* certificate,certificate_purchase: CertificatePrice |
119
|
|
|
* certificate,certificate_renewal: CertificatePrice |
120
|
|
|
*/ |
121
|
|
|
private function groupCertificatePrices(): array |
122
|
|
|
{ |
123
|
|
|
$result = []; |
124
|
|
|
|
125
|
|
|
foreach ($this->plan->prices as $price) { |
126
|
|
|
/** @var CertificatePrice $price */ |
127
|
|
|
$result[$price->object_id][$price->type] = $price; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $result; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|