ldc-boutique-cnc/app/Models/Product.php
Olivier Legat bce4c2f88c Offres Spéciales :
commit 2bdbfaec541c3d51489f32e0fa6f7523ca4e4f6f
Author: Olivier Legat <olivier.legat@ldc.fr>
Date:   Thu Apr 8 15:48:13 2021 +0200

    arrondis de taxe dans le panier

commit ce748bcac15a0b633cacc06b8232cbb92a8e455c
Author: Olivier Legat <olivier.legat@ldc.fr>
Date:   Thu Apr 8 11:53:54 2021 +0200

    Prix calcules - affichés

commit cc79e7c633d81290371eceabe9864d4d0ee9e4a5
Author: Olivier Legat <olivier.legat@ldc.fr>
Date:   Wed Apr 7 18:09:12 2021 +0200

    Affichage des promowork in progress => prix remisés

commit 8545d1079da2456f090557eeef005e4d37154a1e
Author: Olivier Legat <olivier.legat@ldc.fr>
Date:   Wed Apr 7 13:04:28 2021 +0200

    resource BO Promotions

commit d7145835de675da32e57bec3f0b8f06e1e447b27
Author: Olivier Legat <olivier.legat@ldc.fr>
Date:   Tue Apr 6 18:23:36 2021 +0200

    structure DB, debut BO
2021-04-08 15:49:20 +02:00

267 lines
7.2 KiB
PHP

<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\CanBeBought;
class Product extends Model implements Buyable
{
use HasFactory;
use SoftDeletes;
use CanBeBought;
/**
* Store in protected variables instead of attributes
* Because those are not set in attributes, Laravel will not try to save it to database
*/
protected $priceHt;
protected $discountedPriceHt;
protected $priceTtc;
protected $discountedPriceTtc;
protected $discountAmount;
protected $discountRate;
/**
* init prices on boot
*/
protected static function boot()
{
parent::boot();
//on data retrived from DB
static::retrieved(function ($model) {
$model->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 <olivier.legat@ldc.fr>
*/
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');
}
}