[ 'unit' => 'piece', 'wording_unit' => 'la pièce', 'wording' => 'pièce(s)', 'wording_short' => 'p.', 'coefficient' => 1 ], 'g' => [ 'unit' => 'g', 'wording_unit' => 'le g', 'wording' => 'g', 'wording_short' => 'g', 'coefficient' => 1000 ], 'kg' => [ 'unit' => 'kg', 'wording_unit' => 'le kg', 'wording' => 'kg', 'wording_short' => 'kg', 'coefficient' => 1 ], 'mL' => [ 'unit' => 'mL', 'wording_unit' => 'le mL', 'wording' => 'mL', 'wording_short' => 'mL', 'coefficient' => 1000 ], 'L' => [ 'unit' => 'L', 'wording_unit' => 'le litre', 'wording' => 'L', 'wording_short' => 'L', 'coefficient' => 1 ], ]; /** * @inheritdoc */ public static function tableName() { return 'product'; } /** * @inheritdoc */ public function rules() { return [ [['name', 'id_producer'], 'required'], [['active', 'order', 'id_producer', 'id_tax_rate'], 'integer'], [['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'unavailable', 'apply_distributions'], 'boolean'], [['price', 'weight', 'step', 'quantity_max', 'quantity_max_monday', 'quantity_max_tuesday', 'quantity_max_wednesday', 'quantity_max_thursday', 'quantity_max_friday', 'quantity_max_saturday', 'quantity_max_sunday'], 'number'], [['photo'], 'file'], [['name', 'description', 'photo', 'unit'], 'string', 'max' => 255], [['recipe'], 'string', 'max' => 1000], ['step', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { if ($model->unit != 'piece') { return true; } return false; }], [['price_with_tax', 'wording_unit'], 'safe'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'name' => 'Nom', 'description' => 'Description', 'active' => 'Actif', 'photo' => 'Photo', 'price' => 'Prix (€) TTC', 'weight' => 'Poids', 'recipe' => 'Recette', 'monday' => 'Lundi', 'tuesday' => 'Mardi', 'wednesday' => 'Mercredi', 'thursday' => 'Jeudi', 'friday' => 'Vendredi', 'saturday' => 'Samedi', 'sunday' => 'Dimanche', 'order' => 'Ordre', 'quantity_max' => 'Quantité max par défaut', 'quantity_max_monday' => 'Quantité max : lundi', 'quantity_max_tuesday' => 'Quantité max : mardi', 'quantity_max_wednesday' => 'Quantité max : mercredi', 'quantity_max_thursday' => 'Quantité max : jeudi', 'quantity_max_friday' => 'Quantité max : vendredi', 'quantity_max_saturday' => 'Quantité max : samedi', 'quantity_max_sunday' => 'Quantité max : dimanche', 'unavailable' => 'Épuisé', 'apply_distributions' => 'Appliquer ces modifications dans les distributions futures', 'unit' => 'Unité', 'step' => 'Pas', 'id_tax_rate' => 'TVA' ]; } public function afterFind() { if ($this->taxRate == null) { $producer = Producer::searchOne(['id' => GlobalParam::getCurrentProducerId()]) ; if($producer) { $this->populateRelation('taxRate', $producer->taxRate); } //$this->populateRelation('taxRate', GlobalParam::getCurrentProducer()->taxRate); } $this->wording_unit = Product::strUnit($this->unit) ; $this->price_with_tax = $this->getpriceWithTax() ; parent::afterFind(); } public function getProductDistribution() { return $this->hasMany(ProductDistribution::className(), ['id_product' => 'id']); } public function getProductSubscription() { return $this->hasMany(ProductSubscription::className(), ['id_product' => 'id']); } public function getTaxRate() { return $this->hasOne(TaxRate::className(), ['id' => 'id_tax_rate']); } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => ['taxRate'], 'join_with' => [], 'orderby' => 'order ASC', 'attribute_id_producer' => 'product.id_producer' ]; } /** * Retourne la description du produit. * * @return string */ public function getDescription() { $description = $this->description; if (isset($this->weight) && is_numeric($this->weight) && $this->weight > 0) { if ($this->weight >= 1000) { $description .= ' (' . ($this->weight / 1000) . 'kg)'; } else { $description .= ' (' . $this->weight . 'g)'; } } return $description; } /** * Retourne le libellé (admin) du produit. * @return type */ public function getStrWordingAdmin() { return $this->name; } /** * Enregistre le produit. * * @param boolean $runValidation * @param array $attributeNames * @return boolean */ public function save($runValidation = true, $attributeNames = NULL) { $this->id_producer = GlobalParam::getCurrentProducerId(); return parent::save($runValidation, $attributeNames); } /** * Retourne les produits d'une production donnée. * * @param integer $idDistribution * @return array */ public static function searchByDistribution($idDistribution) { return Product::find() ->leftJoin('product_distribution', 'product.id = product_distribution.id_product') ->where([ 'id_producer' => GlobalParam::getCurrentProducerId(), 'product_distribution.id_distribution' => $idDistribution ]) ->orderBy('product_distribution.active DESC, product.order ASC') ->all(); } /** * Retourne le nombre de produits du producteur courant. * * @return integer */ public static function count() { return self::searchCount(); } /** * Retourne le produit "Don". * * @return Product */ public static function getProductGift() { $productGift = Product::find() ->where([ 'product.id_producer' => 0 ]) ->andFilterWhere(['like', 'product.name', 'Don']) ->one(); return $productGift; } /** * Retourne le libellé d'une unité. * * @param $format wording_unit, wording, short * @param $unitInDb Unité stockée en base de données (ex: si g > kg, si mL > L) * @return $string Libellé de l'unité */ public static function strUnit($unit, $format = 'wording_short', $unitInDb = false) { $strUnit = ''; if ($unitInDb) { if ($unit == 'g') { $unit = 'kg'; } if ($unit == 'mL') { $unit = 'L'; } } if (isset(self::$unitsArray[$unit]) && isset(self::$unitsArray[$unit][$format])) { $strUnit = self::$unitsArray[$unit][$format]; } return $strUnit; } public function getPrice() { return $this->price ; } /** * Retourne le prix du produit avec taxe */ public function getPriceWithTax() { $taxRateValue = $this->taxRate ? $this->taxRate->value : 0 ; return Price::getPriceWithTax($this->price, $taxRateValue); } public function getTheTaxRate() { if($this->id_tax_rate) { return $this->id_tax_rate ; } else { return GlobalParam::getCurrentProducer()->taxRate->id; } } }