|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
-
- namespace backend\controllers;
-
- use yii\web\Controller;
- use yii\filters\AccessControl;
- use Yii;
- use common\models\User;
- use common\models\Commande;
- use DateTime;
- use DateInterval;
- use DatePeriod;
-
- class StatsController extends Controller {
-
- public function behaviors()
- {
- return [
- 'access' => [
- '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 = [] ;
- $data_vrac = [] ;
-
- // 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 ;
- $data_vrac[$mois] = 0 ;
- }
-
- // commandes
- $commandes = Commande::find()
- ->with('commandeProduits')
- ->joinWith('production')
- ->where('production.date > '.$date_start)
- ->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 ;
- $data_vrac[$mois] += $c->montant_vrac ;
- }
- }
-
- return $this->render('index', [
- 'data_labels' => $data_labels,
- 'data_pain' => $data_pain,
- 'data_vrac' => $data_vrac,
- ]);
-
- }
-
- }
-
-
- ?>
|