|
- <?php
-
-
-
- namespace common\models;
-
- use Yii;
- use yii\base\Model;
- use common\models\Subscription;
- use common\models\SubscriptionProduct;
-
-
- class SubscriptionForm extends Model
- {
- public $isAdmin = false ;
- public $id;
- public $id_user;
- public $username;
- public $id_producer;
- public $id_point_sale;
- public $date_begin;
- public $date_end;
- public $monday;
- public $tuesday;
- public $wednesday;
- public $thursday;
- public $friday;
- public $saturday;
- public $sunday;
- public $week_frequency;
- public $products;
- public $auto_payment;
- public $comment ;
-
-
-
- public function rules()
- {
- return [
- [['id_producer', 'week_frequency', 'id_point_sale','id'], 'integer'],
- [['date_begin', 'date_end'], 'date', 'format' => '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'
- ];
- }
-
-
-
- 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->comment = $this->comment;
-
- $subscription->save();
-
-
- 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;
- }
-
- }
|