[ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsProducer($this->getUserCurrent()); } ] ], ], ]; } /** * Affiche le CA réalisé par mois sur une année donnée */ public function actionIndex(int $year = null, string $displayBy = 'month') { $producerCurrent = $this->getProducerCurrent(); if(!$year) { $year = date('Y'); } $isVatNotApplicable = !$producerCurrent->taxRate->value; $yearsWithTurnoverArray = $this->getProducerModule()->getRepository()->getYearsWithTurnover($producerCurrent); $dataChartTurnover = $this->getProducerModule()->getRepository()->getDatasChartTurnoverStatistics($producerCurrent, $year, $displayBy); $dataChartTurnoverWithTax = $this->getProducerModule()->getRepository()->getDatasChartTurnoverStatistics($producerCurrent, $year, $displayBy, true); return $this->render('index', [ 'isVatNotApplicable' => $isVatNotApplicable, 'displayBy' => $displayBy, 'yearCurrent' => $year, 'dataLabels' => $dataChartTurnover['labels'], 'data' => $dataChartTurnover['data'], 'dataWithTax' => $dataChartTurnoverWithTax['data'], 'yearsWithTurnoverArray' => $yearsWithTurnoverArray, ]); } const TOTALS = 13; /** * Affiche un tableau avec les totaux (maximums, commandés) de chaque produit * par mois d'une année donnée. */ public function actionProducts(int $year = 0, $section = 1) { $productModule = $this->getProductModule(); if (!$year) { $year = date('Y'); } $productsArray = $productModule->findProducts(); $dataProducts = []; $dataProducts[self::TOTALS] = ['max' => [], 'orders' => []]; foreach ($productsArray as $product) { $dataProducts[self::TOTALS]['max'][$product['name']] = 0; $dataProducts[self::TOTALS]['orders'][$product['name']] = 0; } if (!in_array($section, [1, 2, 3, 4])) { $section = 1; } $iStart = (3 * ($section - 1)) + 1; $iEnd = 3 * $section; $empty = true; for ($i = $iStart; $i <= $iEnd; $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 = " . GlobalParam::getCurrentProducerId() . " 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::TOTALS]['max'][$productMax['name']])) { $dataProducts[self::TOTALS]['max'][$productMax['name']] = 0; } $dataProducts[self::TOTALS]['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 = ' . GlobalParam::getCurrentProducerId() . ' 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::TOTALS]['orders'][$productOrder['name']])) { $dataProducts[self::TOTALS]['orders'][$productOrder['name']] = 0; } $dataProducts[self::TOTALS]['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('products', [ 'year' => $year, 'monthArray' => $monthArray, 'productsArray' => $productsArray, 'dataProducts' => $dataProducts, 'empty' => $empty, 'section' => $section, 'iStart' => $iStart, 'iEnd' => $iEnd, ]); } } ?>