Completed
Push — master ( b55261...576c8c )
by Dmitry
14:00
created

Charge::getLatestCommonObject()   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 3
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
 */
26
class Charge extends \hiqdev\hiart\ActiveRecord
27
{
28
    use QuantityTrait;
29
30
    const SCENARIO_CREATE = 'create';
31
    const SCENARIO_UPDATE = 'update';
32
33
    private $isNewRecord;
34
35
    public function rules()
36
    {
37
        return [
38
            [['id', 'type_id', 'object_id', 'bill_id', 'parent_id'], 'integer'],
39
            [['class', 'name', 'unit'], 'string'],
40
            [['type', 'label', 'ftype', 'time', 'type_label', 'currency'], 'safe'],
41
            [['sum', 'quantity'], 'number'],
42
            [['unit'], 'default', 'value' => 'items', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
43
            [['object_id', 'sum', 'type', 'quantity', 'unit'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
44
            [['id'], 'safe', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]]
45
        ];
46
    }
47
48
    public function attributeLabels()
49
    {
50
        return array_merge(parent::attributeLabels(), [
51
            'sum' => Yii::t('hipanel', 'Sum'),
52
            'type' => Yii::t('hipanel', 'Type'),
53
            'quantity' => Yii::t('hipanel', 'Quantity'),
54
            'label' => Yii::t('hipanel', 'Description'),
55
            'time' => Yii::t('hipanel', 'Time'),
56
            'object_id' => Yii::t('hipanel', 'Object'),
57
        ]);
58
    }
59
60
    public function markAsNotNew()
61
    {
62
        $this->isNewRecord = false;
63
    }
64
65
    public function getCommonObject()
66
    {
67
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
68
    }
69
70
    public function getLatestCommonObject()
71
    {
72
        return $this->hasOne(TargetObject::class, ['id' => 'id']);
73
    }
74
75
    public function getIsNewRecord()
76
    {
77
        return $this->isNewRecord !== false && parent::getIsNewRecord();
78
    }
79
80
    public function typeLabel(): string
81
    {
82
        return Yii::t('hipanel.finance.priceTypes', \yii\helpers\Inflector::titleize($this->type));
83
    }
84
85
    public function isMonthly(): bool
86
    {
87
        return strpos($this->ftype, 'monthly,') === 0;
88
    }
89
}
90