Completed
Pull Request — master (#126)
by
unknown
04:29
created

Charge::tableName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
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\modules\finance\models\query\ChargeQuery;
14
use Yii;
15
16
/**
17
 * Class Charge.
18
 *
19
 * @author Dmytro Naumenko <[email protected]>
20
 *
21
 * @property int $id
22
 * @property string $unit
23
 * @property string $quantity
24
 * @property int $parent_id
25
 * @property string $type
26
 * @property string $name
27
 * @property string $ftype
28
 * @property string $currency
29
 * @property float $sum
30
 * @property int object_id
31
 * @property TargetObject $commonObject
32
 * @property TargetObject $latestCommonObject
33
 * @property Bill $bill
34
 */
35
class Charge extends Resource
36
{
37
    use \hipanel\base\ModelTrait;
38
39
    const SCENARIO_CREATE = 'create';
40
    const SCENARIO_UPDATE = 'update';
41
42
    private $isNewRecord;
43
44
    /**
45
     * @inheritDoc
46
     */
47
    public static function tableName()
48
    {
49
        return 'resource';
50
    }
51
52
    public function rules()
53
    {
54
        return [
55
            [['id', 'type_id', 'object_id', 'bill_id', 'parent_id'], 'integer'],
56
            [['class', 'name', 'unit'], 'string'],
57
            [['type', 'label', 'ftype', 'time', 'type_label', 'currency'], 'safe'],
58
            [['sum', 'quantity'], 'number'],
59
            [['unit'], 'default', 'value' => 'items', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
60
            [['object_id', 'sum', 'type', 'quantity', 'unit'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
61
            [['id'], 'safe', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
62
        ];
63
    }
64
65
    public function attributeLabels()
66
    {
67
        return array_merge(parent::attributeLabels(), [
68
            'sum' => Yii::t('hipanel', 'Sum'),
69
            'type' => Yii::t('hipanel', 'Type'),
70
            'quantity' => Yii::t('hipanel', 'Quantity'),
71
            'label' => Yii::t('hipanel', 'Description'),
72
            'time' => Yii::t('hipanel', 'Time'),
73
            'object_id' => Yii::t('hipanel', 'Object'),
74
        ]);
75
    }
76
77
    public function markAsNotNew()
78
    {
79
        $this->isNewRecord = false;
80
    }
81
82
    public function getCommonObject()
83
    {
84
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
85
    }
86
87
    public function getLatestCommonObject()
88
    {
89
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
90
    }
91
92
    public function getIsNewRecord()
93
    {
94
        return $this->isNewRecord !== false && parent::getIsNewRecord();
95
    }
96
97
    public function getBill()
98
    {
99
        return $this->hasOne(Bill::class, ['id' => 'id'])->inverseOf('charges');
100
    }
101
102
    public function isMonthly(): bool
103
    {
104
        return strpos($this->ftype, 'monthly,') === 0;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     * @return ChargeQuery
110
     */
111
    public static function find($options = [])
112
    {
113
        return new ChargeQuery(get_called_class(), [
114
            'options' => $options,
115
        ]);
116
    }
117
}
118