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.

91 satır
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. {
  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. $data_vrac = [] ;
  38. // labels
  39. $data_labels = [] ;
  40. $start = new DateTime($date_start);
  41. $interval = new DateInterval('P1M');
  42. $end = new DateTime($date_end);
  43. $period = new DatePeriod($start, $interval, $end);
  44. foreach($period as $date) {
  45. $mois = date('m/Y',$date->getTimestamp()) ;
  46. $data_labels[] = $mois ;
  47. $data_pain[$mois] = 0 ;
  48. $data_vrac[$mois] = 0 ;
  49. }
  50. // commandes
  51. $commandes = Commande::find()
  52. ->with('commandeProduits')
  53. ->joinWith('production')
  54. ->where('production.date > '.$date_start)
  55. ->all() ;
  56. foreach($commandes as $c) {
  57. $c->init();
  58. $mois = date('m/Y',strtotime($c->production->date)) ;
  59. if(isset($data_pain[$mois]))
  60. {
  61. $data_pain[$mois] += $c->montant_pain ;
  62. $data_vrac[$mois] += $c->montant_vrac ;
  63. }
  64. }
  65. return $this->render('index', [
  66. 'data_labels' => $data_labels,
  67. 'data_pain' => $data_pain,
  68. 'data_vrac' => $data_vrac,
  69. ]);
  70. }
  71. }
  72. ?>