Completed
Push — master ( 4b854a...47989e )
by Dmitry
15:07
created

Plan::getTypeOptions()   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
 * 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
    public const TYPE_ANYCASTCDN = 'anycastcdn';
53
    public const TYPE_REFERRAL = 'referral';
54
    public const TYPE_VPS = 'vps';
55
    public const TYPE_SNAPSHOT = 'snapshot';
56
    public const TYPE_VOLUME = 'volume';
57
    public const TYPE_STORAGE = 'storage';
58
    public const TYPE_PRIVATE_CLOUD_BACKUP = 'private_cloud_backup';
59
    public const TYPE_PRIVATE_CLOUD = 'private_cloud';
60
61
    protected $knownTypes = [
62
        self::TYPE_SERVER               => self::TYPE_SERVER,
63
        self::TYPE_PCDN                 => self::TYPE_PCDN,
64
        self::TYPE_VCDN                 => self::TYPE_VCDN,
65
        self::TYPE_TEMPLATE             => self::TYPE_TEMPLATE,
66
        self::TYPE_CERTIFICATE          => self::TYPE_CERTIFICATE,
67
        self::TYPE_DOMAIN               => self::TYPE_DOMAIN,
68
        self::TYPE_SWITCH               => self::TYPE_SWITCH,
69
        self::TYPE_AVDS                 => self::TYPE_AVDS,
70
        self::TYPE_OVDS                 => self::TYPE_OVDS,
71
        self::TYPE_SVDS                 => self::TYPE_SVDS,
72
        self::TYPE_CLIENT               => self::TYPE_CLIENT,
73
        self::TYPE_HARDWARE             => self::TYPE_HARDWARE,
74
        self::TYPE_ANYCASTCDN           => self::TYPE_ANYCASTCDN,
75
        self::TYPE_VPS                  => self::TYPE_VPS,
76
        self::TYPE_SNAPSHOT             => self::TYPE_SNAPSHOT,
77
        self::TYPE_VOLUME               => self::TYPE_VOLUME,
78
        self::TYPE_STORAGE              => self::TYPE_STORAGE,
79
        self::TYPE_PRIVATE_CLOUD_BACKUP => self::TYPE_PRIVATE_CLOUD_BACKUP,
80
        self::TYPE_PRIVATE_CLOUD        => self::TYPE_PRIVATE_CLOUD,
81
    ];
82
83
    use ModelTrait;
84
85
    /**
86
     * @var string
87
     */
88
    public $monthly;
89
90
    public $servers = [];
91
92
    public function rules()
93
    {
94
        return array_merge(parent::rules(), [
95
            [['id', 'type_id', 'state_id', 'client_id', 'currency_id'], 'integer'],
96
            [['type', 'state', 'client', 'name', 'plan', 'note', 'currency', 'is_grouping'], 'string'],
97
98
            [['type', 'name', 'currency'], 'required', 'on' => ['create', 'update']],
99
            [['id'], 'required', 'on' => ['update', 'delete', 'set-note']],
100
            [['id'], 'required', 'on' => ['delete', 'restore']],
101
            [['id', 'server_ids'], 'safe', 'on' => ['copy']],
102
            [['your_tariff'], 'boolean'],
103
        ]);
104
    }
105
106
    public function attributeLabels()
107
    {
108
        return array_merge(parent::attributeLabels(), [
109
            'client' => Yii::t('hipanel', 'Seller'),
110
            'name' => Yii::t('hipanel:finance', 'Name'),
111
            'server_ids' => Yii::t('hipanel.finance.plan', 'Servers'),
112
            'monthly' => Yii::t('hipanel.finance.plan', 'Monthly'),
113
            'is_grouping' => Yii::t('hipanel.finance.plan', 'Grouping'),
114
            'currency' => Yii::t('hipanel:finance', 'Currency'),
115
        ]);
116
    }
117
118
    public function getPrices()
119
    {
120
        if ($this->type === Plan::TYPE_CERTIFICATE) {
121
            return $this->hasMany(CertificatePrice::class, ['plan_id' => 'id'])->inverseOf('plan');
122
        }
123
124
        return $this->hasMany(Price::class, ['plan_id' => 'id'])->indexBy('id')->inverseOf('plan');
125
    }
126
127
    public function getPriceHistory()
128
    {
129
        return $this->hasMany(PriceHistory::class, ['tariff_id' => 'id']);
130
    }
131
132
    public function getDesiredPriceClass()
133
    {
134
        if ($this->type === Plan::TYPE_CERTIFICATE) {
135
            return CertificatePrice::class;
136
        }
137
138
        return Price::class;
139
    }
140
141
    public function getSales()
142
    {
143
        return $this->hasMany(Sale::class, ['tariff_id' => 'id']);
144
    }
145
146
    public function getTypeOptions()
147
    {
148
        return Ref::getList('type,tariff');
149
    }
150
151
    public function getStateOptions()
152
    {
153
        return Ref::getList('state,tariff');
154
    }
155
156
    public function isDeleted(): bool
157
    {
158
        return $this->state === 'deleted';
159
    }
160
161
    public function supportsPrices(): bool
162
    {
163
        return !in_array($this->type, [
164
            self::TYPE_SNAPSHOT,
165
            self::TYPE_STORAGE,
166
            self::TYPE_PRIVATE_CLOUD_BACKUP,
167
            self::TYPE_PRIVATE_CLOUD,
168
        ], true);
169
    }
170
171
    public function supportsSharedPrices(): bool
172
    {
173
        return !in_array($this->type, [
174
            self::TYPE_TEMPLATE,
175
            self::TYPE_CERTIFICATE,
176
            self::TYPE_DOMAIN,
177
            self::TYPE_VPS,
178
        ], true);
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     * @return PlanQuery
184
     */
185
    public static function find($options = [])
186
    {
187
        return new PlanQuery(get_called_class(), [
188
            'options' => $options,
189
        ]);
190
    }
191
192
    public function isKnownType(string $type = null): bool
193
    {
194
        return isset($this->knownTypes[$type ?: $this->type]);
195
    }
196
}
197