Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CommandeautoController.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. {
  24. return [
  25. 'access' => [
  26. 'class' => AccessControl::className(),
  27. 'rules' => [
  28. [
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. 'matchCallback' => function ($rule, $action) {
  32. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  33. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  34. }
  35. ]
  36. ],
  37. ],
  38. ];
  39. }
  40. public function actionIndex()
  41. {
  42. $dataProvider = new ActiveDataProvider([
  43. 'query' => CommandeAuto::find()
  44. ->with(['user','etablissement', 'pointVente','commandeAutoProduit'])
  45. ->joinWith(['user'])
  46. ->where(['commande_auto.id_etablissement' => Yii::$app->user->identity->id_etablissement])
  47. ->orderBy('commande_auto.id_point_vente ASC, CASE `commande_auto`.`username` WHEN "" THEN `user`.`nom` ELSE `commande_auto`.`username` END ASC'),
  48. 'pagination' => [
  49. 'pageSize' => 1000,
  50. ],
  51. ]);
  52. return $this->render('index',[
  53. 'dataProvider' => $dataProvider
  54. ]) ;
  55. }
  56. public function actionCreate()
  57. {
  58. // form
  59. $model = new CommandeAutoForm ;
  60. $model->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  61. // produits
  62. $produits = Produit::find()
  63. ->where(['id_etablissement' => $model->id_etablissement])
  64. ->orderBy('order ASC')
  65. ->all();
  66. if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save())
  67. {
  68. $this->redirect(['commandeauto/index']) ;
  69. }
  70. return $this->render('create',[
  71. 'model' => $model,
  72. 'produits' => $produits
  73. ]) ;
  74. }
  75. public function actionUpdate($id)
  76. {
  77. // form
  78. $model = new CommandeAutoForm ;
  79. $commandeauto = CommandeAuto::findOne($id) ;
  80. if($commandeauto)
  81. {
  82. $model->id = $id ;
  83. $model->id_etablissement = $commandeauto->id_etablissement ;
  84. $model->id_user = $commandeauto->id_user ;
  85. $model->username = $commandeauto->username ;
  86. $model->id_point_vente = $commandeauto->id_point_vente ;
  87. $model->date_debut = date('d/m/Y',strtotime($commandeauto->date_debut)) ;
  88. if(strlen($model->date_fin))
  89. $model->date_fin = date('d/m/Y',strtotime($commandeauto->date_fin)) ;
  90. $model->lundi = $commandeauto->lundi ;
  91. $model->lundi = $commandeauto->lundi ;
  92. $model->mardi = $commandeauto->mardi ;
  93. $model->mercredi = $commandeauto->mercredi ;
  94. $model->jeudi = $commandeauto->jeudi ;
  95. $model->vendredi = $commandeauto->vendredi ;
  96. $model->samedi = $commandeauto->samedi ;
  97. $model->dimanche = $commandeauto->dimanche ;
  98. // produits
  99. $commandeauto_produits = CommandeAutoProduit::find()->where(['id_commande_auto' => $model->id])->all() ;
  100. foreach($commandeauto_produits as $commandeauto_produit)
  101. {
  102. $model->produits['produit_'.$commandeauto_produit->id_produit] = $commandeauto_produit->quantite ;
  103. }
  104. }
  105. else {
  106. throw new NotFoundHttpException('La commande récurrente est introuvable.', 404);
  107. }
  108. // produits
  109. $produits = Produit::find()
  110. ->where(['id_etablissement' => $model->id_etablissement])
  111. ->orderBy('order ASC')
  112. ->all();
  113. if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save())
  114. {
  115. $this->redirect(['commandeauto/index']) ;
  116. }
  117. return $this->render('update',[
  118. 'model' => $model,
  119. 'produits' => $produits
  120. ]) ;
  121. }
  122. public function actionDelete($id)
  123. {
  124. CommandeAutoProduit::deleteAll(['id_commande_auto' => $id]) ;
  125. CommandeAuto::findOne($id)->delete() ;
  126. $this->redirect(['commandeauto/index']) ;
  127. }
  128. }