Completed
Push — master ( f7bdb9...8d2b6e )
by Andrii
10:42
created

Resource::rules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Model;
14
use hipanel\base\ModelTrait;
15
use hipanel\modules\finance\models\decorators\ResourceDecoratorInterface;
16
use hipanel\modules\stock\models\Part;
17
use Money\Money;
18
use Money\MoneyParser;
19
use Yii;
20
use yii\base\InvalidConfigException;
21
22
/**
23
 * @property Tariff $tariff
24
 */
25
class Resource extends Model
26
{
27
    use ModelTrait;
28
29
    protected ResourceDecoratorInterface $decorator;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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