Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CommandeautoController.php 4.6KB

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