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