ldc-legaulois/app/Models/Products.php
2022-02-10 16:14:58 +01:00

152 lines
5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Session;
class Products extends Model
{
protected $casts = [
'star' => 'boolean'
];
protected static function booted()
{
static::creating(function (Products $product) {
if (!$product->incoNutritionalValues) {
$inc = new IncoNutritionalValues();
$inc->product()->associate($inc);
}
$product->preparation_advice = clean($product->preparation_advice);
$product->ingredients = clean($product->ingredients);
$product->tips = clean($product->tips);
self::setStarLogic($product);
});
static::updating(function (Products $product) {
$product->preparation_advice = clean($product->preparation_advice);
$product->ingredients = clean($product->ingredients);
$product->tips = clean($product->tips);
self::setStarLogic($product);
if ($product->status !== $product->getOriginal('status')) {
Session::flash('info', trans('Le produit :product à été :status', [
'product' => $product->name,
'status' => $product->status ? trans('publié') : trans('dépublié'),
]));
}
});
}
public function pictograms()
{
return $this->belongsToMany(
ProductPictograms::class,
"products_pictograms",
"product_id",
"pictogram_id"
);
}
public function range()
{
return $this->belongsTo(Range::class);
}
public function subrange()
{
return $this->belongsTo(SubRange::class, 'sub_range_id');
}
public function meatType()
{
return $this->belongsTo(MeatTypes::class, 'meat_type_id');
}
public function label()
{
return $this->belongsTo(Label::class, 'label_id');
}
public function consumptionTimes()
{
return $this->belongsTo(ConsumptionTimes::class, 'consumption_times_id');
}
public function recipe()
{
return $this->belongsTo(Recipes::class);
}
public function incoNutritionalValues()
{
return $this->hasOne(IncoNutritionalValues::class, 'product_id');
}
public function recipes()
{
return $this->belongsToMany(Recipes::class, 'recipes_products', 'product_id', 'recipe_id');
}
public function customers()
{
return $this->belongsToMany(Customers::class, 'cart', 'product_id', 'customer_id');
}
public function reductions()
{
return $this->belongsToMany(Reduction::class, 'reduction_config', 'product_id', 'reduction_id');
}
public function xmlInco()
{
return $this->belongsTo(XmlInco::class, 'xml_inco_id');
}
protected static function setStarLogic(self $product)
{
// * Prevent a product beeing star if not published
if (!$product->getOriginal('star') and $product->star and !$product->status) {
Session::flash('info', trans(
'Le produit :product ne peut être mis en avant car il est non publié.',
['product' => $product->name]
));
$product->star = false;
return;
}
if (!$product->status and $product->star) {
$product->star = false;
}
// * Set Star logic for range
$rangeStarProductQuery = Products::whereStar(true)->whereHas('range', function ($q) use ($product) {
$q->whereId($product->range_id);
});
$removedAllStar = false;
// * Make sur only one product is star
if ($product->star and $product->range_id) {
// * Ensure only one product will be star for this range
$updated = $rangeStarProductQuery
->where('id', '!=', $product->id)
->update(['star' => false]);
if ($updated) {
Session::flash('warning', trans('Le produit mis en avant de la gamme :range à été changé.', [
'range' => optional(Range::whereId($product->range_id)->first())->name
]));
}
$removedAllStar = true;
}
if ($product->getOriginal('star') !== null && $product->getOriginal('star') !== $product->star) {
Session::flash('warning', trans('Le produit mis en avant de la gamme :range à été changé.', [
'range' => optional(Range::whereId($product->range_id)->first())->name
]));
}
// * If this product is not star then no product will be star
if (
$product->getOriginal('star') and !$product->star and
($removedAllStar or $rangeStarProductQuery->count())
) {
Session::flash('warning', trans('La gamme :range n\'a plus de produits mis en avant.', [
'range' => optional(Range::whereId($product->range_id)->first())->name
]));
}
}
}