8af13427
Yarik
For leha commit.
|
1
2
3
4
5
6
7
8
9
10
|
<?php
namespace common\modules\product\models;
use common\behaviors\MultipleImgBehavior;
use common\behaviors\SaveMultipleFileBehavior;
use common\modules\language\behaviors\LanguageBehavior;
use common\modules\rubrication\models\TaxGroup;
use common\modules\rubrication\models\TaxOption;
use Yii;
|
772a3ca4
Yarik
Big commit
|
11
|
use yii\base\InvalidParamException;
|
8af13427
Yarik
For leha commit.
|
12
13
|
use yii\db\ActiveQuery;
use yii\db\ActiveRecord;
|
8af13427
Yarik
For leha commit.
|
14
15
16
17
18
|
use yii\web\Request;
/**
* This is the model class for table "product_variant".
*
|
4428da8c
Yarik
Almost all databa...
|
19
|
* @property integer $id
|
8af13427
Yarik
For leha commit.
|
20
21
22
23
24
25
26
|
* @property integer $product_id
* @property integer $remote_id
* @property string $sku
* @property double $price
* @property double $price_old
* @property double $stock
* @property integer $product_unit_id
|
772a3ca4
Yarik
Big commit
|
27
|
* @property string $fullname
|
8af13427
Yarik
For leha commit.
|
28
29
30
31
|
* @property TaxOption[] $options
* @property ProductUnit $productUnit
* @property Product $product
* @property Category[] $categories
|
772a3ca4
Yarik
Big commit
|
32
|
* @property Category $category
|
c70f24ea
Yarik
For Leha commit.
|
33
|
* @property TaxOption[] $filters
|
772a3ca4
Yarik
Big commit
|
34
35
36
37
38
39
|
* @property ProductStock[] $productStocks
* @property int $quantity
* @property ProductStock[] $variantStocks
* @property Stock[] $stocks
* @property TaxGroup[] $properties
* @property TaxGroup[] $taxGroupsByLevel
|
8af13427
Yarik
For leha commit.
|
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
|
* * From language behavior *
* @property ProductVariantLang $lang
* @property ProductVariantLang[] $langs
* @property ProductVariantLang $objectLang
* @property string $ownerKey
* @property string $langKey
* @property ProductVariantLang[] $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 ProductVariantLang[] generateLangs()
* @method void loadLangs( Request $request )
* @method bool linkLangs()
* @method bool saveLangs()
* @method bool getTransactionStatus()
* * End language behavior *
* * From multipleImage behavior
* @property ProductImage $image
* @property ProductImage[] $images
* @property array $imagesConfig
* @method ActiveQuery getImage()
* @method ActiveQuery getImages()
* @method array getImagesConfig()
* @method array getImagesHTML( string $preset )
* * End multipleImage behavior
*/
class ProductVariant extends ActiveRecord
{
|
772a3ca4
Yarik
Big commit
|
72
73
74
|
/**
* @var int[] $options
*/
|
8af13427
Yarik
For leha commit.
|
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
|
private $options;
/** @var array $_images */
public $imagesUpload = [];
/**
* @var array $stocks
*/
protected $stocks = [];
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product_variant';
}
public function behaviors()
{
return [
'language' => [
'class' => LanguageBehavior::className(),
],
'images' => [
'class' => SaveMultipleFileBehavior::className(),
'name' => 'imagesUpload',
'directory' => 'products',
'column' => 'image',
'links' => [
|
c70f24ea
Yarik
For Leha commit.
|
105
|
'product_id' => 'product_id',
|
772a3ca4
Yarik
Big commit
|
106
|
'id' => 'product_variant_id',
|
8af13427
Yarik
For leha commit.
|
107
108
109
110
111
112
|
],
'model' => ProductImage::className(),
],
'multipleImage' => [
'class' => MultipleImgBehavior::className(),
'links' => [
|
c70f24ea
Yarik
For Leha commit.
|
113
|
'product_variant_id' => 'id',
|
8af13427
Yarik
For leha commit.
|
114
115
116
117
|
],
'model' => ProductImage::className(),
'config' => [
'caption' => 'image',
|
c70f24ea
Yarik
For Leha commit.
|
118
|
'delete_action' => '/product/variant/delete-image',
|
772a3ca4
Yarik
Big commit
|
119
|
'id' => 'id',
|
8af13427
Yarik
For leha commit.
|
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
|
],
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[
[
'product_id',
'product_unit_id',
],
'required',
],
[
[
'product_id',
'product_unit_id',
],
'integer',
],
[
[
'price',
'price_old',
'stock',
],
'number',
],
[
[
'sku',
],
'string',
'max' => 255,
],
[
[
'options',
],
'safe',
],
[
[ 'product_unit_id' ],
'exist',
'skipOnError' => true,
'targetClass' => ProductUnit::className(),
|
4428da8c
Yarik
Almost all databa...
|
171
|
'targetAttribute' => [ 'product_unit_id' => 'id' ],
|
8af13427
Yarik
For leha commit.
|
172
|
],
|
772a3ca4
Yarik
Big commit
|
173
174
175
176
177
178
179
|
[
[ 'product_id' ],
'exist',
'skipOnError' => true,
'targetClass' => Product::className(),
'targetAttribute' => [ 'product_id' => 'id' ],
],
|
8af13427
Yarik
For leha commit.
|
180
181
182
183
184
185
186
187
188
|
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
|
4428da8c
Yarik
Almost all databa...
|
189
190
191
192
193
194
195
196
197
198
|
'id' => Yii::t('product', 'Product Variant ID'),
'product_id' => Yii::t('product', 'Product ID'),
'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'),
|
8af13427
Yarik
For leha commit.
|
199
200
201
202
203
204
205
206
|
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductUnit()
{
|
4428da8c
Yarik
Almost all databa...
|
207
|
return $this->hasOne(ProductUnit::className(), [ 'id' => 'product_unit_id' ]);
|
8af13427
Yarik
For leha commit.
|
208
209
210
211
212
213
214
215
216
217
|
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProduct()
{
return $this->hasOne(Product::className(), [ 'id' => 'product_id' ]);
}
|
772a3ca4
Yarik
Big commit
|
218
219
220
221
|
/**
* @return \yii\db\ActiveQuery
*/
public function getProductStocks()
|
8af13427
Yarik
For leha commit.
|
222
|
{
|
4428da8c
Yarik
Almost all databa...
|
223
|
return $this->hasMany(ProductStock::className(), [ 'product_variant_id' => 'id' ]);
|
8af13427
Yarik
For leha commit.
|
224
225
|
}
|
772a3ca4
Yarik
Big commit
|
226
227
228
229
230
231
232
233
234
|
/**
* Get qunatity for current ProductVariant
* If $recalculate set to true will recalculate stock via product_stock table
*
* @param bool $recalculate
*
* @return int
*/
public function getQuantity(bool $recalculate = false): int
|
8af13427
Yarik
For leha commit.
|
235
|
{
|
772a3ca4
Yarik
Big commit
|
236
237
238
239
240
241
242
243
244
245
246
247
248
|
if (!$recalculate) {
return $this->stock;
} else {
$quantity = $this->getProductStocks()
->sum('quantity');
if (empty( $quantity )) {
$this->stock = 0;
} else {
$this->stock = (int) $quantity;
}
$this->save(false, [ 'stock' ]);
return $this->stock;
}
|
8af13427
Yarik
For leha commit.
|
249
250
|
}
|
772a3ca4
Yarik
Big commit
|
251
252
253
254
255
256
|
/**
* Get ProductStocks query woth preloaded Stocks for current ProductVariant
* **Used in dynamic fields in product variant form**
*
* @return ActiveQuery
*/
|
8af13427
Yarik
For leha commit.
|
257
258
|
public function getVariantStocks()
{
|
772a3ca4
Yarik
Big commit
|
259
|
return $this->getProductStocks()
|
8af13427
Yarik
For leha commit.
|
260
261
262
|
->joinWith('stock');
}
|
772a3ca4
Yarik
Big commit
|
263
264
265
|
/**
* @return ActiveQuery
*/
|
8af13427
Yarik
For leha commit.
|
266
267
|
public function getStocks()
{
|
4428da8c
Yarik
Almost all databa...
|
268
|
return $this->hasMany(Stock::className(), [ 'id' => 'stock_id' ])
|
772a3ca4
Yarik
Big commit
|
269
|
->via('productStocks');
|
8af13427
Yarik
For leha commit.
|
270
271
|
}
|
772a3ca4
Yarik
Big commit
|
272
273
274
275
|
/**
* @return ActiveQuery
*/
public function getOptions()
|
8af13427
Yarik
For leha commit.
|
276
|
{
|
c70f24ea
Yarik
For Leha commit.
|
277
|
return $this->hasMany(TaxOption::className(), [ 'id' => 'option_id' ])
|
772a3ca4
Yarik
Big commit
|
278
|
->viaTable('product_variant_option', [ 'product_variant_id' => 'id' ]);
|
8af13427
Yarik
For leha commit.
|
279
280
|
}
|
772a3ca4
Yarik
Big commit
|
281
282
283
284
285
286
|
/**
* Get TaxOptions with preloaded TaxGroups for current ProductVariant
*
* @return ActiveQuery
*/
public function getFilters()
|
8af13427
Yarik
For leha commit.
|
287
|
{
|
772a3ca4
Yarik
Big commit
|
288
289
290
|
return $this->getOptions()
->joinWith('taxGroup.lang')
->joinWith('lang');
|
8af13427
Yarik
For leha commit.
|
291
292
|
}
|
772a3ca4
Yarik
Big commit
|
293
294
295
296
297
298
|
/**
* Get Product title concanated with current ProductVariant title
*
* @return string
*/
public function getFullname(): string
|
8af13427
Yarik
For leha commit.
|
299
|
{
|
772a3ca4
Yarik
Big commit
|
300
|
return $this->product->lang->title . ' ' . $this->lang->title;
|
8af13427
Yarik
For leha commit.
|
301
302
|
}
|
772a3ca4
Yarik
Big commit
|
303
304
305
306
307
308
|
/**
* Set Options to override previous
*
* @param int[] $values
*/
public function setOptions($values)
|
8af13427
Yarik
For leha commit.
|
309
|
{
|
772a3ca4
Yarik
Big commit
|
310
|
$this->options = $values;
|
8af13427
Yarik
For leha commit.
|
311
312
313
|
}
/**
|
772a3ca4
Yarik
Big commit
|
314
315
|
* Get all TaxGroups for current ProductVariant filled with $customOptions that satisfy current ProductVariant
*
|
8af13427
Yarik
For leha commit.
|
316
317
318
319
320
321
322
323
|
* @return TaxGroup[]
*/
public function getProperties()
{
$groups = $options = [];
foreach ($this->getOptions()
->with('lang')
->all() as $option) {
|
c70f24ea
Yarik
For Leha commit.
|
324
325
326
|
/**
* @var TaxOption $option
*/
|
8af13427
Yarik
For leha commit.
|
327
328
329
|
$options[ $option->tax_group_id ][] = $option;
}
foreach (TaxGroup::find()
|
c70f24ea
Yarik
For Leha commit.
|
330
|
->where([ 'tax_group.id' => array_keys($options) ])
|
8af13427
Yarik
For leha commit.
|
331
332
333
|
->orderBy([ 'sort' => SORT_ASC ])
->with('lang')
->all() as $group) {
|
c70f24ea
Yarik
For Leha commit.
|
334
335
336
337
|
/**
* @var TaxGroup $group
*/
if (!empty( $options[ $group->id ] )) {
|
772a3ca4
Yarik
Big commit
|
338
|
$group->customOptions = $options[ $group->id ];
|
8af13427
Yarik
For leha commit.
|
339
340
341
342
343
344
|
$groups[] = $group;
}
}
return $groups;
}
|
8af13427
Yarik
For leha commit.
|
345
|
/**
|
772a3ca4
Yarik
Big commit
|
346
|
* Set stocks to override existing in product_stock table
|
8af13427
Yarik
For leha commit.
|
347
348
349
350
351
352
353
354
|
*
* @param mixed $stocks
*/
public function setStocks($stocks)
{
$this->stocks = (array) $stocks;
}
|
772a3ca4
Yarik
Big commit
|
355
356
357
|
/**
* @return ActiveQuery
*/
|
8af13427
Yarik
For leha commit.
|
358
359
360
361
362
363
|
public function getCategory()
{
return $this->hasOne(Category::className(), [ 'id' => 'category_id' ])
->viaTable('product_category', [ 'product_id' => 'product_id' ]);
}
|
772a3ca4
Yarik
Big commit
|
364
365
366
|
/**
* @return ActiveQuery
*/
|
8af13427
Yarik
For leha commit.
|
367
368
369
370
371
372
|
public function getCategories()
{
return $this->hasMany(Category::className(), [ 'id' => 'category_id' ])
->viaTable('product_category', [ 'product_id' => 'product_id' ]);
}
|
772a3ca4
Yarik
Big commit
|
373
374
375
376
377
378
379
380
381
382
383
|
/**
* Get TaxGroups query for current ProductVariant according to level
* * 0 - Product Tax Groups
* * 1 - ProductVariant Tax Groups
*
* @param int $level
*
* @return ActiveQuery
* @throws InvalidParamException
*/
public function getTaxGroupsByLevel(int $level = 0)
|
8af13427
Yarik
For leha commit.
|
384
|
{
|
772a3ca4
Yarik
Big commit
|
385
|
return $this->product->getTaxGroupsByLevel($level);
|
8af13427
Yarik
For leha commit.
|
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
}
public function afterSave($insert, $changedAttributes)
{
parent::afterSave($insert, $changedAttributes);
if (!empty( $this->options )) {
$options = TaxOption::findAll($this->options);
$this->unlinkAll('options', true);
foreach ($options as $option) {
$this->link('options', $option);
}
}
if (!empty( $this->stocks )) {
|
4428da8c
Yarik
Almost all databa...
|
400
|
ProductStock::deleteAll([ 'product_variant_id' => $this->id ]);
|
8af13427
Yarik
For leha commit.
|
401
402
403
404
405
406
407
|
foreach ($this->stocks as $id => $quantity) {
/**
* @var ProductStock $productStock
*/
$productStock = ProductStock::find()
->where(
[
|
4428da8c
Yarik
Almost all databa...
|
408
|
'product_variant_id' => $this->id,
|
8af13427
Yarik
For leha commit.
|
409
410
411
412
413
414
415
416
417
418
|
'stock_id' => $id,
]
)
->one();
$productStock->quantity = $quantity;
$productStock->save();
}
}
}
}
|