Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

229 lines
11KB

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