'ID', 'id_user' => 'Utilisateur', 'id_etablissement' => 'Etablissement', 'id_point_vente' => 'Point de vente', 'date_debut' => 'Date de début', 'date_fin' => 'Date de fin', 'lundi' => 'Lundi', 'mardi' => 'Mardi', 'mercredi' => 'Mercredi', 'jeudi' => 'Jeudi', 'vendredi' => 'Vendredi', 'samedi' => 'Samedi', 'dimanche' => 'Dimanche', 'periodicite_semaine' => 'Périodicité', 'paiement_automatique' => 'Paiement automatique' ]; } /* * Relations */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'id_user']); } public function getEtablissement() { return $this->hasOne(Etablissement::className(), ['id' => 'id_etablissement']); } public function getPointVente() { return $this->hasOne(PointVente::className(), ['id' => 'id_point_vente']); } public function getCommandeAutoProduit() { return $this->hasMany(CommandeAutoProduit::className(), ['id_commande_auto' => 'id'])->with('produit'); } /** * Retourne les commandes récurrentes pour une date donnée. * * @param string $date * @return array */ public static function getAll($date) { $date = date('Y-m-d', strtotime($date)); // commandes auto $commandes_auto = self::find() ->with('commandeAutoProduit') ->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement]) ->all(); $arr_commandes_auto = []; foreach ($commandes_auto as $c) { // vérif dates if ($date >= $c->date_debut && (!$c->date_fin || $date <= $c->date_fin)) { // périodicite $nb_jours = (strtotime($date) - strtotime($c->date_debut)) / (24 * 60 * 60); if ($nb_jours % ($c->periodicite_semaine * 7) < 7) { // jour de la semaine $jour = date('N', strtotime($date)); switch ($jour) { case 1 : $jour = 'lundi'; break; case 2 : $jour = 'mardi'; break; case 3 : $jour = 'mercredi'; break; case 4 : $jour = 'jeudi'; break; case 5 : $jour = 'vendredi'; break; case 6 : $jour = 'samedi'; break; case 7 : $jour = 'dimanche'; break; } if ($c->$jour) { $arr_commandes_auto[] = $c; } } } } return $arr_commandes_auto; } /** * Ajoute les commandes pour une date donnée à partir des commnandes * récurrentes. * * @param string $date * @param boolean $force */ public static function addAll($date, $force = false) { // production $production = Production::findOne([ 'date' => date('Y-m-d', strtotime($date)), 'id_etablissement' => Yii::$app->user->identity->id_etablissement ]); if ($production) { $count_commandes_prod = Commande::find() ->where(['id_production' => $production->id]) ->count(); if (!$count_commandes_prod || $force) { $commandes_auto = self::getAll($date); foreach ($commandes_auto as $c) { $c->add($date); } } } } /** * Ajoute la commande récurrente pour une date donnée. * * @param string $date */ public function add($date) { // production $production = Production::find() ->where([ 'date' => date('Y-m-d', strtotime($date)), 'id_etablissement' => Yii::$app->user->identity->id_etablissement ]) ->with('productionProduit') ->one(); if ($production && count($this->commandeAutoProduit)) { // commande $commande = new Commande; if (strlen($this->username)) { $commande->username = $this->username; $commande->id_user = 0; } else { $commande->id_user = $this->id_user; } $commande->date = date('Y-m-d H:i:s'); $commande->type = Commande::TYPE_AUTO; $commande->id_point_vente = $this->id_point_vente; $commande->id_production = $production->id; $commande->paiement_automatique = $this->paiement_automatique ; $point_vente_user = PointVenteUser::find() ->where(['id_point_vente' => $this->id_point_vente, 'id_user' => $this->id_user]) ->one(); if ($point_vente_user && strlen($point_vente_user->commentaire)) { $commande->commentaire_point_vente = $point_vente_user->commentaire; } $commande->save(); // produits $montant_total = 0; $produits_add = false; foreach ($this->commandeAutoProduit as $commande_auto_produit) { $commande_produit = new CommandeProduit; $commande_produit->id_commande = $commande->id; $commande_produit->id_produit = $commande_auto_produit->produit->id; $commande_produit->quantite = $commande_auto_produit->quantite; $commande_produit->prix = $commande_auto_produit->produit->prix; $commande_produit->save(); $produits_add = true; } if (!$produits_add) { $commande->delete(); } } } }