diff --git a/app/Http/Controllers/Bo/BrandController.php b/app/Http/Controllers/Bo/BrandController.php index 8568d21..98cf714 100644 --- a/app/Http/Controllers/Bo/BrandController.php +++ b/app/Http/Controllers/Bo/BrandController.php @@ -10,7 +10,6 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; - class BrandController extends Controller { /** diff --git a/app/Models/Basket.php b/app/Models/Basket.php index 50ec75a..86b7c7a 100644 --- a/app/Models/Basket.php +++ b/app/Models/Basket.php @@ -11,19 +11,31 @@ class Basket extends Model { use HasFactory; + /** + * Fillable + * + * @var string[] + */ protected $fillable = [ 'reference', 'customer_id', 'campaign_id' ]; - public function content() : HasMany + /** + * Get the content owned by the Basket + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ + public function content(): HasMany { return $this->hasMany(BasketsContent::class); } /** * The "booted" method of the model. + * + * @return void */ protected static function booted(): void { diff --git a/app/Models/BasketsContent.php b/app/Models/BasketsContent.php index 400ff5c..673e683 100644 --- a/app/Models/BasketsContent.php +++ b/app/Models/BasketsContent.php @@ -10,12 +10,22 @@ class BasketsContent extends Model { use HasFactory; - public function basket() : BelongsTo + /** + * Get the basket that owns the BasketContent + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function basket(): BelongsTo { return $this->belongsTo(Basket::class); } - public function coupon() : BelongsTo + /** + * Get the coupon owned by the BasketContent + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function coupon(): BelongsTo { return $this->BelongsTo(Coupon::class); } diff --git a/app/Models/Brand.php b/app/Models/Brand.php index 411fc88..36c1081 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -2,12 +2,9 @@ namespace App\Models; -use Illuminate\Contracts\Encryption\DecryptException; -use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Facades\Crypt; use App\Lib\Helpers\FileStorageHelper; class Brand extends Model @@ -28,50 +25,27 @@ class Brand extends Model 'crm_password' ]; + /** + * The attributes that should be cast. + * + * @var array + */ + protected $casts = [ + 'crm_token' => 'encrypted', + 'crm_password' => 'encrypted', + ]; + + /** + * Get campaign that owned by the Brand + * + * @return \Illuminate\Database\Eloquent\Relations\HasMany + */ public function campaigns(): HasMany { return $this->hasMany(Campaign::class); } - /** - * Interact with the brand's crm_token. - */ - protected function crmToken(): Attribute - { - return Attribute::make( - get: function (string $value) { - try { - $decrypted = Crypt::decryptString($value); - } catch (DecryptException $e) { - report($e); - $decrypted = null; - } - return $decrypted; - }, - set: fn (string $value) => Crypt::encryptString($value), - ); - } - - /** - * Interact with the brand's crm_password. - */ - protected function crmPassword(): Attribute - { - return Attribute::make( - get: function (string $value) { - try { - $decrypted = Crypt::decryptString($value); - } catch (DecryptException $e) { - report($e); - $decrypted = null; - } - return $decrypted; - }, - set: fn (string $value) => Crypt::encryptString($value), - ); - } - // * EVENTS /** diff --git a/app/Models/Campaign.php b/app/Models/Campaign.php index 3b95d2c..c3197ac 100644 --- a/app/Models/Campaign.php +++ b/app/Models/Campaign.php @@ -21,11 +21,24 @@ class Campaign extends Model 'type' => CampaignTypes::class, ]; - public function brand() : BelongsTo { + // * RELATIONS + + /** + * Get the brand that owns the Campaign + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo + */ + public function brand(): BelongsTo + { return $this->belongsTo(Brand::class); } - public function coupons() : BelongsToMany + /** + * Get coupons that owned by the Campaign + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function coupons(): BelongsToMany { return $this->belongsToMany(Coupon::class); } diff --git a/app/Models/Coupon.php b/app/Models/Coupon.php index 7daaa0e..23d8d39 100644 --- a/app/Models/Coupon.php +++ b/app/Models/Coupon.php @@ -10,7 +10,7 @@ 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 + // FIXME : Ajouter logs impressions. use HasFactory; /** @@ -24,36 +24,54 @@ class Coupon extends Model 'end_validity_date' => 'date:Y-m-d', ]; - public function campaigns() : BelongsToMany + /** + * Get campaigns that owns the Coupon + * + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function campaigns(): BelongsToMany { return $this->belongsToMany(Campaign::class); } /** * The "booted" method of the model. + * + * @return void */ protected static function booted(): void { static::creating(function (Coupon $coupon) { - $coupon->can_be_used = $this->checkIfCanBeUsed(); + $coupon->can_be_used = self::checkIfCanBeUsed($coupon); }); static::updating(function (Coupon $coupon) { - $coupon->can_be_used = $this->checkIfCanBeUsed(); + $coupon->can_be_used = self::checkIfCanBeUsed($coupon); }); } - public function checkIfCanBeUsed() + /** + * 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($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))) - ){ + ( + 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 - + // FIXME : Ajouter calendrier de diffusion et le prendre en compte dans la condition. return false; } } diff --git a/app/Models/CouponLog.php b/app/Models/CouponLog.php index 6b9d634..7184b30 100644 --- a/app/Models/CouponLog.php +++ b/app/Models/CouponLog.php @@ -9,6 +9,11 @@ class CouponLog extends Model { use HasFactory; + /** + * Fillable + * + * @var string[] + */ protected $fillable = [ 'coupon_id', 'campaign_id', diff --git a/app/Models/Customer.php b/app/Models/Customer.php index 25a86fa..ff1db46 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -9,6 +9,11 @@ class Customer extends Model { use HasFactory; + /** + * Fillable + * + * @var string[] + */ protected $fillable = [ 'hashcode', 'email'