Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

97 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. ->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. {
  59. if($mois == '06/2016') {
  60. //print_r($c) ;
  61. }
  62. $data_pain[$mois] += $c->montant_pain ;
  63. }
  64. }
  65. // création d'un tableau sans index car chart.js n'accepte pas les index
  66. $data_pain_noindex = [] ;
  67. foreach($data_pain as $key => $val)
  68. {
  69. $data_pain_noindex[] = $val ;
  70. }
  71. return $this->render('index', [
  72. 'data_labels' => $data_labels,
  73. 'data_pain' => $data_pain_noindex,
  74. ]);
  75. }
  76. }
  77. ?>