*/ 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 /** * The campaigns that belong to the Coupon * * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function campaigns(): BelongsToMany { return $this->belongsToMany(Campaign::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(); } /** * Get can be used * * @return boolean */ public function checkIfCanBeUsed() { if ( (!is_null($this->start_diffusion_date) and $this->start_diffusion_date->lessThanOrEqualTo(Carbon::now()) ) and (!is_null($this->end_diffusion_date) and $this->end_diffusion_date->greaterThanOrEqualTo(Carbon::now()) ) and (!is_null($this->end_validity_date) and $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; } }