function ($model) { return !$model->id_point_sale && !$model->id_user_group && !$model->from_quantity; }, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente, un groupe d\'utilisateur ou une quantité' ], [ 'id_point_sale', 'required', 'when' => function ($model) { return !$model->id_user && !$model->id_user_group && !$model->from_quantity; }, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente, un groupe d\'utilisateur ou une quantité' ], [ 'id_user_group', 'required', 'when' => function ($model) { return !$model->id_user && !$model->id_point_sale && !$model->from_quantity; }, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente, un groupe d\'utilisateur ou une quantité' ], [ 'from_quantity', 'required', 'when' => function ($model) { return !$model->id_user && !$model->id_user_group && !$model->id_point_sale; }, 'message' => 'Vous devez renseigner au moins un utilisateur, un point de vente, un groupe d\'utilisateur ou une quantité' ], [['id_product', 'price'], 'required'], [['id_product', 'id_user', 'id_point_sale', 'id_user_group', 'percent'], 'integer'], [['price', 'from_quantity'], 'double'], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'id_product' => 'Produit', 'id_user' => 'Utilisateur', 'id_point_sale' => 'Point de vente', 'id_user_group' => "Groupe d'utilisateur", 'price' => 'Prix (HT)', 'percent' => 'Pourcentage', 'from_quantity' => 'À partir de la quantité', ]; } /* * Relations */ public function getProduct() { return $this->hasOne( Product::className(), ['id' => 'id_product'] ); } public function getPointSale() { return $this->hasOne( PointSale::className(), ['id' => 'id_point_sale'] ); } public function getUserGroup() { return $this->hasOne( UserGroup::className(), ['id' => 'id_user_group'] ); } public function getUser() { return $this->hasOne( User::className(), ['id' => 'id_user'] ); } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => ['user', 'pointSale'], 'join_with' => ['product'], 'orderby' => '', 'attribute_id_producer' => 'product.id_producer' ]; } public static function percentValues() { $percentValues = [ '' => 'Aucun' ]; for ($i = -50; $i < 51; $i = $i + 5) { $percentValues[$i] = $i . ' %'; } return $percentValues; } public static function hasMatchOfType($specificPriceArray, $typeMatch, $user, $pointSale) { foreach($specificPriceArray as $specificPrice) { if($specificPrice->$typeMatch($user, $pointSale)) { return true; } } return false; } public static function getPriorityMatchOfSpecificPriceArray($specificPriceArray, $user, $pointSale) { if(self::hasMatchOfType($specificPriceArray, 'matchUser', $user, $pointSale)) { return 'matchUser'; } if(self::hasMatchOfType($specificPriceArray, 'matchUserGroup', $user, $pointSale)) { return 'matchUserGroup'; } if(self::hasMatchOfType($specificPriceArray, 'matchPointSale', $user, $pointSale)) { return 'matchPointSale'; } if(self::hasMatchOfType($specificPriceArray, 'matchUserPointSale', $user, $pointSale)) { return 'matchUserPointSale'; } return null; } public function matchUser($user, $pointSale) { return $user && $this->id_user && !$this->id_point_sale && !$this->id_user_group && $this->id_user == $user->id; } public function matchUserGroup($user, $pointSale) { return $user && $this->id_user_group && !$this->id_point_sale && !$this->id_user && $user->belongsToUserGroup($this->id_user_group); } public function matchPointSale($user, $pointSale) { return $pointSale && $this->id_point_sale && !$this->id_user && !$this->id_user_group && $this->id_point_sale == $pointSale->id; } public function matchUserPointSale($user, $pointSale) { return $pointSale && $user && $this->id_point_sale && $this->id_user && $this->id_point_sale == $pointSale->id && $this->id_user == $user->id; } public function matchFromQuantityOnly() { return !$this->id_user && !$this->id_point_sale && !$this->id_user_group && $this->from_quantity; } }