You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 line
2.4KB

  1. <?php
  2. namespace backend\controllers;
  3. use yii\web\Controller;
  4. use yii\filters\AccessControl;
  5. use Yii;
  6. use common\models\User;
  7. use common\models\Commande;
  8. use DateTime;
  9. use DateInterval;
  10. use DatePeriod;
  11. class StatsController extends BackendController {
  12. public function behaviors() {
  13. return [
  14. 'access' => [
  15. 'class' => AccessControl::className(),
  16. 'rules' => [
  17. [
  18. 'allow' => true,
  19. 'roles' => ['@'],
  20. 'matchCallback' => function ($rule, $action) {
  21. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  22. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  23. }
  24. ]
  25. ],
  26. ],
  27. ];
  28. }
  29. public function actionIndex() {
  30. /*
  31. * Volume de commande de l'année passée (par mois)
  32. */
  33. $date_start = date('Y-m-d', time() - 60 * 60 * 24 * 365);
  34. $date_end = date('Y-m-d');
  35. $data_pain = [];
  36. // labels
  37. $data_labels = [];
  38. $start = new DateTime($date_start);
  39. $interval = new DateInterval('P1M');
  40. $end = new DateTime($date_end);
  41. $period = new DatePeriod($start, $interval, $end);
  42. foreach ($period as $date) {
  43. $mois = date('m/Y', $date->getTimestamp());
  44. $data_labels[] = $mois;
  45. $data_pain[$mois] = 0;
  46. }
  47. // commandes
  48. $commandes = Commande::find()
  49. ->with('commandeProduits')
  50. ->joinWith('production')
  51. ->where('production.date > ' . $date_start)
  52. ->andWhere('production.id_etablissement = ' . Yii::$app->user->identity->id_etablissement)
  53. ->all();
  54. foreach ($commandes as $c) {
  55. $c->init();
  56. $mois = date('m/Y', strtotime($c->production->date));
  57. if (isset($data_pain[$mois])) {
  58. $data_pain[$mois] += $c->montant_pain;
  59. }
  60. }
  61. // création d'un tableau sans index car chart.js n'accepte pas les index
  62. $data_pain_noindex = [];
  63. foreach ($data_pain as $key => $val) {
  64. $data_pain_noindex[] = $val;
  65. }
  66. return $this->render('index', [
  67. 'data_labels' => $data_labels,
  68. 'data_pain' => $data_pain_noindex,
  69. ]);
  70. }
  71. }
  72. ?>