Browse Source

[backend] Envoi de mail aux utilisateurs (via points de vente) : possibilité de choisir un jour de distribution.

Ajout automatique d'un lien pour passer commande ainsi que les produits disponibles pour cette distribution.
dev
Guillaume Bourgeois 5 years ago
parent
commit
ae29b11ec6
4 changed files with 77 additions and 5 deletions
  1. +10
    -2
      backend/controllers/UserController.php
  2. +63
    -2
      backend/models/MailForm.php
  3. +2
    -0
      backend/views/user/emails.php
  4. +2
    -1
      common/models/Distribution.php

+ 10
- 2
backend/controllers/UserController.php View File



use common\models\User ; use common\models\User ;
use common\models\Producer ; use common\models\Producer ;
use common\models\Distribution ;
use backend\models\MailForm ; use backend\models\MailForm ;


/** /**
return $this->redirect(['mail','idPointSale' => $idPointSale]); return $this->redirect(['mail','idPointSale' => $idPointSale]);
} }

$incomingDistributions = Distribution::getIncomingDistributions() ;
$incomingDistributionsArray = ['0' => '--'] ;
foreach($incomingDistributions as $distribution) {
$incomingDistributionsArray[$distribution->id] = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
}
return $this->render('emails', [ return $this->render('emails', [
'usersArray' => $usersArray, 'usersArray' => $usersArray,
'pointsSaleArray' => $pointsSaleArray, 'pointsSaleArray' => $pointsSaleArray,
'pointSale' => $pointSale, 'pointSale' => $pointSale,
'mailForm' => $mailForm, 'mailForm' => $mailForm,
'idPointSaleActive' => $idPointSale
'idPointSaleActive' => $idPointSale,
'incomingDistributionsArray' => $incomingDistributionsArray,
]); ]);
} }



+ 63
- 2
backend/models/MailForm.php View File



use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
use common\helpers\Price ;


/** /**
* ContactForm is the model behind the contact form. * ContactForm is the model behind the contact form.
class MailForm extends Model class MailForm extends Model
{ {


public $id_distribution ;
public $subject; public $subject;
public $message; public $message;


{ {
return [ return [
[['subject', 'message'], 'required', 'message' => 'Champs obligatoire'], [['subject', 'message'], 'required', 'message' => 'Champs obligatoire'],
[['id_distribution'],'integer']
]; ];
} }


return [ return [
'subject' => 'Sujet', 'subject' => 'Sujet',
'message' => 'Message', 'message' => 'Message',
'id_distribution' => 'Distribution'
]; ];
} }




$body = ['Messages' => []] ; $body = ['Messages' => []] ;
$messageAutoText = '' ;
$messageAutoHtml = '' ;
if($this->id_distribution) {
$messageAutoText = '

' ;
$messageAutoHtml = '<br /><br />' ;
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ;
if($distribution) {
$linkOrder = Yii::$app->urlManagerProducer->createAbsoluteUrl(['order/order','slug_producer' => Producer::getCurrent()->slug, 'date' => $distribution->date]) ;
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
$messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ;
$messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' :
'.$linkOrder ;
$productsArray = Product::find()
->where([
'id_producer' => Producer::getId(),
])
->innerJoinWith(['productDistribution' => function($query) use($distribution) {
$query->andOnCondition([
'product_distribution.id_distribution' => $distribution->id,
'product_distribution.active' => 1
]);
}])
->orderBy('product.name ASC')
->all();

if(count($productsArray) > 1) {
$messageAutoHtml .= '<br /><br />Produits disponibles : <br /><ul>' ;
$messageAutoText .= '

Produits disponibles :
' ;
foreach($productsArray as $product) {
$productDescription = $product->name ;
if(strlen($product->description)) {
$productDescription .= ' / '.$product->description ;
}
if($product->price) {
$productDescription .= ' / '.Price::format($product->price) ;
}
$messageAutoText .= '- '.$productDescription.'
' ;
$messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
}
$messageAutoHtml .= '</ul>' ;
}
}
}
foreach($usersArray as $user) { foreach($usersArray as $user) {
$body['Messages'][] = [ $body['Messages'][] = [
'From' => [ 'From' => [
] ]
], ],
'Subject' => $this->subject, 'Subject' => $this->subject,
'TextPart' => $this->message,
'HTMLPart' => nl2br($this->message)
'TextPart' => $this->message.$messageAutoText,
'HTMLPart' => nl2br($this->message).$messageAutoHtml
] ; ] ;
} }

+ 2
- 0
backend/views/user/emails.php View File

</div> </div>
<div class="panel-body"> <div class="panel-body">
<?php $form = ActiveForm::begin(); ?> <?php $form = ActiveForm::begin(); ?>
<?= $form->field($mailForm, 'id_distribution')->dropDownList($incomingDistributionsArray)
->hint("Sélectionnez une distribution pour ajouter automatiquement au message :<br /><ul><li>un lien vers la prise de commande de cette distribution</li><li>la liste des produits disponibles pour cette distribution</li></ul>"); ?>
<?= $form->field($mailForm, 'subject')->textInput() ; ?> <?= $form->field($mailForm, 'subject')->textInput() ; ?>
<?= $form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?> <?= $form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?>
<div class="form-group"> <div class="form-group">

+ 2
- 1
common/models/Distribution.php View File

public static function getIncomingDistributions() public static function getIncomingDistributions()
{ {
$distributionsArray = Distribution::find() $distributionsArray = Distribution::find()
->where('date > ' . date('Y-m-d'))
->where('date > \'' . date('Y-m-d').'\'')
->andWhere(['id_producer' => Producer::getId()]) ->andWhere(['id_producer' => Producer::getId()])
->orderBy('date ASC')
->all(); ->all();
return $distributionsArray ; return $distributionsArray ;

Loading…
Cancel
Save