|
- <?php
-
-
-
- namespace backend\controllers;
-
- use yii\web\Controller;
- use yii\filters\AccessControl;
- use Yii;
- use common\models\User;
- use common\models\Order;
- use DateTime;
- use DateInterval;
- use DatePeriod;
-
- class StatsController extends BackendController
- {
-
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::hasAccessBackend() ;
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
-
-
-
- $dateStart = date('Y-m-d', time() - 60 * 60 * 24 * 365);
- $dateEnd = date('Y-m-d', time() + 60 * 60 * 24 * 31);
-
- $data = [];
-
-
- $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;
-
- $res = Yii::$app->db->createCommand("SELECT SUM(product_order.price * product_order.quantity) AS total
- FROM `order`, product_order, distribution
- WHERE distribution.id_producer = :id_producer
- AND `order`.id_distribution = distribution.id
- AND `order`.id = product_order.id_order
- AND distribution.date >= :date_start
- AND distribution.date <= :date_end
- ")
- ->bindValue(':id_producer', Producer::getId())
- ->bindValue(':date_start', date('Y-m-', $date->getTimestamp()).'01')
- ->bindValue(':date_end', date('Y-m-', $date->getTimestamp()).'31' )
- ->queryOne();
-
- if($res['total']) {
- $data[$month] = $res['total'];
- }
- else {
- $data[$month] = 0;
- }
-
- }
-
-
- $dataNoIndex = [];
- foreach ($data as $key => $val) {
- $dataNoIndex[] = round($val, 2);
- }
-
- return $this->render('index', [
- 'dataLabels' => $dataLabels,
- 'data' => $dataNoIndex,
- ]);
- }
-
- const TOTALS = 13 ;
-
-
-
- public function actionProducts($year = 0, $section = 1)
- {
- if(!$year) $year = date('Y') ;
-
- $productsArray = Product::searchAll() ;
-
- $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++) {
-
- $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 = ".Producer::getId()."
- 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'] ;
- }
-
-
- $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::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,
- ]);
- }
-
- }
-
- ?>
|