hasOne(Producer::className(), ['id' => 'id_producer']); } /** * @inheritdoc */ public function rules() { return [ [['date'], 'required'], [['date'], 'safe'], [['active'], 'integer'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'date' => 'Date', 'active' => 'Actif', ]; } /* * Relations */ public function getOrder() { return $this->hasMany(Order::className(), ['id_distribution' => 'id']); } public function getProductDistribution() { return $this->hasMany(ProductDistribution::className(), ['id_distribution' => 'id']); } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => [], 'join_with' => [], 'orderby' => 'date ASC', 'attribute_id_producer' => 'distribution.id_producer' ] ; } /** * Retourne si un produit est actif ou non. * * @param integer $idProduit * @return boolean */ public function isActiveProduct($idProduit) { if ($idProduit && isset($this->productDistribution) && count($this->productDistribution) > 0) { foreach ($this->productDistribution as $productDistribution) { if ($productDistribution['id_product'] == $idProduct && $productDistribution['active']) { return true; } } } return false; } /** * Initialise un jour de production. * * @param string $date * @return Production */ public static function initDistribution($date) { $distribution = null ; if ($date != '') { $distribution = Distribution::searchOne([ 'date' => $date ]) ; if (!$distribution) { $distribution = new Distribution; $distribution->date = $date; $distribution->delivery = 1; $distribution->id_producer = Producer::getId(); $distribution->save(); } } // point_sale_distribution à définir s'ils ne sont pas initialisés if ($distribution) { $countPointSaleDistribution = PointSaleDistribution::searchCount([ 'id_distribution' => $distribution->id ]) ; if (!$countPointSaleDistribution) { PointSaleDistribution::setAll($distribution->id, true); } } // init produits sélectionnés pour cette production $products = Product::searchAll(); if ($distribution) { $productsDistribution = ProductDistribution::searchAll([ 'id_distribution' => $distribution->id ]) ; if (!count($productsDistribution)) { foreach ($products as $product) { $distribution->linkProduct($product) ; } } $distribution->linkProductGift() ; } return $distribution ; } /** * Retourne les distributions futures. * * @return array */ public static function getIncomingDistributions() { $distributionsArray = Distribution::find() ->where('date > \'' . date('Y-m-d').'\'') ->andWhere([ 'id_producer' => Producer::getId(), 'active' => 1 ]) ->orderBy('date ASC') ->all(); return $distributionsArray ; } /** * Lie un produit aux jours de distribution futurs. * * @param Product $product */ public static function linkProductIncomingDistributions($product) { $distributionsArray = self::getIncomingDistributions() ; foreach ($distributionsArray as $distribution) { $distribution->linkProduct($product) ; } } /** * Lie un produit à la distribution. * * @param Product $product */ public function linkProduct($product) { $productDistribution = ProductDistribution::searchOne([ 'id_distribution' => $this->id, 'id_product' => $product->id ]) ; if(!$productDistribution) { $productDistribution = new ProductDistribution(); $productDistribution->id_distribution = $this->id; $productDistribution->id_product = $product->id; } $productDistribution->quantity_max = $product->quantity_max; $dayDistribution = date('N', strtotime($this->date)); $daysArray = [ 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saturday', 7 => 'sunday', ] ; $productDistribution->active = 0; $day = $daysArray[$dayDistribution] ; if($product->active && $product->$day) { $productDistribution->active = 1; } $productDistribution->save(); } /** * Lie le produit "Don" à la distribution */ public function linkProductGift() { $productGift = Product::getProductGift() ; if($productGift) { $productDistribution = ProductDistribution::searchOne([ 'id_distribution' => $this->id, 'id_product' => $productGift->id ]) ; if(!$productDistribution) { $productDistribution = new ProductDistribution(); $productDistribution->id_distribution = $this->id; $productDistribution->id_product = $productGift->id; $productDistribution->active = 1; $productDistribution->save() ; } } } /** * Lie un point de vente aux jours de distribution futurs. * * @param PointSale $pointSale */ public static function linkPointSaleIncomingDistributions($pointSale) { $distributionsArray = self::getIncomingDistributions() ; foreach ($distributionsArray as $distribution) { $distribution->linkPointSale($pointSale) ; } } /** * * @param type $pointSale */ public function linkPointSale($pointSale) { $pointSaleDistribution = PointSaleDistribution::searchOne([ 'id_distribution' => $this->id, 'id_point_sale' => $pointSale->id ]) ; if(!$pointSaleDistribution) { $pointSaleDistribution = new PointSaleDistribution(); $pointSaleDistribution->id_distribution = $this->id; $pointSaleDistribution->id_point_sale = $pointSale->id; } $dayDistribution = date('N', strtotime($this->date)); $daysArray = [ 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saturday', 7 => 'sunday', ] ; $pointSaleDistribution->delivery = 0 ; $deliveryDay = 'delivery_'.$daysArray[$dayDistribution] ; if($pointSale->$deliveryDay) { $pointSaleDistribution->delivery = 1; } $pointSaleDistribution->save() ; } }