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
'access' => [ | 'access' => [ | ||||
'class' => AccessControl::className(), | 'class' => AccessControl::className(), | ||||
'rules' => [ | 'rules' => [ | ||||
[ | |||||
'actions' => ['report-cron'], | |||||
'allow' => true, | |||||
'roles' => ['?'] | |||||
], | |||||
[ | [ | ||||
'allow' => true, | 'allow' => true, | ||||
'roles' => ['@'], | 'roles' => ['@'], | ||||
]; | ]; | ||||
} | } | ||||
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() | $commandes = Commande::find() | ||||
->with('commandeProduits', 'user') | ->with('commandeProduits', 'user') | ||||
->joinWith('production') | ->joinWith('production') | ||||
->where(['production.date' => $date]) | ->where(['production.date' => $date]) | ||||
->andWhere(['production.id_etablissement' => $id_etablissement]) | |||||
->orderBy('date ASC') | ->orderBy('date ASC') | ||||
->all(); | ->all(); | ||||
// produits | // produits | ||||
$produits = Produit::find() | $produits = Produit::find() | ||||
->where(['id_etablissement' => Yii::$app->user->identity->id_etablissement]) | |||||
->where(['id_etablissement' => $id_etablissement]) | |||||
->orderBy('order ASC') | ->orderBy('order ASC') | ||||
->all(); | ->all(); | ||||
$date_str = date('d/m/Y',strtotime($date)) ; | $date_str = date('d/m/Y',strtotime($date)) ; | ||||
if($save) | |||||
{ | |||||
$destination = Pdf::DEST_FILE ; | |||||
} | |||||
else { | |||||
$destination = Pdf::DEST_BROWSER ; | |||||
} | |||||
$pdf = new Pdf([ | $pdf = new Pdf([ | ||||
// set to use core fonts only | // set to use core fonts only | ||||
'mode' => Pdf::MODE_UTF8, | 'mode' => Pdf::MODE_UTF8, | ||||
// portrait orientation | // portrait orientation | ||||
'orientation' => Pdf::ORIENT_LANDSCAPE, | 'orientation' => Pdf::ORIENT_LANDSCAPE, | ||||
// stream to browser inline | // 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 | // your html content input | ||||
'content' => $content, | 'content' => $content, | ||||
// format content from your own css file if needed or use the | // format content from your own css file if needed or use the |
<?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(); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
} |