[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]], ['order_delay', 'in', 'range' => [1, 2, 3, 4, 5, 6, 7]], ['code', function($attribute, $params) { $code = $this->$attribute; $producer = Producer::findOne(['code' => $code]); if ($producer && $producer->id != $this->id) { $this->addError($attribute, 'Ce code est déjà utilisé par un autre producteur.'); } }], [['description', 'order_infos','slug'], 'string'], [['negative_balance', 'credit', 'active'], 'boolean'], [['name', 'siret', 'logo', 'photo', 'postcode', 'city', 'code','type'], 'string', 'max' => 255], ['free_price', 'double'], ['free_price', 'compare', 'compareValue' => 0, 'operator' => '>=', 'type' => 'number', 'message' => 'Prix libre doit être supérieur ou égal à 0'], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'name' => 'Nom', 'siret' => 'Siret', 'logo' => 'Logo', 'photo' => 'Photo', 'description' => 'Description', 'postcode' => 'Code postal', 'city' => 'Ville', 'code' => 'Code', 'order_deadline' => 'Heure limite de commande', 'order_delay' => 'Délai de commande', 'negative_balance' => 'Solde négatif', 'credit' => 'Crédit pain', 'active' => 'Actif', 'date_creation' => 'Date de création', 'order_infos' => 'Informations', 'slug' => 'Slug', 'type' => 'Type de producteur' ]; } /* * Relations */ public function getUserProducer() { return $this->hasMany( UserProducer::className(), ['id_producer' => 'id'] ); } public function getUser() { return $this->hasMany(User::className(), ['id_producer' => 'id']); } public function getContact() { return $this->hasMany(User::className(),['id_producer' => 'id']) ->where(['status' => User::STATUS_PRODUCER]); } /** * Retourne les options de base nécessaires à la fonction de recherche. * * @return array */ public static function defaultOptionsSearch() { return [ 'with' => [], 'join_with' => [], 'orderby' => 'name ASC', 'attribute_id_producer' => 'id' ] ; } /** * Retourne la liste des établissements pour l'initialisation d'une liste * sélective. * * @return array */ public static function getProducerPopulateDropdown() { $producers = Producer::searchAll( ['active' => 1], ['orderby' => 'postcode, city ASC'] ) ; $departments = Departments::get(); $dataProducers = []; $optionsProducers = []; foreach ($producers as $p) { if (!key_exists('d' . substr($p->postcode, 0, 2), $dataProducers)) { $dataProducers['d' . substr($p->postcode, 0, 2)] = '' . $departments[substr($p->postcode, 0, 2)] . ''; $optionsProducers['d' . substr($p->postcode, 0, 2)] = ['disabled' => true]; } $dataProducers[$p->id] = ' ' . Html::encode($p->name) . ' - ' . Html::encode($p->postcode) . ' ' . Html::encode($p->city) . ' '; if (strlen($p->code)) { $optionsProducers[$p->id] = ['class' => 'lock']; } } return ['data' => $dataProducers, 'options' => $optionsProducers]; } /** * Retourne le CA de l'établissement pour un mois donné. * * @param string $period * @param boolean $format * @return string */ public function getTurnover($period = '', $format = false) { if (!$period) { $period = date('Y-m'); } $connection = Yii::$app->getDb(); $command = $connection->createCommand(' SELECT SUM(product_order.price * product_order.quantity) AS turnover FROM `order`, product_order, distribution, product WHERE `order`.id = product_order.id_order AND distribution.id_producer = :id_producer AND `order`.id_distribution = distribution.id AND product_order.id_product = product.id AND distribution.date > :date_begin AND distribution.date < :date_end', [ ':date_begin' => date('Y-m-31', strtotime("-1 month", strtotime($period))), ':date_end' => date('Y-m-01', strtotime("+1 month", strtotime($period))), ':id_producer' => $this->id ]); $result = $command->queryOne(); $turnover = $result['turnover']; if ($format) { return number_format($turnover, 2) . ' €'; } else { return $turnover; } } /** * Retourne le montant à facturer pour une période donnée. * * @param string $periode * @param float $ca * @param boolean $format * @return string */ public function getMAmountBilled($format = false) { if ($format) { return number_format($this->free_price, 2) . ' €' ; } else { return $this->free_price ; } } /** * Retourne la facture d'une période donnée. * * @param string $periode * @return Facture */ public function getInvoice($period = '') { if (!$period) { $period = date('Y-m', strtotime('-1 month')); } $invoice = Invoice::searchOne( ['id_producer' => $this->id, 'period' => ':period'], ['params' => [':period' => $period]] ) ; return $facture; } /** * Retourne la facture du mois dernier. * * @return Facture */ public function getInvoiceLastMonth() { return $this->getInvoice(date('Y-m', strtotime('-1 month'))); } /** * Retourne une configuration d'un établissement donné. * * @param string $config * @param integer $id_etablissement * @return mixed */ public static function getConfig($config = '', $idProducer = 0) { if (strlen($config)) { if (!$idProducer) { $idProducer = Producer::getId() ; } $producer = self::findOne($idProducer); if ($producer) { return $producer->$config; } } return false; } /** * Retourne le montant de l'abonnement à prix libre définit par * le producteur. * * @param boolean $format * @return mixed */ public function getFreePrice($format = true) { if (!is_null($this->free_price)) { if($format) { return number_format($this->free_price, 2, ',', false) . ' € HT'; } else { return $this->free_price; } } } /** * Lie un utilisateur à un producteur. * * @param integer $id_user * @param integer $id_producer * @return UserProducer */ public static function addUser($idUser, $idProducer) { $userProducer = UserProducer::searchOne([ 'id_user' => $idUser, 'id_producer' => $idProducer ]) ; if (!$userProducer) { $newUserProducer = new UserProducer; $newUserProducer->id_producer = $idProducer; $newUserProducer->id_user = $idUser; $newUserProducer->credit = 0; $newUserProducer->active = 1; $newUserProducer->bookmark = 1; $newUserProducer->save(); } else { if (!$userProducer->active) { $userProducer->active = 1; $userProducer->save(); } } return $userProducer ; } /** * Retourne le producteur courant (le producteur auquel l'utilisateur * connecté est rattaché). * * @return integer|boolean */ public static function getId() { if(!Yii::$app->user->isGuest) { return Yii::$app->user->identity->id_producer ; } return false ; } /** * Retourne le producteur courant. * * @return Producer|boolean */ public static function get() { if(self::getId()) { return self::searchOne() ; } return false ; } }