$automaticEmail = $automaticEmailModule->getManager()->createAutomaticEmail( | $automaticEmail = $automaticEmailModule->getManager()->createAutomaticEmail( | ||||
$this->getProducerCurrent(), | $this->getProducerCurrent(), | ||||
$automaticEmailModel->getDay(), | $automaticEmailModel->getDay(), | ||||
$automaticEmailModel->getDelayBeforeDistribution(), | |||||
$automaticEmailModel->getSubject(), | $automaticEmailModel->getSubject(), | ||||
$automaticEmailModel->getMessage(), | $automaticEmailModel->getMessage(), | ||||
$automaticEmailModel->getIntegrateProductList() | $automaticEmailModel->getIntegrateProductList() |
use backend\models\MailForm; | use backend\models\MailForm; | ||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use domain\Communication\Email\ContactListResolver; | use domain\Communication\Email\ContactListResolver; | ||||
use domain\Communication\Email\EmailManager; | |||||
use domain\Communication\Email\EmailGenerator; | |||||
use domain\PointSale\PointSale\PointSale; | use domain\PointSale\PointSale\PointSale; | ||||
use kartik\mpdf\Pdf; | use kartik\mpdf\Pdf; | ||||
use yii\filters\AccessControl; | use yii\filters\AccessControl; | ||||
$idDistribution = 0) | $idDistribution = 0) | ||||
{ | { | ||||
$emailModule = $this->getEmailModule(); | $emailModule = $this->getEmailModule(); | ||||
$emailManager = $emailModule->getManager(); | |||||
$mailForm = new MailForm(); | $mailForm = new MailForm(); | ||||
// Sujet et message par défaut | // Sujet et message par défaut | ||||
? $this->getDistributionModule()->getRepository()->findOneDistributionById($mailForm->id_distribution) | ? $this->getDistributionModule()->getRepository()->findOneDistributionById($mailForm->id_distribution) | ||||
: null; | : null; | ||||
$email = $emailManager->createEmail( | |||||
$email = $emailModule->getGenerator()->createEmail( | |||||
$mailForm->subject, | $mailForm->subject, | ||||
$mailForm->message, | $mailForm->message, | ||||
$mailForm->integrate_product_list, | $mailForm->integrate_product_list, | ||||
$this->getProducerCurrent(), | $this->getProducerCurrent(), | ||||
$distribution | $distribution | ||||
); | ); | ||||
$emailManager->sendBulkEmail($email, $usersArray); | |||||
$emailModule->getBulkMailer()->sendEmail($email, $usersArray); | |||||
$this->setFlash('success', 'Votre email a bien été envoyé.'); | $this->setFlash('success', 'Votre email a bien été envoyé.'); | ||||
} | } |
namespace backend\models; | namespace backend\models; | ||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use domain\Communication\Email\EmailManager; | |||||
use domain\Communication\Email\EmailGenerator; | |||||
use domain\Distribution\Distribution\DistributionModule; | use domain\Distribution\Distribution\DistributionModule; | ||||
use Yii; | use Yii; | ||||
use yii\base\Model; | use yii\base\Model; |
<?php | |||||
namespace console\commands; | |||||
use domain\Communication\AutomaticEmail\AutomaticEmailModule; | |||||
use domain\Communication\Email\EmailModule; | |||||
use domain\Producer\Producer\ProducerModule; | |||||
use yii\console\Controller; | |||||
class AutomaticEmailController extends Controller | |||||
{ | |||||
// ./yii automatic-email/send | |||||
public function actionSend() | |||||
{ | |||||
$automaticEmailModule = AutomaticEmailModule::getInstance(); | |||||
$producerModule = ProducerModule::getInstance(); | |||||
$emailModule = EmailModule::getInstance(); | |||||
$producersArray = $producerModule->getRepository()->findProducers(); | |||||
foreach($producersArray as $producer) { | |||||
\Yii::$app->logic->setProducerContext($producer); | |||||
$automaticEmailsArray = $automaticEmailModule->getRepository()->findAutomaticEmails(); | |||||
foreach($automaticEmailsArray as $automaticEmail) { | |||||
$distribution = $automaticEmailModule->getResolver()->getMatchedDistribution($automaticEmail); | |||||
if($distribution) { | |||||
$email = $automaticEmailModule->getManager()->createEmailFromAutomaticEmail($automaticEmail, $distribution); | |||||
$usersArray = $emailModule->getContactListResolver()->search($producer, $distribution); | |||||
$emailModule->getBulkMailer()->sendEmail($email, $usersArray); | |||||
echo 'Email automatique "'.$automaticEmail->getSubject().'" envoyé à '.count($usersArray)." utilisateur(s)\n"; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} | |||||
?> |
termes. | termes. | ||||
*/ | */ | ||||
setlocale(LC_TIME, 'fr_FR.utf8','fra'); | |||||
$params = array_merge( | $params = array_merge( | ||||
require(__DIR__ . '/../../common/config/params.php'), | require(__DIR__ . '/../../common/config/params.php'), | ||||
require(__DIR__ . '/../../common/config/params-local.php'), | require(__DIR__ . '/../../common/config/params-local.php'), |
namespace domain\Communication\AutomaticEmail; | namespace domain\Communication\AutomaticEmail; | ||||
use domain\_\AbstractManager; | use domain\_\AbstractManager; | ||||
use domain\Communication\Email\Email; | |||||
use domain\Communication\Email\EmailGenerator; | |||||
use domain\Distribution\Distribution\Distribution; | |||||
use domain\Producer\Producer\Producer; | use domain\Producer\Producer\Producer; | ||||
class AutomaticEmailManager extends AbstractManager | class AutomaticEmailManager extends AbstractManager | ||||
{ | { | ||||
protected AutomaticEmailBuilder $automaticEmailBuilder; | protected AutomaticEmailBuilder $automaticEmailBuilder; | ||||
protected EmailGenerator $emailGenerator; | |||||
public function loadDependencies(): void | public function loadDependencies(): void | ||||
{ | { | ||||
$this->automaticEmailBuilder = $this->loadService(AutomaticEmailBuilder::class); | $this->automaticEmailBuilder = $this->loadService(AutomaticEmailBuilder::class); | ||||
$this->emailGenerator = $this->loadService(EmailGenerator::class); | |||||
} | } | ||||
public function createAutomaticEmail( | public function createAutomaticEmail( | ||||
Producer $producer, | Producer $producer, | ||||
int $day, | int $day, | ||||
int $delayBeforeDistribution, | |||||
string $subject, | string $subject, | ||||
string $message, | string $message, | ||||
bool $integrateProductList = null | bool $integrateProductList = null | ||||
{ | { | ||||
$automaticEmail = $this->automaticEmailBuilder->instanciateAutomaticEmail($producer); | $automaticEmail = $this->automaticEmailBuilder->instanciateAutomaticEmail($producer); | ||||
$automaticEmail->setDay($day); | $automaticEmail->setDay($day); | ||||
$automaticEmail->setDelayBeforeDistribution($delayBeforeDistribution); | |||||
$automaticEmail->setSubject($subject); | $automaticEmail->setSubject($subject); | ||||
$automaticEmail->setMessage($message); | $automaticEmail->setMessage($message); | ||||
$automaticEmail->setIntegrateProductList($integrateProductList); | $automaticEmail->setIntegrateProductList($integrateProductList); | ||||
return $automaticEmail; | return $automaticEmail; | ||||
} | } | ||||
public function createEmailFromAutomaticEmail(AutomaticEmail $automaticEmail, Distribution $distribution): Email | |||||
{ | |||||
return $this->emailGenerator->createEmail( | |||||
$automaticEmail->getSubject(), | |||||
$automaticEmail->getMessage(), | |||||
$automaticEmail->getIntegrateProductList(), | |||||
$automaticEmail->getProducer(), | |||||
$distribution | |||||
); | |||||
} | |||||
} | } |
AutomaticEmailDefinition::class, | AutomaticEmailDefinition::class, | ||||
AutomaticEmailBuilder::class, | AutomaticEmailBuilder::class, | ||||
AutomaticEmailRepository::class, | AutomaticEmailRepository::class, | ||||
AutomaticEmailResolver::class, | |||||
AutomaticEmailManager::class | AutomaticEmailManager::class | ||||
]; | ]; | ||||
} | } | ||||
return AutomaticEmailRepository::getInstance(); | return AutomaticEmailRepository::getInstance(); | ||||
} | } | ||||
public function getResolver(): AutomaticEmailResolver | |||||
{ | |||||
return AutomaticEmailResolver::getInstance(); | |||||
} | |||||
public function getManager(): AutomaticEmailManager | public function getManager(): AutomaticEmailManager | ||||
{ | { | ||||
return AutomaticEmailManager::getInstance(); | return AutomaticEmailManager::getInstance(); |
{ | { | ||||
return $this->createDefaultQuery(); | return $this->createDefaultQuery(); | ||||
} | } | ||||
public function findAutomaticEmails(): array | |||||
{ | |||||
return $this->queryAutomaticEmails()->find(); | |||||
} | |||||
} | } |
<?php | |||||
namespace domain\Communication\AutomaticEmail; | |||||
use domain\_\AbstractResolver; | |||||
use domain\Distribution\Distribution\Distribution; | |||||
use domain\Distribution\Distribution\DistributionRepository; | |||||
use domain\Producer\Producer\ProducerSolver; | |||||
class AutomaticEmailResolver extends AbstractResolver | |||||
{ | |||||
protected DistributionRepository $distributionRepository; | |||||
protected ProducerSolver $producerSolver; | |||||
public function loadDependencies(): void | |||||
{ | |||||
$this->distributionRepository = $this->loadService(DistributionRepository::class); | |||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | |||||
} | |||||
public function getMatchedDistribution(AutomaticEmail $automaticEmail): ?Distribution | |||||
{ | |||||
$date = (new \DateTime('+'.$automaticEmail->getDelayBeforeDistribution().' days'))->format('Y-m-d'); | |||||
$distribution = $this->distributionRepository->findOneDistribution($date); | |||||
if($distribution && $distribution->active && !$this->producerSolver->isOnLeavePeriodByDistribution($distribution)) { | |||||
return $distribution; | |||||
} | |||||
return null; | |||||
} | |||||
} |
} | } | ||||
else { | else { | ||||
$users = $this->userRepository->queryUsersBy([ | $users = $this->userRepository->queryUsersBy([ | ||||
'id_producer' => GlobalParam::getCurrentProducerId(), | |||||
'id_producer' => $producer->id, | |||||
'id_point_sale' => $pointSale ? $pointSale->id : null, | 'id_point_sale' => $pointSale ? $pointSale->id : null, | ||||
'users_point_sale_link' => $usersPointSaleLink, | 'users_point_sale_link' => $usersPointSaleLink, | ||||
'users_point_sale_has_order' => $usersPointSaleHasOrder, | 'users_point_sale_has_order' => $usersPointSaleHasOrder, |
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use common\helpers\Price; | use common\helpers\Price; | ||||
use domain\_\AbstractResolver; | use domain\_\AbstractResolver; | ||||
use domain\Communication\AutomaticEmail\AutomaticEmail; | |||||
use domain\Config\Unit\UnitDefinition; | use domain\Config\Unit\UnitDefinition; | ||||
use domain\Distribution\Distribution\Distribution; | use domain\Distribution\Distribution\Distribution; | ||||
use domain\Distribution\Distribution\DistributionSolver; | use domain\Distribution\Distribution\DistributionSolver; | ||||
use domain\Product\Product\ProductSolver; | use domain\Product\Product\ProductSolver; | ||||
use yii\helpers\Html; | use yii\helpers\Html; | ||||
class EmailManager extends AbstractResolver | |||||
class EmailGenerator extends AbstractResolver | |||||
{ | { | ||||
protected EmailBuilder $emailBuilder; | protected EmailBuilder $emailBuilder; | ||||
protected ProductSolver $productSolver; | protected ProductSolver $productSolver; | ||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | $this->producerSolver = $this->loadService(ProducerSolver::class); | ||||
} | } | ||||
public function sendBulkEmail(Email $email, array $usersArray) | |||||
{ | |||||
\Yii::$app->bulkMailer->sendEmail($email, $usersArray); | |||||
} | |||||
public function createEmail( | public function createEmail( | ||||
string $subject, | string $subject, | ||||
string $message, | string $message, | ||||
$fromName = $producer->name ; | $fromName = $producer->name ; | ||||
$linkProducer = 'https://'.$producer->slug.'.souke.fr'; | $linkProducer = 'https://'.$producer->slug.'.souke.fr'; | ||||
$linkUnsubscribe = \Yii::$app->urlManagerProducer->createAbsoluteUrl(['newsletter/unsubscribe', 'slug_producer' => $producer->slug]); | |||||
//$linkUnsubscribe = \Yii::$app->urlManagerProducer->createAbsoluteUrl(['newsletter/unsubscribe', 'slug_producer' => $producer->slug]); | |||||
$linkUnsubscribe = ''; | |||||
// Message inscription newsletter | // Message inscription newsletter | ||||
$messageAutoText .= " | $messageAutoText .= " | ||||
$fromName = 'Souke' ; | $fromName = 'Souke' ; | ||||
} | } | ||||
$htmlContent = nl2br($message).$messageAutoHtml; | |||||
$textContent = $message.$messageAutoText; | |||||
$htmlContent = $message.$messageAutoHtml; | |||||
$textContent = strip_tags($message).$messageAutoText; | |||||
return $this->emailBuilder->instanciateEmail( | return $this->emailBuilder->instanciateEmail( | ||||
$fromName, | $fromName, |
namespace domain\Communication\Email; | namespace domain\Communication\Email; | ||||
use common\components\BulkMailer\BulkMailerInterface; | |||||
use domain\_\AbstractService; | use domain\_\AbstractService; | ||||
class EmailModule extends AbstractService | class EmailModule extends AbstractService | ||||
return [ | return [ | ||||
EmailBuilder::class, | EmailBuilder::class, | ||||
ContactListResolver::class, | ContactListResolver::class, | ||||
EmailManager::class, | |||||
EmailGenerator::class, | |||||
]; | ]; | ||||
} | } | ||||
return EmailBuilder::getInstance(); | return EmailBuilder::getInstance(); | ||||
} | } | ||||
public function getManager(): EmailManager | |||||
public function getGenerator(): EmailGenerator | |||||
{ | { | ||||
return EmailManager::getInstance(); | |||||
return EmailGenerator::getInstance(); | |||||
} | } | ||||
public function getContactListResolver(): ContactListResolver | public function getContactListResolver(): ContactListResolver | ||||
{ | { | ||||
return ContactListResolver::getInstance(); | return ContactListResolver::getInstance(); | ||||
} | } | ||||
public function getBulkMailer(): BulkMailerInterface | |||||
{ | |||||
return \Yii::$app->bulkMailer; | |||||
} | |||||
} | } |
namespace domain\Producer\Producer; | namespace domain\Producer\Producer; | ||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use domain\Distribution\Distribution\Distribution; | |||||
use domain\User\User\User; | use domain\User\User\User; | ||||
use domain\_\AbstractService; | use domain\_\AbstractService; | ||||
use domain\_\SolverInterface; | use domain\_\SolverInterface; | ||||
return $producer->option_leave_period_start && $producer->option_leave_period_end; | return $producer->option_leave_period_start && $producer->option_leave_period_end; | ||||
} | } | ||||
public function isOnLeavePeriodByDistribution(Distribution $distribution): bool | |||||
{ | |||||
return $this->isOnLeavePeriod($distribution->producer, \DateTime::createFromFormat('Y-m-d', $distribution->date)); | |||||
} | |||||
public function isOnLeavePeriod(Producer $producer, \DateTime $date = null): bool | public function isOnLeavePeriod(Producer $producer, \DateTime $date = null): bool | ||||
{ | { | ||||
if(!$date) { | if(!$date) { |