Browse Source

[Administration] Emails automatiques : adaptation pour envois un jour et une heure donnée + gestion par point de vente

develop
Guillaume Bourgeois 2 months ago
parent
commit
7b53ac3b78
12 changed files with 234 additions and 44 deletions
  1. +5
    -2
      backend/controllers/AutomaticEmailController.php
  2. +7
    -2
      backend/views/automatic-email/_form.php
  3. +12
    -7
      backend/views/automatic-email/index.php
  4. +21
    -0
      backend/web/js/backend.js
  5. +19
    -10
      common/components/Date.php
  6. +16
    -6
      console/commands/AutomaticEmailController.php
  7. +34
    -0
      console/migrations/m240912_075936_add_columns_automatic_email.php
  8. +81
    -8
      domain/Communication/AutomaticEmail/AutomaticEmail.php
  9. +0
    -1
      domain/Communication/AutomaticEmail/AutomaticEmailBuilder.php
  10. +25
    -6
      domain/Communication/AutomaticEmail/AutomaticEmailManager.php
  11. +1
    -1
      domain/Communication/Email/ContactListResolver.php
  12. +13
    -1
      domain/Communication/Email/EmailGenerator.php

+ 5
- 2
backend/controllers/AutomaticEmailController.php View File

if ($automaticEmailModel->load(\Yii::$app->request->post()) && $automaticEmailModel->validate()) { if ($automaticEmailModel->load(\Yii::$app->request->post()) && $automaticEmailModel->validate()) {
$automaticEmail = $automaticEmailModule->getManager()->createAutomaticEmail( $automaticEmail = $automaticEmailModule->getManager()->createAutomaticEmail(
$this->getProducerCurrent(), $this->getProducerCurrent(),
$automaticEmailModel->getDay(),
$automaticEmailModel->getDelayBeforeDistribution(),
$automaticEmailModel->getSubject(), $automaticEmailModel->getSubject(),
$automaticEmailModel->getMessage(), $automaticEmailModel->getMessage(),
$automaticEmailModel->getDay(),
$automaticEmailModel->getPointSale(),
$automaticEmailModel->getDelayBeforeDistribution(),
$automaticEmailModel->getSendingDay(),
$automaticEmailModel->getSendingHour(),
$automaticEmailModel->getIntegrateProductList() $automaticEmailModel->getIntegrateProductList()
); );
$this->setFlash('success', "Email automatique ajouté"); $this->setFlash('success', "Email automatique ajouté");

+ 7
- 2
backend/views/automatic-email/_form.php View File

use lo\widgets\Toggle; use lo\widgets\Toggle;
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;


$pointSaleModule = $this->getPointSaleModule();

?> ?>


<div class="automatic-email-form"> <div class="automatic-email-form">
], ],
]); ]);
?> ?>
<?= $form->field($automaticEmail, 'day')->dropDownList(Date::getDaysOfWeekArray()) ?>
<?= $form->field($automaticEmail, 'delay_before_distribution')->dropDownList(Dropdown::numberChoices(1, 7, false, ' jour(s) avant')); ?>
<?= $form->field($automaticEmail, 'day')->dropDownList(Date::getDaysOfWeekArray(true)) ?>
<?= $form->field($automaticEmail, 'id_point_sale')->dropDownList($pointSaleModule->getRepository()->populatePointSaleDropdownList()) ?>
<?= $form->field($automaticEmail, 'delay_before_distribution')->dropDownList(Dropdown::numberChoices(1, 7, true, ' jour(s) avant')); ?>
<?= $form->field($automaticEmail, 'sending_day')->dropDownList(Date::getDaysOfWeekArray(true)) ?>
<?= $form->field($automaticEmail, 'sending_hour')->dropDownList(Dropdown::numberChoices(0, 23, true, 'h')) ?>
<?= $form->field($automaticEmail, 'subject')->textInput() ?> <?= $form->field($automaticEmail, 'subject')->textInput() ?>
<?= $form->field($automaticEmail, 'message')->widget(letyii\tinymce\Tinymce::class, [ <?= $form->field($automaticEmail, 'message')->widget(letyii\tinymce\Tinymce::class, [
'configs' => [ 'configs' => [

+ 12
- 7
backend/views/automatic-email/index.php View File



?> ?>


<div class="callout callout-info">
<p>
<i class="icon fa fa-info-circle"></i>
L'heure d'envoi des emails automatiques est programmée à 7h
</p>
</div>

<div class="automatic-email-index"> <div class="automatic-email-index">
<?= GridView::widget([ <?= GridView::widget([
'dataProvider' => $dataProvider, 'dataProvider' => $dataProvider,
return $automaticEmail->getDelayBeforeDistributionAsString(); return $automaticEmail->getDelayBeforeDistributionAsString();
} }
], ],
[
'attribute' => 'sending_day',
'value' => function(AutomaticEmail $automaticEmail) {
return $automaticEmail->getSendingDayAsString();
}
],
[
'attribute' => 'sending_hour',
'value' => function(AutomaticEmail $automaticEmail) {
return $automaticEmail->getSendingHour().'h';
}
],
'subject', 'subject',
[ [
'attribute' => 'message', 'attribute' => 'message',

+ 21
- 0
backend/web/js/backend.js View File

opendistrib_tinymce_responsive(); opendistrib_tinymce_responsive();
opendistrib_sponsorship(); opendistrib_sponsorship();
opendistrib_clipboard_paste(); opendistrib_clipboard_paste();
opendistrib_automatic_email();
}); });


function opendistrib_automatic_email() {
if($('.automatic-email-form').size()) {
opendistrib_automatic_email_form_event();
$('#automaticemail-day').change(function() {
opendistrib_automatic_email_form_event();
});
}
}

function opendistrib_automatic_email_form_event() {
var day = parseFloat($('#automaticemail-day').val());
if(day) {
$('.field-automaticemail-sending_day').hide();
$('.field-automaticemail-delay_before_distribution').show();
}
else {
$('.field-automaticemail-sending_day').show();
$('.field-automaticemail-delay_before_distribution').hide();
}
}


function label_unit_reference(unit) { function label_unit_reference(unit) {
if(unit == 'piece') { if(unit == 'piece') {

+ 19
- 10
common/components/Date.php View File



class Date class Date
{ {
public static function getDaysOfWeekArray(): array
public static function getDaysOfWeekArray(bool $withNullValue = false): array
{ {
return [
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
6 => 'Samedi',
7 => 'Dimanche'
];
$daysOfWeekArray = [];

if($withNullValue) {
$daysOfWeekArray[null] = '--';
}

$daysOfWeekArray = $daysOfWeekArray +
[
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
6 => 'Samedi',
7 => 'Dimanche'
];

return $daysOfWeekArray;
} }


public static function getDayOfWeekStringByNumber(int $dayOfWeekNumber): string public static function getDayOfWeekStringByNumber(int $dayOfWeekNumber): string

+ 16
- 6
console/commands/AutomaticEmailController.php View File

// ./yii automatic-email/send // ./yii automatic-email/send
public function actionSend() public function actionSend()
{ {
$dayToday = date('N');
$currentHour = date('G');
$automaticEmailModule = AutomaticEmailModule::getInstance(); $automaticEmailModule = AutomaticEmailModule::getInstance();
$producerModule = ProducerModule::getInstance(); $producerModule = ProducerModule::getInstance();
$emailModule = EmailModule::getInstance(); $emailModule = EmailModule::getInstance();
\Yii::$app->logic->setProducerContext($producer); \Yii::$app->logic->setProducerContext($producer);
$automaticEmailsArray = $automaticEmailModule->getRepository()->findAutomaticEmails(); $automaticEmailsArray = $automaticEmailModule->getRepository()->findAutomaticEmails();
foreach($automaticEmailsArray as $automaticEmail) { foreach($automaticEmailsArray as $automaticEmail) {
$distribution = $automaticEmailModule->getResolver()->getMatchedDistribution($automaticEmail);
if($automaticEmail->isEnabled() && $distribution) {
$email = $automaticEmailModule->getManager()->createEmailFromAutomaticEmail($automaticEmail, $distribution);
$usersArray = $emailModule->getContactListResolver()->search($producer, Email::TYPE_ORDER_TAKING, $distribution);
$emailModule->getBulkMailer()->sendEmail($email, $usersArray);
echo 'Email automatique "'.$automaticEmail->getSubject().'" envoyé à '.count($usersArray)." utilisateur(s)\n";
if($automaticEmail->isEnabled()) {
$distribution = $automaticEmailModule->getResolver()->getMatchedDistribution($automaticEmail);
if(($distribution || $automaticEmail->getSendingDay() == $dayToday) && $automaticEmail->getSendingHour() == $currentHour) {
$email = $automaticEmailModule->getManager()->createEmailFromAutomaticEmail($automaticEmail, $distribution);
$usersArray = $emailModule->getContactListResolver()->search(
$producer,
Email::TYPE_ORDER_TAKING,
$distribution,
$automaticEmail->getPointSale()
);
//$emailModule->getBulkMailer()->sendEmail($email, $usersArray);
echo $email->getHtmlContent();
echo 'Email automatique "'.$automaticEmail->getSubject().'" envoyé à '.count($usersArray)." utilisateur(s)\n";
}
} }
} }
} }

+ 34
- 0
console/migrations/m240912_075936_add_columns_automatic_email.php View File

<?php

use yii\db\Migration;
use yii\db\Schema;

/**
* Class m240912_075936_add_columns_automatic_email
*/
class m240912_075936_add_columns_automatic_email extends Migration
{
/**
* {@inheritdoc}
*/
public function safeUp()
{
$this->addColumn('automatic_email', 'id_point_sale', Schema::TYPE_INTEGER);
$this->addColumn('automatic_email', 'sending_day', Schema::TYPE_INTEGER);
$this->addColumn('automatic_email', 'sending_hour', Schema::TYPE_INTEGER);
$this->addForeignKey('automatic_email_fk_id_point_sale', 'automatic_email', 'id_point_sale', 'point_sale', 'id');

$this->alterColumn('automatic_email', 'day', Schema::TYPE_INTEGER);
$this->alterColumn('automatic_email', 'delay_before_distribution', Schema::TYPE_INTEGER);
}

/**
* {@inheritdoc}
*/
public function safeDown()
{
$this->dropColumn('automatic_email', 'id_point_sale');
$this->dropColumn('automatic_email', 'sending_day');
$this->dropColumn('automatic_email', 'sending_hour');
}
}

+ 81
- 8
domain/Communication/AutomaticEmail/AutomaticEmail.php View File

use common\components\ActiveRecordCommon; use common\components\ActiveRecordCommon;
use common\components\Date; use common\components\Date;
use domain\_\StatusInterface; use domain\_\StatusInterface;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer; use domain\Producer\Producer\Producer;
use yii\db\ActiveQuery; use yii\db\ActiveQuery;


public function rules() public function rules()
{ {
return [ return [
[['id_producer', 'day', 'subject', 'message', 'status'], 'required'],
[['id_producer', 'subject', 'message', 'status', 'sending_hour'], 'required'],
['sending_day', 'required', 'when' => function($model) {
return !$model->getDay();
}],
['delay_before_distribution', 'required', 'when' => function($model) {
return $model->getDay();
}],
[['subject', 'message'], 'string'], [['subject', 'message'], 'string'],
[['id_producer', 'day', 'delay_before_distribution', 'status'], 'integer'],
[['id_producer', 'day', 'delay_before_distribution', 'status', 'id_point_sale', 'sending_day', 'sending_hour'], 'integer'],
[['integrate_product_list'], 'boolean'] [['integrate_product_list'], 'boolean']
]; ];
} }
{ {
return [ return [
'id_producer' => 'Producteur', 'id_producer' => 'Producteur',
'day' => 'Jour de distribution',
'day' => 'Distribution',
'delay_before_distribution' => 'Envoi du message', 'delay_before_distribution' => 'Envoi du message',
'subject' => 'Sujet', 'subject' => 'Sujet',
'message' => 'Message', 'message' => 'Message',
'integrate_product_list' => 'Intégrer la liste des produits au message', 'integrate_product_list' => 'Intégrer la liste des produits au message',
'status' => 'Statut'
'status' => 'Statut',
'id_point_sale' => 'Point de vente',
'sending_day' => "Jour d'envoi",
'sending_hour' => "Heure d'envoi",
]; ];
} }




public function getDayAsString(): string public function getDayAsString(): string
{ {
if(!$this->getDay()) {
return '';
}

return Date::getDayOfWeekStringByNumber($this->getDay()); return Date::getDayOfWeekStringByNumber($this->getDay());
} }


public function getSendingDayAsString(): string
{
if(!$this->getSendingDay()) {
return '';
}

return Date::getDayOfWeekStringByNumber($this->getSendingDay());
}

public function getDelayBeforeDistributionAsString(): string public function getDelayBeforeDistributionAsString(): string
{ {
if(!$this->getDelayBeforeDistribution()) {
return '';
}

return $this->getDelayBeforeDistribution().' jour(s) avant'; return $this->getDelayBeforeDistribution().' jour(s) avant';
} }


return $this; return $this;
} }


public function getDay(): int
public function getDay(): ?int
{ {
if(!$this->day) {
return null;
}

return $this->day; return $this->day;
} }


public function setDay(int $day): self
public function setDay(?int $day): self
{ {
$this->day = $day; $this->day = $day;
return $this; return $this;
} }


public function getDelayBeforeDistribution(): int
public function getDelayBeforeDistribution(): ?int
{ {
if(!$this->delay_before_distribution) {
return 0;
}

return $this->delay_before_distribution; return $this->delay_before_distribution;
} }


public function setDelayBeforeDistribution(int $delayBeforeDistribution): self
public function setDelayBeforeDistribution(?int $delayBeforeDistribution): self
{ {
$this->delay_before_distribution = $delayBeforeDistribution; $this->delay_before_distribution = $delayBeforeDistribution;
return $this; return $this;
return $this; return $this;
} }


public function getPointSale(): ?PointSale
{
return $this->pointSaleRelation;
}

public function setPointSale(PointSale $pointSale): self
{
$this->populateFieldObject('id_point_sale', 'pointSaleRelation', $pointSale);
return $this;
}

public function getSendingDay(): ?int
{
return $this->sending_day;
}

public function setSendingDay(?int $sendingDay): self
{
$this->sending_day = $sendingDay;
return $this;
}

public function getSendingHour(): ?int
{
return $this->sending_hour;
}

public function setSendingHour(?int $sendingHour): self
{
$this->sending_hour = $sendingHour;
return $this;
}

/* Relations */ /* Relations */


public function getProducerRelation(): ActiveQuery public function getProducerRelation(): ActiveQuery
{ {
return $this->hasOne(Producer::class, ['id' => 'id_producer']); return $this->hasOne(Producer::class, ['id' => 'id_producer']);
} }

public function getPointSaleRelation(): ActiveQuery
{
return $this->hasOne(PointSale::class, ['id' => 'id_point_sale']);
}
} }

+ 0
- 1
domain/Communication/AutomaticEmail/AutomaticEmailBuilder.php View File

{ {
$automaticEmail = new AutomaticEmail(); $automaticEmail = new AutomaticEmail();
$automaticEmail->setProducer($producer); $automaticEmail->setProducer($producer);
$automaticEmail->setDelayBeforeDistribution(2);
$automaticEmail->setStatus(StatusInterface::STATUS_ONLINE); $automaticEmail->setStatus(StatusInterface::STATUS_ONLINE);
return $automaticEmail; return $automaticEmail;
} }

+ 25
- 6
domain/Communication/AutomaticEmail/AutomaticEmailManager.php View File

use domain\Communication\Email\Email; use domain\Communication\Email\Email;
use domain\Communication\Email\EmailGenerator; use domain\Communication\Email\EmailGenerator;
use domain\Distribution\Distribution\Distribution; use domain\Distribution\Distribution\Distribution;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer; use domain\Producer\Producer\Producer;


class AutomaticEmailManager extends AbstractManager class AutomaticEmailManager extends AbstractManager


public function createAutomaticEmail( public function createAutomaticEmail(
Producer $producer, Producer $producer,
int $day,
int $delayBeforeDistribution,
string $subject, string $subject,
string $message, string $message,
int $day = null,
PointSale $pointSale = null,
int $delayBeforeDistribution = null,
int $sendingDay = null,
int $sendingHour = null,
bool $integrateProductList = null bool $integrateProductList = null
): AutomaticEmail ): AutomaticEmail
{ {
$automaticEmail = $this->automaticEmailBuilder->instanciateAutomaticEmail($producer); $automaticEmail = $this->automaticEmailBuilder->instanciateAutomaticEmail($producer);
$automaticEmail->setDay($day);
$automaticEmail->setDelayBeforeDistribution($delayBeforeDistribution);

if($day) {
$automaticEmail->setDay($day);
$automaticEmail->setSendingDay(null);
$automaticEmail->setDelayBeforeDistribution($delayBeforeDistribution);
}
else {
$automaticEmail->setSendingDay($sendingDay);
$automaticEmail->setDelayBeforeDistribution(null);
}

if($pointSale) {
$automaticEmail->setPointSale($pointSale);
}

$automaticEmail->setSendingHour($sendingHour);
$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
public function createEmailFromAutomaticEmail(AutomaticEmail $automaticEmail, Distribution $distribution = null): Email
{ {
return $this->emailGenerator->createEmail( return $this->emailGenerator->createEmail(
$automaticEmail->getSubject(), $automaticEmail->getSubject(),
$automaticEmail->getMessage(), $automaticEmail->getMessage(),
$automaticEmail->getIntegrateProductList(), $automaticEmail->getIntegrateProductList(),
$automaticEmail->getProducer(), $automaticEmail->getProducer(),
$distribution
$distribution,
true
); );
} }



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

$users = []; $users = [];


foreach($distribution->pointSaleDistribution as $pointSaleDistribution) { foreach($distribution->pointSaleDistribution as $pointSaleDistribution) {
if($pointSaleDistribution->delivery) {
if($pointSaleDistribution->delivery && (!$pointSale || $pointSale->id == $pointSaleDistribution->id_point_sale)) {
$usersPointSaleArray = $this->userRepository->queryUsersBy([ $usersPointSaleArray = $this->userRepository->queryUsersBy([
'id_producer' => $producer->id, 'id_producer' => $producer->id,
'id_point_sale' => $pointSaleDistribution->id_point_sale, 'id_point_sale' => $pointSaleDistribution->id_point_sale,

+ 13
- 1
domain/Communication/Email/EmailGenerator.php View File

string $message, string $message,
bool $integrateProductList, bool $integrateProductList,
Producer $producer = null, Producer $producer = null,
Distribution $distribution = null
Distribution $distribution = null,
bool $addLinkOrderWhenNoDistribution = false
): Email ): Email
{ {
$messageAutoText = '' ; $messageAutoText = '' ;
} }
} }
} }
else {
if($addLinkOrderWhenNoDistribution) {
$linkOrder = \Yii::$app->urlManagerProducer->createAbsoluteUrl([
'order/order',
'slug_producer' => $this->getProducerContext()->slug
]) ;
$messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande</a>' ;
$messageAutoText .= 'Suivez ce lien pour passer votre commande :
'.$linkOrder ;
}
}


if($producer) { if($producer) {
$fromEmail = $this->producerSolver->getProducerEmailPlatform($producer) ; $fromEmail = $this->producerSolver->getProducerEmailPlatform($producer) ;

Loading…
Cancel
Save