[8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]], ['delai_commande', 'in', 'range' => [1, 2, 3, 4, 5, 6, 7]], ['code', function($attribute, $params) { $code = $this->$attribute; $etablissement = Etablissement::findOne(['code' => $code]); if ($etablissement && $etablissement->id != $this->id) { $this->addError($attribute, 'Ce code est déjà utilisé par un autre producteur.'); } }], [['description', 'infos_commande','slug'], 'string'], [['solde_negatif', 'credit_pain', 'actif'], 'boolean'], [['nom', 'siret', 'logo', 'photo', 'code_postal', 'ville', 'code','type'], 'string', 'max' => 255], ['prix_libre', 'double'], ['prix_libre', 'compare', 'compareValue' => 0, 'operator' => '>=', 'type' => 'number', 'message' => 'Prix libre doit être supérieur ou égal à 0'], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'nom' => 'Nom', 'siret' => 'Siret', 'logo' => 'Logo', 'photo' => 'Photo', 'description' => 'Description', 'code_postal' => 'Code postal', 'ville' => 'Ville', 'code' => 'Code', 'heure_limite_commande' => 'Heure limite de commande', 'delai_commande' => 'Délai de commande', 'solde_negatif' => 'Solde négatif', 'credit_pain' => 'Crédit pain', 'actif' => 'Actif', 'date_creation' => 'Date de création', 'infos_commande' => 'Informations', 'slug' => 'Slug', 'type' => 'Type d\'établissement' ]; } /* * Relations */ public function getUserEtablissement() { return $this->hasMany( UserEtablissement::className(), ['id_etablissement' => 'id'] ); } public function getUser() { return $this->hasMany(User::className(), ['id_etablissement' => 'id']); } public function getContact() { return $this->hasMany(User::className(),['id_etablissement' => 'id']) ->where(['status' => User::STATUS_BOULANGER]); } /** * Retourne la liste des établissements pour l'initialisation d'une liste * sélective. * * @return array */ public static function getEtablissementsPopulateDropdown() { $etablissements_dispos = Etablissement::find() ->where(['actif' => 1]) ->orderby('code_postal, ville ASC') ->all(); $departements = Departements::get(); $data_etablissements_dispos = []; $options_etablissements_dispos = []; foreach ($etablissements_dispos as $e) { if ($e->etatPaiement() == self::PAIEMENT_OK || $e->etatPaiement() == self::PAIEMENT_ESSAI) { if (!key_exists('d' . substr($e->code_postal, 0, 2), $data_etablissements_dispos)) { $data_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = '' . $departements[substr($e->code_postal, 0, 2)] . ''; $options_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = ['disabled' => true]; } $data_etablissements_dispos[$e->id] = ' ' . Html::encode($e->nom) . ' - ' . Html::encode($e->code_postal) . ' ' . Html::encode($e->ville) . ' '; if (strlen($e->code)) $options_etablissements_dispos[$e->id] = ['class' => 'lock']; } } return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos]; } /** * Retourne le CA de l'établissement pour un mois donné. * * @param string $periode * @param boolean $format * @return string */ public function getCA($periode = '', $format = false) { if (!$periode) $periode = date('Y-m'); $connection = Yii::$app->getDb(); $command = $connection->createCommand(' SELECT SUM(IF(produit.vrac,0,commande_produit.prix * commande_produit.quantite)) AS CA FROM commande, commande_produit, production, produit WHERE commande.id = commande_produit.id_commande AND production.id_etablissement = :id_etablissement AND commande.id_production = production.id AND commande_produit.id_produit = produit.id AND production.date > :date_debut AND production.date < :date_fin', [ ':date_debut' => date('Y-m-31', strtotime("-1 month", strtotime($periode))), ':date_fin' => date('Y-m-01', strtotime("+1 month", strtotime($periode))), ':id_etablissement' => $this->id ]); $result = $command->queryOne(); $ca = $result['CA']; if ($format) return number_format($ca, 2) . ' €'; else return $ca; } /** * Retourne le montant à facturer pour une période donnée. * * @param string $periode * @param float $ca * @param boolean $format * @return string */ public function getMontantFacturer($periode = '', $ca = 0, $format = false) { if (!$periode) $periode = date('Y-m'); if (!$ca) $ca = $this->getCA($periode); if ($ca < 500) { $montant = 0; } else { $montant = $ca * 0.02; } if ($format) { return number_format($montant, 2) . ' €'; } else { return $montant; } } /** * Retourne la facture d'une période donnée. * * @param string $periode * @return Facture */ public function getFacture($periode = '') { if (!$periode) $periode = date('Y-m', strtotime('-1 month')); $facture = Facture::find() ->where('id_etablissement = :id_etablissement') ->andWhere('periode = :periode') ->addParams([ ':id_etablissement' => $this->id, ':periode' => $periode, ]) ->one(); return $facture; } /** * Retourne la facture du mois dernier. * * @return Facture */ public function factureMoisDernier() { return $this->getFacture(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 = '', $id_etablissement = 0) { if (strlen($config)) { if (!$id_etablissement) { $id_etablissement = Yii::$app->user->identity->id_etablissement; } $etablissement = self::findOne($id_etablissement); if ($etablissement) { return $etablissement->$config; } } return false; } /** * Retourne le montant de l'abonnement à prix libre définit par * l'établissement * * @param boolean $format * @return mixed */ public function getPrixLibre($format = true) { if (!is_null($this->prix_libre)) { if($format) { return number_format($this->prix_libre, 2, ',', false) . ' € HT'; } else { return $this->prix_libre; } } } /** * Lie un utilisateur à un producteur. * * @param integer $id_user * @param integer $id_producer * @return UserProducer */ public static function addUser($id_user, $id_producer) { $user_producer = UserEtablissement::find() ->where([ 'id_user' => $id_user, 'id_etablissement' => $id_producer ])->one(); if (!$user_producer) { $new_user_producer = new UserEtablissement; $new_user_producer->id_etablissement = $id_producer; $new_user_producer->id_user = $id_user; $new_user_producer->credit = 0; $new_user_producer->actif = 1; $new_user_producer->favoris = 1; $new_user_producer->save(); } else { if (!$user_producer->actif) { $user_producer->actif = 1; $user_producer->save(); } } return $user_producer ; } }