255], [['adresse', 'localite', 'horaires_lundi', 'horaires_mardi', 'horaires_mercredi', 'horaires_jeudi', 'horaires_vendredi', 'horaires_samedi', 'horaires_dimanche'], 'string'], [['point_fabrication', 'vrac', 'pain', 'credit_pain', 'livraison_lundi', 'livraison_mardi', 'livraison_mercredi', 'livraison_jeudi', 'livraison_vendredi', 'livraison_samedi', 'livraison_dimanche'], 'boolean'], ['point_fabrication', 'default', 'value' => 0], ['id_etablissement', 'integer'], ['id_etablissement', 'required'], [['users', 'users_commentaire', 'code'], 'safe'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'nom' => 'Nom', 'adresse' => 'Adresse', 'localite' => 'Localité', 'point_fabrication' => 'Point de fabrication', 'horaires_lundi' => 'Lundi', 'horaires_mardi' => 'Mardi', 'horaires_mercredi' => 'Mercredi', 'horaires_jeudi' => 'Jeudi', 'horaires_vendredi' => 'Vendredi', 'horaires_samedi' => 'Samedi', 'horaires_dimanche' => 'Dimanche', 'vrac' => 'Livraison de vrac', 'pain' => 'Livraison de pain', 'acces_restreint' => 'Accès restreint', 'credit_pain' => 'Activer le Crédit Pain', 'livraison_lundi' => 'Lundi', 'livraison_mardi' => 'Mardi', 'livraison_mercredi' => 'Mercredi', 'livraison_jeudi' => 'Jeudi', 'livraison_vendredi' => 'Vendredi', 'livraison_samedi' => 'Samedi', 'livraison_dimanche' => 'Dimanche', 'code' => 'Code', ]; } /* * Relations */ public function getPointVenteUser() { return $this->hasMany(PointVenteUser::className(), ['id_point_vente' => 'id']); } public function getProductionPointVente() { return $this->hasMany(ProductionPointVente::className(), ['id_point_vente' => 'id']); } /** * Initialise les commandes liées au point de vente. * * @param array $commandes */ public function initCommandes($commandes) { $this->commandes = []; $this->recettes = 0; $this->recettes_pain = 0; $this->recettes_vrac = 0; foreach ($commandes as $c) { if ($this->id == $c->id_point_vente) { $this->commandes[] = $c; if(is_null($c->date_delete)) { $this->recettes += (float) $c->montant; } $this->recettes_pain += (float) $c->montant_pain; $this->recettes_vrac += (float) $c->montant_vrac; } } } /** * Retourne les commandes liées à ce point de vente. * * @return array */ public function getCommandes() { return $this->commandes; } /** * Enregistre le point de vente. * * @param boolean $runValidation * @param array $attributeNames * @return type */ public function save($runValidation = true, $attributeNames = NULL) { $this->id_etablissement = Yii::$app->user->identity->id_etablissement; $this->pain = 1; return parent::save($runValidation, $attributeNames); } /** * Traite la mise à jour de l'attribut 'point_fabrication'. */ public function gestionPointFabrication() { if ($this->point_fabrication) { PointVente::updateAll( ['point_fabrication' => 0], ['id_etablissement' => $this->id_etablissement] ); $this->point_fabrication = 1; $this->save(); } } /** * Traite les accès restreints d'un point de vente. */ public function gestionAccesRestreint() { PointVenteUser::deleteAll(['id_point_vente' => $this->id]); if (is_array($this->users) && count($this->users)) { foreach ($this->users as $key => $val) { $user = User::findOne($val); if ($user) { $point_vente_user = new PointVenteUser; $point_vente_user->id_user = $val; $point_vente_user->id_point_vente = $this->id; if (isset($this->users_commentaire[$val]) && strlen($this->users_commentaire[$val])) { $point_vente_user->commentaire = $this->users_commentaire[$val]; } $point_vente_user->save(); } } } } /** * Retourne le commentaire de l'utilisateur courant lié au point de vente. * * @return string|null */ public function getCommentaire() { if (isset($this->pointVenteUser)) { foreach ($this->pointVenteUser as $pvu) { if ($pvu->id_user == Yii::$app->user->identity->id) { return $pvu->commentaire; } } } return null ; } /** * Retourne le nombre de points de vente pour l'établissement courant. * * @return integer */ public static function count() { return PointVente::find() ->where([ 'id_etablissement' => Yii::$app->user->identity->id_etablissement ]) ->count(); } /** * Vérifie le code d'accès à un point de vente. * * @param string $code * @return boolean */ public function verifCode($code) { if (strlen($this->code)) { if (trim(strtolower($code)) == trim(strtolower($this->code))) { return true; } else { return false; } } else { return true; } } /** * Retourne les jours de livraison du point de vente sous forme d'une chaine * de caractères. * * @return string */ public function strJoursLivraison() { $str = '' ; if($this->livraison_lundi) $str .= 'lundi, ' ; if($this->livraison_mardi) $str .= 'mardi, ' ; if($this->livraison_mercredi) $str .= 'mercredi, ' ; if($this->livraison_jeudi) $str .= 'jeudi, ' ; if($this->livraison_vendredi) $str .= 'vendredi, ' ; if($this->livraison_samedi) $str .= 'samedi, ' ; if($this->livraison_dimanche) $str .= 'dimanche, ' ; if(strlen($str)) return substr($str, 0, strlen($str)-2) ; else return '' ; } /** * Retourne un commentaire informant l'utilisateur sur les détails de * livraison d'un point de vente et pour un jour donné. * * @param string $jour * @return string */ public function strInfos($jour) { $str = '' ; $champs = 'horaires_'.$jour ; if(strlen($this->$champs)) { $str = nl2br(Html::encode($this->$champs)) ; $str = preg_replace('/\[select_previous_day\](.*?)\[\/select_previous_day\]/', '$1' , $str) ; } return $str ; } }