1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\logic; |
4
|
|
|
|
5
|
|
|
use hipanel\base\Model; |
6
|
|
|
use hipanel\modules\finance\models\Calculation; |
7
|
|
|
use hipanel\modules\finance\models\CalculableModelInterface; |
8
|
|
|
use hiqdev\hiart\ErrorResponseException; |
9
|
|
|
use yii\web\UnprocessableEntityHttpException; |
10
|
|
|
|
11
|
|
|
class TariffCalculator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Model[]|CalculableModelInterface[] |
15
|
|
|
*/ |
16
|
|
|
private $tariffs; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Calculation[] |
20
|
|
|
*/ |
21
|
|
|
private $calculations; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* TariffCalculator constructor. |
25
|
|
|
* @param Model[]|Model $tariffs |
26
|
|
|
*/ |
27
|
|
|
public function __construct($tariffs) |
28
|
|
|
{ |
29
|
|
|
if ($tariffs instanceof Model) { |
30
|
|
|
$tariffs = [$tariffs]; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->tariffs = $tariffs; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Gets [[Calculation]] for the $tariffId |
38
|
|
|
* |
39
|
|
|
* @param integer $tariffId |
40
|
|
|
* @return Calculation |
41
|
|
|
*/ |
42
|
|
|
public function getCalculation($tariffId) |
43
|
|
|
{ |
44
|
|
|
if ($this->calculations === null) { |
45
|
|
|
$this->execute(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->calculations[$tariffId]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return Calculation[] |
53
|
|
|
*/ |
54
|
|
|
public function getCalculations() |
55
|
|
|
{ |
56
|
|
|
if ($this->calculations === null) { |
57
|
|
|
$this->execute(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->calculations; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return \hipanel\modules\finance\models\Calculation[] |
65
|
|
|
* @throws UnprocessableEntityHttpException |
66
|
|
|
*/ |
67
|
|
|
public function execute() |
68
|
|
|
{ |
69
|
|
|
try { |
70
|
|
|
$rows = Calculation::perform('CalcValue', $this->collectData(), true); |
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
throw new UnprocessableEntityHttpException('Failed to calculate tariffs value', 0, $e); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->calculations = $this->createCalculations($rows); |
76
|
|
|
|
77
|
|
|
return $this->calculations; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
private function collectData() |
84
|
|
|
{ |
85
|
|
|
$data = []; |
86
|
|
|
foreach ($this->tariffs as $tariff) { |
87
|
|
|
$calculation = $tariff->getCalculationModel(); |
|
|
|
|
88
|
|
|
$data[$tariff->getPrimaryKey()] = $calculation->getAttributes(); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
return $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $rows |
95
|
|
|
* @return \hipanel\modules\finance\models\Calculation[] |
96
|
|
|
*/ |
97
|
|
|
private function createCalculations($rows) |
98
|
|
|
{ |
99
|
|
|
$query = Calculation::find()->joinWith(['value'])->indexBy('tariff_id')->prepare(); |
100
|
|
|
return $query->populate($rows); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: