135 lines
3.8 KiB
PHP
135 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use IPLib\Address\AddressInterface;
|
|
|
|
/**
|
|
* Participation.
|
|
*
|
|
* @property string $ip IP
|
|
* @property string $sticker Path of the sticker file
|
|
* @property string $ticket Path of the ticket file
|
|
* @property string $barcode Path of the barcode file
|
|
* @property-read \Illuminate\Support\Carbon $created_at Creation Date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Last update Date.
|
|
*
|
|
* @property-read string|null $code_ean Accessor for code_ean, from product table.
|
|
*
|
|
* @property-read \App\Models\Customer $customer Associated customer.
|
|
* @property-read \App\Models\Code $code Associated code.
|
|
* @property-read \App\Models\Product $product Associated product.
|
|
* @property-read \App\Models\InstantWin $instantWin Associated instantWin.
|
|
* @property-read \App\Models\Discount $discount Associated discount.
|
|
*/
|
|
class Participation extends Model
|
|
{
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsTo User relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function customer()
|
|
{
|
|
return $this->belongsTo(Customer::class);
|
|
}
|
|
|
|
/**
|
|
* BelongsTo Codes relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function code()
|
|
{
|
|
return $this->belongsTo(Code::class, 'code_unique_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo Product relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
/**
|
|
* One to one InstantWin relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function instantWin()
|
|
{
|
|
return $this->hasOne(InstantWin::class);
|
|
}
|
|
|
|
/**
|
|
* One to one Discount relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function discount()
|
|
{
|
|
return $this->hasOne(Discount::class);
|
|
}
|
|
|
|
// * GETTERS
|
|
|
|
/**
|
|
* Accessor for code_ean, from product table.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Casts\Attribute;
|
|
*/
|
|
public function codeEan(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => optional($this->product)->code_ean,
|
|
);
|
|
}
|
|
|
|
// * 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.
|
|
* @param \App\Models\Code $code The unique code used to play.
|
|
* @param null|\App\Models\Product $product The product that holds the sticker if codes are actived.
|
|
* @return \App\Models\Participation
|
|
*/
|
|
public static function makeNew(
|
|
AddressInterface $ip,
|
|
Customer $customer,
|
|
Code $code,
|
|
Product|null $product = null
|
|
): self {
|
|
$self = new self();
|
|
$self->ip = $ip;
|
|
$self->customer()->associate($customer);
|
|
$self->code()->associate($code);
|
|
if ($product) {
|
|
$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')
|
|
->whereHas('user', fn (Builder $q) => $q->where('id', $customer->id))
|
|
->exists();
|
|
}
|
|
}
|