Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

145 lines
4.8KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use common\models\CommandeAuto ;
  6. use common\models\CommandeAutoProduit ;
  7. /**
  8. * Login form
  9. */
  10. class CommandeAutoForm extends Model
  11. {
  12. public $id ;
  13. public $id_user ;
  14. public $username ;
  15. public $id_etablissement ;
  16. public $id_point_vente ;
  17. public $date_debut;
  18. public $date_fin ;
  19. public $lundi ;
  20. public $mardi ;
  21. public $mercredi ;
  22. public $jeudi ;
  23. public $vendredi ;
  24. public $samedi ;
  25. public $dimanche ;
  26. public $periodicite_semaine ;
  27. public $produits ;
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules()
  32. {
  33. return [
  34. [['id_etablissement', 'periodicite_semaine', 'id_point_vente'], 'integer'],
  35. [['date_debut', 'date_fin'], 'date', 'format' => 'php:d/m/Y'],
  36. [['lundi','mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'], 'boolean'],
  37. [['id_point_vente', 'id_etablissement', 'date_debut'],'required', 'message' => 'Champs obligatoire'],
  38. [['produits','id_user','username'], 'safe'],
  39. ['id_user', function ($attribute, $params) {
  40. if(!$this->id_user && !strlen($this->username))
  41. {
  42. $this->addError($attribute,'Vous devez sélectionner ou saisir un utilisateur.') ;
  43. }
  44. },'skipOnEmpty' => false],
  45. ['produits', function($attribute, $params) {
  46. $produits = $this->produits ;
  47. $quantite = 0 ;
  48. if(count($produits))
  49. {
  50. foreach($produits as $name_input => $quantite_produit) {
  51. if($quantite_produit)
  52. $quantite += $quantite_produit ;
  53. }
  54. }
  55. if(!$quantite)
  56. {
  57. $this->addError($attribute, 'Vous devez saisir au moins un produit');
  58. }
  59. }],
  60. ];
  61. }
  62. public function attributeLabels()
  63. {
  64. return [
  65. 'id' => 'ID',
  66. 'id_user' => 'Utilisateur',
  67. 'id_etablissement' => 'ID Etablissement',
  68. 'id_point_vente' => 'Point de vente',
  69. 'date_debut' => 'Date de début',
  70. 'date_fin' => 'Date de fin',
  71. 'lundi' => 'Lundi',
  72. 'mardi' => 'Mardi',
  73. 'mercredi' => 'Mercredi',
  74. 'jeudi' => 'Jeudi',
  75. 'vendredi' => 'Vendredi',
  76. 'samedi' => 'Samedi',
  77. 'dimanche' => 'Dimanche',
  78. 'periodicite_semaine' => 'Périodicité (semaines)',
  79. 'username' => 'Nom d\'utilisateur',
  80. ];
  81. }
  82. public function save()
  83. {
  84. if($this->id)
  85. {
  86. $commandeauto = CommandeAuto::findOne($this->id) ;
  87. }
  88. else {
  89. $commandeauto = new CommandeAuto ;
  90. }
  91. if($commandeauto)
  92. {
  93. $commandeauto->id_user = $this->id_user ;
  94. $commandeauto->username = $this->username ;
  95. $commandeauto->id_etablissement = $this->id_etablissement ;
  96. $commandeauto->id_point_vente = $this->id_point_vente ;
  97. $commandeauto->date_debut = date('Y-m-d',strtotime(str_replace('/','-',$this->date_debut))) ;
  98. if(strlen($this->date_fin))
  99. {
  100. $commandeauto->date_fin = date('Y-m-d',strtotime(str_replace('/','-',$this->date_fin))) ;
  101. }
  102. $commandeauto->lundi = $this->lundi ;
  103. $commandeauto->mardi = $this->mardi ;
  104. $commandeauto->mercredi = $this->mercredi ;
  105. $commandeauto->jeudi = $this->jeudi ;
  106. $commandeauto->vendredi = $this->vendredi ;
  107. $commandeauto->samedi = $this->samedi ;
  108. $commandeauto->dimanche = $this->dimanche ;
  109. $commandeauto->periodicite_semaine = $this->periodicite_semaine ;
  110. $commandeauto->save() ;
  111. // produits
  112. if($this->id)
  113. {
  114. CommandeAutoProduit::deleteAll(['id_commande_auto' => $this->id]) ;
  115. }
  116. foreach($this->produits as $name_input => $quantite)
  117. {
  118. if($quantite)
  119. {
  120. $id_produit = str_replace('produit_', '', $name_input) ;
  121. $commandeauto_produit = new CommandeAutoProduit ;
  122. $commandeauto_produit->id_commande_auto = $commandeauto->id ;
  123. $commandeauto_produit->id_produit = $id_produit ;
  124. $commandeauto_produit->quantite = $quantite ;
  125. $commandeauto_produit->save() ;
  126. }
  127. }
  128. }
  129. return true ;
  130. }
  131. }