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\models; |
12
|
|
|
|
13
|
|
|
use hipanel\base\ModelTrait; |
14
|
|
|
use Money\Currencies\ISOCurrencies; |
15
|
|
|
use Money\Currency; |
|
|
|
|
16
|
|
|
use Money\Formatter\DecimalMoneyFormatter; |
17
|
|
|
use Money\Money; |
18
|
|
|
use Money\Parser\DecimalMoneyParser; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
21
|
|
|
use yii\validators\NumberValidator; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @property array $sums |
25
|
|
|
*/ |
26
|
|
|
class CertificatePrice extends Price |
27
|
|
|
{ |
28
|
|
|
use ModelTrait; |
29
|
|
|
|
30
|
|
|
public const TYPE_CERT_PURCHASE = 'certificate,certificate_purchase'; |
31
|
|
|
public const TYPE_CERT_RENEWAL = 'certificate,certificate_renewal'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DecimalMoneyFormatter |
35
|
|
|
*/ |
36
|
|
|
private $moneyFormatter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var DecimalMoneyParser |
40
|
|
|
*/ |
41
|
|
|
private $moneyParser; |
42
|
|
|
|
43
|
|
|
public function __construct(array $config = []) |
44
|
|
|
{ |
45
|
|
|
parent::__construct($config); |
46
|
|
|
$this->moneyFormatter = new DecimalMoneyFormatter(new ISOCurrencies()); |
47
|
|
|
$this->moneyParser = new DecimalMoneyParser(new ISOCurrencies()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function tableName() |
51
|
|
|
{ |
52
|
|
|
return 'price'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function behaviors() |
56
|
|
|
{ |
57
|
|
|
return [ |
58
|
|
|
'typecastAfterFind' => [ |
59
|
|
|
'class' => AttributeTypecastBehavior::class, |
60
|
|
|
'attributeTypes' => [ |
61
|
|
|
'sums' => function ($sums) { |
62
|
|
|
foreach ($sums as $key => $value) { |
63
|
|
|
$sums[$key] = new Money($value, new Currency(strtoupper($this->currency))); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $sums; |
67
|
|
|
}, ], |
68
|
|
|
'typecastAfterFind' => true, |
69
|
|
|
'typecastAfterValidate' => false, |
70
|
|
|
'typecastBeforeSave' => false, |
71
|
|
|
], |
72
|
|
|
'typecastBeforeSave' => [ |
73
|
|
|
'class' => AttributeTypecastBehavior::class, |
74
|
|
|
'attributeTypes' => [ |
75
|
|
|
'sums' => function ($sums) { |
76
|
|
|
foreach ($sums as $key => $value) { |
77
|
|
|
$sums[$key] = $this->moneyParser |
78
|
|
|
->parse($value, strtoupper($this->currency)) |
79
|
|
|
->getAmount(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $sums; |
83
|
|
|
}, ], |
84
|
|
|
'typecastAfterFind' => false, |
85
|
|
|
'typecastAfterValidate' => false, |
86
|
|
|
'typecastBeforeSave' => true, |
87
|
|
|
], |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function rules() |
92
|
|
|
{ |
93
|
|
|
$rules = parent::rules(); |
94
|
|
|
$rules['create-required'] = [ |
95
|
|
|
['object_id'], |
96
|
|
|
'required', |
97
|
|
|
'on' => ['create', 'update'], |
98
|
|
|
'when' => function ($model) { |
99
|
|
|
/** @var self $model */ |
100
|
|
|
return $model->isTypeCorrect(); |
|
|
|
|
101
|
|
|
}, |
102
|
|
|
]; |
103
|
|
|
$rules[] = [['certificateType'], 'safe']; |
104
|
|
|
$rules[] = [['sums'], 'validatePrices', 'on' => ['create', 'update']]; |
105
|
|
|
$rules[] = [['sums'], 'number', 'min' => 0, 'when' => function () { |
106
|
|
|
return false; |
107
|
|
|
}]; |
108
|
|
|
$rules[] = [['sums'], 'each', 'rule' => ['number', 'min' => 0]]; |
109
|
|
|
|
110
|
|
|
return $rules; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function isTypeCorrect(): bool |
117
|
|
|
{ |
118
|
|
|
return isset($this->getTypes()[$this->type]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public static function getPeriods() |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
1 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 1]), |
128
|
|
|
// XXX only 1 year available now |
129
|
|
|
// 2 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 2]), |
130
|
|
|
]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getAvailablePeriods() |
137
|
|
|
{ |
138
|
|
|
$periods = []; |
139
|
|
|
foreach (self::getPeriods() as $period => $message) { |
140
|
|
|
if ($this->hasPriceForPeriod($period)) { |
141
|
|
|
$periods[$period] = $message; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $periods; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param int $period |
150
|
|
|
* @return string|null |
151
|
|
|
*/ |
152
|
|
|
public function getPriceForPeriod(int $period) |
153
|
|
|
{ |
154
|
|
|
return $this->moneyFormatter->format($this->getMoneyForPeriod($period)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function getMoneyForPeriod(int $period): ?Money |
158
|
|
|
{ |
159
|
|
|
if (!$this->hasPriceForPeriod($period)) { |
160
|
|
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $this->sums[$period]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param int $period |
168
|
|
|
* @return bool |
169
|
|
|
*/ |
170
|
|
|
public function hasPriceForPeriod(int $period) |
171
|
|
|
{ |
172
|
|
|
return !empty($this->sums[$period]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function validatePrices(): bool |
176
|
|
|
{ |
177
|
|
|
$periods = $this->getPeriods(); |
178
|
|
|
$validator = new NumberValidator(); |
179
|
|
|
|
180
|
|
|
foreach (array_keys($periods) as $period) { |
181
|
|
|
$validation = $validator->validate($this->sums[$period]); |
182
|
|
|
if ($validation === false) { |
183
|
|
|
unset($this->sums[$period]); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
public static function getTypes() |
194
|
|
|
{ |
195
|
|
|
return [ |
196
|
|
|
static::TYPE_CERT_PURCHASE => Yii::t('hipanel:finance:tariff', 'Purchase'), |
197
|
|
|
static::TYPE_CERT_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'), |
198
|
|
|
]; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: