'ID', 'id_user' => 'Utilisateur', 'id_producer' => 'Etablissement', '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', 'saterday' => 'Samedi', 'sunday' => 'Dimanche', 'week_frequency' => 'Périodicité', 'auto_payment' => 'Paiement automatique' ]; } /* * Relations */ public function getUser() { return $this->hasOne(User::className(), ['id' => 'id_user']); } public function getProducer() { return $this->hasOne( Producer::className(), ['id' => 'id_producer'] ); } public function getPointSale() { return $this->hasOne( PointSale::className(), ['id' => 'id_point_sale'] ); } public function getProductSubscription() { return $this->hasMany( ProductSubscription::className(), ['id_subscription' => 'id'] )->with('product'); } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => ['producer', 'pointSale', 'productSubscription'], 'join_with' => ['user'], 'orderby' => 'user.name ASC', 'attribute_id_producer' => 'subscription.id_producer' ] ; } /** * Ajoute la commande pour une date donnée. * * @param string $date */ public function add($date) { // distribution $distribution = Distribution::searchOne([ 'distribution.date' => date('Y-m-d', strtotime($date)) ]) ; if ($distribution && count($this->productSubscription)) { // commande $order = new Order; if (strlen($this->username)) { $order->username = $this->username; $order->id_user = 0; } else { $order->id_user = $this->id_user; } $order->date = date('Y-m-d H:i:s'); $order->origin = Order::ORIGIN_AUTO; $order->id_point_sale = $this->id_point_sale; $order->id_distribution = $distribution->id; $order->auto_payment = $this->auto_payment ; $userPointSale = UserPointSale::searchOne([ 'id_point_sale' => $this->id_point_sale, 'id_user' => $this->id_user ]) ; if ($userPointSale && strlen($userPointSale->comment)) { $order->comment_point_sale = $userPointSale->comment; } $order->save(); // produits $amountTotal = 0; $productsAdd = false; foreach ($this->productSubscription as $productSubscription) { $productOrder = new ProductOrder; $productOrder->id_order = $order->id; $productOrder->id_product = $productSubscription->product->id; $productOrder->quantity = $productSubscription->quantity; $productOrder->price = $productSubscription->product->price; $productOrder->save(); $productsAdd = true; } if (!$productsAdd) { $order->delete(); } } } /** * Ajoute les commandes pour une date donnée à partir des abonnements. * * @param string $date * @param boolean $force */ public static function addAll($date, $force = false) { $distribution = Distribution::searchOne([ 'date' => date('Y-m-d', strtotime($date)) ]) ; if ($distribution) { $countOrdersProd = Order::searchCount([ Order::tableName().'.id_distribution' => $distribution->id ]) ; if (!$countOrdersProd || $force) { $subscriptions = self::searchByDate($date); foreach ($subscriptions as $s) { $s->add($date); } } } } /** * Retourne les abonnements pour une date donnée. * * @param string $date * @return array */ public static function searchByDate($date) { $date = date('Y-m-d', strtotime($date)); $subscriptions = Subscription::searchAll() ; $arrSubscriptions = []; foreach ($subscriptions as $s) { // vérif dates if ($date >= $s->date_begin && (!$s->date_end || $date <= $s->date_end)) { // périodicite $nbDays = (strtotime($date) - strtotime($s->date_begin)) / (24 * 60 * 60); if ($nbDays % ($s->week_frequency * 7) < 7) { // jour de la semaine $day = date('N', strtotime($date)); switch ($day) { case 1 : $day = 'monday'; break; case 2 : $day = 'tuesday'; break; case 3 : $day = 'wednesday'; break; case 4 : $day = 'thursday'; break; case 5 : $day = 'friday'; break; case 6 : $day = 'saturday'; break; case 7 : $day = 'sunday'; break; } if ($s->$day) { $arrSubscriptions[] = $s; } } } } return $arrSubscriptions; } }