Mise en place d'un système, via crontab, permettant l'envoi automatique d'un récapitulatif au format PDF avec toutes les commandes classées par point de vente.prodstable
@@ -26,6 +26,11 @@ class CommandeController extends BackendController { | |||
'access' => [ | |||
'class' => AccessControl::className(), | |||
'rules' => [ | |||
[ | |||
'actions' => ['report-cron'], | |||
'allow' => true, | |||
'roles' => ['?'] | |||
], | |||
[ | |||
'allow' => true, | |||
'roles' => ['@'], | |||
@@ -39,12 +44,25 @@ class CommandeController extends BackendController { | |||
]; | |||
} | |||
public function actionReport($date = '') { | |||
public function actionReportCron($date = '', $save = false, $id_etablissement = 0, $key = '') | |||
{ | |||
if($key == '64ac0bdab7e9f5e48c4d991ec5201d57') | |||
{ | |||
$this->actionReport($date, $save, $id_etablissement) ; | |||
} | |||
} | |||
public function actionReport($date = '', $save = false, $id_etablissement = 0) | |||
{ | |||
if(!Yii::$app->user->isGuest) | |||
$id_etablissement = Yii::$app->user->identity->id_etablissement ; | |||
$commandes = Commande::find() | |||
->with('commandeProduits', 'user') | |||
->joinWith('production') | |||
->where(['production.date' => $date]) | |||
->andWhere(['production.id_etablissement' => $id_etablissement]) | |||
->orderBy('date ASC') | |||
->all(); | |||
@@ -63,7 +81,7 @@ class CommandeController extends BackendController { | |||
// produits | |||
$produits = Produit::find() | |||
->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement]) | |||
->where(['id_etablissement' => $id_etablissement]) | |||
->orderBy('order ASC') | |||
->all(); | |||
@@ -78,6 +96,14 @@ class CommandeController extends BackendController { | |||
$date_str = date('d/m/Y',strtotime($date)) ; | |||
if($save) | |||
{ | |||
$destination = Pdf::DEST_FILE ; | |||
} | |||
else { | |||
$destination = Pdf::DEST_BROWSER ; | |||
} | |||
$pdf = new Pdf([ | |||
// set to use core fonts only | |||
'mode' => Pdf::MODE_UTF8, | |||
@@ -86,7 +112,8 @@ class CommandeController extends BackendController { | |||
// portrait orientation | |||
'orientation' => Pdf::ORIENT_LANDSCAPE, | |||
// stream to browser inline | |||
'destination' => Pdf::DEST_BROWSER, | |||
'destination' => $destination, | |||
'filename' => Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$id_etablissement.'.pdf'), | |||
// your html content input | |||
'content' => $content, | |||
// format content from your own css file if needed or use the |
@@ -0,0 +1,136 @@ | |||
<?php | |||
namespace backend\controllers; | |||
use Yii; | |||
use common\models\User; | |||
use yii\data\ActiveDataProvider; | |||
use yii\web\Controller; | |||
use yii\web\NotFoundHttpException; | |||
use yii\filters\VerbFilter; | |||
use yii\filters\AccessControl; | |||
use kartik\mpdf\Pdf; | |||
use common\models\Etablissement; | |||
use common\models\Commande ; | |||
use common\models\Production ; | |||
/** | |||
* UserController implements the CRUD actions for User model. | |||
*/ | |||
class CronController extends BackendController | |||
{ | |||
public function behaviors() | |||
{ | |||
return [ | |||
'verbs' => [ | |||
'class' => VerbFilter::className(), | |||
'actions' => [ | |||
'delete' => ['post'], | |||
], | |||
], | |||
'access' => [ | |||
'class' => AccessControl::className(), | |||
'rules' => [ | |||
[ | |||
'allow' => true, | |||
'roles' => ['?'], | |||
] | |||
], | |||
], | |||
]; | |||
} | |||
public function actionSendCommandes($key = '') | |||
{ | |||
if($key == '64ac0bdab7e9f5e48c4d991ec5201d57') | |||
{ | |||
$heure = date('H') ; | |||
if($heure == '00') | |||
{ | |||
$date = date('Y-m-d') ; | |||
} | |||
else { | |||
$date = date('Y-m-d', time()+24*60*60) ; | |||
} | |||
$etablissements = Etablissement::find()->all() ; | |||
foreach($etablissements as $e) | |||
{ | |||
$production = Production::findOne([ | |||
'date' => $date, | |||
'actif' => 1, | |||
'id_etablissement' => $e['id'], | |||
]) ; | |||
if($production && $heure == $e['heure_limite_commande']) | |||
{ | |||
$commandes = Commande::find() | |||
->with('commandeProduits', 'user') | |||
->joinWith('production') | |||
->where(['production.date' => $date]) | |||
->andWhere(['production.id_etablissement' => $e['id']]) | |||
->orderBy('date ASC') | |||
->all(); | |||
$user = User::findOne(['id_etablissement' => $e['id']]) ; | |||
$mail = Yii::$app->mailer->compose() | |||
->setTo($user->email) | |||
->setFrom(['contact@laboiteapain.net' => 'La boîte à pain']) ; | |||
if(count($commandes)) | |||
{ | |||
$sujet = '[La boîte à pain] Commandes du '.date('d/m',strtotime($date)) ; | |||
if(count($commandes) > 1) | |||
$sujet .= 's' ; | |||
// génération du pdf de commande | |||
Yii::$app->runAction('commande/report-cron', [ | |||
'date' => $date, | |||
'save' => true, | |||
'id_etablissement' => $e['id'] , | |||
'key' => '64ac0bdab7e9f5e48c4d991ec5201d57' | |||
]); | |||
$mail->attach(Yii::getAlias('@app/web/pdf/Commandes-'.$date.'-'.$e['id'].'.pdf')) ; | |||
$message = 'Bonjour, | |||
Voici en pièce jointe le récapitulatif des commandes ('.count($commandes).') du '.date('d/m',strtotime($date)).'. | |||
À bientôt, | |||
La boîte à pain | |||
' ; | |||
} | |||
else { | |||
$sujet = '[La boîte à pain] Aucune commande' ; | |||
$message = 'Bonjour, | |||
Vous n\'avez aucune commande pour le '.date('d/m',strtotime($date)).'. | |||
À bientôt, | |||
La boîte à pain | |||
' ; | |||
} | |||
$mail->setSubject($sujet) | |||
->setTextBody($message) | |||
->send(); | |||
} | |||
} | |||
} | |||
} | |||
} |