[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','secret_key_payplug'], 'string'], [['negative_balance', 'credit', 'active','online_payment','user_manage_subscription'], 'boolean'], [['name', 'siret', 'logo', 'photo', 'postcode', 'city', 'code','type'], 'string', 'max' => 255], [['free_price', 'credit_limit_reminder'], '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', 'credit_limit_reminder' => 'Seuil de crédit limite avant relance', 'online_payment' => 'Activer le paiement en ligne', 'secret_key_payplug' => 'Clé secrète Payplug', 'user_manage_subscription' => 'Autoriser les utilisateurs à gérer leurs abonnements' ]; } /* * 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::find() ->where([ 'active' => true, ]) ->orderBy('postcode, city ASC') ->all() ; $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([ 'user_producer.id_user' => $idUser, 'user_producer.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 l'ID du producteur courant. * * @return integer|boolean */ public static function getId() { if(Yii::$app->controller->module->id == 'app-backend') { if(!Yii::$app->user->isGuest) { return Yii::$app->user->identity->id_producer ; } } else { return Yii::$app->controller->getProducer()->id ; ; } return false ; } /** * Retourne le producteur courant. * * @return Producer|boolean */ public static function getCurrent() { if(self::getId()) { return self::searchOne() ; } return false ; } /** * Retourne la date la plus proche où il est possible de commander. * Attention, cette méthode ne garantit pas qu'une distribution a été initialisée * à cette date. * * @return string */ public function getEarliestDateOrder() { $deadline = 20; $earliestDateOrder = date('Y-m-d'); if($this->order_deadline) { $deadline = $this->order_deadline; } if (date('H') >= $deadline) { $earliestDateOrder = date('Y-m-d', strtotime(date('Y-m-d')) + ($this->order_delay) * (24 * 60 * 60)); } else { $earliestDateOrder = date('Y-m-d', strtotime(date('Y-m-d')) + ($this->order_delay - 1) * (24 * 60 * 60)); } return $earliestDateOrder ; } /** * Retourne le chemin vers le fichier contenant la clé secrète d'API de Payplug * * @return string */ public function getFilenameSecretKeyPayplug() { return '../../common/config/payplug/'.$this->id.'.key' ; } /** * Enregistre la clé secrète d'API de Payplug dans un fichier */ public function saveSecretKeyPayplug() { if($this->online_payment) { $handle = fopen($this->getFilenameSecretKeyPayplug(),"w"); fwrite($handle,$this->secret_key_payplug); fclose($handle); } } /** * Retourne la clé secrète d'API de Payplug. * * @return string */ public function getSecretKeyPayplug() { if(file_exists($this->getFilenameSecretKeyPayplug())) { $handle = fopen($this->getFilenameSecretKeyPayplug(), "r") ; $filesize = filesize($this->getFilenameSecretKeyPayplug()) ; if($handle && $filesize) { $secretKey = fread($handle, $filesize); fclose($handle); return $secretKey ; } } return '' ; } public function getApiKeyMailjet($type = 'private') { $filename = '../../common/config/mailjet/api.key' ; if(file_exists($filename)) { $handle = fopen($filename, "r") ; $filesize = filesize($filename) ; if($handle && $filesize) { $apiKeys = fread($handle, $filesize); fclose($handle); $apiKeysArray = explode(':', $apiKeys) ; if(count($apiKeysArray) == 2) { if($type == 'private') { return $apiKeysArray[1] ; } else { return $apiKeysArray[0] ; } } } } return '' ; } }