'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'], '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' ]; } /** * 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) )); if (strlen($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->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; if($this->isAdmin) { $unit = (Yii::$app->getRequest()->post('product_unit_'.$idProduct)) ? Yii::$app->getRequest()->post('product_unit_'.$idProduct) : $product->unit ; $newProductSubscription->unit = $unit; $step = (Yii::$app->getRequest()->post('product_step_'.$idProduct)) ? Yii::$app->getRequest()->post('product_step_'.$idProduct) : $product->step ; $newProductSubscription->step = $step; $price = (Yii::$app->getRequest()->post('product_price_'.$idProduct)) ? Yii::$app->getRequest()->post('product_price_'.$idProduct) : $product->price ; $newProductSubscription->price = $price; } else { if(isset($productsSubscriptionsArray) && is_array($productsSubscriptionsArray)) { foreach($productsSubscriptionsArray as $productSubscription) { if($productSubscription->id_product == $idProduct) { $newProductSubscription->unit = $productSubscription->unit ; $newProductSubscription->step = $productSubscription->step ; $newProductSubscription->price = $productSubscription->price ; } } } if(!$newProductSubscription->unit) { $newProductSubscription->unit = $product->unit ; } if(!$newProductSubscription->step) { $newProductSubscription->step = $product->step; } if(!$newProductSubscription->price) { $newProductSubscription->price = $product->price; } } $newProductSubscription->quantity = $quantity / Product::$unitsArray[$newProductSubscription->unit]['coefficient']; $newProductSubscription->save(); } } if(!$this->id) { $this->id = $subscription->id ; } } return true; } }