1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\logic; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\forms\AbstractTariffForm; |
6
|
|
|
use hipanel\modules\finance\models\Tariff; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\base\Object; |
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use yii\web\ForbiddenHttpException; |
12
|
|
|
|
13
|
|
|
abstract class AbstractTariffManager extends Object |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Tariff[] they array of all available base tariffs |
17
|
|
|
* @see findBaseTariffs() |
18
|
|
|
*/ |
19
|
|
|
protected $baseTariffs; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var AbstractTariffForm |
23
|
|
|
*/ |
24
|
|
|
public $form; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array options used to build [[form]] |
28
|
|
|
* @see buildForm() |
29
|
|
|
*/ |
30
|
|
|
public $formOptions = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public $scenario; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Tariff The actual tariff |
39
|
|
|
*/ |
40
|
|
|
protected $tariff; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string The type used to find base tariff |
44
|
|
|
*/ |
45
|
|
|
protected $type; |
46
|
|
|
|
47
|
|
|
public function init() |
48
|
|
|
{ |
49
|
|
|
$this->findBaseTariffs(); |
50
|
|
|
$this->buildForm(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Fills [[form]] property with a proper [[AbstractTariffForm]] object |
55
|
|
|
*/ |
56
|
|
|
protected function buildForm() |
57
|
|
|
{ |
58
|
|
|
$this->form = Yii::createObject(array_merge([ |
59
|
|
|
'scenario' => $this->scenario, |
60
|
|
|
'baseTariffs' => $this->baseTariffs, |
61
|
|
|
'tariff' => $this->tariff |
62
|
|
|
], $this->getFormOptions())); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
protected function getFormOptions() |
66
|
|
|
{ |
67
|
|
|
return $this->formOptions; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function findBaseTariffs() |
71
|
|
|
{ |
72
|
|
|
$availableTariffs = Tariff::find(['scenario' => 'get-available-info']) |
73
|
|
|
->andFilterWhere(['type' => $this->type]) |
74
|
|
|
->all(); |
75
|
|
|
|
76
|
|
|
if (empty($availableTariffs)) { |
77
|
|
|
throw new ForbiddenHttpException('No available tariffs found'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->baseTariffs = Tariff::find() |
81
|
|
|
->where(['id' => ArrayHelper::getColumn($availableTariffs, 'id')]) |
82
|
|
|
->details() |
83
|
|
|
->all(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getType() |
87
|
|
|
{ |
88
|
|
|
return $this->type; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var TariffCalculator |
93
|
|
|
*/ |
94
|
|
|
protected $_calculator; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return TariffCalculator |
98
|
|
|
*/ |
99
|
|
|
protected function calculator() |
100
|
|
|
{ |
101
|
|
|
if (isset($this->_calculator)) { |
102
|
|
|
return $this->_calculator; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->_calculator = new TariffCalculator($this->tariff); |
106
|
|
|
|
107
|
|
|
return $this->_calculator; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function calculation() |
111
|
|
|
{ |
112
|
|
|
return $this->calculator()->getCalculation($this->tariff->id)->forCurrency($this->tariff->currency); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Tariff $tariff |
117
|
|
|
*/ |
118
|
|
|
public function setTariff($tariff) |
119
|
|
|
{ |
120
|
|
|
$this->tariff = $tariff; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.