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.3KB

  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 Controller {
  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. }
  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. $data_vrac = [] ;
  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. $data_vrac[$mois] = 0 ;
  48. }
  49. // commandes
  50. $commandes = Commande::find()
  51. ->with('commandeProduits')
  52. ->joinWith('production')
  53. ->where('production.date > '.$date_start)
  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. $data_vrac[$mois] += $c->montant_vrac ;
  62. }
  63. }
  64. return $this->render('index', [
  65. 'data_labels' => $data_labels,
  66. 'data_pain' => $data_pain,
  67. 'data_vrac' => $data_vrac,
  68. ]);
  69. }
  70. }
  71. ?>