Blame view

common/modules/product/models/Product.php 20.7 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php

36d1807a   Yarik   Big commit.
2
3
4
      

      namespace common\modules\product\models;

      

5c846dcb   Alexey Boroda   -Default product'...
5
      use common\behaviors\DefaultVariantBehavior;

af036678   Yarik   Image behaviors
6
7
      use common\behaviors\MultipleImgBehavior;

      use common\behaviors\SaveMultipleFileBehavior;

36d1807a   Yarik   Big commit.
8
9
      use common\models\ProductToRating;

      use common\modules\comment\models\CommentModel;

d55d2fe0   Yarik   Multilanguage
10
      use common\modules\language\behaviors\LanguageBehavior;

36d1807a   Yarik   Big commit.
11
      use common\modules\rubrication\models\TaxGroup;

772a3ca4   Yarik   Big commit
12
      use common\modules\rubrication\models\TaxGroupToCategory;

36d1807a   Yarik   Big commit.
13
14
      use common\modules\rubrication\models\TaxOption;

      use Yii;

772a3ca4   Yarik   Big commit
15
      use yii\base\InvalidParamException;

36d1807a   Yarik   Big commit.
16
17
18
      use yii\db\ActiveQuery;

      use yii\db\ActiveRecord;

      use yii\helpers\ArrayHelper;

772a3ca4   Yarik   Big commit
19
      use yii\web\NotFoundHttpException;

d55d2fe0   Yarik   Multilanguage
20
      use yii\web\Request;

36d1807a   Yarik   Big commit.
21
      

d8c1a2e0   Yarik   Big commit artbox
22
      /**

36d1807a   Yarik   Big commit.
23
       * This is the model class for table "{{%product}}".

4428da8c   Yarik   Almost all databa...
24
       *

772a3ca4   Yarik   Big commit
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
       * @property integer              $brand_id

       * @property integer              $id

       * @property Category             $category

       * @property Category[]           $categories

       * @property ProductVariant       $variant

       * @property ProductVariant[]     $variants

       * @property ProductVariant       $productVariant

       * @property ProductVariant[]     $productVariants

       * @property boolean              $is_top

       * @property boolean              $is_new

       * @property boolean              $is_discount

       * @property ProductToRating      $averageRating

       * @property TaxGroup[]           $properties

       * @property ProductVariant       $enabledVariant

       * @property ProductVariant[]     $enabledVariants

       * @property string               $video

       * @property TaxOption[]          $options

       * @property Brand                $brand

       * @property TaxOption[]          $filters

       * @property ProductVariant[]     $variantsWithFilters

       * @property string               $remote_id

       * @property string               $fullname

       * @property float                $variantPrice

       * @property float                $enabledVariantPrice

       * @property array                $categoryNames

       * @property Stock[]              $stocks

       * @property ProductStock[]       $productStocks

       * @property int                  $quantity

       * @property TaxGroupToCategory[] $categoriesToGroups

       * @property TaxGroup[]           $taxGroupsByLevel

d55d2fe0   Yarik   Multilanguage
55
       * * From language behavior *

772a3ca4   Yarik   Big commit
56
57
58
59
60
61
62
       * @property ProductLang          $lang

       * @property ProductLang[]        $langs

       * @property ProductLang          $objectLang

       * @property string               $ownerKey

       * @property string               $langKey

       * @property ProductLang[]        $modelLangs

       * @property bool                 $transactionStatus

d55d2fe0   Yarik   Multilanguage
63
       * @method string           getOwnerKey()

af036678   Yarik   Image behaviors
64
       * @method void             setOwnerKey( string $value )

d55d2fe0   Yarik   Multilanguage
65
       * @method string           getLangKey()

af036678   Yarik   Image behaviors
66
       * @method void             setLangKey( string $value )

d55d2fe0   Yarik   Multilanguage
67
68
69
       * @method ActiveQuery      getLangs()

       * @method ActiveQuery      getLang( integer $language_id )

       * @method ProductLang[]    generateLangs()

af036678   Yarik   Image behaviors
70
       * @method void             loadLangs( Request $request )

72a992f5   Yarik   Import browser v1.0
71
72
73
       * @method bool             linkLangs()

       * @method bool             saveLangs()

       * @method bool             getTransactionStatus()

d55d2fe0   Yarik   Multilanguage
74
       * * End language behavior *

af036678   Yarik   Image behaviors
75
       * * From multipleImage behavior

772a3ca4   Yarik   Big commit
76
77
78
       * @property ProductImage         $image

       * @property ProductImage[]       $images

       * @property array                imagesConfig

af036678   Yarik   Image behaviors
79
80
81
82
83
       * @method ActiveQuery getImage()

       * @method ActiveQuery getImages()

       * @method array getImagesConfig()

       * @method array getImagesHTML( string $preset )

       * * End multipleImage behavior

d8c1a2e0   Yarik   Big commit artbox
84
       */

5c2eb7c8   Yarik   Big commit almost...
85
      class Product extends ActiveRecord

d8c1a2e0   Yarik   Big commit artbox
86
      {

36d1807a   Yarik   Big commit.
87
          

af036678   Yarik   Image behaviors
88
          public $imagesUpload = [];

36d1807a   Yarik   Big commit.
89
          

36d1807a   Yarik   Big commit.
90
91
92
93
94
95
          /**

           * @inheritdoc

           */

          public function behaviors()

          {

              return [

af036678   Yarik   Image behaviors
96
97
98
99
100
101
                  'images'        => [

                      'class'     => SaveMultipleFileBehavior::className(),

                      'name'      => 'imagesUpload',

                      'directory' => 'products',

                      'column'    => 'image',

                      'links'     => [

772a3ca4   Yarik   Big commit
102
                          'id' => 'product_id',

af036678   Yarik   Image behaviors
103
104
105
                      ],

                      'model'     => ProductImage::className(),

                  ],

af036678   Yarik   Image behaviors
106
                  'multipleImage' => [

5c2eb7c8   Yarik   Big commit almost...
107
108
                      'class'      => MultipleImgBehavior::className(),

                      'links'      => [

c70f24ea   Yarik   For Leha commit.
109
                          'product_id' => 'id',

af036678   Yarik   Image behaviors
110
                      ],

740819d4   Yarik   Images fix
111
                      'conditions' => [

4428da8c   Yarik   Almost all databa...
112
                          'product_variant_id' => null,

740819d4   Yarik   Images fix
113
                      ],

5c2eb7c8   Yarik   Big commit almost...
114
115
                      'model'      => ProductImage::className(),

                      'config'     => [

4e55ce81   Yarik   Another one admin...
116
                          'caption'       => 'image',

c70f24ea   Yarik   For Leha commit.
117
                          'delete_action' => '/product/manage/delete-image',

772a3ca4   Yarik   Big commit
118
                          'id'            => 'id',

af036678   Yarik   Image behaviors
119
120
                      ],

                  ],

af036678   Yarik   Image behaviors
121
                  'language'      => [

d55d2fe0   Yarik   Multilanguage
122
123
                      'class' => LanguageBehavior::className(),

                  ],

5c846dcb   Alexey Boroda   -Default product'...
124
                  'defaultVariant' => DefaultVariantBehavior::className(),

36d1807a   Yarik   Big commit.
125
              ];

d8c1a2e0   Yarik   Big commit artbox
126
          }

36d1807a   Yarik   Big commit.
127
128
129
130
131
132
          

          /**

           * @inheritdoc

           */

          public static function tableName()

          {

248f559f   Yarik   Import browser be...
133
              return 'product';

d8c1a2e0   Yarik   Big commit artbox
134
          }

36d1807a   Yarik   Big commit.
135
136
137
138
139
140
141
142
143
144
145
146
          

          /**

           * @inheritdoc

           */

          public function rules()

          {

              return [

                  [

                      [ 'brand_id' ],

                      'integer',

                  ],

                  [

36d1807a   Yarik   Big commit.
147
148
149
150
151
                      [

                          'categories',

                          'variants',

                          'options',

                          'imagesUpload',

36d1807a   Yarik   Big commit.
152
153
154
155
156
                      ],

                      'safe',

                  ],

                  [

                      [

36d1807a   Yarik   Big commit.
157
158
159
160
161
162
163
164
                          'video',

                      ],

                      'safe',

                  ],

                  [

                      [

                          'is_top',

                          'is_new',

4428da8c   Yarik   Almost all databa...
165
                          'is_discount',

36d1807a   Yarik   Big commit.
166
167
168
169
                      ],

                      'boolean',

                  ],

              ];

d8c1a2e0   Yarik   Big commit artbox
170
          }

36d1807a   Yarik   Big commit.
171
172
173
174
175
176
177
          

          /**

           * @inheritdoc

           */

          public function attributeLabels()

          {

              return [

4428da8c   Yarik   Almost all databa...
178
179
180
                  'id'          => Yii::t('product', 'ID'),

                  'brand_id'    => Yii::t('product', 'Brand'),

                  'categories'  => Yii::t('product', 'Categories'),

4428da8c   Yarik   Almost all databa...
181
                  'category'    => Yii::t('product', 'Category'),

4428da8c   Yarik   Almost all databa...
182
183
184
185
186
187
188
                  'image'       => Yii::t('product', 'Image'),

                  'images'      => Yii::t('product', 'Images'),

                  'video'       => Yii::t('product', 'Video embeded'),

                  'variants'    => Yii::t('product', 'Variants'),

                  'is_top'      => Yii::t('product', 'Is top'),

                  'is_new'      => Yii::t('product', 'Is new'),

                  'is_discount' => Yii::t('product', 'Is promo'),

36d1807a   Yarik   Big commit.
189
              ];

d8c1a2e0   Yarik   Big commit artbox
190
          }

36d1807a   Yarik   Big commit.
191
          

36d1807a   Yarik   Big commit.
192
          /**

772a3ca4   Yarik   Big commit
193
194
           * Get Brand query to current Product

           *

36d1807a   Yarik   Big commit.
195
196
197
198
           * @return \yii\db\ActiveQuery

           */

          public function getBrand()

          {

8af13427   Yarik   For leha commit.
199
              return $this->hasOne(Brand::className(), [ 'id' => 'brand_id' ]);

36d1807a   Yarik   Big commit.
200
          }

36d1807a   Yarik   Big commit.
201
202
          

          /**

772a3ca4   Yarik   Big commit
203
204
           * Get ProductVariant query to current Product

           *

36d1807a   Yarik   Big commit.
205
206
207
208
           * @return \yii\db\ActiveQuery

           */

          public function getVariant()

          {

8af13427   Yarik   For leha commit.
209
              return $this->hasOne(ProductVariant::className(), [ 'product_id' => 'id' ]);

36d1807a   Yarik   Big commit.
210
211
212
          }

          

          /**

772a3ca4   Yarik   Big commit
213
214
215
           * Synonim of getVariant()

           *

           * @see Product::getVariant()

36d1807a   Yarik   Big commit.
216
217
           * @return \yii\db\ActiveQuery

           */

772a3ca4   Yarik   Big commit
218
          public function getProductVariant()

36d1807a   Yarik   Big commit.
219
          {

772a3ca4   Yarik   Big commit
220
              return $this->getVariant();

36d1807a   Yarik   Big commit.
221
222
          }

          

772a3ca4   Yarik   Big commit
223
224
225
226
227
228
          /**

           * Get ProductVariants query to current Product

           *

           * @return \yii\db\ActiveQuery

           */

          public function getVariants()

36d1807a   Yarik   Big commit.
229
          {

772a3ca4   Yarik   Big commit
230
              return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'id' ]);

36d1807a   Yarik   Big commit.
231
232
          }

          

772a3ca4   Yarik   Big commit
233
234
235
236
237
238
239
          /**

           * Synonim of getVariants()

           *

           * @see Product::getVariants()

           * @return \yii\db\ActiveQuery

           */

          public function getProductVariants()

36d1807a   Yarik   Big commit.
240
          {

772a3ca4   Yarik   Big commit
241
              return $this->getVariant();

36d1807a   Yarik   Big commit.
242
243
244
          }

          

          /**

772a3ca4   Yarik   Big commit
245
246
247
           * Get ProductVariant query fetching only available in stock to current Product

           *

           * @see Product::getVariant()

36d1807a   Yarik   Big commit.
248
249
           * @return \yii\db\ActiveQuery

           */

772a3ca4   Yarik   Big commit
250
          public function getEnabledVariant()

36d1807a   Yarik   Big commit.
251
          {

772a3ca4   Yarik   Big commit
252
253
254
255
256
257
258
259
              return $this->hasOne(ProductVariant::className(), [ 'product_id' => 'id' ])

                          ->andWhere(

                              [

                                  '!=',

                                  ProductVariant::tableName() . '.stock',

                                  0,

                              ]

                          );

36d1807a   Yarik   Big commit.
260
261
          }

          

772a3ca4   Yarik   Big commit
262
263
264
265
266
267
          /**

           * Get ProductVariants query fetching only available in stock to current Product

           *

           * @see Product::getVariants()

           * @return \yii\db\ActiveQuery

           */

36d1807a   Yarik   Big commit.
268
269
          public function getEnabledVariants()

          {

8af13427   Yarik   For leha commit.
270
              return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'id' ])

772a3ca4   Yarik   Big commit
271
                          ->andWhere(

4428da8c   Yarik   Almost all databa...
272
273
274
275
276
                              [

                                  '!=',

                                  ProductVariant::tableName() . '.stock',

                                  0,

                              ]

772a3ca4   Yarik   Big commit
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
                          );

          }

          

          /**

           * Get random ProductVariant price or 0 if not exist

           *

           * @param bool $exception Whether to throw exception if variant not exist

           *

           * @return float

           * @throws \yii\web\NotFoundHttpException

           */

          public function getVariantPrice(bool $exception = false): float

          {

              if (!empty( $this->variant )) {

                  return $this->variant->price;

              } elseif ($exception) {

                  throw new NotFoundHttpException('Product with ID ' . $this->id . ' hasn\'t got variants');

              } else {

                  return 0;

              }

36d1807a   Yarik   Big commit.
297
298
          }

          

772a3ca4   Yarik   Big commit
299
300
301
302
303
304
305
306
307
          /**

           * Get random ProductVariant that in stock price or 0 or exception if not exist

           *

           * @param bool $exception Whether to throw exception if variant not exist

           *

           * @return float

           * @throws \yii\web\NotFoundHttpException

           */

          public function getEnabledVariantPrice(bool $exception = false): float

36d1807a   Yarik   Big commit.
308
          {

772a3ca4   Yarik   Big commit
309
310
311
312
313
314
315
              if (!empty( $this->enabledVariant )) {

                  return $this->enabledVariant->price;

              } elseif ($exception) {

                  throw new NotFoundHttpException('Product with ID ' . $this->id . ' hasn\'t got enabled variants');

              } else {

                  return 0;

              }

36d1807a   Yarik   Big commit.
316
317
          }

          

772a3ca4   Yarik   Big commit
318
319
320
321
322
323
          /**

           * Get Product name concatenated with Brand name

           *

           * @return string

           */

          public function getFullname():string

36d1807a   Yarik   Big commit.
324
          {

8af13427   Yarik   For leha commit.
325
              return empty( $this->brand ) ? $this->lang->title : $this->brand->lang->title . ' ' . $this->lang->title;

36d1807a   Yarik   Big commit.
326
327
          }

          

af036678   Yarik   Image behaviors
328
          /**

772a3ca4   Yarik   Big commit
329
330
331
332
333
334
335
336
337
338
339
340
341
           * Get Category query for current Product

           *

           * @return ActiveQuery

           */

          public function getCategory()

          {

              return $this->hasOne(Category::className(), [ 'id' => 'category_id' ])

                          ->viaTable('product_category', [ 'product_id' => 'id' ]);

          }

          

          /**

           * Get Categories query for current Product

           *

af036678   Yarik   Image behaviors
342
343
           * @return ActiveQuery

           */

36d1807a   Yarik   Big commit.
344
345
          public function getCategories()

          {

8af13427   Yarik   For leha commit.
346
347
              return $this->hasMany(Category::className(), [ 'id' => 'category_id' ])

                          ->viaTable('product_category', [ 'product_id' => 'id' ]);

36d1807a   Yarik   Big commit.
348
349
          }

          

772a3ca4   Yarik   Big commit
350
351
352
353
354
355
          /**

           * @param bool $index

           *

           * @return array

           */

          public function getCategoryNames(bool $index = false): array

36d1807a   Yarik   Big commit.
356
          {

772a3ca4   Yarik   Big commit
357
358
359
360
              if ($index) {

                  $result = ArrayHelper::map($this->categories, 'id', 'lang.title');

              } else {

                  $result = ArrayHelper::getColumn($this->categories, 'lang.title');

d8c1a2e0   Yarik   Big commit artbox
361
              }

36d1807a   Yarik   Big commit.
362
              return $result;

d8c1a2e0   Yarik   Big commit artbox
363
          }

36d1807a   Yarik   Big commit.
364
          

772a3ca4   Yarik   Big commit
365
366
367
368
369
          /**

           * Get ProductVariants query with lang, filters and image for current Product

           *

           * @return ActiveQuery

           */

36d1807a   Yarik   Big commit.
370
371
          public function getVariantsWithFilters()

          {

8af13427   Yarik   For leha commit.
372
              return $this->hasMany(ProductVariant::className(), [ 'product_id' => 'id' ])

772a3ca4   Yarik   Big commit
373
                          ->joinWith('lang')

4428da8c   Yarik   Almost all databa...
374
375
376
377
378
379
                          ->with(

                              [

                                  'filters',

                                  'image',

                              ]

                          );

36d1807a   Yarik   Big commit.
380
381
          }

          

36d1807a   Yarik   Big commit.
382
          /**

772a3ca4   Yarik   Big commit
383
384
           * Get TaxOptions query for current Product

           *

36d1807a   Yarik   Big commit.
385
386
           * @return ActiveQuery

           */

36d1807a   Yarik   Big commit.
387
388
          public function getOptions()

          {

c70f24ea   Yarik   For Leha commit.
389
              return $this->hasMany(TaxOption::className(), [ 'id' => 'option_id' ])

8af13427   Yarik   For leha commit.
390
                          ->viaTable('product_option', [ 'product_id' => 'id' ]);

36d1807a   Yarik   Big commit.
391
392
          }

          

4e55ce81   Yarik   Another one admin...
393
          /**

772a3ca4   Yarik   Big commit
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
           * Get TaxOptions query for current Product joined with TaxGroups

           *

           * @see Product::getOptions()

           * @return ActiveQuery

           */

          public function getFilters()

          {

              return $this->getOptions()

                          ->joinWith('taxGroup.lang')

                          ->joinWith('lang');

          }

          

          /**

           * Get all TaxGroups for current Product filled with $customOptions that satisfy current Product

           *

4e55ce81   Yarik   Another one admin...
409
410
           * @return TaxGroup[]

           */

772a3ca4   Yarik   Big commit
411
          public function getProperties(): array

36d1807a   Yarik   Big commit.
412
413
          {

              $groups = $options = [];

4428da8c   Yarik   Almost all databa...
414
415
416
              foreach ($this->getOptions()

                            ->with('lang')

                            ->all() as $option) {

c70f24ea   Yarik   For Leha commit.
417
418
419
                  /**

                   * @var TaxOption $option

                   */

772a3ca4   Yarik   Big commit
420
                  $options[ $option[ 'tax_group_id' ] ][] = $option;

d8c1a2e0   Yarik   Big commit artbox
421
              }

4428da8c   Yarik   Almost all databa...
422
              foreach (TaxGroup::find()

c70f24ea   Yarik   For Leha commit.
423
                               ->where([ 'id' => array_keys($options) ])

4428da8c   Yarik   Almost all databa...
424
425
                               ->with('lang')

                               ->all() as $group) {

c70f24ea   Yarik   For Leha commit.
426
427
428
429
                  /**

                   * @var TaxGroup $group

                   */

                  if (!empty( $options[ $group->id ] )) {

772a3ca4   Yarik   Big commit
430
                      $group->customOptions = $options[ $group->id ];

36d1807a   Yarik   Big commit.
431
                      $groups[] = $group;

d8c1a2e0   Yarik   Big commit artbox
432
                  }

d8c1a2e0   Yarik   Big commit artbox
433
              }

36d1807a   Yarik   Big commit.
434
435
436
              return $groups;

          }

          

772a3ca4   Yarik   Big commit
437
438
439
440
441
          /**

           * Get Stock query where current Product is in stock

           *

           * @return ActiveQuery

           */

36d1807a   Yarik   Big commit.
442
443
          public function getStocks()

          {

772a3ca4   Yarik   Big commit
444
445
              return $this->hasMany(Stock::className(), [ 'id' => 'stock_id' ])

                          ->via('productStocks');

36d1807a   Yarik   Big commit.
446
447
          }

          

772a3ca4   Yarik   Big commit
448
449
450
451
452
          /**

           * Get ProductStocks query for current Product

           *

           * @return ActiveQuery

           */

c70f24ea   Yarik   For Leha commit.
453
454
455
456
457
458
          public function getProductStocks()

          {

              return $this->hasMany(ProductStock::className(), [ 'product_variant_id' => 'id' ])

                          ->via('variants');

          }

          

772a3ca4   Yarik   Big commit
459
460
461
462
463
464
465
          /**

           * Get quantity of all ProductVariants for current Product

           *

           * @see Product::getProductStocks()

           * @return int

           */

          public function getQuantity():int

36d1807a   Yarik   Big commit.
466
          {

c70f24ea   Yarik   For Leha commit.
467
468
              return $this->getProductStocks()

                          ->sum('quantity');

36d1807a   Yarik   Big commit.
469
          }

af036678   Yarik   Image behaviors
470
          

772a3ca4   Yarik   Big commit
471
472
473
474
475
          /**

           * Override Categories and TaxOptions

           *

           * @inheritdoc

           */

8c4b7f44   Yarik   Import browser be...
476
477
478
          public function afterSave($insert, $changedAttributes)

          {

              parent::afterSave($insert, $changedAttributes);

af036678   Yarik   Image behaviors
479
              

4428da8c   Yarik   Almost all databa...
480
              if (!empty( $this->categories )) {

8c4b7f44   Yarik   Import browser be...
481
482
                  $categories = Category::findAll($this->categories);

                  $this->unlinkAll('categories', true);

4428da8c   Yarik   Almost all databa...
483
                  foreach ($categories as $category) {

8c4b7f44   Yarik   Import browser be...
484
485
486
                      $this->link('categories', $category);

                  }

              }

af036678   Yarik   Image behaviors
487
              

4428da8c   Yarik   Almost all databa...
488
              if (!empty( $this->options )) {

8c4b7f44   Yarik   Import browser be...
489
                  $options = TaxOption::findAll($this->options);

af036678   Yarik   Image behaviors
490
                  $this->unlinkAll('options', true);

4428da8c   Yarik   Almost all databa...
491
                  foreach ($options as $option) {

8c4b7f44   Yarik   Import browser be...
492
493
494
                      $this->link('options', $option);

                  }

              }

d8c1a2e0   Yarik   Big commit artbox
495
          }

36d1807a   Yarik   Big commit.
496
          

772a3ca4   Yarik   Big commit
497
498
499
500
501
502
503
          /**

           * Recalculate rating for artboxcomment module

           *

           * @todo Rewrite with behavior

           * @return bool

           */

          public function recalculateRating():bool

36d1807a   Yarik   Big commit.
504
505
506
507
508
509
510
511
          {

              /**

               * @var ProductToRating $averageRating

               */

              $average = $this->getComments()

                              ->joinWith('rating')

                              ->select([ 'average' => 'avg(artbox_comment_rating.value)::float' ])

                              ->scalar();

4428da8c   Yarik   Almost all databa...
512
              if (!$average) {

36d1807a   Yarik   Big commit.
513
514
515
                  $average = 0;

              }

              $averageRating = $this->averageRating;

4428da8c   Yarik   Almost all databa...
516
              if (!empty( $averageRating )) {

36d1807a   Yarik   Big commit.
517
518
                  $averageRating->value = $average;

              } else {

4428da8c   Yarik   Almost all databa...
519
520
521
522
523
524
                  $averageRating = new ProductToRating(

                      [

                          'product_id' => $this->id,

                          'value'      => $average,

                      ]

                  );

36d1807a   Yarik   Big commit.
525
              }

4428da8c   Yarik   Almost all databa...
526
              if ($averageRating->save()) {

36d1807a   Yarik   Big commit.
527
528
529
                  return true;

              } else {

                  return false;

d8c1a2e0   Yarik   Big commit artbox
530
531
              }

          }

36d1807a   Yarik   Big commit.
532
          

772a3ca4   Yarik   Big commit
533
534
535
536
537
538
          /**

           * Get CommmentModel query for artboxcomment module

           *

           * @todo Rewrite with behavior

           * @return ActiveQuery

           */

36d1807a   Yarik   Big commit.
539
540
          public function getComments()

          {

8af13427   Yarik   For leha commit.
541
              return $this->hasMany(CommentModel::className(), [ 'entity_id' => 'id' ])

4428da8c   Yarik   Almost all databa...
542
543
544
545
546
547
548
                          ->where(

                              [

                                  'artbox_comment.entity'             => self::className(),

                                  'artbox_comment.status'             => CommentModel::STATUS_ACTIVE,

                                  'artbox_comment.artbox_comment_pid' => null,

                              ]

                          );

d8c1a2e0   Yarik   Big commit artbox
549
          }

36d1807a   Yarik   Big commit.
550
          

772a3ca4   Yarik   Big commit
551
552
553
554
555
          /**

           * Get ProductToRating query in order to get average rating for current Product

           *

           * @return \yii\db\ActiveQuery

           */

36d1807a   Yarik   Big commit.
556
557
          public function getAverageRating()

          {

8af13427   Yarik   For leha commit.
558
              return $this->hasOne(ProductToRating::className(), [ 'product_id' => 'id' ]);

d8c1a2e0   Yarik   Big commit artbox
559
          }

36d1807a   Yarik   Big commit.
560
          

772a3ca4   Yarik   Big commit
561
562
563
564
565
566
          /**

           * Get TaxGroupToCategories query via product_category table

           *

           * @return ActiveQuery

           */

          public function getCategoriesToGroups()

36d1807a   Yarik   Big commit.
567
          {

772a3ca4   Yarik   Big commit
568
569
              return $this->hasMany(TaxGroupToCategory::className(), [ 'category_id' => 'category_id' ])

                          ->viaTable('product_category', [ 'product_id' => 'id' ]);

36d1807a   Yarik   Big commit.
570
571
          }

          

772a3ca4   Yarik   Big commit
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
          /**

           * Get TaxGroups query for current Product according to level

           * * 0 - Product Tax Groups

           * * 1 - ProductVariant Tax Groups

           *

           * @param int $level

           *

           * @return ActiveQuery

           * @throws InvalidParamException

           */

          public function getTaxGroupsByLevel(int $level = 0)

          {

              if ($level !== 0 && $level !== 1) {

                  throw new InvalidParamException(

                      'Level must be 0 for Product Tax Groups or 1 for Product Variant Tax Groups'

                  );

              }

              return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ])

                          ->via('categoriesToGroups')

                          ->where([ 'level' => $level ])

                          ->distinct();

          }

          

          /**

           * Setter for Categories

           *

           * @param array $values

           */

36d1807a   Yarik   Big commit.
600
601
602
603
604
          public function setCategories($values)

          {

              $this->categories = $values;

          }

          

772a3ca4   Yarik   Big commit
605
606
607
608
609
          /**

           * Setter for Options

           *

           * @param array $values

           */

36d1807a   Yarik   Big commit.
610
611
612
613
          public function setOptions($values)

          {

              $this->options = $values;

          }

d8c1a2e0   Yarik   Big commit artbox
614
      }