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