Browse Source

Refactoring/traduction modèle Etablissement > Producer

refactoring
Guillaume Bourgeois 5 years ago
parent
commit
6f0689e8a1
2 changed files with 119 additions and 139 deletions
  1. +115
    -135
      common/models/Producer.php
  2. +4
    -4
      common/models/User.php

+ 115
- 135
common/models/Producer.php View File

namespace common\models; namespace common\models;


use Yii; use Yii;
use common\helpers\Departements;
use common\helpers\Departments;
use yii\helpers\Html; use yii\helpers\Html;


/** /**
* This is the model class for table "etablissement". * This is the model class for table "etablissement".
* *
* @property integer $id * @property integer $id
* @property string $nom
* @property string $name
* @property string $siret * @property string $siret
* @property string $logo * @property string $logo
* @property string $photo * @property string $photo
* @property string $description * @property string $description
* @property string $code_postal
* @property string $ville
* @property string $postcode
* @property string $city
*/ */
class Producer extends \yii\db\ActiveRecord class Producer extends \yii\db\ActiveRecord
{ {

const PAIEMENT_OK = 'ok';
const PAIEMENT_ESSAI = 'essai';
const PAIEMENT_ESSAI_TERMINE = 'essai-terminee';
const PAIEMENT_RETARD = 'retard-paiement';

/** /**
* @inheritdoc * @inheritdoc
*/ */
public function rules() public function rules()
{ {
return [ return [
[['nom', 'siret', 'heure_limite_commande', 'delai_commande','type'], 'required'],
[['heure_limite_commande', 'delai_commande'], 'integer'],
['heure_limite_commande', 'in', 'range' => [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]],
[['name', 'siret', 'order_deadline', 'order_delay','type'], 'required'],
[['order_deadline', 'order_delay'], 'integer'],
['order_deadline', 'in', 'range' => [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', function($attribute, $params) {
$code = $this->$attribute; $code = $this->$attribute;

$etablissement = Etablissement::findOne(['code' => $code]);

if ($etablissement && $etablissement->id != $this->id) {
$producer = Producer::findOne(['code' => $code]);
if ($producer && $producer->id != $this->id) {
$this->addError($attribute, 'Ce code est déjà utilisé par un autre producteur.'); $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'],
[['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'],
]; ];
} }


{ {
return [ return [
'id' => 'ID', 'id' => 'ID',
'nom' => 'Nom',
'name' => 'Nom',
'siret' => 'Siret', 'siret' => 'Siret',
'logo' => 'Logo', 'logo' => 'Logo',
'photo' => 'Photo', 'photo' => 'Photo',
'description' => 'Description', 'description' => 'Description',
'code_postal' => 'Code postal',
'ville' => 'Ville',
'postcode' => 'Code postal',
'city' => 'Ville',
'code' => 'Code', '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',
'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', 'date_creation' => 'Date de création',
'infos_commande' => 'Informations',
'order_infos' => 'Informations',
'slug' => 'Slug', 'slug' => 'Slug',
'type' => 'Type d\'établissement'
'type' => 'Type de producteur'
]; ];
} }


* Relations * Relations
*/ */
public function getUserEtablissement()
public function getUserProducer()
{ {
return $this->hasMany( return $this->hasMany(
UserEtablissement::className(),
['id_etablissement' => 'id']
UserProducer::className(),
['id_producer' => 'id']
); );
} }


public function getUser() public function getUser()
{ {
return $this->hasMany(User::className(), ['id_etablissement' => 'id']);
return $this->hasMany(User::className(), ['id_producer' => 'id']);
} }
public function getContact() public function getContact()
{ {
return $this->hasMany(User::className(),['id_etablissement' => 'id'])
->where(['status' => User::STATUS_BOULANGER]);
return $this->hasMany(User::className(),['id_producer' => 'id'])
->where(['status' => User::STATUS_PRODUCER]);
} }


/** /**
* *
* @return array * @return array
*/ */
public static function getEtablissementsPopulateDropdown()
public static function getProducerPopulateDropdown()
{ {
$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 (!key_exists('d' . substr($e->code_postal, 0, 2), $data_etablissements_dispos)) {
$data_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = '<strong>' . $departements[substr($e->code_postal, 0, 2)] . '</strong>';
$options_etablissements_dispos['d' . substr($e->code_postal, 0, 2)] = ['disabled' => true];
$producers = Producer::searchAll(
['active' => 1],
['orderby' => 'postcode, city ASC']
) ;
$departments = Departmments::get();
$dataProducers = [];
$optionsProducers = [];
foreach ($producers as $p) {
if (!key_exists('d' . substr($p->postcode, 0, 2), $dataProducers)) {
$dataProducers['d' . substr($p->postcode, 0, 2)] = '<strong>' . $departments[substr($e->postcode, 0, 2)] . '</strong>';
$optionsProducers['d' . substr($p->postcode, 0, 2)] = ['disabled' => true];
} }


$data_etablissements_dispos[$e->id] = '<span class="glyphicon glyphicon-lock"></span> ' . Html::encode($e->nom) . ' - ' . Html::encode($e->code_postal) . ' ' . Html::encode($e->ville) . ' <span class="glyphicon glyphicon-lock"></span>';
$dataProducers[$p->id] = '<span class="glyphicon glyphicon-lock"></span> ' . Html::encode($p->name) . ' - ' . Html::encode($p->postcode) . ' ' . Html::encode($p->city) . ' <span class="glyphicon glyphicon-lock"></span>';


if (strlen($e->code))
$options_etablissements_dispos[$e->id] = ['class' => 'lock'];
if (strlen($p->code)) {
$optionsProducers[$p->id] = ['class' => 'lock'];
}
} }


return ['data' => $data_etablissements_dispos, 'options' => $options_etablissements_dispos];
return ['data' => $dataProducers, 'options' => $optionsProducers];
} }


/** /**
* @param boolean $format * @param boolean $format
* @return string * @return string
*/ */
public function getCA($periode = '', $format = false)
public function getTurnover($period = '', $format = false)
{ {
if (!$periode)
if (!$periode) {
$periode = date('Y-m'); $periode = date('Y-m');

}
$connection = Yii::$app->getDb(); $connection = Yii::$app->getDb();
$command = $connection->createCommand(' $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
SELECT SUM(productOrder.price * productOrder.quantity) AS turnover
FROM order, productOrder, distribution, product
WHERE order.id = productOrder.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(); $result = $command->queryOne();
$ca = $result['CA'];
$turnover = $result['turnover'];


if ($format)
return number_format($ca, 2) . ' €';
else
return $ca;
if ($format) {
return number_format($turnover, 2) . ' €';
}
else {
return $turnover;
}
} }


/** /**
* @param boolean $format * @param boolean $format
* @return string * @return string
*/ */
public function getMontantFacturer($periode = '', $ca = 0, $format = false)
public function getMAmountBilled($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) { if ($format) {
return number_format($montant, 2) . ' €';
return number_format($this->free_price, 2) . ' €' ;
} else { } else {
return $montant;
return $this->free_price ;
} }
} }


* @param string $periode * @param string $periode
* @return Facture * @return Facture
*/ */
public function getFacture($periode = '')
public function getInvoice($period = '')
{ {
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();
if (!$period) {
$period = date('Y-m', strtotime('-1 month'));
}
$invoice = Invoice::searchOne(
['id_producer' => $this->id, 'period' => ':period'],
['params' => [':period' => $period]]
) ;


return $facture; return $facture;
} }
* *
* @return Facture * @return Facture
*/ */
public function factureMoisDernier()
public function getInvoiceLastMonth()
{ {
return $this->getFacture(date('Y-m', strtotime('-1 month')));
return $this->getInvoice(date('Y-m', strtotime('-1 month')));
} }


/** /**
* @param integer $id_etablissement * @param integer $id_etablissement
* @return mixed * @return mixed
*/ */
public static function getConfig($config = '', $id_etablissement = 0)
public static function getConfig($config = '', $idProducer = 0)
{ {
if (strlen($config)) { if (strlen($config)) {
if (!$id_etablissement) {
$id_etablissement = Yii::$app->user->identity->id_etablissement;
if (!$idProducer) {
$idProducer = Producer::getCurrent() ;
} }
$etablissement = self::findOne($id_etablissement);
if ($etablissement) {
return $etablissement->$config;
$producer = self::findOne($idProducer);
if ($producer) {
return $producer->$config;
} }
} }




/** /**
* Retourne le montant de l'abonnement à prix libre définit par * Retourne le montant de l'abonnement à prix libre définit par
* l'établissement
* le producteur.
* *
* @param boolean $format * @param boolean $format
* @return mixed * @return mixed
*/ */
public function getPrixLibre($format = true)
public function getFreePrice($format = true)
{ {
if (!is_null($this->prix_libre)) {
if (!is_null($this->free_price)) {
if($format) { if($format) {
return number_format($this->prix_libre, 2, ',', false) . ' € HT';
return number_format($this->free_price, 2, ',', false) . ' € HT';
} }
else { else {
return $this->prix_libre;
return $this->free_price;
} }
} }
} }
* @param integer $id_producer * @param integer $id_producer
* @return UserProducer * @return UserProducer
*/ */
public static function addUser($id_user, $id_producer)
public static function addUser($idUser, $idProducer)
{ {
$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();
$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 { } else {
if (!$user_producer->actif) {
$user_producer->actif = 1;
$user_producer->save();
if (!$userProducer->active) {
$userProducer->active = 1;
$userProducer->save();
} }
} }
return $user_producer ;
return $userProducer ;
} }
/** /**

+ 4
- 4
common/models/User.php View File

class User extends ActiveRecord implements IdentityInterface class User extends ActiveRecord implements IdentityInterface
{ {


const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const STATUS_BOULANGER = 11;
const STATUS_ADMIN = 13;
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const STATUS_PRODUCER = 11;
const STATUS_ADMIN = 13;


const ID_USER_SYSTEM = 0 ; const ID_USER_SYSTEM = 0 ;

Loading…
Cancel
Save