This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Product.php
540 lines (493 loc) · 15.6 KB
/
Product.php
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php
namespace xalberteinsteinx\shop\common\entities;
use xalberteinsteinx\shop\common\components\user\models\Profile;
use xalberteinsteinx\shop\models\Order;
use xalberteinsteinx\shop\common\components\user\models\UserGroup;
use xalberteinsteinx\shop\helpers\ShopArrayHelper;
use bl\multilang\behaviors\TranslationBehavior;
use bl\multilang\entities\Language;
use dektrium\user\models\User;
use Exception;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\{
ActiveRecord, Expression
};
use yii\helpers\Url;
use yii2tech\ar\position\PositionBehavior;
/**
* This is the model class for table "shop_product".
* @author Albert Gainutdinov
*
* @property integer $id
* @property integer $position
* @property integer $category_id
* @property integer $vendor_id
* @property integer $country_id
* @property string $sku
* @property string $creation_time
* @property string $update_time
* @property integer $status
* @property integer $availability
* @property integer $owner
* @property boolean $sale
* @property integer $views
* @property boolean $popular
* @property boolean $new
* @property integer $price_id
* @property integer $number
* @property boolean $show
* @property integer $shows
*
* @property Category $category
* @property RelatedProduct $relatedProducts
* @property Param[] $params
* @property ProductCountry $productCountry
* @property Vendor $vendor
* @property ProductAvailability $productAvailability
* @property Order[] $orders Gets orders, which have this product
* @property integer $orderedNumber Gets number of units that have already bought.
* @property User $productOwner
* @property ProductPrice $productPrice
* @property Profile $ownerProfile
* @property Price $price Gets price for current user group.
* @property Price[] $prices Gets all product prices
* @property float $oldPrice
* @property float $discountPrice
* @property ProductFile[] $files
* @property ProductImage[] $images Gets all product images
* @property ProductImage $image Gets one product image
* @property ProductVideo[] $videos
* @property ProductTranslation $translation
* @property ProductTranslation[] $translations
* @property Combination[] $shopCombinations
* @property Combination $defaultCombination
* @property Combination[] $combinations
* @property ShopAttribute[] $productAttributes
* @property boolean $isFavorite
* @property ProductAdditionalProduct[] $productAdditionalProducts
*
*/
class Product extends ActiveRecord
{
/**
* Constants for status column
*/
const STATUS_ON_MODERATION = 1;
const STATUS_DECLINED = 2;
const STATUS_SUCCESS = 10;
/**
* This properties are used for the Cart component.
*/
public $count;
public $combinationId;
public $additionalProducts;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'shop_product';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['position', 'category_id', 'vendor_id', 'country_id', 'status', 'availability', 'owner', 'views', 'price_id', 'number', 'shows'], 'integer'],
[['category_id', 'vendor_id', 'country_id', 'availability', 'number'], 'filter', 'filter' => function($value) {
$value = (!empty($value)) ? (int)$value : NULL;
return $value;
}],
[['sale', 'popular'], 'filter', 'filter' => function($value) {
$value = (int)$value;
return $value;
}],
[['sale', 'popular', 'new', 'show'], 'boolean'],
[['creation_time', 'update_time'], 'safe'],
[['sku'], 'string', 'max' => 255],
[['sku'], 'trim'],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
[['country_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductCountry::className(), 'targetAttribute' => ['country_id' => 'id']],
[['vendor_id'], 'exist', 'skipOnError' => true, 'targetClass' => Vendor::className(), 'targetAttribute' => ['vendor_id' => 'id']],
[['availability'], 'exist', 'skipOnError' => true, 'targetClass' => ProductAvailability::className(), 'targetAttribute' => ['availability' => 'id']],
[['owner'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['owner' => 'id']],
[['price_id'], 'exist', 'skipOnError' => true, 'targetClass' => Price::className(), 'targetAttribute' => ['price_id' => 'id']],
];
}
public function fields()
{
return [
'id',
'position',
'category_id',
'vendor_id',
'country_id',
'sku',
'creation_time',
// 'update_time',
'status',
'availability',
// 'owner',
'views',
'new',
'sale',
'popular',
//'price_id',
'number',
'show',
'translations',
'params',
// 'productAvailability',
'productPrices',
'images',
'combinations',
'productAdditionalProducts'
];
}
public function behaviors()
{
return [
'translation' => [
'class' => TranslationBehavior::className(),
'translationClass' => ProductTranslation::className(),
'relationColumn' => 'product_id'
],
'positionBehavior' => [
'class' => PositionBehavior::className(),
'positionAttribute' => 'position',
'groupAttributes' => [
'category_id'
],
],
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['creation_time', 'update_time'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
],
'value' => new Expression('NOW()'),
],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('shop', 'ID'),
'position' => Yii::t('shop', 'Position'),
'category_id' => Yii::t('shop', 'Category'),
'vendor_id' => Yii::t('shop', 'Vendor'),
'country_id' => Yii::t('shop', 'Country'),
'sku' => Yii::t('shop', 'Sku'),
'creation_time' => Yii::t('shop', 'Creation Time'),
'update_time' => Yii::t('shop', 'Update Time'),
'status' => Yii::t('shop', 'Status'),
'availability' => Yii::t('shop', 'Availability'),
'owner' => Yii::t('shop', 'Owner'),
'sale' => Yii::t('shop', 'Sale'),
'new' => Yii::t('shop', 'New'),
'views' => Yii::t('shop', 'Views'),
'popular' => Yii::t('shop', 'Popular'),
'price_id' => Yii::t('shop', 'Price'),
'number' => Yii::t('shop', 'Number'),
'show' => Yii::t('shop', 'Show'),
'shows' => Yii::t('shop', 'Shows'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getParams()
{
return $this->hasMany(Param::className(), ['product_id' => 'id'])->orderBy('position');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductCountry()
{
return $this->hasOne(ProductCountry::className(), ['id' => 'country_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getVendor()
{
return $this->hasOne(Vendor::className(), ['id' => 'vendor_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductAvailability()
{
return $this->hasOne(ProductAvailability::className(), ['id' => 'availability']);
}
/**
* Gets orders, which have this product
* @return \yii\db\ActiveQuery
*/
public function getOrders()
{
$orders = Order::find()->joinWith('orderProducts')->where(['product_id' => $this->id])->all();
return $orders;
}
/**
* Gets number of units that have already bought.
* @return int|mixed
*/
public function getOrderedNumber()
{
$orderedProductsNumber = Order::find()->select('count')->joinWith('orderProducts')->where(['product_id' => $this->id])->sum('count');
return $orderedProductsNumber ?? 0;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductOwner()
{
return $this->hasOne(User::className(), ['id' => 'owner']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRelatedProducts()
{
return $this->hasMany(RelatedProduct::className(), ['product_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getRelatedProductsWhereItRelated()
{
return $this->hasMany(RelatedProduct::className(), ['related_product_id' => 'id']);
}
/**
* Gets user profile, which created product.
* @return \yii\db\ActiveQuery
*/
public function getOwnerProfile()
{
return $this->hasOne(Profile::className(), ['user_id' => 'owner']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductPrices() {
return $this->hasMany(ProductPrice::className(), ['product_id' => 'id']);
}
/**
* Gets prices for current user group
* @return mixed
*/
public function getPrice()
{
$user_group_id = (\Yii::$app->user->isGuest) ? UserGroup::USER_GROUP_ALL_USERS :
\Yii::$app->user->identity->user_group_id;
$productPrice = ProductPrice::find()
->where(['product_id' => $this->id, 'user_group_id' => $user_group_id])->one();
return $productPrice->price ?? 0;
}
/**
* @param $userGroupId
* @return mixed
* @throws Exception
*/
public function getPriceByUserGroup($userGroupId)
{
if (!empty($userGroupId)) {
$productPrice = ProductPrice::find()
->where(['product_id' => $this->id, 'user_group_id' => $userGroupId])
->one();
if (!empty($productPrice)) return $productPrice->price;
else return false;
}
else throw new Exception('User group id is empty');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getPrices()
{
$productPrice = ProductPrice::findOne($this->price_id);
$prices = $productPrice->prices;
return $prices;
}
/**
* @return float|int
*/
public function getOldPrice()
{
$price = $this->price;
if (!empty($price)) {
$oldPrice = $price->oldPrice;
}
else $oldPrice = 0;
return $oldPrice;
}
/**
* Gets price with discount
* @return float|int
* @throws Exception
*/
public function getDiscountPrice()
{
$price = $this->price;
if (!empty($price)) {
$discountPrice = $price->discountPrice;
}
else $discountPrice = 0;
return $discountPrice;
}
/**
* @return \yii\db\ActiveQuery
*/
public function getFiles()
{
return $this->hasMany(ProductFile::className(), ['product_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getImages()
{
return $this->hasMany(ProductImage::className(), ['product_id' => 'id'])->orderBy('position');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getImage()
{
return $this->hasOne(ProductImage::className(), ['product_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getVideos()
{
return $this->hasMany(ProductVideo::className(), ['product_id' => 'id']);
}
/**
* @param int|null $languageId
* @return \yii\db\ActiveQuery
*/
public function getTranslation($languageId = null)
{
return $this->hasOne(ProductTranslation::className(), ['product_id' => 'id'])
->andOnCondition(['language_id' => $languageId ?? Language::getCurrent()->id]);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTranslations()
{
return $this->hasMany(ProductTranslation::className(), ['product_id' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCombinations()
{
return $this->hasMany(Combination::className(), ['product_id' => 'id']);
}
/**
* @return bool
*/
public function hasCombinations()
{
$count = Combination::find()->where(['product_id' => $this->id])->count();
return (boolean) $count;
}
/**
* @param $id
* @return Combination
* @throws \yii\base\Exception
*/
public function getCombination($id)
{
if (!empty($id)) {
$combination = Combination::findOne($id);
if (!empty($combination))
return $combination;
else
throw new \yii\base\Exception('Combination does not exists');
}
else
throw new \yii\base\Exception('Combination id can not be empty');
}
/**
* @return \yii\db\ActiveQuery
*/
public function getDefaultCombination()
{
return $this->hasOne(Combination::className(), ['product_id' => 'id'])
->where(['default' => true]);
}
/**
* Gets attributes, which used in product combinations
* @return ShopAttribute[]
*/
public function getProductAttributes()
{
$attributes = [];
foreach ($this->combinations as $combination) {
if (!empty($combination->combinationAttributes)) {
foreach ($combination->combinationAttributes as $combinationAttribute) {
$attributes[] = $combinationAttribute->productAttribute;
}
}
}
$attributes = ShopArrayHelper::removeDuplicatedArrayElements($attributes);
return $attributes;
}
/**
* Return true if product has added to favorites already or false if not.
* @return boolean
*/
public function isFavorite()
{
$favoriteProduct = FavoriteProduct::find()
->where(['product_id' => $this->id, 'user_id' => \Yii::$app->user->id])->one();
if (!empty($favoriteProduct)) return true;
else return false;
}
/**
* Generates unique name by string.
* @param $baseName
* @return string
*/
public static function generateImageName($baseName)
{
$fileName = hash('crc32', $baseName . time());
if (file_exists(Yii::getAlias('@frontend/web/images/shop/' . $fileName . '-original.jpg'))) {
return static::generateImageName($baseName);
}
return $fileName;
}
/**
* @return string
*/
public function getUrl()
{
$url = '/' . Yii::$app->controller->module->id . '/product/show';
return Url::to([$url, 'id' => $this->id]);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getProductAdditionalProducts() {
return $this->hasMany(ProductAdditionalProduct::className(), ['product_id' => 'id'])
->joinWith('additionalProduct')
->orderBy('position');
}
}