92 lines
2.8 KiB
PHP
92 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Campagne;
|
|
use App\Models\Coupon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property integer $quantity Quantity of distributed coupons.
|
|
* @property \Carbon\Carbon $date Date when the coupon has been distributed.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\ArchiveCoupon $archiveCoupon ArchiveCoupon Belongs to relationship.
|
|
* @property-read \App\Models\ArchiveCampaign $archiveCampaign ArchiveCampaign Belongs to relationship.
|
|
*/
|
|
class ArchiveStat extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
|
|
protected $fillable = [
|
|
'date',
|
|
'quantity',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $model): void {
|
|
$campaign = Campagne::withTrashed()->where('camp_id', $model->archive_campaign_id)->firstOrFail();
|
|
$archiveCampaign = ArchiveCampaign::query()->firstOrCreate(
|
|
[
|
|
'campaign_id' => $model->archive_campaign_id
|
|
],
|
|
[
|
|
'name' => $campaign->camp_name,
|
|
'archive_site_id' => $campaign->camp_siteId,
|
|
]
|
|
);
|
|
$model->archiveCampaign()->associate($archiveCampaign);
|
|
|
|
$coupon = Coupon::withTrashed()->where('coupon_id', $model->archive_coupon_id)->firstOrFail();
|
|
$archiveCoupon = ArchiveCoupon::query()->firstOrCreate(
|
|
[
|
|
'coupon_code' => $model->archive_coupon_id
|
|
],
|
|
[
|
|
'name' => $coupon->coupon_code,
|
|
]
|
|
);
|
|
$model->archiveCoupon()->associate($archiveCoupon);
|
|
});
|
|
static::updating(function (): void {
|
|
throw new \RuntimeException('Upating an archived stat is not authorized');
|
|
});
|
|
static::deleting(function (): void {
|
|
throw new \RuntimeException('Deleting an archived stat is not authorized');
|
|
});
|
|
}
|
|
|
|
// * RELATIONS
|
|
|
|
/**
|
|
* ArchiveCoupon model relation
|
|
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function archiveCoupon()
|
|
{
|
|
return $this->belongsTo(ArchiveCoupon::class);
|
|
}
|
|
|
|
/**
|
|
* ArchiveCampaign model relation
|
|
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function archiveCampaign()
|
|
{
|
|
return $this->belongsTo(ArchiveCampaign::class);
|
|
}
|
|
}
|