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\modules\finance\models\query\TariffQuery; |
14
|
|
|
use hipanel\modules\finance\models\stubs\ServerResourceStub; |
15
|
|
|
use Yii; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Tariff. |
19
|
|
|
* @property resource[]|DomainResource[]|ServerResource[] $resources |
20
|
|
|
*/ |
21
|
|
|
class Tariff extends \hipanel\base\Model implements CalculableModelInterface |
22
|
|
|
{ |
23
|
|
|
use \hipanel\base\ModelTrait; |
24
|
|
|
|
25
|
|
|
const TYPE_DOMAIN = 'domain'; |
26
|
|
|
const TYPE_XEN = 'svds'; |
27
|
|
|
const TYPE_OPENVZ = 'ovds'; |
28
|
|
|
const TYPE_SERVER = 'server'; |
29
|
|
|
const TYPE_CERT = 'certificate'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function rules() |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
[['id'], 'filter', 'filter' => function ($value) { return explode(',', $value); }, 'on' => ['search']], |
38
|
|
|
[['id'], 'each', 'rule' => ['trim', 'integer'], 'on' => ['search']], |
39
|
|
|
[['client_id', 'seller_id', 'id', 'parent_id'], 'integer', 'on' => ['create', 'update']], |
40
|
|
|
[['client', 'seller', 'bill', 'name'], 'safe'], |
41
|
|
|
[['domain', 'server'], 'safe'], |
42
|
|
|
[['tariff', 'tariff_id'], 'safe'], |
43
|
|
|
[['type_id', 'state_id'], 'integer'], |
44
|
|
|
[['type', 'state', 'currency'], 'safe'], |
45
|
|
|
[['used'], 'integer'], |
46
|
|
|
[['note', 'label'], 'safe'], |
47
|
|
|
[['is_personal'], 'boolean'], |
48
|
|
|
[['id'], 'required', 'on' => ['delete', 'set-note']], |
49
|
|
|
[['note'], 'string', 'on' => ['set-note']], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getResources() |
54
|
|
|
{ |
55
|
|
|
if ($this->getGeneralType() === 'domain') { |
56
|
|
|
return $this->hasMany(DomainResource::class, ['tariff_id' => 'id'])->inverseOf('tariff'); |
57
|
|
|
} elseif ($this->getGeneralType() === 'server') { |
58
|
|
|
return $this->hasMany(ServerResource::class, ['tariff_id' => 'id'])->inverseOf('tariff'); |
59
|
|
|
} elseif ($this->getGeneralType() === 'certificate') { |
60
|
|
|
return $this->hasMany(CertificateResource::class, ['tariff_id' => 'id'])->inverseOf('tariff'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->hasMany(Resource::class, ['tariff_id' => 'id'])->inverseOf('tariff'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $type |
68
|
|
|
* @param bool $stubWhenNotFound whether to return [[ServerResourceStub]] when |
69
|
|
|
* the tariff does not have a relevant resource |
70
|
|
|
* @return DomainResource|ServerResource|ServerResourceStub|resource |
71
|
|
|
*/ |
72
|
|
|
public function getResourceByType($type, $stubWhenNotFound = true) |
73
|
|
|
{ |
74
|
|
|
foreach ($this->resources as $resource) { |
75
|
|
|
if ($resource->type === $type) { |
76
|
|
|
return $resource; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $stubWhenNotFound ? $this->getStubResource($type) : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $type |
85
|
|
|
* @return ServerResourceStub |
86
|
|
|
*/ |
87
|
|
|
public function getStubResource($type) |
88
|
|
|
{ |
89
|
|
|
return new ServerResourceStub([ |
90
|
|
|
'tariff' => $this, |
91
|
|
|
'type' => $type, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function attributeLabels() |
99
|
|
|
{ |
100
|
|
|
return $this->mergeAttributeLabels([ |
101
|
|
|
'used' => Yii::t('hipanel:finance:tariff', 'Used'), |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
* @return TariffQuery |
108
|
|
|
*/ |
109
|
|
|
public static function find($options = []) |
110
|
|
|
{ |
111
|
|
|
return new TariffQuery(get_called_class(), [ |
112
|
|
|
'options' => $options, |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getGeneralType() |
117
|
|
|
{ |
118
|
|
|
if ($this->type === static::TYPE_DOMAIN) { |
|
|
|
|
119
|
|
|
return 'domain'; |
120
|
|
|
} elseif ($this->type === static::TYPE_CERT) { |
|
|
|
|
121
|
|
|
return 'certificate'; |
122
|
|
|
} elseif (in_array($this->type, [static::TYPE_OPENVZ, static::TYPE_XEN, static::TYPE_SERVER], true)) { |
|
|
|
|
123
|
|
|
return 'server'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return null; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Method creates and returns corresponding Calculation model. |
131
|
|
|
* |
132
|
|
|
* @return Calculation |
133
|
|
|
*/ |
134
|
|
|
public function getCalculationModel() |
135
|
|
|
{ |
136
|
|
|
return new Calculation([ |
137
|
|
|
'calculation_id' => $this->id, |
|
|
|
|
138
|
|
|
'tariff_id' => $this->id, |
|
|
|
|
139
|
|
|
'object' => $this->getGeneralType(), |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.