1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\logic; |
4
|
|
|
|
5
|
|
|
use hipanel\modules\finance\models\Calculation; |
6
|
|
|
use hipanel\modules\finance\models\CalculableModelInterface; |
7
|
|
|
use yii\base\Model; |
8
|
|
|
use yii\web\UnprocessableEntityHttpException; |
9
|
|
|
|
10
|
|
|
class Calculator |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Model[]|CalculableModelInterface[] |
14
|
|
|
*/ |
15
|
|
|
protected $models; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Calculation[] |
19
|
|
|
*/ |
20
|
|
|
protected $calculations; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* TariffCalculator constructor. |
24
|
|
|
* @param Model[] $models |
25
|
|
|
*/ |
26
|
|
|
public function __construct($models) |
27
|
|
|
{ |
28
|
|
|
$this->models = $models; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Gets [[Calculation]] for the $id |
33
|
|
|
* |
34
|
|
|
* @param integer $id |
35
|
|
|
* @return Calculation |
36
|
|
|
*/ |
37
|
|
|
public function getCalculation($id) |
38
|
|
|
{ |
39
|
|
|
if ($this->calculations === null) { |
40
|
|
|
$this->execute(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return isset($this->calculations[$id]) ? $this->calculations[$id] : null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return Calculation[] |
48
|
|
|
*/ |
49
|
|
|
public function getCalculations() |
50
|
|
|
{ |
51
|
|
|
if ($this->calculations === null) { |
52
|
|
|
$this->execute(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->calculations; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return \hipanel\modules\finance\models\Calculation[] |
60
|
|
|
* @throws UnprocessableEntityHttpException |
61
|
|
|
*/ |
62
|
|
|
public function execute() |
63
|
|
|
{ |
64
|
|
|
$data = $this->collectData(); |
65
|
|
|
|
66
|
|
|
if (empty($data)) { |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
$rows = Calculation::perform('CalcValue', $data, true); |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
throw new UnprocessableEntityHttpException('Failed to calculate value: ' . $e->getMessage(), 0, $e); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->calculations = $this->createCalculations($rows); |
77
|
|
|
|
78
|
|
|
return $this->calculations; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
protected function collectData() |
85
|
|
|
{ |
86
|
|
|
$data = []; |
87
|
|
|
foreach ($this->models as $model) { |
88
|
|
|
$calculation = $model->getCalculationModel(); |
|
|
|
|
89
|
|
|
$data[$calculation->calculation_id] = $calculation->toArray(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $data; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $rows |
97
|
|
|
* @return \hipanel\modules\finance\models\Calculation[] |
98
|
|
|
*/ |
99
|
|
|
private function createCalculations($rows) |
100
|
|
|
{ |
101
|
|
|
$query = Calculation::find()->joinWith(['value'])->indexBy('calculation_id'); |
102
|
|
|
$query->prepare(); |
103
|
|
|
|
104
|
|
|
return $query->populate($rows); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
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: