[ 'class' => LanguageBehavior::className(), ], 'timestamp' => [ 'class' => TimestampBehavior::className(), ], [ 'class' => ManyToManyBehavior::className(), ], ]; } /** * @inheritdoc */ public function rules() { return [ [ [ 'sku', ], 'required', ], [ [ 'stock', 'sort', 'image_id', ], 'integer', ], [ [ 'price', 'price_old', ], 'number', ], [ [ 'status' ], 'boolean', ], [ [ 'sku' ], 'string', 'max' => 255, ], [ [ 'variantOptionCompls', 'variantOptionExcls', ], 'safe', ], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => Yii::t('catalog', 'ID'), 'product_id' => Yii::t('catalog', 'Product ID'), 'sku' => Yii::t('catalog', 'Sku'), 'price' => Yii::t('catalog', 'Price'), 'price_old' => Yii::t('catalog', 'Price Old'), 'stock' => Yii::t('catalog', 'Stock'), 'status' => Yii::t('catalog', 'Status'), 'sort' => Yii::t('catalog', 'Sort'), 'image_id' => Yii::t('catalog', 'image_id'), 'created_at' => Yii::t('catalog', 'Created At'), 'updated_at' => Yii::t('catalog', 'Updated At'), ]; } /** * @return \yii\db\ActiveQuery */ public function getProduct() { return $this->hasOne(Product::className(), [ 'id' => 'product_id' ]) ->inverseOf('variants'); } /** * @return \yii\db\ActiveQuery */ public function getVariantLangs() { return $this->hasMany(VariantLang::className(), [ 'variant_id' => 'id' ]) ->inverseOf('variant'); } /** * @return \yii\db\ActiveQuery */ public function getLanguages() { return $this->hasMany(Language::className(), [ 'id' => 'language_id' ]) ->viaTable('variant_lang', [ 'variant_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getVariantToImages() { return $this->hasMany(VariantToImage::className(), [ 'variant_id' => 'id' ]) ->inverseOf('variant'); } /** * @return \yii\db\ActiveQuery */ public function getImages() { return $this->hasMany(Image::className(), [ 'id' => 'image_id' ]) ->viaTable('variant_to_image', [ 'variant_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getImage() { return $this->hasOne(Image::className(), [ 'id' => 'image_id' ]); } /** * @return \yii\db\ActiveQuery */ public function getVariantToVariantOptionCompls() { return $this->hasMany(VariantToVariantOptionCompl::className(), [ 'variant_id' => 'id' ]) ->inverseOf('variant'); } /** * @return \yii\db\ActiveQuery */ public function getVariantOptionCompls() { return $this->hasMany(VariantOptionCompl::className(), [ 'id' => 'variant_option_compl_id' ]) ->viaTable('variant_to_variant_option_compl', [ 'variant_id' => 'id' ]); } /** * @return \yii\db\ActiveQuery */ public function getVariantToVariantOptionExcls() { return $this->hasMany(VariantToVariantOptionExcl::className(), [ 'variant_id' => 'id' ]) ->inverseOf('variant'); } /** * @return \yii\db\ActiveQuery */ public function getVariantOptionExcls() { return $this->hasMany(VariantOptionExcl::className(), [ 'id' => 'variant_option_excl_id' ]) ->viaTable('variant_to_variant_option_excl', [ 'variant_id' => 'id' ]); } public function getStock() { if (class_exists('\artbox\stock\models\VariantToShop')) { return $this->hasMany('\artbox\stock\models\VariantToShop', [ 'variant_id' => 'id' ]) ->inverseOf('variant'); } else { return ( new Query() )->where('1 = 0'); } } public function shopCount($shop_id) { if (class_exists('\artbox\stock\models\VariantToShop')) { if ($this->isNewRecord) { return null; } $count = VariantToShop::find() ->where([ 'variant_id' => $this->id ]) ->andWhere([ 'shop_id' => $shop_id ]) ->one(); if ($count == null) { return null; } return $count->count; } else { return null; } } public function getCounts() { if (class_exists('\artbox\stock\models\VariantToShop')) { return $this->getVariantToShops() ->select( [ 'variant_id', 'sum' => 'sum(count)', ] ) ->asArray() ->groupBy('variant_id'); } else { return $this->stock; } } /** * @return \yii\db\ActiveQuery */ public function getVariantToShops() { return $this->hasMany(VariantToShop::className(), [ 'variant_id' => 'id' ]); } /** * Setter for Variant Options Complementary * * @param $value */ public function setVariantOptionCompls($value) { $this->variantOptionCompls = $value; } /** * Setter for Variant Options Exclusion * * @param $value */ public function setVariantOptionExcls($value) { $this->variantOptionExcls = $value; } /** * Sort groups for current variant and return them * * @return array */ public function getSortedGroups(): array { if (empty($this->sortedGroups)) { $productOptionCompls = $this->product->productOptionCompls; $productOptionExcls = $this->product->productOptionExcls; $variantOptionCompls = $this->variantOptionCompls; $variantOptionExcls = $this->variantOptionExcls; if (!empty($productOptionCompls)) { $this->sortedGroups[ ProductOptionGroupCompl::className() ] = []; foreach ($productOptionCompls as $option) { if (empty($this->sortedGroups[ ProductOptionGroupCompl::className() ][ $option->groupId ])) { $this->sortedGroups[ ProductOptionGroupCompl::className( ) ][ $option->groupId ] = $option->group; } $option->group->currentOptions[] = $option; } usort( $this->sortedGroups[ ProductOptionGroupCompl::className() ], function ($a, $b) { /** * @var OptionGroup $a * @var OptionGroup $b */ return $a->sort - $b->sort; } ); } if (!empty($productOptionExcls)) { $this->sortedGroups[ ProductOptionGroupExcl::className() ] = []; foreach ($productOptionExcls as $option) { if (empty($this->sortedGroups[ ProductOptionGroupExcl::className() ][ $option->groupId ])) { $this->sortedGroups[ ProductOptionGroupExcl::className( ) ][ $option->groupId ] = $option->group; } $option->group->currentOptions[] = $option; } usort( $this->sortedGroups[ ProductOptionGroupExcl::className() ], function ($a, $b) { /** * @var OptionGroup $a * @var OptionGroup $b */ return $a->sort - $b->sort; } ); } if (!empty($variantOptionCompls)) { $this->sortedGroups[ ProductOptionGroupExcl::className() ] = []; foreach ($variantOptionCompls as $option) { if (empty($this->sortedGroups[ VariantOptionGroupCompl::className() ][ $option->groupId ])) { $this->sortedGroups[ VariantOptionGroupCompl::className( ) ][ $option->groupId ] = $option->group; } $option->group->currentOptions[] = $option; } usort( $this->sortedGroups[ VariantOptionGroupCompl::className() ], function ($a, $b) { /** * @var OptionGroup $a * @var OptionGroup $b */ return $a->sort - $b->sort; } ); } if (!empty($variantOptionExcls)) { $this->sortedGroups[ ProductOptionGroupExcl::className() ] = []; foreach ($variantOptionExcls as $option) { if (empty($this->sortedGroups[ VariantOptionGroupExcl::className() ][ $option->groupId ])) { $this->sortedGroups[ VariantOptionGroupExcl::className( ) ][ $option->groupId ] = $option->group; } $option->group->currentOptions[] = $option; } usort( $this->sortedGroups[ VariantOptionGroupExcl::className() ], function ($a, $b) { /** * @var OptionGroup $a * @var OptionGroup $b */ return $a->sort - $b->sort; } ); } } return $this->sortedGroups; } public function canBuy() { return ( $this->stock && $this->price && $this->status ); } /** * @return ActiveQuery */ public static function findWithFilters() { return self::find() ->with( [ 'product' => function ($query) { /** * @var ActiveQuery $query */ $query->with( [ 'productOptionCompls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ) ->with( [ 'productOptionExcls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ); }, ] ) ->with( [ 'variantOptionCompls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ) ->with( [ 'variantOptionExcls' => function ($query) { /** * @var ActiveQuery $query */ $query->with('lang') ->with('group.lang'); }, ] ); } }