Просмотр исходного кода

Refactoring/traduction modèle Etablissement > Producer

refactoring
Guillaume Bourgeois 6 лет назад
Родитель
Сommit
6f0689e8a1
2 измененных файлов: 119 добавлений и 139 удалений
  1. +115
    -135
      common/models/Producer.php
  2. +4
    -4
      common/models/User.php

+ 115
- 135
common/models/Producer.php Просмотреть файл

@@ -39,29 +39,23 @@ termes.
namespace common\models;

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

/**
* This is the model class for table "etablissement".
*
* @property integer $id
* @property string $nom
* @property string $name
* @property string $siret
* @property string $logo
* @property string $photo
* @property string $description
* @property string $code_postal
* @property string $ville
* @property string $postcode
* @property string $city
*/
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
*/
@@ -76,24 +70,22 @@ class Producer extends \yii\db\ActiveRecord
public function rules()
{
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 = $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.');
}
}],
[['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'],
];
}

@@ -104,23 +96,23 @@ class Producer extends \yii\db\ActiveRecord
{
return [
'id' => 'ID',
'nom' => 'Nom',
'name' => 'Nom',
'siret' => 'Siret',
'logo' => 'Logo',
'photo' => 'Photo',
'description' => 'Description',
'code_postal' => 'Code postal',
'ville' => 'Ville',
'postcode' => 'Code postal',
'city' => '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',
'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',
'infos_commande' => 'Informations',
'order_infos' => 'Informations',
'slug' => 'Slug',
'type' => 'Type d\'établissement'
'type' => 'Type de producteur'
];
}

@@ -128,23 +120,23 @@ class Producer extends \yii\db\ActiveRecord
* Relations
*/
public function getUserEtablissement()
public function getUserProducer()
{
return $this->hasMany(
UserEtablissement::className(),
['id_etablissement' => 'id']
UserProducer::className(),
['id_producer' => 'id']
);
}

public function getUser()
{
return $this->hasMany(User::className(), ['id_etablissement' => 'id']);
return $this->hasMany(User::className(), ['id_producer' => 'id']);
}
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]);
}

/**
@@ -153,29 +145,30 @@ class Producer extends \yii\db\ActiveRecord
*
* @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];
}

/**
@@ -185,33 +178,36 @@ class Producer extends \yii\db\ActiveRecord
* @param boolean $format
* @return string
*/
public function getCA($periode = '', $format = false)
public function getTurnover($period = '', $format = false)
{
if (!$periode)
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
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();
$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;
}
}

/**
@@ -222,24 +218,12 @@ class Producer extends \yii\db\ActiveRecord
* @param boolean $format
* @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) {
return number_format($montant, 2) . ' €';
return number_format($this->free_price, 2) . ' €' ;
} else {
return $montant;
return $this->free_price ;
}
}

@@ -249,19 +233,16 @@ class Producer extends \yii\db\ActiveRecord
* @param string $periode
* @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;
}
@@ -271,9 +252,9 @@ class Producer extends \yii\db\ActiveRecord
*
* @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')));
}

/**
@@ -283,16 +264,16 @@ class Producer extends \yii\db\ActiveRecord
* @param integer $id_etablissement
* @return mixed
*/
public static function getConfig($config = '', $id_etablissement = 0)
public static function getConfig($config = '', $idProducer = 0)
{
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;
}
}

@@ -301,19 +282,19 @@ class Producer extends \yii\db\ActiveRecord

/**
* Retourne le montant de l'abonnement à prix libre définit par
* l'établissement
* le producteur.
*
* @param boolean $format
* @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) {
return number_format($this->prix_libre, 2, ',', false) . ' € HT';
return number_format($this->free_price, 2, ',', false) . ' € HT';
}
else {
return $this->prix_libre;
return $this->free_price;
}
}
}
@@ -325,30 +306,29 @@ class Producer extends \yii\db\ActiveRecord
* @param integer $id_producer
* @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 {
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 Просмотреть файл

@@ -59,10 +59,10 @@ use yii\db\Query ;
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 ;

Загрузка…
Отмена
Сохранить