TaxGroup.php
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace artweb\artbox\modules\catalog\models;
use artweb\artbox\modules\language\behaviors\LanguageBehavior;
use artweb\artbox\modules\language\models\Language;
use artweb\artbox\modules\catalog\models\Category;
use yii\base\InvalidValueException;
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
use yii\web\Request;
/**
* This is the model class for table "{{%tax_group}}".
*
* @property integer $id
* @property boolean $is_filter
* @property integer $level
* @property integer $sort
* @property boolean $display
* @property boolean $is_menu
* @property string $remote_id
* @property TaxOption[] $taxOptions
* @property Category[] $categories
* @property TaxOption[] $options
* @property TaxOption[] $customOptions
* @property string $alias
* * From language behavior *
* @property TaxGroupLang $lang
* @property TaxGroupLang[] $langs
* @property TaxGroupLang $objectLang
* @property string $ownerKey
* @property string $langKey
* @property TaxGroupLang[] $modelLangs
* @property bool $transactionStatus
* @method string getOwnerKey()
* @method void setOwnerKey( string $value )
* @method string getLangKey()
* @method void setLangKey( string $value )
* @method ActiveQuery getLangs()
* @method ActiveQuery getLang( integer $language_id )
* @method TaxGroupLang[] generateLangs()
* @method void loadLangs( Request $request )
* @method bool linkLangs()
* @method bool saveLangs()
* @method bool getTransactionStatus()
* * End language behavior *
*/
class TaxGroup extends ActiveRecord
{
const GROUP_PRODUCT = 0;
const GROUP_VARIANT = 1;
/**
* @var TaxOption[] $options
*/
protected $customOptions = [];
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'language' => [
'class' => LanguageBehavior::className(),
],
];
}
/**
* @inheritdoc
*/
public static function tableName()
{
return 'tax_group';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'is_filter',
'display',
'is_menu',
],
'boolean',
],
[
[
'level',
'sort',
],
'integer',
],
[
[ 'categories' ],
'safe',
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'Tax Group ID',
'is_filter' => 'Use in filter',
'sort' => 'Sort',
'display' => 'Display',
'is_menu' => 'Отображать в меню',
];
}
/**
* @return ActiveQuery
*/
public function getCategories()
{
return $this->hasMany(Category::className(), [ 'id' => 'category_id' ])
->viaTable('tax_group_to_category', [ 'tax_group_id' => 'id' ]);
}
/**
* Set categories to override tax_group_to_category table data
*
* @param int[] $values
*/
public function setCategories(array $values)
{
$this->categories = $values;
}
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
$this->unlinkAll('categories', true);
$categories = [];
if (!empty( $this->categories )) {
$categories = Category::findAll($this->categories);
}
foreach ($categories as $category) {
$this->link('categories', $category);
}
}
/**
* @return ActiveQuery
*/
public function getTaxOptions()
{
return $this->hasMany(TaxOption::className(), [ 'tax_group_id' => 'id' ])
->inverseOf('taxGroup');
}
/**
* Synonim for getTaxOptions()
*
* @see TaxGroup::getTaxOptions()
* @return \yii\db\ActiveQuery
*/
public function getOptions()
{
return $this->getTaxOptions();
}
/**
* Get customOptins that were filled dynamically.
* If $fillDefault is true then fill $customOptions with TaxOptions for current TaxGroup
*
* @param bool $fillDefault
*
* @return TaxOption[]
*/
public function getCustomOptions(bool $fillDefault = false): array
{
if ($fillDefault && empty( $this->custom_options )) {
$this->customOptions = $this->getTaxOptions()
->with('lang')
->all();
}
return $this->customOptions;
}
/**
* Set customOptions
*
* @param TaxOption[] $value
*/
public function setCustomOptions(array $value)
{
foreach ($value as $item) {
if (!( $item instanceof TaxOption )) {
throw new InvalidValueException('All elements must be instances of ' . TaxOption::className());
}
}
$this->customOptions = $value;
}
/**
* Get default lang alias
*
* @return string
*/
public function getAlias()
{
$default_lang = Language::getDefaultLanguage();
/**
* @var TaxGroupLang $lang
*/
$lang = $this->getLang($default_lang->id)
->one();
return $lang->alias;
}
}