d8c1a2e0
Yarik
Big commit artbox
|
1
|
<?php
|
36d1807a
Yarik
Big commit.
|
2
3
4
|
namespace common\modules\product\models;
|
af036678
Yarik
Image behaviors
|
5
6
|
use common\behaviors\MultipleImgBehavior;
use common\behaviors\SaveMultipleFileBehavior;
|
d55d2fe0
Yarik
Multilanguage
|
7
|
use common\modules\language\behaviors\LanguageBehavior;
|
36d1807a
Yarik
Big commit.
|
8
9
10
|
use common\modules\rubrication\models\TaxGroup;
use common\modules\rubrication\models\TaxOption;
use Yii;
|
d55d2fe0
Yarik
Multilanguage
|
11
12
|
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
|
36d1807a
Yarik
Big commit.
|
13
|
use yii\helpers\ArrayHelper;
|
d55d2fe0
Yarik
Multilanguage
|
14
|
use yii\web\Request;
|
36d1807a
Yarik
Big commit.
|
15
|
|
d8c1a2e0
Yarik
Big commit artbox
|
16
|
/**
|
36d1807a
Yarik
Big commit.
|
17
|
* This is the model class for table "product_variant".
|
72a992f5
Yarik
Import browser v1.0
|
18
|
* @todo Refactor
|
d55d2fe0
Yarik
Multilanguage
|
19
20
|
* @property integer $product_variant_id
* @property integer $product_id
|
72a992f5
Yarik
Import browser v1.0
|
21
|
* @property integer $remote_id
|
d55d2fe0
Yarik
Multilanguage
|
22
23
24
25
26
|
* @property string $sku
* @property double $price
* @property double $price_old
* @property double $stock
* @property integer $product_unit_id
|
d55d2fe0
Yarik
Multilanguage
|
27
28
|
* @property TaxOption[] $options
* @property ProductUnit $productUnit
|
93c267f7
Yarik
Multilanguage big...
|
29
|
* @property Product $product
|
d55d2fe0
Yarik
Multilanguage
|
30
31
32
33
34
35
|
* * From language behavior *
* @property ProductVariantLang $lang
* @property ProductVariantLang[] $langs
* @property ProductVariantLang $object_lang
* @property string $ownerKey
* @property string $langKey
|
72a992f5
Yarik
Import browser v1.0
|
36
37
|
* @property ProductVariantLang[] $model_langs
* @property bool $transactionStatus
|
d55d2fe0
Yarik
Multilanguage
|
38
39
40
41
42
43
44
|
* @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 ProductVariantLang[] generateLangs()
|
72a992f5
Yarik
Import browser v1.0
|
45
46
47
48
|
* @method void loadLangs( Request $request )
* @method bool linkLangs()
* @method bool saveLangs()
* @method bool getTransactionStatus()
|
d55d2fe0
Yarik
Multilanguage
|
49
|
* * End language behavior *
|
af036678
Yarik
Image behaviors
|
50
51
52
53
54
55
56
57
|
* * From multipleImage behavior
* @property ProductImage $image
* @property ProductImage[] $images
* @method ActiveQuery getImage()
* @method ActiveQuery getImages()
* @method array getImagesConfig()
* @method array getImagesHTML( string $preset )
* * End multipleImage behavior
|
d8c1a2e0
Yarik
Big commit artbox
|
58
|
*/
|
72a992f5
Yarik
Import browser v1.0
|
59
|
class ProductVariant extends ActiveRecord
|
d8c1a2e0
Yarik
Big commit artbox
|
60
|
{
|
36d1807a
Yarik
Big commit.
|
61
|
|
36d1807a
Yarik
Big commit.
|
62
63
64
65
|
public $sum_cost;
public $product_name;
|
72a992f5
Yarik
Import browser v1.0
|
66
67
|
private $_options;
|
36d1807a
Yarik
Big commit.
|
68
|
/** @var array $_images */
|
af036678
Yarik
Image behaviors
|
69
|
public $imagesUpload = [];
|
36d1807a
Yarik
Big commit.
|
70
71
72
73
74
75
76
|
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_variant';
|
d8c1a2e0
Yarik
Big commit artbox
|
77
|
}
|
36d1807a
Yarik
Big commit.
|
78
|
|
d55d2fe0
Yarik
Multilanguage
|
79
80
81
|
public function behaviors()
{
return [
|
af036678
Yarik
Image behaviors
|
82
|
'language' => [
|
d55d2fe0
Yarik
Multilanguage
|
83
84
|
'class' => LanguageBehavior::className(),
],
|
af036678
Yarik
Image behaviors
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
'images' => [
'class' => SaveMultipleFileBehavior::className(),
'name' => 'imagesUpload',
'directory' => 'products',
'column' => 'image',
'links' => [
'product_id' => 'product_id',
'product_variant_id' => 'product_variant_id',
],
'model' => ProductImage::className(),
],
'multipleImage' => [
'class' => MultipleImgBehavior::className(),
'links' => [
'product_variant_id' => 'product_variant_id',
],
'model' => ProductImage::className(),
'config' => [
'caption' => 'image',
'delete_action' => '/product/variant/delimg',
'id' => 'product_image_id',
],
],
|
d55d2fe0
Yarik
Multilanguage
|
108
109
110
|
];
}
|
36d1807a
Yarik
Big commit.
|
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'product_id',
'product_unit_id',
],
'required',
],
[
[
'product_id',
'product_unit_id',
|
d8c1a2e0
Yarik
Big commit artbox
|
128
|
],
|
36d1807a
Yarik
Big commit.
|
129
130
131
132
133
134
135
136
137
138
139
140
|
'integer',
],
[
[
'price',
'price_old',
'stock',
],
'number',
],
[
[
|
36d1807a
Yarik
Big commit.
|
141
142
143
144
145
146
|
'sku',
],
'string',
'max' => 255,
],
[
|
36d1807a
Yarik
Big commit.
|
147
148
|
[
'options',
|
36d1807a
Yarik
Big commit.
|
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
],
'safe',
],
[
[ 'product_unit_id' ],
'exist',
'skipOnError' => true,
'targetClass' => ProductUnit::className(),
'targetAttribute' => [ 'product_unit_id' => 'product_unit_id' ],
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'product_variant_id' => Yii::t('product', 'Product Variant ID'),
'product_id' => Yii::t('product', 'Product ID'),
|
36d1807a
Yarik
Big commit.
|
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
|
'sku' => Yii::t('product', 'Sku'),
'price' => Yii::t('product', 'Price'),
'price_old' => Yii::t('product', 'Price Old'),
'stock' => Yii::t('product', 'Stock'),
'product_unit_id' => Yii::t('product', 'Product Unit ID'),
'stock_caption' => Yii::t('product', 'Stock'),
'image' => Yii::t('product', 'Image'),
'images' => Yii::t('product', 'Images'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductUnit()
{
return $this->hasOne(ProductUnit::className(), [ 'product_unit_id' => 'product_unit_id' ]);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProduct()
{
return $this->hasOne(Product::className(), [ 'product_id' => 'product_id' ]);
}
public function getProductStock()
{
return $this->hasMany(ProductStock::className(), [ 'product_variant_id' => 'product_variant_id' ]);
}
public function getQuantity()
{
return ProductStock::find()
->where([ 'product_variant_id' => $this->product_variant_id ])
->sum('quantity');
}
public function getStock_caption()
{
|
cc658b4c
Yarik
Big commit
|
211
|
return is_null($this->stock) ? '∞' : ( $this->stock > 0 ? Yii::t('product', 'Enable') : Yii::t('product', 'Disable') );
|
36d1807a
Yarik
Big commit.
|
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
}
public function getVariantStocks()
{
return $this->hasMany(ProductStock::className(), [ 'product_variant_id' => 'product_variant_id' ])
->joinWith('stock');
}
public function getStocks()
{
return $this->hasMany(Stock::className(), [ 'stock_id' => 'stock_id' ])
->viaTable(ProductStock::tableName(), [ 'product_variant_id' => 'product_variant_id' ]);
}
public function getFilters()
{
return $this->hasMany(TaxOption::className(), [ 'tax_option_id' => 'option_id' ])
->viaTable('product_variant_option', [ 'product_variant_id' => 'product_variant_id' ])
|
96410438
Yarik
Project admin com...
|
233
234
|
->joinWith('taxGroup.lang', true, 'INNER JOIN')
->joinWith('lang', true, 'INNER JOIN');
|
36d1807a
Yarik
Big commit.
|
235
236
|
}
|
36d1807a
Yarik
Big commit.
|
237
238
|
public function getFullname()
{
|
93c267f7
Yarik
Multilanguage big...
|
239
|
return empty( $this->product ) ? NULL : ( $this->product->lang->name . ( empty( $this->lang->name ) ? '' : ' ' . $this->lang->name ) );
|
36d1807a
Yarik
Big commit.
|
240
241
|
}
|
36d1807a
Yarik
Big commit.
|
242
243
|
public function setOptions($values)
{
|
72a992f5
Yarik
Import browser v1.0
|
244
|
$this->_options = $values;
|
36d1807a
Yarik
Big commit.
|
245
246
247
248
249
250
251
|
}
public function getOptions()
{
return $this->hasMany(TaxOption::className(), [ 'tax_option_id' => 'option_id' ])
->viaTable('product_variant_option', [ 'product_variant_id' => 'product_variant_id' ]);
}
|
d55d2fe0
Yarik
Multilanguage
|
252
|
|
cc658b4c
Yarik
Big commit
|
253
254
255
|
/**
* @return TaxGroup[]
*/
|
36d1807a
Yarik
Big commit.
|
256
257
258
|
public function getProperties()
{
$groups = $options = [];
|
d55d2fe0
Yarik
Multilanguage
|
259
260
261
|
foreach($this->getOptions()
->with('lang')
->all() as $option) {
|
36d1807a
Yarik
Big commit.
|
262
|
$options[ $option->tax_group_id ][] = $option;
|
d8c1a2e0
Yarik
Big commit artbox
|
263
|
}
|
36d1807a
Yarik
Big commit.
|
264
|
foreach(TaxGroup::find()
|
d55d2fe0
Yarik
Multilanguage
|
265
|
->where([ 'tax_group.tax_group_id' => array_keys($options) ])
|
cc658b4c
Yarik
Big commit
|
266
|
->orderBy([ 'sort' => SORT_ASC ])
|
d55d2fe0
Yarik
Multilanguage
|
267
|
->with('lang')
|
36d1807a
Yarik
Big commit.
|
268
269
270
271
272
273
274
|
->all() as $group) {
if(!empty( $options[ $group->tax_group_id ] )) {
$group->_options = $options[ $group->tax_group_id ];
$groups[] = $group;
}
}
return $groups;
|
d8c1a2e0
Yarik
Big commit artbox
|
275
|
}
|
36d1807a
Yarik
Big commit.
|
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
public function getId()
{
return $this->product_variant_id;
}
public function setStocks($stocks)
{
$this->stocks = (array) $stocks;
}
public function getCategory()
{
return $this->hasOne(Category::className(), [ 'category_id' => 'category_id' ])
->viaTable('product_category', [ 'product_id' => 'product_id' ]);
}
public function getCategories()
{
return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ])
->viaTable('product_category', [ 'product_id' => 'product_id' ]);
}
public function getTaxGroupsByLevel($level)
{
$categories = ArrayHelper::getColumn($this->categories, 'category_id');
return TaxGroup::find()
->distinct()
->innerJoin('tax_group_to_category', 'tax_group_to_category.tax_group_id = tax_group.tax_group_id')
->where([ 'tax_group_to_category.category_id' => $categories ])
->where([ 'level' => $level ]);
}
|
72a992f5
Yarik
Import browser v1.0
|
308
|
|
36d1807a
Yarik
Big commit.
|
309
310
|
public function afterSave($insert, $changedAttributes)
{
|
72a992f5
Yarik
Import browser v1.0
|
311
312
|
parent::afterSave($insert, $changedAttributes);
if(!empty( $this->_options )) {
|
e8ccb1b4
Yarik
Import beta
|
313
|
$options = TaxOption::findAll($this->_options);
|
72a992f5
Yarik
Import browser v1.0
|
314
315
|
$this->unlinkAll('options', true);
foreach($options as $option) {
|
e8ccb1b4
Yarik
Import beta
|
316
317
|
$this->link('options', $option);
}
|
36d1807a
Yarik
Big commit.
|
318
|
}
|
72a992f5
Yarik
Import browser v1.0
|
319
320
321
|
if(!empty( $this->stocks )) {
ProductStock::deleteAll([ 'product_variant_id' => $this->product_variant_id ]);
|
72a992f5
Yarik
Import browser v1.0
|
322
323
324
325
326
327
328
|
foreach($this->stocks as $id => $quantity) {
$productStock = ProductStock::find()
->where([
'product_variant_id' => $this->product_variant_id,
'stock_id' => $id,
])
->one();
|
e8ccb1b4
Yarik
Import beta
|
329
330
331
332
|
$productStock->quantity = $quantity;
$productStock->save();
}
}
|
36d1807a
Yarik
Big commit.
|
333
334
|
}
|
d8c1a2e0
Yarik
Big commit artbox
|
335
|
}
|