Browse Source

[Administration] Emails automatiques

feature/rotating_product
Guillaume Bourgeois 4 months ago
parent
commit
efe352df87
13 changed files with 126 additions and 18 deletions
  1. +1
    -0
      backend/controllers/AutomaticEmailController.php
  2. +3
    -4
      backend/controllers/CommunicateController.php
  3. +1
    -1
      backend/models/MailForm.php
  4. +36
    -0
      console/commands/AutomaticEmailController.php
  5. +2
    -0
      console/config/main.php
  6. +18
    -0
      domain/Communication/AutomaticEmail/AutomaticEmailManager.php
  7. +6
    -0
      domain/Communication/AutomaticEmail/AutomaticEmailModule.php
  8. +5
    -0
      domain/Communication/AutomaticEmail/AutomaticEmailRepository.php
  9. +32
    -0
      domain/Communication/AutomaticEmail/AutomaticEmailResolver.php
  10. +1
    -1
      domain/Communication/Email/ContactListResolver.php
  11. +6
    -9
      domain/Communication/Email/EmailGenerator.php
  12. +9
    -3
      domain/Communication/Email/EmailModule.php
  13. +6
    -0
      domain/Producer/Producer/ProducerSolver.php

+ 1
- 0
backend/controllers/AutomaticEmailController.php View File

@@ -87,6 +87,7 @@ class AutomaticEmailController extends BackendController
$automaticEmail = $automaticEmailModule->getManager()->createAutomaticEmail(
$this->getProducerCurrent(),
$automaticEmailModel->getDay(),
$automaticEmailModel->getDelayBeforeDistribution(),
$automaticEmailModel->getSubject(),
$automaticEmailModel->getMessage(),
$automaticEmailModel->getIntegrateProductList()

+ 3
- 4
backend/controllers/CommunicateController.php View File

@@ -41,7 +41,7 @@ namespace backend\controllers;
use backend\models\MailForm;
use common\helpers\GlobalParam;
use domain\Communication\Email\ContactListResolver;
use domain\Communication\Email\EmailManager;
use domain\Communication\Email\EmailGenerator;
use domain\PointSale\PointSale\PointSale;
use kartik\mpdf\Pdf;
use yii\filters\AccessControl;
@@ -87,7 +87,6 @@ class CommunicateController extends BackendController
$idDistribution = 0)
{
$emailModule = $this->getEmailModule();
$emailManager = $emailModule->getManager();

$mailForm = new MailForm();
// Sujet et message par défaut
@@ -119,14 +118,14 @@ class CommunicateController extends BackendController
? $this->getDistributionModule()->getRepository()->findOneDistributionById($mailForm->id_distribution)
: null;

$email = $emailManager->createEmail(
$email = $emailModule->getGenerator()->createEmail(
$mailForm->subject,
$mailForm->message,
$mailForm->integrate_product_list,
$this->getProducerCurrent(),
$distribution
);
$emailManager->sendBulkEmail($email, $usersArray);
$emailModule->getBulkMailer()->sendEmail($email, $usersArray);

$this->setFlash('success', 'Votre email a bien été envoyé.');
}

+ 1
- 1
backend/models/MailForm.php View File

@@ -39,7 +39,7 @@ termes.
namespace backend\models;

use common\helpers\GlobalParam;
use domain\Communication\Email\EmailManager;
use domain\Communication\Email\EmailGenerator;
use domain\Distribution\Distribution\DistributionModule;
use Yii;
use yii\base\Model;

+ 36
- 0
console/commands/AutomaticEmailController.php View File

@@ -0,0 +1,36 @@
<?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";
}
}
}
}
}

?>

+ 2
- 0
console/config/main.php View File

@@ -36,6 +36,8 @@ pris connaissance de la licence CeCILL, et que vous en avez accepté les
termes.
*/

setlocale(LC_TIME, 'fr_FR.utf8','fra');

$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),

+ 18
- 0
domain/Communication/AutomaticEmail/AutomaticEmailManager.php View File

@@ -3,20 +3,26 @@
namespace domain\Communication\AutomaticEmail;

use domain\_\AbstractManager;
use domain\Communication\Email\Email;
use domain\Communication\Email\EmailGenerator;
use domain\Distribution\Distribution\Distribution;
use domain\Producer\Producer\Producer;

class AutomaticEmailManager extends AbstractManager
{
protected AutomaticEmailBuilder $automaticEmailBuilder;
protected EmailGenerator $emailGenerator;

public function loadDependencies(): void
{
$this->automaticEmailBuilder = $this->loadService(AutomaticEmailBuilder::class);
$this->emailGenerator = $this->loadService(EmailGenerator::class);
}

public function createAutomaticEmail(
Producer $producer,
int $day,
int $delayBeforeDistribution,
string $subject,
string $message,
bool $integrateProductList = null
@@ -24,6 +30,7 @@ class AutomaticEmailManager extends AbstractManager
{
$automaticEmail = $this->automaticEmailBuilder->instanciateAutomaticEmail($producer);
$automaticEmail->setDay($day);
$automaticEmail->setDelayBeforeDistribution($delayBeforeDistribution);
$automaticEmail->setSubject($subject);
$automaticEmail->setMessage($message);
$automaticEmail->setIntegrateProductList($integrateProductList);
@@ -33,4 +40,15 @@ class AutomaticEmailManager extends AbstractManager
return $automaticEmail;
}

public function createEmailFromAutomaticEmail(AutomaticEmail $automaticEmail, Distribution $distribution): Email
{
return $this->emailGenerator->createEmail(
$automaticEmail->getSubject(),
$automaticEmail->getMessage(),
$automaticEmail->getIntegrateProductList(),
$automaticEmail->getProducer(),
$distribution
);
}

}

+ 6
- 0
domain/Communication/AutomaticEmail/AutomaticEmailModule.php View File

@@ -12,6 +12,7 @@ class AutomaticEmailModule extends AbstractModule
AutomaticEmailDefinition::class,
AutomaticEmailBuilder::class,
AutomaticEmailRepository::class,
AutomaticEmailResolver::class,
AutomaticEmailManager::class
];
}
@@ -31,6 +32,11 @@ class AutomaticEmailModule extends AbstractModule
return AutomaticEmailRepository::getInstance();
}

public function getResolver(): AutomaticEmailResolver
{
return AutomaticEmailResolver::getInstance();
}

public function getManager(): AutomaticEmailManager
{
return AutomaticEmailManager::getInstance();

+ 5
- 0
domain/Communication/AutomaticEmail/AutomaticEmailRepository.php View File

@@ -35,4 +35,9 @@ class AutomaticEmailRepository extends AbstractRepository
{
return $this->createDefaultQuery();
}

public function findAutomaticEmails(): array
{
return $this->queryAutomaticEmails()->find();
}
}

+ 32
- 0
domain/Communication/AutomaticEmail/AutomaticEmailResolver.php View File

@@ -0,0 +1,32 @@
<?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;
}
}

+ 1
- 1
domain/Communication/Email/ContactListResolver.php View File

@@ -56,7 +56,7 @@ class ContactListResolver extends AbstractResolver
}
else {
$users = $this->userRepository->queryUsersBy([
'id_producer' => GlobalParam::getCurrentProducerId(),
'id_producer' => $producer->id,
'id_point_sale' => $pointSale ? $pointSale->id : null,
'users_point_sale_link' => $usersPointSaleLink,
'users_point_sale_has_order' => $usersPointSaleHasOrder,

domain/Communication/Email/EmailManager.php → domain/Communication/Email/EmailGenerator.php View File

@@ -5,6 +5,7 @@ namespace domain\Communication\Email;
use common\helpers\GlobalParam;
use common\helpers\Price;
use domain\_\AbstractResolver;
use domain\Communication\AutomaticEmail\AutomaticEmail;
use domain\Config\Unit\UnitDefinition;
use domain\Distribution\Distribution\Distribution;
use domain\Distribution\Distribution\DistributionSolver;
@@ -14,7 +15,7 @@ use domain\Product\Product\Product;
use domain\Product\Product\ProductSolver;
use yii\helpers\Html;

class EmailManager extends AbstractResolver
class EmailGenerator extends AbstractResolver
{
protected EmailBuilder $emailBuilder;
protected ProductSolver $productSolver;
@@ -29,11 +30,6 @@ class EmailManager extends AbstractResolver
$this->producerSolver = $this->loadService(ProducerSolver::class);
}

public function sendBulkEmail(Email $email, array $usersArray)
{
\Yii::$app->bulkMailer->sendEmail($email, $usersArray);
}

public function createEmail(
string $subject,
string $message,
@@ -119,7 +115,8 @@ Produits disponibles :
$fromName = $producer->name ;

$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
$messageAutoText .= "
@@ -137,8 +134,8 @@ Me désinscrire : ".$linkUnsubscribe;
$fromName = 'Souke' ;
}

$htmlContent = nl2br($message).$messageAutoHtml;
$textContent = $message.$messageAutoText;
$htmlContent = $message.$messageAutoHtml;
$textContent = strip_tags($message).$messageAutoText;

return $this->emailBuilder->instanciateEmail(
$fromName,

+ 9
- 3
domain/Communication/Email/EmailModule.php View File

@@ -2,6 +2,7 @@

namespace domain\Communication\Email;

use common\components\BulkMailer\BulkMailerInterface;
use domain\_\AbstractService;

class EmailModule extends AbstractService
@@ -16,7 +17,7 @@ class EmailModule extends AbstractService
return [
EmailBuilder::class,
ContactListResolver::class,
EmailManager::class,
EmailGenerator::class,
];
}

@@ -25,13 +26,18 @@ class EmailModule extends AbstractService
return EmailBuilder::getInstance();
}

public function getManager(): EmailManager
public function getGenerator(): EmailGenerator
{
return EmailManager::getInstance();
return EmailGenerator::getInstance();
}

public function getContactListResolver(): ContactListResolver
{
return ContactListResolver::getInstance();
}

public function getBulkMailer(): BulkMailerInterface
{
return \Yii::$app->bulkMailer;
}
}

+ 6
- 0
domain/Producer/Producer/ProducerSolver.php View File

@@ -3,6 +3,7 @@
namespace domain\Producer\Producer;

use common\helpers\GlobalParam;
use domain\Distribution\Distribution\Distribution;
use domain\User\User\User;
use domain\_\AbstractService;
use domain\_\SolverInterface;
@@ -276,6 +277,11 @@ class ProducerSolver extends AbstractService implements SolverInterface
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
{
if(!$date) {

Loading…
Cancel
Save