Completed
Push — master ( d0e0e5...0154ba )
by Andrii
05:43 queued 03:12
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', 'client_id', 'tariff_id', 'seller_id', 'order_id'], 'integer'],
56
            [['class', 'name', 'unit', 'tariff', 'order_name', 'client', 'seller'], '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 Id'),
74
            'order_id' => Yii::t('hipanel', 'Order'),
75
        ]);
76
    }
77
78
    public function markAsNotNew()
79
    {
80
        $this->isNewRecord = false;
81
    }
82
83
    public function getCommonObject()
84
    {
85
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
86
    }
87
88
    public function getLatestCommonObject()
89
    {
90
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
91
    }
92
93
    public function getIsNewRecord()
94
    {
95
        return $this->isNewRecord !== false && parent::getIsNewRecord();
96
    }
97
98
    public function getBill()
99
    {
100
        return $this->hasOne(Bill::class, ['id' => 'id'])->inverseOf('charges');
101
    }
102
103
    public function isMonthly(): bool
104
    {
105
        return strpos($this->ftype, 'monthly,') === 0;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     * @return ChargeQuery
111
     */
112
    public static function find($options = [])
113
    {
114
        return new ChargeQuery(get_called_class(), [
115
            'options' => $options,
116
        ]);
117
    }
118
}
119