- <?php
-
-
-
- namespace common\models;
-
- use Yii;
- use common\components\ActiveRecordCommon ;
-
-
- class Product extends ActiveRecordCommon
- {
- var $total = 0;
- var $apply_distributions = true ;
-
- public static $unitsArray = [
- 'piece' => [
- 'unit' => 'piece',
- 'wording_unit' => 'la pièce',
- 'wording' => 'pièce(s)',
- 'wording_short' => 'p.',
- 'coefficient' => 1
- ],
- 'g' => [
- 'unit' => 'g',
- 'wording_unit' => 'le g',
- 'wording' => 'g',
- 'wording_short' => 'g',
- 'coefficient' => 1000
- ],
- 'kg' => [
- 'unit' => 'kg',
- 'wording_unit' => 'le kg',
- 'wording' => 'kg',
- 'wording_short' => 'kg',
- 'coefficient' => 1
- ],
- 'mL' => [
- 'unit' => 'mL',
- 'wording_unit' => 'le mL',
- 'wording' => 'mL',
- 'wording_short' => 'mL',
- 'coefficient' => 1000
- ],
- 'L' => [
- 'unit' => 'L',
- 'wording_unit' => 'le litre',
- 'wording' => 'L',
- 'wording_short' => 'L',
- 'coefficient' => 1
- ],
- ];
-
-
-
- public static function tableName()
- {
- return 'product';
- }
-
-
-
- public function rules()
- {
- return [
- [['name', 'id_producer'], 'required'],
- [['active', 'order', 'quantity_max', 'id_producer'], 'integer'],
- [['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'unavailable','apply_distributions'], 'boolean'],
- [['price', 'weight', 'step'], 'number'],
- [[ 'photo'], 'file'],
- [['name', 'description', 'photo', 'unit'], 'string', 'max' => 255],
- [['recipe'], 'string', 'max' => 1000],
- ['step', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
- if($model->unit != 'piece') {
- return true ;
- }
- return false ;
- }],
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => 'Nom',
- 'description' => 'Description',
- 'active' => 'Actif',
- 'photo' => 'Photo',
- 'price' => 'Prix (€)',
- 'weight' => 'Poids',
- 'recipe' => 'Recette',
- 'monday' => 'Lundi',
- 'tuesday' => 'Mardi',
- 'wednesday' => 'Mercredi',
- 'thursday' => 'Jeudi',
- 'friday' => 'Vendredi',
- 'saturday' => 'Samedi',
- 'sunday' => 'Dimanche',
- 'order' => 'Ordre',
- 'quantity_max' => 'Quantité max par défaut',
- 'unavailable' => 'Épuisé',
- 'apply_distributions' => 'Appliquer ces modifications dans les distributions futures',
- 'unit' => 'Unité',
- 'step' => 'Pas'
- ];
- }
-
- public function getProductDistribution()
- {
- return $this->hasMany(ProductDistribution::className(), ['id_product' => 'id']);
- }
-
- public function getProductSubscription()
- {
- return $this->hasMany(ProductSubscription::className(), ['id_product' => 'id']);
- }
-
-
-
- public static function defaultOptionsSearch() {
- return [
- 'with' => [],
- 'join_with' => [],
- 'orderby' => 'order ASC',
- 'attribute_id_producer' => 'product.id_producer'
- ] ;
- }
-
-
-
- public function getDescription()
- {
- $description = $this->description;
- if (isset($this->weight) && is_numeric($this->weight) && $this->weight > 0) {
- if ($this->weight >= 1000) {
- $description .= ' (' . ($this->weight / 1000) . 'kg)';
- } else {
- $description .= ' (' . $this->weight . 'g)';
- }
- }
- return $description;
- }
-
-
-
- public function getStrWordingAdmin()
- {
- return $this->name;
- }
-
-
-
- public function save($runValidation = true, $attributeNames = NULL)
- {
- $this->id_producer = Producer::getId();
- return parent::save($runValidation, $attributeNames);
- }
-
-
-
- public static function searchByDistribution($idDistribution)
- {
- return Product::find()
- ->leftJoin('product_distribution', 'product.id = product_distribution.id_product')
- ->where([
- 'id_producer' => Producer::getId(),
- 'product_distribution.id_distribution' => $idDistribution
- ])
- ->orderBy('product_distribution.active DESC, product.order ASC')
- ->all();
- }
-
-
-
- public static function count()
- {
- return self::searchCount() ;
- }
-
-
-
- public static function getProductGift()
- {
- $productGift = Product::find()
- ->where([
- 'product.id_producer' => 0
- ])
- ->andFilterWhere(['like','product.name','Don'])
- ->one() ;
-
- return $productGift ;
- }
-
-
-
- public static function strUnit($unit, $format = 'wording_short', $unitInDb = false)
- {
- $strUnit = '' ;
-
- if($unitInDb) {
- if($unit == 'g') {
- $unit = 'kg' ;
- }
- if($unit == 'mL') {
- $unit = 'L' ;
- }
- }
-
- if(isset(self::$unitsArray[$unit]) && isset(self::$unitsArray[$unit][$format])) {
- $strUnit = self::$unitsArray[$unit][$format] ;
- }
-
- return $strUnit ;
- }
- }
|