adrema/app/Invoice/Models/InvoicePosition.php

47 lines
1.1 KiB
PHP
Raw Normal View History

2023-12-13 00:35:39 +01:00
<?php
namespace App\Invoice\Models;
2023-12-17 00:45:03 +01:00
use App\Member\Member;
2024-09-22 17:32:29 +02:00
use Database\Factories\Invoice\Models\InvoicePositionFactory;
2023-12-13 00:35:39 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2023-12-17 00:45:03 +01:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2023-12-13 00:35:39 +01:00
class InvoicePosition extends Model
{
2024-09-22 17:32:29 +02:00
/** @use HasFactory<InvoicePositionFactory> */
2023-12-13 00:35:39 +01:00
use HasFactory;
public $guarded = [];
2023-12-17 00:45:03 +01:00
/**
* @return BelongsTo<Member, self>
2023-12-17 00:45:03 +01:00
*/
public function member(): BelongsTo
{
return $this->belongsTo(Member::class);
}
/**
* @return BelongsTo<Invoice, self>
*/
public function invoice(): BelongsTo
{
return $this->belongsTo(Invoice::class);
}
public static function booted(): void
{
2024-01-28 11:42:32 +01:00
static::saved(function ($model) {
$model->member->touch();
});
static::deleted(function ($model) {
if ($model->invoice->positions()->get()->count() === 0) {
$model->invoice->delete();
}
2024-01-28 11:42:32 +01:00
$model->member->touch();
});
}
2023-12-13 00:35:39 +01:00
}