@@ -86,10 +86,13 @@ class AutomaticEmailController extends BackendController | |||
if ($automaticEmailModel->load(\Yii::$app->request->post()) && $automaticEmailModel->validate()) { | |||
$automaticEmail = $automaticEmailModule->getManager()->createAutomaticEmail( | |||
$this->getProducerCurrent(), | |||
$automaticEmailModel->getDay(), | |||
$automaticEmailModel->getDelayBeforeDistribution(), | |||
$automaticEmailModel->getSubject(), | |||
$automaticEmailModel->getMessage(), | |||
$automaticEmailModel->getDay(), | |||
$automaticEmailModel->getPointSale(), | |||
$automaticEmailModel->getDelayBeforeDistribution(), | |||
$automaticEmailModel->getSendingDay(), | |||
$automaticEmailModel->getSendingHour(), | |||
$automaticEmailModel->getIntegrateProductList() | |||
); | |||
$this->setFlash('success', "Email automatique ajouté"); |
@@ -41,6 +41,8 @@ use common\helpers\Dropdown; | |||
use lo\widgets\Toggle; | |||
use yii\widgets\ActiveForm; | |||
$pointSaleModule = $this->getPointSaleModule(); | |||
?> | |||
<div class="automatic-email-form"> | |||
@@ -63,8 +65,11 @@ use yii\widgets\ActiveForm; | |||
], | |||
]); | |||
?> | |||
<?= $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, 'message')->widget(letyii\tinymce\Tinymce::class, [ | |||
'configs' => [ |
@@ -51,13 +51,6 @@ $this->addButton(['label' => 'Nouvel email automatique <span class="glyphicon gl | |||
?> | |||
<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"> | |||
<?= GridView::widget([ | |||
'dataProvider' => $dataProvider, | |||
@@ -74,6 +67,18 @@ $this->addButton(['label' => 'Nouvel email automatique <span class="glyphicon gl | |||
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', | |||
[ | |||
'attribute' => 'message', |
@@ -61,8 +61,29 @@ $(document).ready(function () { | |||
opendistrib_tinymce_responsive(); | |||
opendistrib_sponsorship(); | |||
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) { | |||
if(unit == 'piece') { |
@@ -4,17 +4,26 @@ namespace common\components; | |||
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 |
@@ -13,6 +13,8 @@ class AutomaticEmailController extends Controller | |||
// ./yii automatic-email/send | |||
public function actionSend() | |||
{ | |||
$dayToday = date('N'); | |||
$currentHour = date('G'); | |||
$automaticEmailModule = AutomaticEmailModule::getInstance(); | |||
$producerModule = ProducerModule::getInstance(); | |||
$emailModule = EmailModule::getInstance(); | |||
@@ -22,12 +24,20 @@ class AutomaticEmailController extends Controller | |||
\Yii::$app->logic->setProducerContext($producer); | |||
$automaticEmailsArray = $automaticEmailModule->getRepository()->findAutomaticEmails(); | |||
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"; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,34 @@ | |||
<?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'); | |||
} | |||
} |
@@ -5,6 +5,7 @@ namespace domain\Communication\AutomaticEmail; | |||
use common\components\ActiveRecordCommon; | |||
use common\components\Date; | |||
use domain\_\StatusInterface; | |||
use domain\PointSale\PointSale\PointSale; | |||
use domain\Producer\Producer\Producer; | |||
use yii\db\ActiveQuery; | |||
@@ -18,9 +19,15 @@ class AutomaticEmail extends ActiveRecordCommon | |||
public function rules() | |||
{ | |||
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'], | |||
[['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'] | |||
]; | |||
} | |||
@@ -29,12 +36,15 @@ class AutomaticEmail extends ActiveRecordCommon | |||
{ | |||
return [ | |||
'id_producer' => 'Producteur', | |||
'day' => 'Jour de distribution', | |||
'day' => 'Distribution', | |||
'delay_before_distribution' => 'Envoi du message', | |||
'subject' => 'Sujet', | |||
'message' => '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", | |||
]; | |||
} | |||
@@ -47,11 +57,28 @@ class AutomaticEmail extends ActiveRecordCommon | |||
public function getDayAsString(): string | |||
{ | |||
if(!$this->getDay()) { | |||
return ''; | |||
} | |||
return Date::getDayOfWeekStringByNumber($this->getDay()); | |||
} | |||
public function getSendingDayAsString(): string | |||
{ | |||
if(!$this->getSendingDay()) { | |||
return ''; | |||
} | |||
return Date::getDayOfWeekStringByNumber($this->getSendingDay()); | |||
} | |||
public function getDelayBeforeDistributionAsString(): string | |||
{ | |||
if(!$this->getDelayBeforeDistribution()) { | |||
return ''; | |||
} | |||
return $this->getDelayBeforeDistribution().' jour(s) avant'; | |||
} | |||
@@ -83,23 +110,31 @@ class AutomaticEmail extends ActiveRecordCommon | |||
return $this; | |||
} | |||
public function getDay(): int | |||
public function getDay(): ?int | |||
{ | |||
if(!$this->day) { | |||
return null; | |||
} | |||
return $this->day; | |||
} | |||
public function setDay(int $day): self | |||
public function setDay(?int $day): self | |||
{ | |||
$this->day = $day; | |||
return $this; | |||
} | |||
public function getDelayBeforeDistribution(): int | |||
public function getDelayBeforeDistribution(): ?int | |||
{ | |||
if(!$this->delay_before_distribution) { | |||
return 0; | |||
} | |||
return $this->delay_before_distribution; | |||
} | |||
public function setDelayBeforeDistribution(int $delayBeforeDistribution): self | |||
public function setDelayBeforeDistribution(?int $delayBeforeDistribution): self | |||
{ | |||
$this->delay_before_distribution = $delayBeforeDistribution; | |||
return $this; | |||
@@ -149,10 +184,48 @@ class AutomaticEmail extends ActiveRecordCommon | |||
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 */ | |||
public function getProducerRelation(): ActiveQuery | |||
{ | |||
return $this->hasOne(Producer::class, ['id' => 'id_producer']); | |||
} | |||
public function getPointSaleRelation(): ActiveQuery | |||
{ | |||
return $this->hasOne(PointSale::class, ['id' => 'id_point_sale']); | |||
} | |||
} |
@@ -12,7 +12,6 @@ class AutomaticEmailBuilder extends AbstractBuilder | |||
{ | |||
$automaticEmail = new AutomaticEmail(); | |||
$automaticEmail->setProducer($producer); | |||
$automaticEmail->setDelayBeforeDistribution(2); | |||
$automaticEmail->setStatus(StatusInterface::STATUS_ONLINE); | |||
return $automaticEmail; | |||
} |
@@ -6,6 +6,7 @@ use domain\_\AbstractManager; | |||
use domain\Communication\Email\Email; | |||
use domain\Communication\Email\EmailGenerator; | |||
use domain\Distribution\Distribution\Distribution; | |||
use domain\PointSale\PointSale\PointSale; | |||
use domain\Producer\Producer\Producer; | |||
class AutomaticEmailManager extends AbstractManager | |||
@@ -21,16 +22,33 @@ class AutomaticEmailManager extends AbstractManager | |||
public function createAutomaticEmail( | |||
Producer $producer, | |||
int $day, | |||
int $delayBeforeDistribution, | |||
string $subject, | |||
string $message, | |||
int $day = null, | |||
PointSale $pointSale = null, | |||
int $delayBeforeDistribution = null, | |||
int $sendingDay = null, | |||
int $sendingHour = null, | |||
bool $integrateProductList = null | |||
): AutomaticEmail | |||
{ | |||
$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->setMessage($message); | |||
$automaticEmail->setIntegrateProductList($integrateProductList); | |||
@@ -40,14 +58,15 @@ class AutomaticEmailManager extends AbstractManager | |||
return $automaticEmail; | |||
} | |||
public function createEmailFromAutomaticEmail(AutomaticEmail $automaticEmail, Distribution $distribution): Email | |||
public function createEmailFromAutomaticEmail(AutomaticEmail $automaticEmail, Distribution $distribution = null): Email | |||
{ | |||
return $this->emailGenerator->createEmail( | |||
$automaticEmail->getSubject(), | |||
$automaticEmail->getMessage(), | |||
$automaticEmail->getIntegrateProductList(), | |||
$automaticEmail->getProducer(), | |||
$distribution | |||
$distribution, | |||
true | |||
); | |||
} | |||
@@ -40,7 +40,7 @@ class ContactListResolver extends AbstractResolver | |||
$users = []; | |||
foreach($distribution->pointSaleDistribution as $pointSaleDistribution) { | |||
if($pointSaleDistribution->delivery) { | |||
if($pointSaleDistribution->delivery && (!$pointSale || $pointSale->id == $pointSaleDistribution->id_point_sale)) { | |||
$usersPointSaleArray = $this->userRepository->queryUsersBy([ | |||
'id_producer' => $producer->id, | |||
'id_point_sale' => $pointSaleDistribution->id_point_sale, |
@@ -33,7 +33,8 @@ class EmailGenerator extends AbstractResolver | |||
string $message, | |||
bool $integrateProductList, | |||
Producer $producer = null, | |||
Distribution $distribution = null | |||
Distribution $distribution = null, | |||
bool $addLinkOrderWhenNoDistribution = false | |||
{ | |||
$messageAutoText = '' ; | |||
@@ -107,6 +108,17 @@ Produits disponibles : | |||
} | |||
} | |||
} | |||
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) { | |||
$fromEmail = $this->producerSolver->getProducerEmailPlatform($producer) ; |