|
- <?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 BackendController {
-
- 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 = [];
-
- // 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;
- }
-
- // 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_pain;
- }
- }
-
- // 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;
- }
-
- return $this->render('index', [
- 'data_labels' => $data_labels,
- 'data_pain' => $data_pain_noindex,
- ]);
- }
-
- }
-
- ?>
|