59 lines
1.7 KiB
PHP
59 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
class Coupon extends Model
|
|
{
|
|
// FIXME : Ajouter les mouvement de quantité, mettre juste une colonne et se servir des logs avec un type pour mouvement ?
|
|
// FIXME / Ajouter logs impressions
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'start_diffusion_date' => 'date:Y-m-d',
|
|
'end_diffusion_date' => 'date:Y-m-d',
|
|
'end_validity_date' => 'date:Y-m-d',
|
|
];
|
|
|
|
public function campaigns() : BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Campaign::class);
|
|
}
|
|
|
|
/**
|
|
* The "booted" method of the model.
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Coupon $coupon) {
|
|
$coupon->can_be_used = $this->checkIfCanBeUsed();
|
|
});
|
|
static::updating(function (Coupon $coupon) {
|
|
$coupon->can_be_used = $this->checkIfCanBeUsed();
|
|
});
|
|
}
|
|
|
|
public function checkIfCanBeUsed()
|
|
{
|
|
if (
|
|
(is_null($this->start_diffusion_date) or $this->start_diffusion_date->lessThanOrEqualTo(Carbon::now())) and
|
|
(is_null($this->end_diffusion_date) or $this->end_diffusion_date->greaterThanOrEqualTo(Carbon::now())) and
|
|
(is_null($this->end_validity_date) or $this->end_validity_date->greaterThanOrEqualTo(Carbon::now()->addDays($this->validity_period)))
|
|
){
|
|
return true;
|
|
}
|
|
|
|
// FIXME : Ajouter calendrier de diffusion et le prendre en compte dans la condition
|
|
|
|
return false;
|
|
}
|
|
}
|