[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return Yii::$app->user->identity->status == USER::STATUS_ADMIN || Yii::$app->user->identity->status == USER::STATUS_BOULANGER; } ] ], ], ]; } public function actionDeleteCommande($date, $id_commande) { $commande = Commande::find()->where(['id' => $id_commande])->one(); if ($commande) { $commande->delete(); CommandeProduit::deleteAll(['id_commande' => $id_commande]); } $this->redirect(['index', 'date' => $date]); } public function gestionFormCommandes($production, $date, $points_vente, $produits, $users) { if ($date != '') { // commandes $commandes = Commande::find() ->with('commandeProduits', 'user') ->joinWith('production') ->where(['production.date' => $date]) ->all(); foreach ($commandes as $c) $c->init(); foreach ($points_vente as $pv) { $pv->initCommandes($commandes); if (isset($_POST['submit_pv']) && $_POST['submit_pv']) { // modifs foreach ($pv->commandes as $c) { // suppression des commande_produit $commande_produits = CommandeProduit::find()->where(['id_commande' => $c->id])->all(); foreach ($commande_produits as $cp) $cp->delete(); // création des commande_produit modifiés foreach ($produits as $p) { $quantite = Yii::$app->getRequest()->post('produit_' . $c->id . '_' . $p->id, 0); if ($quantite) { $commande_produit = new CommandeProduit; $commande_produit->id_commande = $c->id; $commande_produit->id_produit = $p->id; $commande_produit->quantite = $quantite; $commande_produit->prix = $p->prix; $commande_produit->save(); } } } // ajout //$id_client = Yii::$app->getRequest()->post('user_pv_'.$pv->id, 0) ; $username = Yii::$app->getRequest()->post('username_pv_' . $pv->id, 0); $date = Yii::$app->getRequest()->post('date_commande_pv_' . $pv->id, 0); $one_product = false; foreach ($produits as $p) { $quantite = Yii::$app->getRequest()->post('produit_pv_' . $pv->id . '_' . $p->id, 0); if ($quantite) { $one_product = true; } } if (strlen($username) && $date && $one_product) { $commande = new Commande; $commande->id_point_vente = $pv->id; $commande->id_production = $production->id; $commande->id_user = 0; $commande->username = $username; $tab_date = explode('/', $date); $commande->date = $tab_date[2] . '-' . $tab_date[1] . '-' . $tab_date[0] . ' 00:00:00'; $commande->save(); foreach ($produits as $p) { $quantite = Yii::$app->getRequest()->post('produit_pv_' . $pv->id . '_' . $p->id, 0); if ($quantite) { $commande_produit = new CommandeProduit; $commande_produit->id_commande = $commande->id; $commande_produit->id_produit = $p->id; $commande_produit->quantite = $quantite; $commande_produit->prix = $p->prix; $commande_produit->save(); } } } } } } } public function actionIndex($date = '', $return_data = false) { $commandes = []; // points de vente $points_vente = PointVente::find()->all(); // produits $produits = Produit::find()->orderBy('order ASC')->all(); // users $arr_users = [0 => '--']; $users = User::find()->orderBy('prenom, nom ASC')->all(); foreach ($users as $u) { $arr_users[$u->id] = $u->prenom . ' ' . $u->nom; } // création du jour de production $production = null; if ($date != '') { $production = Production::find()->where(['date' => $date])->one(); if (!$production) { $production = new Production; $production->date = $date; $production->livraison = 1; $production->save(); } } // gestion des commandes $this->gestionFormCommandes($production, $date, $points_vente, $produits, $users); // commandes $commandes = Commande::find() ->with('commandeProduits', 'user') ->joinWith('production') ->where(['production.date' => $date]) ->orderBy('date ASC') ->all(); $recettes = 0; $recettes_pain = 0; $recettes_vrac = 0; $recettes_pain_livre = 0; $poids_pain = 0; $poids_vrac = 0; foreach ($commandes as $c) { $c->init(); $recettes += $c->montant; $recettes_pain += $c->montant_pain; $recettes_vrac += $c->montant_vrac; if ($c->id_point_vente != 1) $recettes_pain_livre += $c->montant_pain; $poids_pain += $c->poids_pain; $poids_vrac += $c->poids_vrac; } $recettes = number_format($recettes, 2); $recettes_pain = number_format($recettes_pain, 2); $recettes_vrac = number_format($recettes_vrac, 2); // init commandes point de vente foreach ($points_vente as $pv) { $pv->initCommandes($commandes); } // gestion produits selec if (isset($_POST['valider_produit_selec'])) { if (isset($_POST['Produit'])) { foreach ($produits as $p) { $produit_production = ProductionProduit::find()->where(['id_production' => $production->id, 'id_produit' => $p->id])->one(); if (!$produit_production) { $produit_production = new ProductionProduit(); $produit_production->id_production = $production->id; $produit_production->id_produit = $p->id; $produit_production->actif = 0; if (isset($p->quantite_max)) $produit_production->quantite_max = $p->quantite_max; $produit_production->save(); } if (isset($_POST['Produit'][$p->id]['actif'])) { $produit_production->actif = 1; } else { $produit_production->actif = 0; } if ((isset($_POST['Produit'][$p->id]['quantite_max']) && $_POST['Produit'][$p->id]['quantite_max'] != '')) { $produit_production->quantite_max = (int) $_POST['Produit'][$p->id]['quantite_max']; } else { if (isset($p->quantite_max) && is_numeric($p->quantite_max) && $p->quantite_max > 0) { $produit_production->quantite_max = $p->quantite_max; } } $produit_production->save(); } } } // init produits sélectionnés pour cette production $produits_selec = []; if ($production) { $day_production = date('N', strtotime($production->date)); $produits_production = ProductionProduit::find()->where(['id_production' => $production->id])->all(); if (!count($produits_production)) { foreach ($produits as $p) { $pp = new ProductionProduit(); $pp->id_production = $production->id; $pp->id_produit = $p->id; $pp->actif = 0; if ($day_production == 1 && $p->lundi) $pp->actif = 1; if ($day_production == 2 && $p->mardi) $pp->actif = 1; if ($day_production == 3 && $p->mercredi) $pp->actif = 1; if ($day_production == 4 && $p->jeudi) $pp->actif = 1; if ($day_production == 5 && $p->vendredi) $pp->actif = 1; if ($day_production == 6 && $p->samedi) $pp->actif = 1; if ($day_production == 7 && $p->dimanche) $pp->actif = 1; $pp->quantite_max = $p->quantite_max; $pp->save(); } } // produits selec pour production $produits_selec = ProductionProduit::findProduits($production->id); } // produit en vrac forcément activé if ($date != '') { foreach ($produits as $p) { $produit_production = ProductionProduit::find()->where(['id_production' => $production->id, 'id_produit' => $p->id])->one(); if ($p->vrac) { if (!$produit_production) { $produit_production = new ProductionProduit(); $produit_production->id_production = $production->id; $produit_production->id_produit = $p->id; $produit_production->quantite_max = 0; $produit_production->actif = 1; $produit_production->save(); } else { $produit_production->actif = 1; $produit_production->save(); } } } } // poids total de la production et CA potentiel $ca_potentiel = 0; $poids_total = 0; foreach ($produits_selec as $id_produit_selec => $produit_selec) { if ($produit_selec['actif']) { foreach ($produits as $produit) { if ($produit->id == $id_produit_selec) { //echo $produit->nom.' : '.$produit_selec['quantite_max'].'
' ; $ca_potentiel += $produit_selec['quantite_max'] * $produit->prix; $poids_total += $produit_selec['quantite_max'] * $produit->poids; } } } } // jours de production $jours_production = Production::find()->where(['actif' => 1])->all(); $datas = [ 'produits' => $produits, 'points_vente' => $points_vente, 'commandes' => $commandes, 'date' => $date, 'production' => $production, 'jours_production' => $jours_production, 'produits_selec' => $produits_selec, 'users' => $arr_users, 'recettes' => $recettes, 'recettes_pain' => $recettes_pain, 'recettes_vrac' => $recettes_vrac, 'recettes_pain_livre' => $recettes_pain_livre, 'poids_pain' => $poids_pain, 'poids_vrac' => $poids_vrac, 'ca_potentiel' => $ca_potentiel, 'poids_total' => $poids_total, ]; if ($return_data) { return $datas; } else { return $this->render('index', $datas); } } public function actionDownload($date = '', $id_point_vente = 0, $global = 0) { // commandes $commandes = Commande::find() ->with('commandeProduits', 'user') ->joinWith('production') ->where(['production.date' => $date]) ->orderBy('date ASC') ->all(); foreach ($commandes as $c) $c->init(); // points de vente $points_vente = PointVente::find()->all(); foreach ($points_vente as $pv) $pv->initCommandes($commandes); // produits $produits = Produit::find()->orderBy('order ASC')->all(); $production = Production::find()->where('date LIKE \'' . $date . '\'')->one(); $produits_selec = ProductionProduit::findProduits($production->id); /* * export global */ if ($global) { $data = []; $filename = 'export_' . $date . '_global'; $num_jour_semaine = date('w', strtotime($date)); $arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi']; $champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine]; // header /* $line = [''] ; foreach($produits as $p) { if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $line[] = $p->getLibelleAdmin() ; } } $data[] = $line ; */ // par point de vente foreach ($points_vente as $pv) { if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) { //$data[] = [$pv->nom] ; $line = [$pv->nom, 'Produits', 'Montant', 'Commentaire']; /* foreach($produits as $p) { if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $line[] = $p->getLibelleAdmin() ; } } */ $data[] = $line; $res = $this->contentPointVenteCSV($date, $produits, $points_vente, $pv->id); foreach ($res['data'] as $line) { $data[] = $line; } } if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) { $line = ['Total pain']; $str_produits = ''; foreach ($produits as $p) { if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $line[] = number_format($pv->recettes_pain, 2) . ' €'; $data[] = $line; $line = ['Total vrac']; $str_produits = ''; foreach ($produits as $p) { if ($p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $quantite = Commande::getQuantiteProduit($p->id, $pv->commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $line[] = number_format($pv->recettes_vrac, 2) . ' €'; $data[] = $line; $data[] = []; } } // récap //$line = ['Totaux'] ; // pain $line = ['Total pain']; $str_produits = ''; foreach ($produits as $p) { if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $quantite = Commande::getQuantiteProduit($p->id, $commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . '' . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $data[] = $line; // vrac $line = ['Total vrac']; $str_produits = ''; foreach ($produits as $p) { if ($p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $quantite = Commande::getQuantiteProduit($p->id, $commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . '' . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $data[] = $line; $infos = $this->actionIndex($date, true); // $data[] = [] ; /* $data[] = [ 'CA potentiel boutique', number_format($infos['ca_potentiel'] - $infos['recettes_pain_livre'], 2).' €', ] ; */ /* $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes) ; $data[] = ['Récapitulatif global'] ; foreach($res['data'] as $line) { $data[] = $line ; } */ CSV::downloadSendHeaders($filename . '.csv'); echo CSV::array2csv($data); die(); } /* * export individuel */ else { if ($commandes && count($commandes)) { $data = []; // par point de vente if ($id_point_vente) { $res = $this->contentPointVenteCSV($date, $produits, $points_vente, $id_point_vente); $data = $res['data']; $filename = $res['filename']; } // récapitulatif else { $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes); $filename = 'recapitulatif_' . $date; $data = $res['data']; } CSV::downloadSendHeaders($filename . '.csv'); echo CSV::array2csv($data); die(); } } } public function contentRecapCSV($date, $produits, $points_vente, $commandes) { $data = []; $filename = 'recapitulatif_' . $date; $production = Production::find()->where('date LIKE \'' . $date . '\'')->one(); $produits_selec = ProductionProduit::findProduits($production->id); // head $data[0] = ['Lieu']; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $data[0][] = $p->description; } } $num_jour_semaine = date('w', strtotime($date)); $arr_jour_semaine = [0 => 'dimanche', 1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi']; $champs_horaires_point_vente = 'horaires_' . $arr_jour_semaine[$num_jour_semaine]; // datas foreach ($points_vente as $pv) { if (count($pv->commandes) && strlen($pv->$champs_horaires_point_vente)) { $data_add = [$pv->nom]; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $data_add[] = Commande::getQuantiteProduit($p->id, $pv->commandes); } } $data[] = $data_add; } } $data_add = ['Total']; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $data_add[] = Commande::getQuantiteProduit($p->id, $commandes); } } $data[] = $data_add; return [ 'data' => $data, 'filename' => $filename ]; } public function contentPointVenteCSV($date, $produits, $points_vente, $id_point_vente) { $data = []; $production = Production::find()->where('date LIKE \'' . $date . '\'')->one(); $produits_selec = ProductionProduit::findProduits($production->id); // head /* $data[0] = ['Client', 'Date commande'] ; foreach($produits as $p) { if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $data[0][] = $p->description ; } } */ // datas foreach ($points_vente as $pv) { if ($pv->id == $id_point_vente) { $filename = 'export_' . $date . '_' . strtolower(str_replace(' ', '-', $pv->nom)); foreach ($pv->commandes as $c) { $str_user = ''; // username if ($c->user) { $str_user = $c->user->prenom . " " . $c->user->nom; //.' - '.date('d/m', strtotime($c->date)) ; } else { $str_user = $c->username; //.' - '.date('d/m', strtotime($c->date)) ; } // téléphone if (strlen($c->user->telephone)) { $str_user .= ' (' . $c->user->telephone . ')'; } $data_add = [$str_user]; // produits $str_produits = ''; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $add = false; foreach ($c->commandeProduits as $cp) { if ($p->id == $cp->id_produit) { $str_produits .= $cp->quantite . '' . $p->diminutif . ', '; $add = true; } } } } $data_add[] = substr($str_produits, 0, strlen($str_produits) - 2); $data_add[] = number_format($c->montant, 2) . ' €'; $data_add[] = $c->commentaire; $data[] = $data_add; } } } return [ 'data' => $data, 'filename' => $filename ]; } public function actionChangeState($date, $actif) { // changement état $production = Production::find()->where(['date' => $date])->one(); $production->actif = $actif; $production->save(); // envoi emails aux personnes qui souhaitent être informées if ($actif) { $jour = date('N', strtotime($date)); $arr_jour_semaine = [1 => 'lundi', 2 => 'mardi', 3 => 'mercredi', 4 => 'jeudi', 5 => 'vendredi', 6 => 'samedi', 7 => 'dimanche']; $users = User::find()->where('mail_prod_' . $arr_jour_semaine[$jour] . ' = 1')->all(); //$str_date = strtolower(date('l j F', strtotime($date))) ; setlocale(LC_ALL, "fr_FR"); $str_date = strtolower(strftime('%A%e %B', strtotime($date))); $str_date2 = date('d/m', strtotime($date)); foreach ($users as $u) { if (!$u->no_mail) { Yii::$app->mailer->compose() ->setTo($u->email) ->setFrom(['matthieu@lechatdesnoisettes.com' => 'Le Chat des Noisettes']) ->setSubject('[Le Chat des Noisettes] Production de pain du ' . $str_date2) ->setTextBody( "Bonjour, Une production de pain est prévue le " . $str_date . ". Cordialement, Matthieu PS : Si vous ne souhaitez plus recevoir ces emails, rendez-vous dans votre compte sur www.lechatdesnoisettes.com. ")->send(); } } } $this->redirect(['index', 'date' => $date]); } public function actionChangeLivraison($date, $livraison) { $production = Production::find()->where(['date' => $date])->one(); $production->livraison = $livraison; $production->save(); $this->redirect(['index', 'date' => $date]); } }