|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453 |
- <?php
-
-
-
- namespace common\models;
-
- use common\helpers\Debug;
- use common\helpers\GlobalParam;
- use common\helpers\Price;
- use Yii;
- use common\components\ActiveRecordCommon;
-
-
- class Product extends ActiveRecordCommon
- {
- public $total = 0;
- public $apply_distributions = true;
-
- public $price_with_tax = 0 ;
- public $wording_unit = '' ;
-
- 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', 'id_producer', 'id_tax_rate', 'id_product_category'], 'integer'],
- [['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday', 'unavailable', 'apply_distributions'], 'boolean'],
- [['price', 'weight', 'step', 'quantity_max', 'quantity_max_monday', 'quantity_max_tuesday', 'quantity_max_wednesday', 'quantity_max_thursday', 'quantity_max_friday', 'quantity_max_saturday', 'quantity_max_sunday'], 'number'],
- [['photo'], 'file'],
- [['name', 'reference', '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;
- }],
- [['price_with_tax', 'wording_unit'], 'safe']
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => 'Nom',
- 'reference' => 'Référence',
- 'description' => 'Description',
- 'active' => 'Actif',
- 'photo' => 'Photo',
- 'price' => 'Prix (€) TTC',
- 'price' => 'Prix (€) TTC',
- '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',
- 'quantity_max_monday' => 'Quantité max : lundi',
- 'quantity_max_tuesday' => 'Quantité max : mardi',
- 'quantity_max_wednesday' => 'Quantité max : mercredi',
- 'quantity_max_thursday' => 'Quantité max : jeudi',
- 'quantity_max_friday' => 'Quantité max : vendredi',
- 'quantity_max_saturday' => 'Quantité max : samedi',
- 'quantity_max_sunday' => 'Quantité max : dimanche',
- 'unavailable' => 'Épuisé',
- 'apply_distributions' => 'Appliquer ces modifications dans les distributions futures',
- 'unit' => 'Unité',
- 'step' => 'Pas',
- 'id_tax_rate' => 'TVA',
- 'id_product_category' => 'Catégorie'
- ];
- }
-
- public function afterFind() {
- if ($this->taxRate == null) {
- $producer = Producer::searchOne(['id' => GlobalParam::getCurrentProducerId()]) ;
- if($producer) {
- $this->populateRelation('taxRate', $producer->taxRate);
- }
- }
-
- $this->wording_unit = Product::strUnit($this->unit) ;
- $this->price_with_tax = $this->getPriceWithTax() ;
-
- parent::afterFind();
- }
-
- public function getProductDistribution()
- {
- return $this->hasMany(ProductDistribution::className(), ['id_product' => 'id']);
- }
-
- public function getProductSubscription()
- {
- return $this->hasMany(ProductSubscription::className(), ['id_product' => 'id']);
- }
-
- public function getTaxRate()
- {
- return $this->hasOne(TaxRate::className(), ['id' => 'id_tax_rate']);
- }
-
- public function getProductPrice()
- {
- return $this->hasMany(ProductPrice::className(), ['id_product' => 'id']);
- }
-
- public function getProductCategory()
- {
- return $this->hasOne(ProductCategory::className(), ['id' => 'id_product_category']) ;
- }
-
-
-
- public static function defaultOptionsSearch()
- {
- return [
- 'with' => ['taxRate'],
- '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 = GlobalParam::getCurrentProducerId();
- 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' => GlobalParam::getCurrentProducerId(),
- '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;
- }
-
- public function getPrice($params = [])
- {
- $specificPrices = $this->productPrice ;
-
- $user = isset($params['user']) ? $params['user'] : false ;
- $userProducer = isset($params['user_producer']) ? $params['user_producer'] : false ;
- $pointSale = isset($params['point_sale']) ? $params['point_sale'] : false ;
-
- if($specificPrices && ($user || $pointSale)) {
-
- $specificPricesArray = [
- 'user' => false,
- 'pointsale' => false,
- 'user_pointsale' => false,
- 'usergroup' => false,
- ] ;
-
- foreach($specificPrices as $specificPrice) {
- if($user
- && $specificPrice->id_user
- && !$specificPrice->id_point_sale
- && !$specificPrice->id_user_group
- && $specificPrice->id_user == $user->id) {
-
- $specificPricesArray['user'] = $specificPrice->price ;
- }
- if($user
- && $specificPrice->id_user_group
- && !$specificPrice->id_point_sale
- && !$specificPrice->id_user
- && $user->belongsToUserGroup($specificPrice->id_user_group)) {
-
- $specificPricesArray['usergroup'] = $specificPrice->price ;
- }
- if($pointSale
- && $specificPrice->id_point_sale
- && !$specificPrice->id_user
- && !$specificPrice->id_user_group
- && $specificPrice->id_point_sale == $pointSale->id) {
-
- $specificPricesArray['pointsale'] = $specificPrice->price ;
- }
-
- if($pointSale && $user
- && $specificPrice->id_point_sale
- && $specificPrice->id_user
- && $specificPrice->id_point_sale == $pointSale->id
- && $specificPrice->id_user == $user->id) {
-
- $specificPricesArray['user_pointsale'] = $specificPrice->price ;
- }
- }
-
- if($specificPricesArray['user_pointsale']) {
- return $specificPricesArray['user_pointsale'] ;
- }
- elseif($specificPricesArray['user']) {
- return $specificPricesArray['user'] ;
- }
- elseif($specificPricesArray['usergroup']) {
- return $specificPricesArray['usergroup'] ;
- }
- elseif($specificPricesArray['pointsale']) {
- return $specificPricesArray['pointsale'] ;
- }
- }
-
- if($userProducer && $userProducer->product_price_percent) {
- return $this->price * (1 + $userProducer->product_price_percent / 100) ;
- }
-
- if($pointSale && $pointSale->product_price_percent) {
- return $this->price * (1 + $pointSale->product_price_percent / 100) ;
- }
-
- return $this->price ;
- }
-
-
-
- public function getPriceWithTax($params = [])
- {
- $taxRateValue = $this->taxRate ? $this->taxRate->value : 0 ;
- return Price::getPriceWithTax($this->getPrice($params), $taxRateValue);
- }
-
- public function getTheTaxRate()
- {
- if($this->id_tax_rate) {
- return $this->id_tax_rate ;
- }
- else {
- return GlobalParam::getCurrentProducer()->taxRate->id;
- }
- }
-
- public function getNameExport()
- {
- $producer = GlobalParam::getCurrentProducer() ;
- if($producer->option_export_display_product_reference && $this->reference && strlen($this->reference) > 0) {
- return $this->reference ;
- }
-
- return $this->name ;
- }
-
-
- }
|