'php:d/m/Y'], [['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'auto_payment'], 'boolean'], [['id_point_sale', 'id_producer', 'date_begin'], 'required', 'message' => 'Champs obligatoire'], [['products', 'id_user', 'username','comment'], 'safe'], ['id_user', function ($attribute, $params) { if (!$this->id_user && !strlen($this->username)) { $this->addError($attribute, 'Vous devez sélectionner ou saisir un utilisateur.'); } }, 'skipOnEmpty' => false], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'id_user' => 'Utilisateur', 'id_producer' => 'Producteur', 'id_point_sale' => 'Point de vente', 'date_begin' => 'Date de début', 'date_end' => 'Date de fin', 'monday' => 'Lundi', 'tuesday' => 'Mardi', 'wednesday' => 'Mercredi', 'thursday' => 'Jeudi', 'friday' => 'Vendredi', 'saturday' => 'Samedi', 'sunday' => 'Dimanche', 'week_frequency' => 'Périodicité (semaines)', 'username' => 'Nom d\'utilisateur', 'auto_payment' => 'Paiement automatique', 'comment' => 'Commentaire' ]; } /** * Enregistre l'abonnement. * * @return boolean */ public function save() { if ($this->id) { $subscription = Subscription::searchOne(['id' => $this->id]) ; } else { $subscription = new Subscription ; } if ($subscription) { $subscription->id_user = $this->id_user; $subscription->username = $this->username; $subscription->id_producer = $this->id_producer; $subscription->id_point_sale = $this->id_point_sale; $subscription->date_begin = date( 'Y-m-d', strtotime(str_replace('/', '-', $this->date_begin) )); $subscription->date_end = null; if ($this->date_end) { $subscription->date_end = date( 'Y-m-d', strtotime(str_replace('/', '-', $this->date_end) )); } $subscription->monday = $this->monday; $subscription->tuesday = $this->tuesday; $subscription->wednesday = $this->wednesday; $subscription->thursday = $this->thursday; $subscription->friday = $this->friday; $subscription->saturday = $this->saturday; $subscription->sunday = $this->sunday; $subscription->week_frequency = $this->week_frequency; $subscription->auto_payment = $this->auto_payment; $subscription->comment = $this->comment; $subscription->save(); // produits if ($this->id) { $productsSubscriptionsArray = ProductSubscription::findAll(['id_subscription' => $this->id]) ; ProductSubscription::deleteAll(['id_subscription' => $this->id]); } foreach ($this->products as $nameInput => $quantity) { if ($quantity) { $idProduct = (int) str_replace('product_', '', $nameInput); $product = Product::findOne($idProduct) ; $newProductSubscription = new ProductSubscription; $newProductSubscription->id_subscription = $subscription->id; $newProductSubscription->id_product = $idProduct; $newProductSubscription->quantity = $quantity / Product::$unitsArray[$product->unit]['coefficient']; $newProductSubscription->save(); } } if(!$this->id) { $this->id = $subscription->id ; } } return true; } }