You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.1KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\models\Production;
  5. use yii\filters\AccessControl;
  6. use yii\web\Controller;
  7. use common\models\LoginForm;
  8. use yii\filters\VerbFilter;
  9. use common\models\Commande;
  10. use common\models\CommandeProduit;
  11. use common\models\PointVente;
  12. use common\models\Produit;
  13. use common\models\User;
  14. use common\models\CommandeAutoForm;
  15. use common\models\ProductionProduit;
  16. use yii\data\ActiveDataProvider;
  17. use common\models\CommandeAuto;
  18. use common\models\CommandeAutoProduit;
  19. use yii\web\NotFoundHttpException;
  20. class CommandeautoController extends BackendController {
  21. var $enableCsrfValidation = false;
  22. public function behaviors() {
  23. return [
  24. 'access' => [
  25. 'class' => AccessControl::className(),
  26. 'rules' => [
  27. [
  28. 'allow' => true,
  29. 'roles' => ['@'],
  30. 'matchCallback' => function ($rule, $action) {
  31. return Yii::$app->user->identity->status == USER::STATUS_ADMIN || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  32. }
  33. ]
  34. ],
  35. ],
  36. ];
  37. }
  38. public function actionIndex() {
  39. $dataProvider = new ActiveDataProvider([
  40. 'query' => CommandeAuto::find()
  41. ->with(['user', 'etablissement', 'pointVente', 'commandeAutoProduit'])
  42. ->joinWith(['user'])
  43. ->where(['commande_auto.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  44. ->orderBy('commande_auto.id_point_vente ASC, CASE `commande_auto`.`username` WHEN "" THEN `user`.`nom` ELSE `commande_auto`.`username` END ASC'),
  45. 'pagination' => [
  46. 'pageSize' => 1000,
  47. ],
  48. ]);
  49. return $this->render('index', [
  50. 'dataProvider' => $dataProvider
  51. ]);
  52. }
  53. public function actionCreate() {
  54. // form
  55. $model = new CommandeAutoForm;
  56. $model->id_etablissement = Yii::$app->user->identity->id_etablissement;
  57. // produits
  58. $produits = Produit::find()
  59. ->where(['id_etablissement' => $model->id_etablissement])
  60. ->orderBy('order ASC')
  61. ->all();
  62. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
  63. $this->redirect(['commandeauto/index']);
  64. }
  65. return $this->render('create', [
  66. 'model' => $model,
  67. 'produits' => $produits
  68. ]);
  69. }
  70. public function actionUpdate($id) {
  71. // form
  72. $model = new CommandeAutoForm;
  73. $commandeauto = CommandeAuto::findOne($id);
  74. if ($commandeauto) {
  75. $model->id = $id;
  76. $model->id_etablissement = $commandeauto->id_etablissement;
  77. $model->id_user = $commandeauto->id_user;
  78. $model->username = $commandeauto->username;
  79. $model->id_point_vente = $commandeauto->id_point_vente;
  80. $model->date_debut = date('d/m/Y', strtotime($commandeauto->date_debut));
  81. if (strlen($commandeauto->date_fin))
  82. $model->date_fin = date('d/m/Y', strtotime($commandeauto->date_fin));
  83. $model->lundi = $commandeauto->lundi;
  84. $model->lundi = $commandeauto->lundi;
  85. $model->mardi = $commandeauto->mardi;
  86. $model->mercredi = $commandeauto->mercredi;
  87. $model->jeudi = $commandeauto->jeudi;
  88. $model->vendredi = $commandeauto->vendredi;
  89. $model->samedi = $commandeauto->samedi;
  90. $model->dimanche = $commandeauto->dimanche;
  91. $model->paiement_automatique = $commandeauto->paiement_automatique;
  92. $model->periodicite_semaine = $commandeauto->periodicite_semaine;
  93. // produits
  94. $commandeauto_produits = CommandeAutoProduit::find()->where(['id_commande_auto' => $model->id])->all();
  95. foreach ($commandeauto_produits as $commandeauto_produit) {
  96. $model->produits['produit_' . $commandeauto_produit->id_produit] = $commandeauto_produit->quantite;
  97. }
  98. } else {
  99. throw new NotFoundHttpException('La commande récurrente est introuvable.', 404);
  100. }
  101. // produits
  102. $produits = Produit::find()
  103. ->where(['id_etablissement' => $model->id_etablissement])
  104. ->orderBy('order ASC')
  105. ->all();
  106. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  107. if (!strlen($model->date_fin)) {
  108. $model->date_fin = null;
  109. }
  110. if ($model->save()) {
  111. $this->redirect(['commandeauto/index']);
  112. }
  113. }
  114. return $this->render('update', [
  115. 'model' => $model,
  116. 'produits' => $produits
  117. ]);
  118. }
  119. public function actionDelete($id) {
  120. CommandeAutoProduit::deleteAll(['id_commande_auto' => $id]);
  121. CommandeAuto::findOne($id)->delete();
  122. $this->redirect(['commandeauto/index']);
  123. }
  124. }