Browse Source

Refactoring/traduction modèle Produit > Product

refactoring
Guillaume Bourgeois 6 years ago
parent
commit
bd73151814
5 changed files with 59 additions and 67 deletions
  1. +1
    -1
      common/components/ActiveRecordCommon.php
  2. +1
    -1
      common/models/Development.php
  3. +2
    -2
      common/models/PointSale.php
  4. +2
    -2
      common/models/Producer.php
  5. +53
    -61
      common/models/Product.php

+ 1
- 1
common/components/ActiveRecordCommon.php View File

$pk = $class::tableName().'.'.$pk[0] ; $pk = $class::tableName().'.'.$pk[0] ;
if (!isset($params[$options['attribute_id_producer']]) && !Yii::$app->user->isGuest) { if (!isset($params[$options['attribute_id_producer']]) && !Yii::$app->user->isGuest) {
$params[$options['attribute_id_producer']] = Producer::getCurrent();
$params[$options['attribute_id_producer']] = Producer::getCurrentId();
} }
if(!isset($options['type_search'])) { if(!isset($options['type_search'])) {

+ 1
- 1
common/models/Development.php View File

return $this->hasOne( return $this->hasOne(
DevelopmentPriority::className(), DevelopmentPriority::className(),
['id_development' => 'id']) ['id_development' => 'id'])
->where(['id_producer' => Producer::getCurrent()])
->where(['id_producer' => Producer::getCurrentId()])
->with('producer'); ->with('producer');
} }



+ 2
- 2
common/models/PointSale.php View File

*/ */
public function save($runValidation = true, $attributeNames = NULL) public function save($runValidation = true, $attributeNames = NULL)
{ {
$this->id_producer = Producer::getCurrent();
$this->id_producer = Producer::getCurrentId();
return parent::save($runValidation, $attributeNames); return parent::save($runValidation, $attributeNames);
} }


*/ */
public static function count() public static function count()
{ {
return self::searchCount(['id_producer' => Producer::getCurrent()]) ;
return self::searchCount(['id_producer' => Producer::getCurrentId()]) ;
} }


/** /**

+ 2
- 2
common/models/Producer.php View File

{ {
if (strlen($config)) { if (strlen($config)) {
if (!$idProducer) { if (!$idProducer) {
$idProducer = Producer::getCurrent() ;
$idProducer = Producer::getCurrentId() ;
} }
$producer = self::findOne($idProducer); $producer = self::findOne($idProducer);
* *
* @return boolean * @return boolean
*/ */
public static function getCurrent()
public static function getCurrentId()
{ {
if(!Yii::$app->user->isGuest) { if(!Yii::$app->user->isGuest) {
return Yii::$app->user->identity->id_producer ; return Yii::$app->user->identity->id_producer ;

+ 53
- 61
common/models/Product.php View File

*/ */
class Product extends \yii\db\ActiveRecord class Product extends \yii\db\ActiveRecord
{ {
var $total = 0;


var $total = 0;

const SALE_MODE_UNIT = 'unit' ;
const SALE_MODE_WEIGHT = 'weight' ;
const SALE_MODE_UNIT = 'unit' ;
const SALE_MODE_WEIGHT = 'weight' ;
/** /**
* @inheritdoc * @inheritdoc
public function rules() public function rules()
{ {
return [ return [
[['nom', 'id_etablissement'], 'required'],
[['actif', 'order', 'quantite_max', 'id_etablissement'], 'integer'],
[['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'epuise'], 'boolean'],
[['prix', 'poids'], 'number'],
[['illustration', 'photo'], 'file'],
[['nom', 'description', 'illustration', 'photo', 'saison', 'diminutif'], 'string', 'max' => 255],
[['recette'], 'string', 'max' => 1000],
[['name', 'id_producer'], 'required'],
[['active', 'order', 'quantity_max', 'id_producer'], 'integer'],
[['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saterday', 'sunday', 'unavailable'], 'boolean'],
[['price', 'weight'], 'number'],
[[ 'photo'], 'file'],
[['name', 'description', 'photo'], 'string', 'max' => 255],
[['recipe'], 'string', 'max' => 1000],
]; ];
} }


{ {
return [ return [
'id' => 'ID', 'id' => 'ID',
'nom' => 'Nom',
'name' => 'Nom',
'description' => 'Description', 'description' => 'Description',
'actif' => 'Actif',
'illustration' => 'Illustration',
'active' => 'Actif',
'photo' => 'Photo', 'photo' => 'Photo',
'saison' => 'Saison',
'prix' => 'Prix',
'poids' => 'Poids (g)',
'recette' => 'Recette',
'lundi' => 'Lundi',
'mardi' => 'Mardi',
'mercredi' => 'Mercredi',
'jeudi' => 'Jeudi',
'vendredi' => 'Vendredi',
'samedi' => 'Samedi',
'dimanche' => 'Dimanche',
'price' => 'Prix',
'weight' => 'Poids (g)',
'recipe' => 'Recette',
'monday' => 'Lundi',
'tuesday' => 'Mardi',
'wednesday' => 'Mercredi',
'thursday' => 'Jeudi',
'friday' => 'Vendredi',
'saterday' => 'Samedi',
'sunday' => 'Dimanche',
'order' => 'Ordre', 'order' => 'Ordre',
'quantite_max' => 'Quantité max par défaut',
'epuise' => 'Épuisé',
'quantity_max' => 'Quantité max par défaut',
'unavailable' => 'Épuisé',
]; ];
} }
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch() {
return [
'with' => [],
'join_with' => [],
'orderby' => 'order ASC',
'attribute_id_producer' => 'order.id_producer'
] ;
}


/** /**
* Retourne la description du produit. * Retourne la description du produit.
public function getDescription() public function getDescription()
{ {
$description = $this->description; $description = $this->description;
if (isset($this->poids) && is_numeric($this->poids) && $this->poids > 0) {
if ($this->poids >= 1000) {
$description .= ' (' . ($this->poids / 1000) . 'kg)';
if (isset($this->weight) && is_numeric($this->weight) && $this->weight > 0) {
if ($this->weight >= 1000) {
$description .= ' (' . ($this->weight / 1000) . 'kg)';
} else { } else {
$description .= ' (' . $this->poids . 'g)';
$description .= ' (' . $this->weight . 'g)';
} }
} }
return $description; return $description;
* Retourne le libellé (admin) du produit. * Retourne le libellé (admin) du produit.
* @return type * @return type
*/ */
public function getLibelleAdmin()
public function getStrWordingAdmin()
{ {
return $this->nom;
return $this->name;
} }


/** /**
*/ */
public function save($runValidation = true, $attributeNames = NULL) public function save($runValidation = true, $attributeNames = NULL)
{ {
$this->id_etablissement = Yii::$app->user->identity->id_etablissement;
$this->id_producer = Producer::getCurrentId();
return parent::save($runValidation, $attributeNames); return parent::save($runValidation, $attributeNames);
} }


/**
* Retourne les produits de l'établissement courant.
*
* @return array
*/
public function getAll()
{
return Produit::find()
->where([
'id_etablissement' => Yii::$app->user->identity->id_etablissement,
])
->orderBy('order ASC')
->all();
}

/** /**
* Retourne les produits d'une production donnée. * Retourne les produits d'une production donnée.
* *
* @param integer $id_production
* @param integer $idDistribution
* @return array * @return array
*/ */
public function getByProduction($id_production)
public function searchByDistribution($idDistribution)
{ {
return Produit::find()
->leftJoin('production_produit', 'produit.id = production_produit.id_produit')
return Product::find()
->leftJoin('product_distribution', 'product.id = product_distribution.id_product')
->where([ ->where([
'id_etablissement' => Yii::$app->user->identity->id_etablissement,
'production_produit.id_production' => $id_production
'id_producer' => Producer::getCurrentId(),
'product_distribution.id_distribution' => $idDistribution
]) ])
->orderBy('production_produit.actif DESC, produit.order ASC')
->orderBy('product_distribution.active DESC, product.order ASC')
->all(); ->all();
} }


*/ */
public static function count() public static function count()
{ {
return Produit::find()
->where([
'id_etablissement' => Yii::$app->user->identity->id_etablissement
])
->count();
return self::searchCount() ;
} }


} }

Loading…
Cancel
Save