ldc-mesbr-app/app/Models/Coupon.php
2023-12-01 15:27:30 +01:00

104 lines
3.6 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;
/**
* Coupon
*
* @property integer $id Coupon id.
* @property string $name Coupon name.
* @property string $process_code Process code for the coupon.
* @property float $amount Amount of reduction for the coupon.
* @property string $valid_on Reduction valid on.
* @property integer $validity_period Validity period of the coupon.
* @property string $legal_notices Legal notice for the coupon.
* @property string $decoration Path for the decoration picture.
* @property string $gs1 Path for the GS1 picture.
* @property string $website_picture Path of the website picture.
* @property string $website_picture_mobile Path of the mobile website picture.
* @property integer $limit_qty Print limit quantity.
* @property integer $limit_period Print period limit.
* @property boolean $status The coupon is published or not.
* @property boolean $can_be_used The coupon is usable or not.
* @property object $start_diffusion_date Diffusion start date.
* @property object $end_diffusion_date Diffusion end date.
* @property object $end_validity_date Validity end date.
*
* * RELATIONS
* @property-read \App\Models\Campaign $campaigns Campaigns that owns the Coupon.
*/
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',
];
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function booted(): void
{
static::creating(function (Coupon $coupon) {
$coupon->can_be_used = self::checkIfCanBeUsed($coupon);
});
static::updating(function (Coupon $coupon) {
$coupon->can_be_used = self::checkIfCanBeUsed($coupon);
});
}
/**
* Check if coupon can be used taking account multiple factors
*
* @param \App\Models\Coupon $coupon
* @return boolean
*/
public static function checkIfCanBeUsed(Coupon $coupon): bool
{
if (
(
is_null($coupon->start_diffusion_date) or
$coupon->start_diffusion_date->lessThanOrEqualTo(Carbon::now())
) and
(
is_null($coupon->end_diffusion_date) or
$coupon->end_diffusion_date->greaterThanOrEqualTo(Carbon::now())) and
(is_null($coupon->end_validity_date) or
$coupon->end_validity_date->greaterThanOrEqualTo(Carbon::now()->addDays($coupon->validity_period))
)
) {
return true;
}
// FIXME : Ajouter calendrier de diffusion et le prendre en compte dans la condition.
return false;
}
// * RELATIONS
/**
* Get campaigns that owns the Coupon
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function campaigns(): BelongsToMany
{
return $this->belongsToMany(Campaign::class);
}
}