This commit is contained in:
Vincent BRIDIER 2023-11-24 15:35:08 +01:00
parent 0a09a36b95
commit 80dbb04671
No known key found for this signature in database
GPG key ID: 8571BFA20420D512
8 changed files with 95 additions and 59 deletions

View file

@ -10,7 +10,6 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class BrandController extends Controller
{
/**

View file

@ -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
{

View file

@ -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);
}

View file

@ -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
/**

View file

@ -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);
}

View file

@ -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;
}
}

View file

@ -9,6 +9,11 @@ class CouponLog extends Model
{
use HasFactory;
/**
* Fillable
*
* @var string[]
*/
protected $fillable = [
'coupon_id',
'campaign_id',

View file

@ -9,6 +9,11 @@ class Customer extends Model
{
use HasFactory;
/**
* Fillable
*
* @var string[]
*/
protected $fillable = [
'hashcode',
'email'