|
- <?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'],
- 'status' => User::STATUS_BOULANGER
- ]) ;
-
- $mail = Yii::$app->mailer->compose(
- [
- 'html' => 'cronRecapCommandes-html',
- 'text' => 'cronRecapCommandes-text',
- ],
- [
- 'date' => $date,
- 'commandes' => $commandes
- ]
- )
- ->setTo($user->email)
- ->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) ;
-
- if(count($commandes))
- {
- $sujet = '[La boîte à pain] Commandes du '.date('d/m',strtotime($date)) ;
-
- // 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')) ;
-
- }
- else {
- $sujet = '[La boîte à pain] Aucune commande' ;
-
- }
-
- $mail->setSubject($sujet)
- ->send();
-
- }
-
-
- }
- }
-
-
-
-
-
- }
-
- }
|