2020-11-04 11:49:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-12-13 10:01:52 +01:00
|
|
|
use App\Casts\Ip;
|
|
|
|
|
use App\Lib\Helpers\FileStorageHelper;
|
2023-12-13 15:47:48 +01:00
|
|
|
use App\Notifications\Customer\PrizeHasBeenSent;
|
2024-02-15 15:53:36 +01:00
|
|
|
use App\Traits\Models\ActivityLog;
|
2025-08-14 14:12:13 +00:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2023-08-24 13:00:43 +02:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2020-11-04 11:49:43 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-12-12 12:55:05 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2023-12-08 15:39:13 +01:00
|
|
|
use IPLib\Address\AddressInterface;
|
2020-11-04 11:49:43 +01:00
|
|
|
|
2023-08-24 13:00:43 +02:00
|
|
|
/**
|
|
|
|
|
* Participation.
|
|
|
|
|
*
|
2025-10-27 16:23:02 +01:00
|
|
|
* @property \IPLib\Address\AddressInterface $ip IP
|
2026-09-28 15:52:43 +02:00
|
|
|
* @property string|null $sticker Path of the sticker file
|
|
|
|
|
* @property string|null $ticket Path of the ticket file
|
|
|
|
|
* @property string|null $barcode Path of the barcode file
|
2025-10-27 16:23:02 +01:00
|
|
|
* @property string|null $discount_code Treatment code of the BR
|
|
|
|
|
* @property string $comment Administrators comments
|
|
|
|
|
* @property boolean $prize_sent Has the won prize been sent ?
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
|
|
|
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
|
2023-08-24 13:00:43 +02:00
|
|
|
*
|
2023-12-12 12:25:45 +01:00
|
|
|
* @property-read string|null $ean_code Accessor for ean_code, from product table.
|
2023-08-24 13:00:43 +02:00
|
|
|
*
|
2023-12-13 10:01:52 +01:00
|
|
|
* @property-read \App\Models\Customer $customer Associated customer.
|
|
|
|
|
* @property-read \App\Models\Code $code Associated code.
|
|
|
|
|
* @property-read \App\Models\Product|null $product Associated product.
|
|
|
|
|
* @property-read \App\Models\InstantWin $instantWin Associated instantWin.
|
2024-02-15 15:53:36 +01:00
|
|
|
*
|
|
|
|
|
* @property-read \App\Models\ActivityLog $activityLogs Activities logs One-to-many relationship.
|
2023-08-24 13:00:43 +02:00
|
|
|
*/
|
2020-11-04 11:49:43 +01:00
|
|
|
class Participation extends Model
|
|
|
|
|
{
|
2024-02-15 15:53:36 +01:00
|
|
|
use ActivityLog;
|
|
|
|
|
|
2023-12-13 10:01:52 +01:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
2025-08-14 14:12:13 +00:00
|
|
|
* @var array<int,string>
|
2023-12-13 10:01:52 +01:00
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
2025-06-19 17:08:36 +02:00
|
|
|
'barcode',
|
|
|
|
|
'ticket',
|
|
|
|
|
'sticker',
|
2025-10-27 16:23:02 +01:00
|
|
|
'discount_code'
|
2023-12-13 10:01:52 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast.
|
|
|
|
|
*
|
2024-01-31 09:03:18 +00:00
|
|
|
* @var array<string, string>
|
2023-12-13 10:01:52 +01:00
|
|
|
*/
|
|
|
|
|
protected $casts = [
|
2025-08-14 14:12:13 +00:00
|
|
|
'ip' => Ip::class,
|
2023-12-13 15:47:48 +01:00
|
|
|
'price_sent' => 'bool'
|
2023-12-13 10:01:52 +01:00
|
|
|
];
|
|
|
|
|
|
2023-12-13 18:10:37 +01:00
|
|
|
/**
|
|
|
|
|
* The model's attributes.
|
|
|
|
|
*
|
2025-08-14 14:12:13 +00:00
|
|
|
* @var array<string,mixed>
|
2023-12-13 18:10:37 +01:00
|
|
|
*/
|
|
|
|
|
protected $attributes = [
|
2025-10-27 16:23:02 +01:00
|
|
|
'sticker' => null,
|
|
|
|
|
'ticket' => null,
|
|
|
|
|
'barcode' => null,
|
|
|
|
|
'discount_code' => null
|
2023-12-13 18:10:37 +01:00
|
|
|
];
|
|
|
|
|
|
2023-12-13 10:01:52 +01:00
|
|
|
/**
|
|
|
|
|
* Perform any actions required after the model boots.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2025-08-14 14:12:13 +00:00
|
|
|
protected static function booted(): void
|
2023-12-13 10:01:52 +01:00
|
|
|
{
|
|
|
|
|
static::creating(function (self $model) {
|
|
|
|
|
self::manageFields($model);
|
|
|
|
|
});
|
|
|
|
|
static::updating(function (self $model) {
|
|
|
|
|
self::manageFields($model);
|
2023-12-14 09:41:19 +01:00
|
|
|
/** @var \App\Models\Settings */
|
|
|
|
|
$settings = app(Settings::class);
|
|
|
|
|
if (
|
|
|
|
|
// ? Les notification doivent être envoyées ?
|
|
|
|
|
$settings->notify_prize_sent and
|
|
|
|
|
// ? On veux marquer le lot comme envoyé ?
|
|
|
|
|
$model->prize_sent and
|
|
|
|
|
!$model->getOriginal('prize_sent')
|
|
|
|
|
) {
|
2023-12-13 15:47:48 +01:00
|
|
|
/** @var \App\Models\Customer */
|
|
|
|
|
$customer = $model->customer;
|
|
|
|
|
$customer->notify(new PrizeHasBeenSent());
|
|
|
|
|
}
|
2023-12-13 10:01:52 +01:00
|
|
|
});
|
|
|
|
|
static::updated(function (self $model) {
|
|
|
|
|
FileStorageHelper::removeOldFile($model, 'barcode');
|
|
|
|
|
FileStorageHelper::removeOldFile($model, 'ticket');
|
|
|
|
|
FileStorageHelper::removeOldFile($model, 'sticker');
|
|
|
|
|
});
|
|
|
|
|
static::deleted(function (self $model) {
|
|
|
|
|
FileStorageHelper::removeFile($model, 'barcode');
|
|
|
|
|
FileStorageHelper::removeFile($model, 'ticket');
|
|
|
|
|
FileStorageHelper::removeFile($model, 'sticker');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 12:33:25 +01:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
2022-01-11 12:39:28 +01:00
|
|
|
/**
|
|
|
|
|
* BelongsTo User relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2023-12-12 12:55:05 +01:00
|
|
|
public function customer(): BelongsTo
|
2021-08-18 17:08:02 +02:00
|
|
|
{
|
2023-08-16 10:36:05 +02:00
|
|
|
return $this->belongsTo(Customer::class);
|
2020-11-04 11:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-11 12:39:28 +01:00
|
|
|
/**
|
|
|
|
|
* BelongsTo Codes relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2023-12-12 12:55:05 +01:00
|
|
|
public function code(): BelongsTo
|
2021-08-18 17:08:02 +02:00
|
|
|
{
|
2022-01-11 12:39:28 +01:00
|
|
|
return $this->belongsTo(Code::class, 'code_unique_id');
|
2020-11-04 11:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-11 12:39:28 +01:00
|
|
|
/**
|
2023-08-24 13:00:43 +02:00
|
|
|
* BelongsTo Product relation
|
2022-01-11 12:39:28 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2023-12-12 12:55:05 +01:00
|
|
|
public function product(): BelongsTo
|
2021-08-18 17:08:02 +02:00
|
|
|
{
|
2023-08-24 13:00:43 +02:00
|
|
|
return $this->belongsTo(Product::class);
|
2020-11-04 11:49:43 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-11 12:39:28 +01:00
|
|
|
/**
|
2023-12-12 14:56:12 +01:00
|
|
|
* One to one InstantWin relation
|
2022-01-11 12:39:28 +01:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
|
|
|
*/
|
2023-12-12 14:56:12 +01:00
|
|
|
public function instantWin(): HasOne
|
2021-08-18 17:08:02 +02:00
|
|
|
{
|
2023-12-12 14:56:12 +01:00
|
|
|
return $this->hasOne(InstantWin::class);
|
2020-11-04 11:49:43 +01:00
|
|
|
}
|
2020-11-20 15:18:31 +01:00
|
|
|
|
2024-02-15 15:53:36 +01:00
|
|
|
/**
|
|
|
|
|
* Activities logs One-to-many relationship.
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function activityLogs(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany(ActivityLog::class);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-05 12:33:25 +01:00
|
|
|
// * GETTERS
|
|
|
|
|
|
2022-01-11 12:39:28 +01:00
|
|
|
/**
|
2023-12-12 12:25:45 +01:00
|
|
|
* Accessor for ean_code, from product table.
|
2022-01-11 12:39:28 +01:00
|
|
|
*
|
2023-08-24 13:00:43 +02:00
|
|
|
* @return \Illuminate\Database\Eloquent\Casts\Attribute;
|
2022-01-11 12:39:28 +01:00
|
|
|
*/
|
2023-12-12 12:25:45 +01:00
|
|
|
public function eanCode(): Attribute
|
2021-08-18 17:08:02 +02:00
|
|
|
{
|
2023-08-24 13:00:43 +02:00
|
|
|
return Attribute::make(
|
2025-06-19 17:08:36 +02:00
|
|
|
get: fn() => optional($this->product)->ean_code,
|
2023-08-24 13:00:43 +02:00
|
|
|
);
|
2020-11-20 15:18:31 +01:00
|
|
|
}
|
2023-12-08 15:39:13 +01:00
|
|
|
|
|
|
|
|
// * METHODS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make new participation but do not persist on DB
|
|
|
|
|
*
|
|
|
|
|
* @param \IPLib\Address\AddressInterface $ip Ip for the participation.
|
|
|
|
|
* @param \App\Models\Customer $customer The Customer playing the game.
|
2025-08-14 14:12:13 +00:00
|
|
|
* @param \App\Models\Code|null $code The unique code used to play.
|
|
|
|
|
* @param \App\Models\Product|null $product The product that holds the sticker if codes are actived.
|
2023-12-08 15:39:13 +01:00
|
|
|
* @return \App\Models\Participation
|
|
|
|
|
*/
|
|
|
|
|
public static function makeNew(
|
|
|
|
|
AddressInterface $ip,
|
|
|
|
|
Customer $customer,
|
2025-08-14 14:12:13 +00:00
|
|
|
?Code $code = null,
|
|
|
|
|
?Product $product = null
|
2023-12-08 15:39:13 +01:00
|
|
|
): self {
|
|
|
|
|
$self = new self();
|
|
|
|
|
$self->ip = $ip;
|
|
|
|
|
$self->customer()->associate($customer);
|
2025-08-14 14:12:13 +00:00
|
|
|
if (!is_null($code)) {
|
|
|
|
|
$self->code()->associate($code);
|
|
|
|
|
}
|
|
|
|
|
if (!is_null($product)) {
|
2023-12-08 15:39:13 +01:00
|
|
|
$self->product()->associate($product);
|
|
|
|
|
}
|
|
|
|
|
return $self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if a customer has already won
|
|
|
|
|
*
|
|
|
|
|
* @param \App\Models\Customer $customer
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public static function customerAlreadyHasAnInstantWin(Customer $customer): bool
|
|
|
|
|
{
|
|
|
|
|
return self::query()->whereHas('instantWin')
|
2025-06-19 17:08:36 +02:00
|
|
|
->whereHas('customer', fn(Builder $q) => $q->where('id', $customer->id))
|
2023-12-08 15:39:13 +01:00
|
|
|
->exists();
|
|
|
|
|
}
|
2023-12-13 10:01:52 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Common methods function.
|
|
|
|
|
*
|
2024-01-31 09:03:18 +00:00
|
|
|
* @param self $model
|
2023-12-13 10:01:52 +01:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2024-01-31 09:03:18 +00:00
|
|
|
private static function manageFields(self $model)
|
2023-12-13 10:01:52 +01:00
|
|
|
{
|
2023-12-13 18:10:37 +01:00
|
|
|
if ($model->barcode) {
|
|
|
|
|
$model->barcode = FileStorageHelper::storeFile($model, $model->barcode, true);
|
|
|
|
|
}
|
|
|
|
|
if ($model->ticket) {
|
|
|
|
|
$model->ticket = FileStorageHelper::storeFile($model, $model->ticket, true);
|
|
|
|
|
}
|
|
|
|
|
if ($model->sticker) {
|
|
|
|
|
$model->sticker = FileStorageHelper::storeFile($model, $model->sticker, true);
|
|
|
|
|
}
|
2025-06-19 17:08:36 +02:00
|
|
|
if (!$model->comment) {
|
|
|
|
|
$model->comment = '';
|
|
|
|
|
}
|
2023-12-13 10:01:52 +01:00
|
|
|
}
|
2020-11-04 11:49:43 +01:00
|
|
|
}
|