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.

CommandeautoController.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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($commandeauto->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. $model->paiement_automatique = $commandeauto->paiement_automatique ;
  99. // produits
  100. $commandeauto_produits = CommandeAutoProduit::find()->where(['id_commande_auto' => $model->id])->all() ;
  101. foreach($commandeauto_produits as $commandeauto_produit)
  102. {
  103. $model->produits['produit_'.$commandeauto_produit->id_produit] = $commandeauto_produit->quantite ;
  104. }
  105. }
  106. else {
  107. throw new NotFoundHttpException('La commande récurrente est introuvable.', 404);
  108. }
  109. // produits
  110. $produits = Produit::find()
  111. ->where(['id_etablissement' => $model->id_etablissement])
  112. ->orderBy('order ASC')
  113. ->all();
  114. if($model->load(Yii::$app->request->post()) && $model->validate())
  115. {
  116. if(!strlen($model->date_fin)){
  117. $model->date_fin = null ;
  118. }
  119. if($model->save())
  120. {
  121. $this->redirect(['commandeauto/index']) ;
  122. }
  123. }
  124. return $this->render('update',[
  125. 'model' => $model,
  126. 'produits' => $produits
  127. ]) ;
  128. }
  129. public function actionDelete($id)
  130. {
  131. CommandeAutoProduit::deleteAll(['id_commande_auto' => $id]) ;
  132. CommandeAuto::findOne($id)->delete() ;
  133. $this->redirect(['commandeauto/index']) ;
  134. }
  135. }