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.

221 lines
10KB

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