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.

133 line
3.9KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\models\User;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. use yii\filters\AccessControl;
  10. use kartik\mpdf\Pdf;
  11. use common\models\Etablissement;
  12. use common\models\Commande ;
  13. use common\models\Production ;
  14. /**
  15. * UserController implements the CRUD actions for User model.
  16. */
  17. class CronController extends BackendController
  18. {
  19. public function behaviors()
  20. {
  21. return [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['post'],
  26. ],
  27. ],
  28. 'access' => [
  29. 'class' => AccessControl::className(),
  30. 'rules' => [
  31. [
  32. 'allow' => true,
  33. 'roles' => ['?'],
  34. ]
  35. ],
  36. ],
  37. ];
  38. }
  39. public function actionInitBddDemo($key = '')
  40. {
  41. if($key == '45432df6e842ac71aa0b5bb6b9f25d44')
  42. {
  43. }
  44. }
  45. public function actionSendCommandes($key = '')
  46. {
  47. if($key == '64ac0bdab7e9f5e48c4d991ec5201d57')
  48. {
  49. $heure = date('H') ;
  50. if($heure == '00')
  51. {
  52. $date = date('Y-m-d') ;
  53. }
  54. else {
  55. $date = date('Y-m-d', time()+24*60*60) ;
  56. }
  57. $etablissements = Etablissement::find()->all() ;
  58. foreach($etablissements as $e)
  59. {
  60. $production = Production::findOne([
  61. 'date' => $date,
  62. 'actif' => 1,
  63. 'id_etablissement' => $e['id'],
  64. ]) ;
  65. if($production && $heure == $e['heure_limite_commande'])
  66. {
  67. $commandes = Commande::find()
  68. ->with('commandeProduits', 'user')
  69. ->joinWith('production')
  70. ->where(['production.date' => $date])
  71. ->andWhere(['production.id_etablissement' => $e['id']])
  72. ->orderBy('date ASC')
  73. ->all();
  74. $user = User::findOne([
  75. 'id_etablissement' => $e['id'],
  76. 'status' => User::STATUS_BOULANGER
  77. ]) ;
  78. $mail = Yii::$app->mailer->compose(
  79. [
  80. 'html' => 'cronRecapCommandes-html',
  81. 'text' => 'cronRecapCommandes-text',
  82. ],
  83. [
  84. 'date' => $date,
  85. 'commandes' => $commandes
  86. ]
  87. )
  88. ->setTo($user->email)
  89. ->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) ;
  90. if(count($commandes))
  91. {
  92. $sujet = '[La boîte à pain] Commandes du '.date('d/m',strtotime($date)) ;
  93. // génération du pdf de commande
  94. Yii::$app->runAction('commande/report-cron', [
  95. 'date' => $date,
  96. 'save' => true,
  97. 'id_etablissement' => $e['id'] ,
  98. 'key' => '64ac0bdab7e9f5e48c4d991ec5201d57'
  99. ]);
  100. $mail->attach(Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$e['id'].'.pdf')) ;
  101. }
  102. else {
  103. $sujet = '[La boîte à pain] Aucune commande' ;
  104. }
  105. $mail->setSubject($sujet)
  106. ->send();
  107. }
  108. }
  109. }
  110. }
  111. }