Browse Source

Adaptations refactoring/traduction backend/controllers/StatsController

dev
Guillaume Bourgeois 5 years ago
parent
commit
102523b25a
1 changed files with 66 additions and 75 deletions
  1. +66
    -75
      backend/controllers/StatsController.php

+ 66
- 75
backend/controllers/StatsController.php View File

use yii\filters\AccessControl; use yii\filters\AccessControl;
use Yii; use Yii;
use common\models\User; use common\models\User;
use common\models\Commande;
use common\models\Order;
use DateTime; use DateTime;
use DateInterval; use DateInterval;
use DatePeriod; use DatePeriod;
'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => ['@'],
'matchCallback' => function ($rule, $action) { 'matchCallback' => function ($rule, $action) {
return Yii::$app->user->identity->status == USER::STATUS_ADMIN
|| Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
return User::hasAccessBackend() ;
} }
] ]
], ],
* Volume de commande de l'année passée (par mois) * 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');
$dateStart = date('Y-m-d', time() - 60 * 60 * 24 * 365);
$dateEnd = date('Y-m-d');


$data_pain = [];
$data = [];


// labels // labels
$data_labels = [];
$dataLabels = [];


$start = new DateTime($date_start);
$start = new DateTime($dateStart);
$interval = new DateInterval('P1M'); $interval = new DateInterval('P1M');
$end = new DateTime($date_end);
$end = new DateTime($dateEnd);


$period = new DatePeriod($start, $interval, $end); $period = new DatePeriod($start, $interval, $end);


foreach ($period as $date) { foreach ($period as $date) {
$mois = date('m/Y', $date->getTimestamp());
$data_labels[] = $mois;
$data_pain[$mois] = 0;
$month = date('m/Y', $date->getTimestamp());
$dataLabels[] = $month;
$data[$month] = 0;
} }


// commandes // 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;
$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 // 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;
$dataNoIndex = [];
foreach ($data as $key => $val) {
$dataNoIndex[] = $val;
} }


return $this->render('index', [ return $this->render('index', [
'data_labels' => $data_labels,
'data_pain' => $data_pain_noindex,
'dataLabels' => $dataLabels,
'data' => $dataNoIndex,
]); ]);
} }
* @param integer $year * @param integer $year
* @return mixed * @return mixed
*/ */
public function actionProduits($year = 0)
public function actionProducts($year = 0)
{ {
if(!$year) $year = date('Y') ; 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() ;
$productsArray = Product::searchAll() ;
$arr_produits = [] ;
$dataProducts = [] ;
$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 ;
$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 ; $empty = true ;
for($i = 1; $i <= 12; $i++) { for($i = 1; $i <= 12; $i++) {
// Maximums // 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")
$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_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')) ->bindValue(':date_end', date($year.'-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-31'))
->queryAll(); ->queryAll();
$arr_produits[$i]['max'] = $res_maximums ;
if(count($res_maximums)) $empty = false ;
$dataProducts[$i]['max'] = $resMaximums ;
if(count($resMaximums)) $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'] ;
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 // 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")
$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_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')) ->bindValue(':date_end', date($year.'-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-31'))
->queryAll(); ->queryAll();
$arr_produits[$i]['commandes'] = $res_commandes ;
if(count($res_commandes)) $empty = false ;
$dataProducts[$i]['orders'] = $resOrders;
if(count($resOrders)) $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'] ;
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($arr_produits) ;
ksort($dataProducts) ;
$arr_mois = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre','Totaux'] ;
$monthArray = ['Janvier','Février','Mars','Avril','Mai','Juin','Juillet','Août','Septembre','Octobre','Novembre','Décembre','Totaux'] ;
return $this->render('produits', [ return $this->render('produits', [
'year' => $year, 'year' => $year,
'arr_mois' => $arr_mois,
'produits' => $produits,
'arr_produits' => $arr_produits,
'monthArray' => $monthArray,
'productsArray' => $productsArray,
'dataProducts' => $dataProducts,
'empty' => $empty 'empty' => $empty
]); ]);
} }

Loading…
Cancel
Save