Completed
Push — master ( 1cc23c...b55261 )
by Dmitry
12:24
created

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