Completed
Push — master ( 691d80...8f3098 )
by Dmitry
13:51
created

Plan::getPriceHistory()   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\base\Model;
14
use hipanel\base\ModelTrait;
15
use hipanel\models\Ref;
16
use hipanel\modules\finance\models\query\PlanQuery;
17
use Yii;
18
19
/**
20
 * Class Plan.
21
 *
22
 * @property string $id
23
 * @property string $name
24
 * @property string $type
25
 * @property string $currency
26
 * @property string $state
27
 * @property int $currency_id
28
 * @property bool $is_grouping
29
 * @property bool $your_tariff
30
 *
31
 * @property PriceHistory[] $priceHistory
32
 * @property Sale[] $sales
33
 * @property Price[]|CertificatePrice[] $prices
34
 * @property-read string[] typeOptions
35
 *
36
 * @author Dmytro Naumenko <[email protected]>
37
 */
38
class Plan extends Model
39
{
40
    public const TYPE_SERVER = 'server';
41
    public const TYPE_PCDN = 'pcdn';
42
    public const TYPE_VCDN = 'vcdn';
43
    public const TYPE_TEMPLATE = 'template';
44
    public const TYPE_CERTIFICATE = 'certificate';
45
    public const TYPE_DOMAIN = 'domain';
46
    public const TYPE_SWITCH = 'switch';
47
    public const TYPE_AVDS = 'avds';
48
    public const TYPE_OVDS = 'ovds';
49
    public const TYPE_SVDS = 'svds';
50
    public const TYPE_CLIENT = 'client';
51
    public const TYPE_HARDWARE = 'hardware';
52
53
    use ModelTrait;
54
55
    /**
56
     * @var string
57
     */
58
    public $monthly;
59
60
    public $servers = [];
61
62
    public function rules()
63
    {
64
        return array_merge(parent::rules(), [
65
            [['id', 'type_id', 'state_id', 'client_id', 'currency_id'], 'integer'],
66
            [['type', 'state', 'client', 'name', 'plan', 'note', 'currency', 'is_grouping'], 'string'],
67
68
            [['type', 'name', 'currency'], 'required', 'on' => ['create', 'update']],
69
            [['id'], 'required', 'on' => ['update', 'delete', 'set-note']],
70
            [['id'], 'required', 'on' => ['delete', 'restore']],
71
            [['id', 'server_ids'], 'safe', 'on' => ['copy']],
72
            [['your_tariff'], 'boolean'],
73
        ]);
74
    }
75
76
    public function attributeLabels()
77
    {
78
        return array_merge(parent::attributeLabels(), [
79
            'client' => Yii::t('hipanel', 'Seller'),
80
            'name' => Yii::t('hipanel:finance', 'Name'),
81
            'server_ids' => Yii::t('hipanel.finance.plan', 'Servers'),
82
            'monthly' => Yii::t('hipanel.finance.plan', 'Monthly'),
83
            'is_grouping' => Yii::t('hipanel.finance.plan', 'Grouping'),
84
            'currency' => Yii::t('hipanel:finance', 'Currency'),
85
        ]);
86
    }
87
88
    public function getPrices()
89
    {
90
        if ($this->type === Plan::TYPE_CERTIFICATE) {
91
            return $this->hasMany(CertificatePrice::class, ['plan_id' => 'id'])->inverseOf('plan');
92
        }
93
94
        return $this->hasMany(Price::class, ['plan_id' => 'id'])->indexBy('id')->inverseOf('plan');
95
    }
96
97
    public function getPriceHistory()
98
    {
99
        return $this->hasMany(PriceHistory::class, ['tariff_id' => 'id']);
100
    }
101
102
    public function getDesiredPriceClass()
103
    {
104
        if ($this->type === Plan::TYPE_CERTIFICATE) {
105
            return CertificatePrice::class;
106
        }
107
108
        return Price::class;
109
    }
110
111
    public function getSales()
112
    {
113
        return $this->hasMany(Sale::class, ['tariff_id' => 'id']);
114
    }
115
116
    public function getTypeOptions()
117
    {
118
        return Ref::getList('type,tariff');
119
    }
120
121
    public function getStateOptions()
122
    {
123
        return Ref::getList('state,tariff');
124
    }
125
126
    public function isDeleted(): bool
127
    {
128
        return $this->state === 'deleted';
129
    }
130
131
    public function supportsSharedPrices(): bool
132
    {
133
        return !\in_array($this->type, [Plan::TYPE_TEMPLATE, Plan::TYPE_CERTIFICATE, Plan::TYPE_DOMAIN], true);
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     * @return PlanQuery
139
     */
140
    public static function find($options = [])
141
    {
142
        return new PlanQuery(get_called_class(), [
143
            'options' => $options,
144
        ]);
145
    }
146
}
147