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.

137 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(['id_etablissement' => $e['id']]) ;
  69. $mail = Yii::$app->mailer->compose()
  70. ->setTo($user->email)
  71. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain']) ;
  72. if(count($commandes))
  73. {
  74. $sujet = '[La boîte à pain] Commandes du '.date('d/m',strtotime($date)) ;
  75. if(count($commandes) > 1)
  76. $sujet .= 's' ;
  77. // génération du pdf de commande
  78. Yii::$app->runAction('commande/report-cron', [
  79. 'date' => $date,
  80. 'save' => true,
  81. 'id_etablissement' => $e['id'] ,
  82. 'key' => '64ac0bdab7e9f5e48c4d991ec5201d57'
  83. ]);
  84. $mail->attach(Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$e['id'].'.pdf')) ;
  85. $message = 'Bonjour,
  86. Voici en pièce jointe le récapitulatif des commandes ('.count($commandes).') du '.date('d/m',strtotime($date)).'.
  87. À bientôt,
  88. La boîte à pain
  89. ' ;
  90. }
  91. else {
  92. $sujet = '[La boîte à pain] Aucune commande' ;
  93. $message = 'Bonjour,
  94. Vous n\'avez aucune commande pour le '.date('d/m',strtotime($date)).'.
  95. À bientôt,
  96. La boîte à pain
  97. ' ;
  98. }
  99. $mail->setSubject($sujet)
  100. ->setTextBody($message)
  101. ->send();
  102. }
  103. }
  104. }
  105. }
  106. }