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-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\logic; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\forms\AbstractTariffForm; |
14
|
|
|
use hipanel\modules\finance\models\Tariff; |
15
|
|
|
use Yii; |
16
|
|
|
use yii\base\Object; |
17
|
|
|
use yii\helpers\ArrayHelper; |
18
|
|
|
use yii\web\ForbiddenHttpException; |
19
|
|
|
use yii\web\NotFoundHttpException; |
20
|
|
|
|
21
|
|
|
abstract class AbstractTariffManager extends Object |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Tariff[] they array of all available parent tariffs |
25
|
|
|
* @see findParentTariffs() |
26
|
|
|
*/ |
27
|
|
|
protected $parentTariffs; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var AbstractTariffForm |
31
|
|
|
*/ |
32
|
|
|
public $form; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array options used to build [[form]] |
36
|
|
|
* @see buildForm() |
37
|
|
|
*/ |
38
|
|
|
public $formOptions = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $scenario; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Tariff The actual tariff |
47
|
|
|
*/ |
48
|
|
|
protected $tariff; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string The type used to find parent tariffs |
52
|
|
|
*/ |
53
|
|
|
protected $type; |
54
|
|
|
|
55
|
|
|
public function init() |
56
|
|
|
{ |
57
|
|
|
$this->fillParentTariffs(); |
58
|
|
|
$this->buildForm(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Fills [[form]] property with a proper [[AbstractTariffForm]] object. |
63
|
|
|
*/ |
64
|
|
|
protected function buildForm() |
65
|
|
|
{ |
66
|
|
|
$this->form = Yii::createObject(array_merge([ |
67
|
|
|
'scenario' => $this->scenario, |
68
|
|
|
'parentTariffs' => $this->parentTariffs, |
69
|
|
|
'tariff' => $this->tariff, |
70
|
|
|
], $this->getFormOptions())); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function getFormOptions() |
74
|
|
|
{ |
75
|
|
|
return $this->formOptions; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
protected function fillParentTariffs() |
79
|
|
|
{ |
80
|
|
|
$ids = $this->collectParentTariffIds(); |
81
|
|
|
|
82
|
|
|
$this->parentTariffs = Tariff::find() |
83
|
|
|
->where(['id' => $ids]) |
84
|
|
|
->details() |
85
|
|
|
->all(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Collects parent tariff ids. Used in [[fillParentTariffs]] |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
* @throws NotFoundHttpException |
93
|
|
|
*/ |
94
|
|
|
protected function collectParentTariffIds() |
95
|
|
|
{ |
96
|
|
|
if (!isset($this->tariff)) { |
97
|
|
|
$availableTariffs = Tariff::find() |
98
|
|
|
->action('get-available-info') |
99
|
|
|
->andFilterWhere(['type' => $this->type]) |
100
|
|
|
->all(); |
101
|
|
|
|
102
|
|
|
return ArrayHelper::getColumn($availableTariffs, 'id'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($this->tariff->parent_id)) { |
106
|
|
|
return [$this->tariff->parent_id]; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw new NotFoundHttpException('No available tariffs found'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getType() |
113
|
|
|
{ |
114
|
|
|
return $this->type; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param Tariff $tariff |
119
|
|
|
*/ |
120
|
|
|
public function setTariff($tariff) |
121
|
|
|
{ |
122
|
|
|
$this->tariff = $tariff; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.