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.

95 lines
2.6KB

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