198 lines
7.2 KiB
PHP
198 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
/**
|
|
* @property integer $id Model id.
|
|
* @property integer $brand_id ID de la marque sur laquelle le coupon s'applique.
|
|
* @property string $name Nom du coupon.
|
|
* @property string $process_code Code de traitement.
|
|
* @property double $amount Montant de la réduction - Max 3 euros.
|
|
* @property string $valid_on Texte réduction valable sur.
|
|
* @property integer $validity_period Période de validité en jours - default 30 j - max 1 mois.
|
|
* @property string $legal_notices Mentions légales.
|
|
* @property string $decoration Path de l'image de décoration
|
|
* - 280x160px
|
|
* - JPG.
|
|
* @property string|null $gs1 Path de l'image GS1 pour coupon Benelux
|
|
* - 628x360px
|
|
* - JPG
|
|
* @property string|null $website_picture Path de l'image a destination des sites internet
|
|
* - 580x190px
|
|
* - JPG/PNG
|
|
* @property string|null $website_picture_mobile Path de l'image a destination des sites internet mobile
|
|
* - 570x560px
|
|
* - JPG/PNG
|
|
* @property integer $limit_qty Limite de quantité d'impression / customer.
|
|
* @property integer $limit_period Limite d'impression dans une periode (en jours) / customer.
|
|
* @property integer $status Statut du coupon, Publié / Non Publié.
|
|
* @property integer $can_be_used Statut d'utilisation du coupon, Utilisable / Non utilisable,
|
|
* Modifié par calcul dans un Job en fonction des dates.
|
|
* @property \Carbon\Carbon|null $start_diffusion_date Date de début de diffusion - diffusion immédiate si null.
|
|
* @property \Carbon\Carbon|null $end_diffusion_date Date de fin de diffusion - diffusion sans fin si null.
|
|
* @property \Carbon\Carbon|null $end_validity_date Date de fin de validité
|
|
* - diffusion immédiate si null
|
|
* - required si end_diffusion_date est set et date
|
|
* au minimum egale à end_diffusion_date + validity_period jours.
|
|
* @package App\Models
|
|
*/
|
|
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 are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'status',
|
|
'brand_id',
|
|
'process_code',
|
|
'amount',
|
|
'valid_on',
|
|
'validity_period',
|
|
'legal_notices',
|
|
'decoration',
|
|
'gs1',
|
|
'website_picture',
|
|
'website_picture_mobile',
|
|
'limit_qty',
|
|
'limit_period',
|
|
'start_diffusion_date',
|
|
'end_diffusion_date',
|
|
];
|
|
|
|
/**
|
|
* The attributes that aren't mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $guarded = [
|
|
'can_be_used',
|
|
'end_validity_date',
|
|
];
|
|
|
|
/**
|
|
* 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',
|
|
'status' => 'bool'
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updating(function (self $model) {
|
|
self::manageFields($model);
|
|
});
|
|
static::updated(function (self $model) {
|
|
FileStorageHelper::removeOldFile($model, 'decoration');
|
|
FileStorageHelper::removeOldFile($model, 'gs1');
|
|
FileStorageHelper::removeOldFile($model, 'website_picture');
|
|
FileStorageHelper::removeOldFile($model, 'website_picture_mobile');
|
|
});
|
|
static::deleted(function (self $model) {
|
|
FileStorageHelper::removeFile($model, 'decoration');
|
|
FileStorageHelper::removeFile($model, 'gs1');
|
|
FileStorageHelper::removeFile($model, 'website_picture');
|
|
FileStorageHelper::removeFile($model, 'website_picture_mobile');
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get campaigns that owns the Coupon
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function campaigns(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Campaign::class);
|
|
}
|
|
|
|
/**
|
|
* Get the brand that owns the Coupon
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function brand(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Brand::class);
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Manage fields
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function manageFields(self $model)
|
|
{
|
|
$model->decoration = FileStorageHelper::storeFile($model, $model->decoration, true);
|
|
$model->gs1 = FileStorageHelper::storeFile($model, $model->gs1, true);
|
|
$model->website_picture = FileStorageHelper::storeFile($model, $model->website_picture, true);
|
|
$model->website_picture_mobile = FileStorageHelper::storeFile($model, $model->website_picture_mobile, true);
|
|
|
|
if (is_null($model->start_diffusion_date)) {
|
|
$model->start_diffusion_date = Carbon::now();
|
|
}
|
|
if (isset($model->end_diffusion_date)) {
|
|
$model->end_validity_date = $model->end_diffusion_date->addDays($model->validity_period);
|
|
} else {
|
|
$model->end_validity_date = Carbon::now()->addDays($model->validity_period);
|
|
}
|
|
// Last action is to test if model can be used.
|
|
$model->can_be_used = $model->checkIfCanBeUsed();
|
|
}
|
|
|
|
/**
|
|
* Check if coupon can be used taking account multiple factors
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public function checkIfCanBeUsed(): bool
|
|
{
|
|
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;
|
|
}
|
|
}
|