[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return User::hasAccessBackend() ; } ] ], ], ]; } /** * Affiche les statistiques de l'année avec le CA réalisé par mois. * * @return mixed */ public function actionIndex() { /* * Volume de commande de l'année passée (par mois) */ $dateStart = date('Y-m-d', time() - 60 * 60 * 24 * 365); $dateEnd = date('Y-m-d'); $data = []; // labels $dataLabels = []; $start = new DateTime($dateStart); $interval = new DateInterval('P1M'); $end = new DateTime($dateEnd); $period = new DatePeriod($start, $interval, $end); foreach ($period as $date) { $month = date('m/Y', $date->getTimestamp()); $dataLabels[] = $month; $data[$month] = 0; } // commandes $ordersArray = Order::searchAll([],[ 'conditions' => 'distribution.date > :date', 'params' => [':date' => $dateStart] ]) ; foreach ($ordersArray as $order) { $month = date('m/Y', strtotime($c->distribution->date)); if (isset($data[$month])) { $data[$month] += $c->amount; } } // création d'un tableau sans index car chart.js n'accepte pas les index $dataNoIndex = []; foreach ($data as $key => $val) { $dataNoIndex[] = $val; } return $this->render('index', [ 'dataLabels' => $dataLabels, 'data' => $dataNoIndex, ]); } const TOTAUX = 13 ; /** * Affiche un tableau avec les totaux (maximums, commandés) de chaque produit * par mois d'une année donnée. * * @param integer $year * @return mixed */ public function actionProducts($year = 0) { if(!$year) $year = date('Y') ; $productsArray = Product::searchAll() ; $dataProducts = [] ; $dataProducts[self::TOTAUX] = ['max' => [], 'orders' => []] ; foreach($productsArray as $product) { $dataProducts[self::TOTAUX]['max'][$p['name']] = 0 ; $dataProducts[self::TOTAUX]['orders'][$p['name']] = 0 ; } $empty = true ; for($i = 1; $i <= 12; $i++) { // Maximums $resMaximums = Yii::$app->db->createCommand("SELECT product.name, SUM(IF(product_distribution.active, product_distribution.quantity_max,0)) AS total FROM distribution, product_distribution, product WHERE distribution.id_producer = ".Yii::$app->user->identity->id_etablissement." AND distribution.date >= :date_begin AND distribution.date <= :date_end AND distribution.id = product_distribution.id_distribution AND product_distribution.id_product = product.id GROUP BY product.id ORDER BY product.name") ->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(); $dataProducts[$i]['max'] = $resMaximums ; if(count($resMaximums)) $empty = false ; foreach($resMaximums as $productMax) { if(!isset($dataProducts[self::TOTAUX]['max'][$productMax['name']])) $dataProducts[self::TOTAUX]['max'][$productMax['name']] = 0 ; $dataProducts[self::TOTAUX]['max'][$productMax['name']] += $productMax['total'] ; } // Commandés $resOrders = Yii::$app->db->createCommand("SELECT product.name, SUM(product_order.quantity) AS total FROM distribution, order, product_order, product WHERE distribution.id_producer = ".Producer::getId()." AND distribution.date >= :date_begin AND distribution.date <= :date_end AND distribution.id = order.id_distribution AND order.id = product_order.id_order AND product_order.id_product = product.id GROUP BY product.id ORDER BY product.name") ->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(); $dataProducts[$i]['orders'] = $resOrders; if(count($resOrders)) $empty = false ; foreach($resOrders as $productOrder) { if(!isset($dataProducts[self::TOTAUX]['orders'][$productOrder['name']])) $dataProducts[self::TOTAUX]['order'][$productOrder['name']] = 0 ; $dataProducts[self::TOTAUX]['orders'][$productOrder['name']] += $productOrder['total'] ; } } ksort($dataProducts) ; $monthArray = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre','Totaux'] ; return $this->render('produits', [ 'year' => $year, 'monthArray' => $monthArray, 'productsArray' => $productsArray, 'dataProducts' => $dataProducts, 'empty' => $empty ]); } } ?>