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::find() ->where(['date' => $date]) ->one() ; 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) { $dayDistribution = date('N', strtotime($distribution->date)); $productsDistribution = ProductDistribution::searchAll([ 'id_distribution' => $distribution->id ]) ; if (!count($productsDistribution)) { foreach ($products as $product) { $productDistribution = new ProductDistribution(); $productDistribution->id_distribution = $distribution->id; $productDistribution->id_product = $product->id; $productDistribution->active = 0; if ($product->active && $dayDistribution == 1 && $product->monday) { $productDistribution->active = 1; } if ($product->active && $dayDistribution == 2 && $product->tuesday) { $productDistribution->active = 1; } if ($product->active && $dayDistribution == 3 && $product->wednesday) { $productDistribution->active = 1; } if ($product->active && $dayDistribution == 4 && $product->thursday) { $productDistribution->active = 1; } if ($product->active && $dayDistribution == 5 && $product->friday) { $productDistribution->active = 1; } if ($product->active && $dayDistribution == 6 && $product->saterday) { $productDistribution->active = 1; } if ($product->active && $dayDistribution == 7 && $product->sunday) { $productDistribution->active = 1; } $productDistribution->quantity_max = $product->quantity_max; $productDistribution->save(); } } } return $distribution ; } }