Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

46 lines
2.0KB

  1. <?php
  2. namespace console\commands;
  3. use domain\Communication\AutomaticEmail\AutomaticEmailModule;
  4. use domain\Communication\Email\Email;
  5. use domain\Communication\Email\EmailModule;
  6. use domain\Producer\Producer\ProducerModule;
  7. use yii\console\Controller;
  8. class AutomaticEmailController extends Controller
  9. {
  10. // ./yii automatic-email/send
  11. public function actionSend()
  12. {
  13. $dayToday = date('N');
  14. $currentHour = date('G');
  15. $automaticEmailModule = AutomaticEmailModule::getInstance();
  16. $producerModule = ProducerModule::getInstance();
  17. $emailModule = EmailModule::getInstance();
  18. $producersArray = $producerModule->getRepository()->findProducers();
  19. foreach($producersArray as $producer) {
  20. \Yii::$app->logic->setProducerContext($producer);
  21. $automaticEmailsArray = $automaticEmailModule->getRepository()->findAutomaticEmails();
  22. foreach($automaticEmailsArray as $automaticEmail) {
  23. if($automaticEmail->isEnabled()) {
  24. $distribution = $automaticEmailModule->getResolver()->getMatchedDistribution($automaticEmail);
  25. if(($distribution || $automaticEmail->getSendingDay() == $dayToday) && $automaticEmail->getSendingHour() == $currentHour) {
  26. $email = $automaticEmailModule->getManager()->createEmailFromAutomaticEmail($automaticEmail, $distribution);
  27. $usersArray = $emailModule->getContactListResolver()->search(
  28. $producer,
  29. Email::TYPE_ORDER_TAKING,
  30. $distribution,
  31. $automaticEmail->getPointSale()
  32. );
  33. $emailModule->getBulkMailer()->sendEmail($email, $usersArray);
  34. echo 'Email automatique "'.$automaticEmail->getSubject().'" envoyé à '.count($usersArray)." utilisateur(s)\n";
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. ?>