[ '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 actionIndex() { /* * Volume de commande de l'année passée (par mois) */ $date_start = date('Y-m-d', time() - 60 * 60 * 24 * 365); $date_end = date('Y-m-d'); $data_pain = []; // labels $data_labels = []; $start = new DateTime($date_start); $interval = new DateInterval('P1M'); $end = new DateTime($date_end); $period = new DatePeriod($start, $interval, $end); foreach ($period as $date) { $mois = date('m/Y', $date->getTimestamp()); $data_labels[] = $mois; $data_pain[$mois] = 0; } // commandes $commandes = Commande::find() ->with('commandeProduits') ->joinWith('production') ->where('production.date > ' . $date_start) ->andWhere('production.id_etablissement = ' . Yii::$app->user->identity->id_etablissement) ->all(); foreach ($commandes as $c) { $c->init(); $mois = date('m/Y', strtotime($c->production->date)); if (isset($data_pain[$mois])) { $data_pain[$mois] += $c->montant_pain; } } // création d'un tableau sans index car chart.js n'accepte pas les index $data_pain_noindex = []; foreach ($data_pain as $key => $val) { $data_pain_noindex[] = $val; } return $this->render('index', [ 'data_labels' => $data_labels, 'data_pain' => $data_pain_noindex, ]); } const TOTAUX = 13 ; public function actionProduits($year = 0) { if(!$year) $year = date('Y') ; $produits = Produit::find() ->where('(vrac IS NULL OR vrac = 0)') ->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement]) ->orderBy('order ASC') ->all() ; $arr_produits = [] ; $arr_produits[self::TOTAUX] = ['max' => [], 'commandes' => []] ; foreach($produits as $p) { $arr_produits[self::TOTAUX]['max'][$p['nom']] = 0 ; $arr_produits[self::TOTAUX]['commandes'][$p['nom']] = 0 ; } $empty = true ; for($i = 1; $i <= 12; $i++) { // Maximums $res_maximums = Yii::$app->db->createCommand("SELECT produit.nom, SUM(IF(production_produit.actif,production_produit.quantite_max,0)) AS total FROM production, production_produit, produit WHERE production.id_etablissement = ".Yii::$app->user->identity->id_etablissement." AND production.date >= :date_begin AND production.date <= :date_end AND production.id = production_produit.id_production AND production_produit.id_produit = produit.id GROUP BY produit.id ORDER BY produit.nom") ->bindValue(':date_begin', date($year.'-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-01')) ->bindValue(':date_end', date($year.'-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-31')) ->queryAll(); $arr_produits[$i]['max'] = $res_maximums ; if(count($res_maximums)) $empty = false ; foreach($res_maximums as $produit_max) { if(!isset($arr_produits[self::TOTAUX]['max'][$produit_max['nom']])) $arr_produits[self::TOTAUX]['max'][$produit_max['nom']] = 0 ; $arr_produits[self::TOTAUX]['max'][$produit_max['nom']] += $produit_max['total'] ; } // Commandés $res_commandes = Yii::$app->db->createCommand("SELECT produit.nom, SUM(commande_produit.quantite) AS total FROM production, commande, commande_produit, produit WHERE production.id_etablissement = ".Yii::$app->user->identity->id_etablissement." AND production.date >= :date_begin AND production.date <= :date_end AND production.id = commande.id_production AND commande.id = commande_produit.id_commande AND commande_produit.id_produit = produit.id GROUP BY produit.id ORDER BY produit.nom") ->bindValue(':date_begin', date($year.'-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-01')) ->bindValue(':date_end', date($year.'-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-31')) ->queryAll(); $arr_produits[$i]['commandes'] = $res_commandes ; if(count($res_commandes)) $empty = false ; foreach($res_commandes as $produit_commandes) { if(!isset($arr_produits[self::TOTAUX]['commandes'][$produit_commandes['nom']])) $arr_produits[self::TOTAUX]['commandes'][$produit_commandes['nom']] = 0 ; $arr_produits[self::TOTAUX]['commandes'][$produit_commandes['nom']] += $produit_commandes['total'] ; } } ksort($arr_produits) ; $arr_mois = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre','Totaux'] ; return $this->render('produits', [ 'year' => $year, 'arr_mois' => $arr_mois, 'produits' => $produits, 'arr_produits' => $arr_produits, 'empty' => $empty ]); } } ?>