priceHt = $model->getPriceHtAttribute(); $model->priceTtc = $model->getPriceTtcAttribute(); $model->discountAmount = $model->getDiscountAmountAttribute(); $model->discountRate = $model->getDiscountRateAttribute(); $model->discountedPriceHt = $model->getDiscountedPriceHtAttribute(); $model->discountedPriceTtc = $model->getDiscountedPriceTtcAttribute(); }); } /** * Price calculations * We need : gross price (prix avant taxe et remises) * discounted price (prix avant taxe avec remises) * net price (prix avec taxe) * net discounted proice (prix avec taxe et remises) */ //Utility to bring prices as a pseudo relation public function getCalcPrices() { //$prices = new Collection(); $prices = array( 'priceHt' => $this->priceHt, 'discountAmount' => $this->discountAmount, 'discountRate' => $this->discountRate, 'discountedPriceHt' => $this->discountedPriceHt, 'priceTtc' => $this->priceTtc, 'discountedPriceTtc' => $this->discountedPriceTtc, ); //$prices->add($cartextendedItem); return $prices; } //gross price /** * Mutator method to set priceHt's value */ public function setPriceHtAttribute($value) { $this->priceHt = $value; } /** * Get the gross price * Accessor method for priceHt * * @author Olivier Legat */ public function getPriceHtAttribute() { if(isset($this->priceHt)) { return $this->priceHt; } else { //no calculations, alias of db property return $this->priceHt = $this->attributes['price']; } } //discounted price public function setDiscountedPriceHtAttribute($value) { $this->discountedPriceHt = $value; } public function getDiscountedPriceHtAttribute() { if (isset($this->discountedPriceHt)) { return $this->discountedPriceHt; } else { return $this->discountedPriceHt = $this->priceHt - $this->discountAmount; } } //net price public function setPriceTtcAttribute($value) { $this->priceTtc = $value; } public function getPriceTtcAttribute() { if (isset($this->priceTtc)) { return $this->priceTtc; } else { return $this->priceTtc = $this->priceHt * ( 1 + ( $this->tax->rate / 100)); } } //net discounted price public function setDiscountedPriceTtcAttribute($value) { $this->discountedPriceTtc = $value; } public function getDiscountedPriceTtcAttribute() { if (isset($this->discountedPriceTtc)) { return $this->discountedPriceTtc; } else { return $this->discountedPriceTtc = $this->discountedPriceHt * ( 1 + ( $this->tax->rate / 100)); } } /** * Discount calc */ //montant de remise public function setDiscountAmountAttribute($value) { $this->discountAmount = $value; } public function getDiscountAmountAttribute() { if (isset($this->discountAmount)) { return $this->discountAmount; } else { $applicablePromotion = $this->promotions->first(); if (null !== $applicablePromotion) { $promo_type = $applicablePromotion->discount_type_id; if ($promo_type == 1) { $discountAmount = $applicablePromotion->discount_value; } else { $discountAmount = $this->priceHt * ($applicablePromotion->discount_value / 100); } } else { $discountAmount = 0; } return $this->discountAmount = $discountAmount; } } //taux de remise public function setDiscountRateAttribute($value) { $this->discountRate = $value; } public function getDiscountRateAttribute() { if (isset($this->discountRate)) { return $this->discountRate; } else { $applicablePromotion = $this->promotions->first(); if (null !== $applicablePromotion) { $promo_type = $applicablePromotion->discount_type_id; if ($promo_type == 1) { //on veut un pourcentage $discountRate = ($applicablePromotion->discount_value / $this->priceHt) * 100; } else { $discountRate = $applicablePromotion->discount_value; } } else { $discountRate = 0; } return $this->discountRate = $discountRate; } } // //Buyable interface // public function getBuyablePrice($options = null) // { // return $this->discountedPriceHt; // } //Relations public function range() { return $this->belongsTo('App\Models\Range', 'range_id'); } public function old_slugs() { return $this->hasMany('App\Model\OldSlug', 'product_id'); } public function stats() { return $this->hasMany('App\Models\Stat', 'product_id'); } public function orders() { return $this->belongsToMany('App\Models\Order', 'orders_products', 'product_id', 'order_id') ->withPivot('quantity'); } //relationship to additionnal product info public function additionnalProductInfo($productType) { return $this->$productType; } //possible additionnals public function foodProduct() { return $this->hasOne(FoodProduct::class, 'product_id'); } //relationship to associated products public function associatedProducts() { return $this->belongsToMany(Product::class, 'products_products', 'product_id', 'associated_id'); } //offres speciales public function promotions() { $now = Carbon::now(); return $this->belongsToMany(Promotion::class) ->whereDate('date_start', '<=', $now) ->whereDate('date_end', '>=', $now); } //taxe public function tax() { return $this->belongsTo(TaxRate::class, 'tax_rate_id'); } //tags public function tags() { return $this->belongsToMany(Tag::class); } }