No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

CronController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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\CommandeAuto ;
  14. use common\models\Production ;
  15. /**
  16. * UserController implements the CRUD actions for User model.
  17. */
  18. class CronController extends BackendController
  19. {
  20. public function behaviors()
  21. {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. 'delete' => ['post'],
  27. ],
  28. ],
  29. 'access' => [
  30. 'class' => AccessControl::className(),
  31. 'rules' => [
  32. [
  33. 'allow' => true,
  34. 'roles' => ['?'],
  35. ]
  36. ],
  37. ],
  38. ];
  39. }
  40. public function actionInitBddDemo($key = '')
  41. {
  42. if($key == '45432df6e842ac71aa0b5bb6b9f25d44' && YII_ENV == 'demo')
  43. {
  44. $arr_noms = [
  45. 'Martin', 'Bernard', 'Thomas','Petit','Robert', 'Richard','Durand','Dubois',
  46. 'Moreau', 'Laurent', 'Simon', 'Michel', 'Lefebvre', 'Leroy', 'Roux', 'David',
  47. 'Bertrand', 'Morel', 'Fournier', 'Girard', 'Bonnet', 'Dupont','Lambert', 'Fontaine',
  48. 'Rousseau', 'Vincent', 'Muller','Lefevre', 'Faure', 'Andre', 'Mercier', 'Blanc', 'Guerin',
  49. 'Boyer', 'Garnier', 'Chevalier', 'François','Legrand','Gauthier','Garcia','Perrin',
  50. 'Robin','Clement','Morin','Nicolas','Henry','Roussel','Mathieu','Gautier','Masson',
  51. 'Marchand','Duval','Denis','Dumont','Marie','Lemaire','Noël','Meyer','Dufour',
  52. 'Meunier','Brun','Blanchard','Giraud','Joly','Rivière','Lucas','Brunet','Gaillard','Barbier',
  53. 'Arnaud','Martinez','Gerard','Roche','Renard','Schmitt','Roy','Leroux','Colin','Vidal',
  54. 'Caron','Picard','Roger','Fabre','Aubert','Lemoine','Renaud','Dumas','Lacroix','Olivier',
  55. 'Philippe','Bourgeois','Pierre','Benoit','Rey','Leclerc','Payet','Rolland','Lecomte','Lopez',
  56. 'Jean','Dupuis','Guillot','Hubert','Berger','Carpentier','Sanchez','Dupuis','Moulin',
  57. 'Louis','Deschamps','Huet','Vasseur','Perez','Trouillot','Fusillier','Massenot','Boucher','Fleury',
  58. 'Royer','Klein','Jacquet','Jaquin','Adam','Paris','Poirier','Aubry','Guyot','Carré','Charles',
  59. 'Charpentier','Menard','Bailly','Bertin','Le Gall','Collet','Leger','Bouvier','Millet','Daniel',
  60. 'Langlois','Pelletier','Perrier','Leblanc','Lebrun','Monnier','Michaud','Laporte','Carlier',
  61. 'Pasquier','Delaunay','Lamy','Gilbert','Lejeune','Pichon','Cordier','Barthelemy','Perret','Reynaud',
  62. 'Humbert','Marechal','Bernard','Lemaitre','Gay','Bouchet','Da Silva','Chauvin','Chevalier','Tessier',
  63. 'Poulain','Girondin','Gillet','Guichard'
  64. ];
  65. $arr_prenoms = [
  66. 'Adel','Antonin','Armand','Arnaud','Aymeric','Baptiste','Barnabé','Bernard','Brice','Baudouin',
  67. 'Camille','Cassandre','Célestin','Christian','Clément','Cyril','Claude','Damien','Daniel','David',
  68. 'Delphin','Denis','Didier','Dimitri','Dorothée','Désiré','Edgard','Etienne','Eugène','Eudes','Eric',
  69. 'Fabien','Fabrice','Flavien','Florent','Francois','Frédéric','Gabin','Gabriel','Gautier','Gilles','Guy',
  70. 'Hervé','Huvert','Hugues','Hector','Jacques','Jason','Jean','Jeannot','Johan','Julien','Léon','Lionel','Loïc',
  71. 'Lucas','Luc','Lucien','Maurice','Maxence','Maxime','Michel','Morgan','Nicolas','Normand','Norbert','Olivier,',
  72. 'Pascal','Patrice','Pierrick','Raphael','Roland','Stéphane','Sylvain','Sylvestre','Timothée','Thomas','Tristan',
  73. 'Ulysse','Vincent','Victor','Vivien',
  74. 'Adeline','Albane','Alix','Amélie','Arielle','Aurelle','Blandine','Blanche','Brigitte','Berthe','Camille','Capucine',
  75. 'Catherine','Cécile','Charlotte','Chloé','Claudine','Clémenence','Constance','Cyrielle','Corinne','Danielle',
  76. 'Delphine','Denise','Dominique','Diane','Édith','Éliane','Éléonore','Émilie','Emmanuelle','Ève','Évelyne',
  77. 'Fanny','Flavie','Flore','Françoise', 'Gabrielle','Gaëlle','Geneviève','Georgette','Germaine','Gertrude','Gisèle',
  78. 'Gwenaëlle','Hélène','Héloïse','Henriette','Huguette','Inès','Isabelle','Jacqueline','Jeanne','Joëlle','Joséphine',
  79. 'Juliette','Justine','Julie','Laura','Laure','Léa','Léonie','Lucie','Lucienne','Lorraine','Lucille','Ludivine','Lydie',
  80. 'Margot','Marion','Marlène','Marthe','Mélodie','Monique','Noémie','Nadine','Nadège','Océane','Odette','Odile','Paulette',
  81. 'Rose','Roseline','Renée','Sabine','Sophie','Suzanne','Sylvie','Thérèse',
  82. ];
  83. $arr_noms_checked = [] ;
  84. $arr_prenoms_checked = [] ;
  85. $users = User::find()
  86. ->joinWith('userEtablissement')
  87. ->where('user_etablissement.id_etablissement = 1')
  88. ->all() ;
  89. foreach($users as $u)
  90. {
  91. if($u->email != 'boulanger@laboiteapain.net')
  92. {
  93. do {
  94. $i_nom = rand(0, count($arr_noms)-1) ;
  95. $i_prenom = rand(0, count($arr_prenoms)-1) ;
  96. } while(isset($arr_noms_checked[$i_nom]) || isset($arr_prenoms_checked[$i_prenom])) ;
  97. $arr_noms_checked[$i_nom] = true ;
  98. $arr_prenoms_checked[$i_prenom] = true ;
  99. $nom = $arr_noms[$i_nom] ;
  100. $prenom = $arr_prenoms[$i_prenom] ;
  101. $u->nom = $nom ;
  102. $u->prenom = $prenom ;
  103. $email = strtolower($prenom).'.'.strtolower($nom).'@yopmail.com' ;
  104. $email = htmlentities($email, ENT_NOQUOTES, 'utf-8');
  105. $email = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $email);
  106. $email = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $email); // pour les ligatures e.g. '&oelig;'
  107. $email = preg_replace('#&[^;]+;#', '', $email); // supprime les autres caractères
  108. $email = str_replace(' ','',$email) ;
  109. $u->email = $email ;
  110. $u->telephone = '0600000000' ;
  111. }
  112. else {
  113. /*
  114. * Le hash du mot de passe en production est volontairement corrompu
  115. * pour empêcher la connexion des utilisateurs via ce compte en prod.
  116. * Lors de l'initialisation des utilisateurs sur l'espace de démo,
  117. * on remet le bon hash pour que ce compte soit disponible.
  118. */
  119. $u->password_hash = '$2y$13$2D5T3Eo0pySmtlAuEGcfmOdTR5mleog8Y6YPGhop/ych6pbj6aN2y' ;
  120. }
  121. $u->save();
  122. }
  123. $commandes = Commande::find()
  124. ->where('username IS NOT NULL')
  125. ->all() ;
  126. foreach($commandes as $c)
  127. {
  128. $nom = $arr_noms[rand(0, count($arr_noms)-1)] ;
  129. $prenom = $arr_prenoms[rand(0, count($arr_prenoms)-1)] ;
  130. $c->username = $prenom.' '.$nom ;
  131. $c->save() ;
  132. }
  133. $commandes_auto = CommandeAuto::find()
  134. ->where('username IS NOT NULL')
  135. ->all() ;
  136. foreach($commandes_auto as $c)
  137. {
  138. $nom = $arr_noms[rand(0, count($arr_noms)-1)] ;
  139. $prenom = $arr_prenoms[rand(0, count($arr_prenoms)-1)] ;
  140. $c->username = $prenom.' '.$nom ;
  141. $c->save() ;
  142. }
  143. }
  144. }
  145. public function actionSendCommandes($key = '')
  146. {
  147. if($key == '64ac0bdab7e9f5e48c4d991ec5201d57')
  148. {
  149. $heure = date('H') ;
  150. if($heure == '00')
  151. {
  152. $date = date('Y-m-d') ;
  153. }
  154. else {
  155. $date = date('Y-m-d', time()+24*60*60) ;
  156. }
  157. $etablissements = Etablissement::find()->all() ;
  158. foreach($etablissements as $e)
  159. {
  160. $production = Production::findOne([
  161. 'date' => $date,
  162. 'actif' => 1,
  163. 'id_etablissement' => $e['id'],
  164. ]) ;
  165. if($production && $heure == $e['heure_limite_commande'])
  166. {
  167. $commandes = Commande::find()
  168. ->with('commandeProduits', 'user')
  169. ->joinWith('production')
  170. ->where(['production.date' => $date])
  171. ->andWhere(['production.id_etablissement' => $e['id']])
  172. ->orderBy('date ASC')
  173. ->all();
  174. $user = User::findOne([
  175. 'id_etablissement' => $e['id'],
  176. 'status' => User::STATUS_BOULANGER
  177. ]) ;
  178. $mail = Yii::$app->mailer->compose(
  179. [
  180. 'html' => 'cronRecapCommandes-html',
  181. 'text' => 'cronRecapCommandes-text',
  182. ],
  183. [
  184. 'date' => $date,
  185. 'commandes' => $commandes
  186. ]
  187. )
  188. ->setTo($user->email)
  189. ->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) ;
  190. if(count($commandes))
  191. {
  192. $sujet = '[La boîte à pain] Commandes du '.date('d/m',strtotime($date)) ;
  193. // génération du pdf de commande
  194. Yii::$app->runAction('commande/report-cron', [
  195. 'date' => $date,
  196. 'save' => true,
  197. 'id_etablissement' => $e['id'] ,
  198. 'key' => '64ac0bdab7e9f5e48c4d991ec5201d57'
  199. ]);
  200. $mail->attach(Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$e['id'].'.pdf')) ;
  201. }
  202. else {
  203. $sujet = '[La boîte à pain] Aucune commande' ;
  204. }
  205. $mail->setSubject($sujet)
  206. ->send();
  207. }
  208. }
  209. }
  210. }
  211. }