adrema/app/Payment/Subscription.php

54 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2021-07-04 01:44:41 +02:00
<?php
namespace App\Payment;
2022-01-02 12:32:57 +01:00
use App\Fee;
2021-07-04 01:44:41 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2022-01-02 12:32:57 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2022-12-13 23:11:32 +01:00
use Illuminate\Database\Eloquent\Relations\HasMany;
2021-07-04 01:44:41 +02:00
class Subscription extends Model
{
use HasFactory;
2022-12-14 00:23:03 +01:00
/**
* @var array<int, string>
*/
2022-12-14 15:49:12 +01:00
public $fillable = ['name', 'fee_id', 'split', 'for_promise'];
2022-12-14 00:23:03 +01:00
/**
* @var array<string, string>
*/
public $casts = [
'split' => 'boolean',
2022-12-14 23:20:05 +01:00
'for_promise' => 'boolean',
2022-12-14 00:23:03 +01:00
];
2021-07-04 01:44:41 +02:00
2022-12-13 23:11:32 +01:00
public function getAmount(): int
{
return $this->children->sum('amount');
}
2023-02-17 18:57:11 +01:00
/**
* @return BelongsTo<Fee, self>
*/
2022-03-11 20:19:17 +01:00
public function fee(): BelongsTo
{
2021-07-04 01:44:41 +02:00
return $this->belongsTo(Fee::class);
}
2022-12-13 23:11:32 +01:00
2023-02-17 18:57:11 +01:00
/**
* @return HasMany<SubscriptionChild>
*/
2022-12-13 23:11:32 +01:00
public function children(): HasMany
{
return $this->hasMany(SubscriptionChild::class, 'parent_id');
}
public static function booted(): void
{
static::deleting(fn ($model) => $model->children()->delete());
}
2021-07-04 01:44:41 +02:00
}