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.

121 satır
4.1KB

  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. public $id;
  12. public $id_user;
  13. public $username;
  14. public $id_etablissement;
  15. public $id_point_vente;
  16. public $date_debut;
  17. public $date_fin;
  18. public $lundi;
  19. public $mardi;
  20. public $mercredi;
  21. public $jeudi;
  22. public $vendredi;
  23. public $samedi;
  24. public $dimanche;
  25. public $periodicite_semaine;
  26. public $produits;
  27. public $paiement_automatique;
  28. /**
  29. * @inheritdoc
  30. */
  31. public function rules() {
  32. return [
  33. [['id_etablissement', 'periodicite_semaine', 'id_point_vente'], 'integer'],
  34. [['date_debut', 'date_fin'], 'date', 'format' => 'php:d/m/Y'],
  35. [['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'paiement_automatique'], 'boolean'],
  36. [['id_point_vente', 'id_etablissement', 'date_debut'], 'required', 'message' => 'Champs obligatoire'],
  37. [['produits', 'id_user', 'username'], 'safe'],
  38. ['id_user', function ($attribute, $params) {
  39. if (!$this->id_user && !strlen($this->username)) {
  40. $this->addError($attribute, 'Vous devez sélectionner ou saisir un utilisateur.');
  41. }
  42. }, 'skipOnEmpty' => false],
  43. ];
  44. }
  45. public function attributeLabels() {
  46. return [
  47. 'id' => 'ID',
  48. 'id_user' => 'Utilisateur',
  49. 'id_etablissement' => 'ID Etablissement',
  50. 'id_point_vente' => 'Point de vente',
  51. 'date_debut' => 'Date de début',
  52. 'date_fin' => 'Date de fin',
  53. 'lundi' => 'Lundi',
  54. 'mardi' => 'Mardi',
  55. 'mercredi' => 'Mercredi',
  56. 'jeudi' => 'Jeudi',
  57. 'vendredi' => 'Vendredi',
  58. 'samedi' => 'Samedi',
  59. 'dimanche' => 'Dimanche',
  60. 'periodicite_semaine' => 'Périodicité (semaines)',
  61. 'username' => 'Nom d\'utilisateur',
  62. 'paiement_automatique' => 'Paiement automatique'
  63. ];
  64. }
  65. public function save() {
  66. if ($this->id) {
  67. $commandeauto = CommandeAuto::findOne($this->id);
  68. } else {
  69. $commandeauto = new CommandeAuto;
  70. }
  71. if ($commandeauto) {
  72. $commandeauto->id_user = $this->id_user;
  73. $commandeauto->username = $this->username;
  74. $commandeauto->id_etablissement = $this->id_etablissement;
  75. $commandeauto->id_point_vente = $this->id_point_vente;
  76. $commandeauto->date_debut = date('Y-m-d', strtotime(str_replace('/', '-', $this->date_debut)));
  77. if (strlen($this->date_fin)) {
  78. $commandeauto->date_fin = date('Y-m-d', strtotime(str_replace('/', '-', $this->date_fin)));
  79. }
  80. $commandeauto->lundi = $this->lundi;
  81. $commandeauto->mardi = $this->mardi;
  82. $commandeauto->mercredi = $this->mercredi;
  83. $commandeauto->jeudi = $this->jeudi;
  84. $commandeauto->vendredi = $this->vendredi;
  85. $commandeauto->samedi = $this->samedi;
  86. $commandeauto->dimanche = $this->dimanche;
  87. $commandeauto->periodicite_semaine = $this->periodicite_semaine;
  88. $commandeauto->paiement_automatique = $this->paiement_automatique;
  89. $commandeauto->save();
  90. // produits
  91. if ($this->id) {
  92. CommandeAutoProduit::deleteAll(['id_commande_auto' => $this->id]);
  93. }
  94. foreach ($this->produits as $name_input => $quantite) {
  95. if ($quantite) {
  96. $id_produit = str_replace('produit_', '', $name_input);
  97. $commandeauto_produit = new CommandeAutoProduit;
  98. $commandeauto_produit->id_commande_auto = $commandeauto->id;
  99. $commandeauto_produit->id_produit = $id_produit;
  100. $commandeauto_produit->quantite = $quantite;
  101. $commandeauto_produit->save();
  102. }
  103. }
  104. }
  105. return true;
  106. }
  107. }