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