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.

138 lines
3.8KB

  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 actionSendCommandes($key = '')
  40. {
  41. if($key == '64ac0bdab7e9f5e48c4d991ec5201d57')
  42. {
  43. $heure = date('H') ;
  44. if($heure == '00')
  45. {
  46. $date = date('Y-m-d') ;
  47. }
  48. else {
  49. $date = date('Y-m-d', time()+24*60*60) ;
  50. }
  51. $etablissements = Etablissement::find()->all() ;
  52. foreach($etablissements as $e)
  53. {
  54. $production = Production::findOne([
  55. 'date' => $date,
  56. 'actif' => 1,
  57. 'id_etablissement' => $e['id'],
  58. ]) ;
  59. if($production && $heure == $e['heure_limite_commande'])
  60. {
  61. $commandes = Commande::find()
  62. ->with('commandeProduits', 'user')
  63. ->joinWith('production')
  64. ->where(['production.date' => $date])
  65. ->andWhere(['production.id_etablissement' => $e['id']])
  66. ->orderBy('date ASC')
  67. ->all();
  68. $user = User::findOne([
  69. 'id_etablissement' => $e['id'],
  70. 'status' => User::STATUS_BOULANGER
  71. ]) ;
  72. $mail = Yii::$app->mailer->compose()
  73. ->setTo($user->email)
  74. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain']) ;
  75. if(count($commandes))
  76. {
  77. $sujet = '[La boîte à pain] Commandes du '.date('d/m',strtotime($date)) ;
  78. // génération du pdf de commande
  79. Yii::$app->runAction('commande/report-cron', [
  80. 'date' => $date,
  81. 'save' => true,
  82. 'id_etablissement' => $e['id'] ,
  83. 'key' => '64ac0bdab7e9f5e48c4d991ec5201d57'
  84. ]);
  85. $mail->attach(Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$e['id'].'.pdf')) ;
  86. $message = 'Bonjour,
  87. Voici en pièce jointe le récapitulatif des '.count($commandes).' commandes du '.date('d/m',strtotime($date)).'.
  88. À bientôt,
  89. La boîte à pain
  90. ' ;
  91. }
  92. else {
  93. $sujet = '[La boîte à pain] Aucune commande' ;
  94. $message = 'Bonjour,
  95. Vous n\'avez aucune commande pour le '.date('d/m',strtotime($date)).'.
  96. À bientôt,
  97. La boîte à pain
  98. ' ;
  99. }
  100. $mail->setSubject($sujet)
  101. ->setTextBody($message)
  102. ->send();
  103. }
  104. }
  105. }
  106. }
  107. }