|
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\modules\finance\models\decorators\AbstractResourceDecorator; |
|
14
|
|
|
use hipanel\modules\stock\models\Part; |
|
15
|
|
|
use Money\Money; |
|
16
|
|
|
use Money\MoneyParser; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\base\InvalidConfigException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @property Tariff $tariff |
|
22
|
|
|
*/ |
|
23
|
|
|
class Resource extends \hipanel\base\Model |
|
24
|
|
|
{ |
|
25
|
|
|
use \hipanel\base\ModelTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** @var AbstractResourceDecorator */ |
|
28
|
|
|
protected $decorator; |
|
29
|
|
|
|
|
30
|
|
|
/** {@inheritdoc} */ |
|
31
|
|
|
public static $i18nDictionary = 'hipanel:finance:tariff'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public function rules() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
[['id', 'tariff_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'hardlim'], 'integer'], |
|
40
|
|
|
[['type', 'ftype', 'unit', 'unit_factor', 'currency'], 'safe'], |
|
41
|
|
|
[['price', 'fee', 'quantity', 'discount'], 'number'], |
|
42
|
|
|
|
|
43
|
|
|
[['object_id', 'type_id'], 'integer', 'on' => ['create', 'update']], |
|
44
|
|
|
[['type'], 'safe', 'on' => ['create', 'update']], |
|
45
|
|
|
[['price'], 'number', 'on' => ['create', 'update']], |
|
46
|
|
|
'create-required' => [['object_id', 'price'], 'required', 'on' => ['create', 'update']], |
|
47
|
|
|
['id', 'integer', 'on' => 'delete'], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getTariff() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->hasOne(Tariff::class, ['id' => 'tariff_id'])->inverseOf('resources'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getPart() |
|
57
|
|
|
{ |
|
58
|
|
|
if (!Yii::getAlias('@part', false)) { |
|
59
|
|
|
throw new InvalidConfigException('Stock module is a must to retrieve resource parts'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->hasOne(Part::class, ['object_id' => 'id']); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function attributeLabels() |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->mergeAttributeLabels([ |
|
71
|
|
|
'price' => Yii::t('hipanel:finance:tariff', 'Price per period'), |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function isTypeCorrect() |
|
76
|
|
|
{ |
|
77
|
|
|
return isset($this->getTypes()[$this->type]); |
|
|
|
|
|
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getTypes() |
|
81
|
|
|
{ |
|
82
|
|
|
return []; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function isPeriodic() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->type === 'periodic'; |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getCurrency() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->currency; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getQuantity() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->quantity; |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function decorator() |
|
101
|
|
|
{ |
|
102
|
|
|
throw new InvalidConfigException('Method "decorator" is not available for class Resource'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return Money |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getMoney(): Money |
|
109
|
|
|
{ |
|
110
|
|
|
// TODO: decide how to get MoneyParser correctly |
|
111
|
|
|
return Yii::$container->get(MoneyParser::class) |
|
112
|
|
|
->parse((string)$this->price, strtoupper($this->currency)); |
|
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
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@propertyannotation 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.