1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\controllers; |
4
|
|
|
|
5
|
|
|
use hipanel\actions\Action; |
6
|
|
|
use hipanel\actions\IndexAction; |
7
|
|
|
use hipanel\actions\SmartCreateAction; |
8
|
|
|
use hipanel\actions\SmartUpdateAction; |
9
|
|
|
use hipanel\actions\ValidateFormAction; |
10
|
|
|
use hipanel\actions\ViewAction; |
11
|
|
|
use hipanel\base\CrudController; |
12
|
|
|
use hipanel\modules\finance\models\FakeSale; |
13
|
|
|
use hipanel\modules\finance\models\Plan; |
14
|
|
|
use hipanel\modules\finance\models\Price; |
15
|
|
|
use hipanel\modules\finance\models\Sale; |
16
|
|
|
use hiqdev\hiart\Query; |
17
|
|
|
use Yii; |
18
|
|
|
use yii\base\Event; |
19
|
|
|
use yii\web\NotFoundHttpException; |
20
|
|
|
|
21
|
|
|
class PlanController extends CrudController |
22
|
|
|
{ |
23
|
|
|
public function actions() |
24
|
|
|
{ |
25
|
|
|
return array_merge(parent::actions(), [ |
26
|
|
|
'create' => [ |
27
|
|
|
'class' => SmartCreateAction::class, |
28
|
|
|
], |
29
|
|
|
'update' => [ |
30
|
|
|
'class' => SmartUpdateAction::class, |
31
|
|
|
], |
32
|
|
|
'index' => [ |
33
|
|
|
'class' => IndexAction::class, |
34
|
|
|
], |
35
|
|
|
'view' => [ |
36
|
|
|
'class' => ViewAction::class, |
37
|
|
|
'on beforePerform' => function (Event $event) { |
38
|
|
|
$action = $event->sender; |
39
|
|
|
$action->getDataProvider()->query |
40
|
|
|
->joinWith('sales') |
41
|
|
|
->with([ |
42
|
|
|
'prices' => function (Query $query) { |
43
|
|
|
$query |
44
|
|
|
->addSelect('main_object_id') |
45
|
|
|
->joinWith('object') |
46
|
|
|
->limit('ALL'); |
47
|
|
|
}, |
48
|
|
|
]); |
49
|
|
|
}, |
50
|
|
|
'data' => function (Action $action, array $data) { |
51
|
|
|
[$salesByObject, $pricesByMainObject] = $this->groupSalesAndPrices($data['model']); |
|
|
|
|
52
|
|
|
|
53
|
|
|
return array_merge($data, [ |
54
|
|
|
'salesByObject' => $salesByObject, |
55
|
|
|
'pricesByMainObject' => $pricesByMainObject, |
56
|
|
|
]); |
57
|
|
|
}, |
58
|
|
|
], |
59
|
|
|
'set-note' => [ |
60
|
|
|
'class' => SmartUpdateAction::class, |
61
|
|
|
'success' => Yii::t('hipanel', 'Note changed'), |
62
|
|
|
], |
63
|
|
|
'validate-form' => [ |
64
|
|
|
'class' => ValidateFormAction::class, |
65
|
|
|
], |
66
|
|
|
]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function actionCreatePrices($id) |
70
|
|
|
{ |
71
|
|
|
$plan = Plan::findOne(['id' => $id]); |
72
|
|
|
if ($plan === null) { |
73
|
|
|
throw new NotFoundHttpException('Not found'); |
74
|
|
|
} |
75
|
|
|
$this->layout = false; |
76
|
|
|
|
77
|
|
|
return $this->renderAjax('_createPrices', ['plan' => $plan]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Groups prices in $plan by main object ID. |
82
|
|
|
* |
83
|
|
|
* Creates fakes sales for price objects, that are not actually sold. |
84
|
|
|
* |
85
|
|
|
* @param Plan $model |
86
|
|
|
* @return array |
87
|
|
|
*/ |
88
|
|
|
protected function groupSalesAndPrices(Plan $model) |
89
|
|
|
{ |
90
|
|
|
/** @var Sale[] $salesByObject */ |
91
|
|
|
$salesByObject = []; |
92
|
|
|
/** @var Price[][] $pricesByMainObject */ |
93
|
|
|
$pricesByMainObject = []; |
94
|
|
|
|
95
|
|
|
foreach ($model->prices as $price) { |
96
|
|
|
$pricesByMainObject[$price->main_object_id ?? $model->id][$price->id] = $price; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (isset($pricesByMainObject[null])) { |
100
|
|
|
$salesByObject[null] = new FakeSale([ |
101
|
|
|
'object' => Yii::t('hipanel.finance.price', 'Applicable for all objects'), |
102
|
|
|
'tariff_id' => $model->id, |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
if (isset($pricesByMainObject[$model->id])) { |
106
|
|
|
$salesByObject[$model->id] = new FakeSale([ |
107
|
|
|
'object' => Yii::t('hipanel.finance.price', 'For the whole tariff'), |
108
|
|
|
'tariff_id' => $model->id, |
109
|
|
|
'object_id' => $model->id, |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
foreach ($model->sales as $sale) { |
113
|
|
|
$salesByObject[$sale->object_id] = $sale; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($pricesByMainObject as $id => $prices) { |
117
|
|
|
if (!isset($salesByObject[$id])) { |
118
|
|
|
$firstPrice = reset($prices); |
119
|
|
|
$salesByObject[$id] = new FakeSale([ |
120
|
|
|
'object' => $firstPrice->object->name, |
121
|
|
|
'tariff_id' => $model->id, |
122
|
|
|
'object_id' => $firstPrice->object_id, |
123
|
|
|
'tariff_type' => $model->type, |
124
|
|
|
]); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return [$salesByObject, $pricesByMainObject]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.