Completed
Push — master ( 40a649...4fd464 )
by Dmitry
12:14
created

Charge::typeLabel()   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 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\models;
4
5
use hipanel\modules\finance\logic\bill\QuantityTrait;
6
use Yii;
7
8
/**
9
 * Class Charge
10
 *
11
 * @author Dmytro Naumenko <[email protected]>
12
 *
13
 * @property int $id
14
 * @property string $unit
15
 * @property string $quantity
16
 * @property int $parent_id
17
 * @property string $type
18
 * @property string $name
19
 * @property string $ftype
20
 * @property string $currency
21
 * @property float $sum
22
 * @property int object_id
23
 * @property TargetObject $commonObject
24
 * @property TargetObject $latestCommonObject
25
 * @property Bill $bill
26
 */
27
class Charge extends \hiqdev\hiart\ActiveRecord
28
{
29
    use QuantityTrait;
30
31
    const SCENARIO_CREATE = 'create';
32
    const SCENARIO_UPDATE = 'update';
33
34
    private $isNewRecord;
35
36
    public function rules()
37
    {
38
        return [
39
            [['id', 'type_id', 'object_id', 'bill_id', 'parent_id'], 'integer'],
40
            [['class', 'name', 'unit'], 'string'],
41
            [['type', 'label', 'ftype', 'time', 'type_label', 'currency'], 'safe'],
42
            [['sum', 'quantity'], 'number'],
43
            [['unit'], 'default', 'value' => 'items', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
44
            [['object_id', 'sum', 'type', 'quantity', 'unit'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
45
            [['id'], 'safe', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]]
46
        ];
47
    }
48
49
    public function attributeLabels()
50
    {
51
        return array_merge(parent::attributeLabels(), [
52
            'sum' => Yii::t('hipanel', 'Sum'),
53
            'type' => Yii::t('hipanel', 'Type'),
54
            'quantity' => Yii::t('hipanel', 'Quantity'),
55
            'label' => Yii::t('hipanel', 'Description'),
56
            'time' => Yii::t('hipanel', 'Time'),
57
            'object_id' => Yii::t('hipanel', 'Object'),
58
        ]);
59
    }
60
61
    public function markAsNotNew()
62
    {
63
        $this->isNewRecord = false;
64
    }
65
66
    public function getCommonObject()
67
    {
68
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
69
    }
70
71
    public function getLatestCommonObject()
72
    {
73
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
74
    }
75
76
    public function getIsNewRecord()
77
    {
78
        return $this->isNewRecord !== false && parent::getIsNewRecord();
79
    }
80
81
    public function getBill()
82
    {
83
        return $this->hasOne(Bill::class, ['id' => 'id'])->inverseOf('charges');
84
    }
85
86
    public function isMonthly(): bool
87
    {
88
        return strpos($this->ftype, 'monthly,') === 0;
89
    }
90
}
91