adrema/app/Invoice/Models/Invoice.php

71 lines
1.7 KiB
PHP
Raw Normal View History

2023-12-13 00:35:39 +01:00
<?php
namespace App\Invoice\Models;
2023-12-16 11:18:00 +01:00
use App\Invoice\BillKind;
use App\Invoice\Enums\InvoiceStatus;
use App\Member\Member;
2023-12-17 01:49:12 +01:00
use Illuminate\Database\Eloquent\Builder;
2023-12-13 00:35:39 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Invoice extends Model
{
use HasFactory;
public $guarded = [];
public $casts = [
'to' => 'json',
2023-12-16 01:13:49 +01:00
'status' => InvoiceStatus::class,
2023-12-16 11:18:00 +01:00
'via' => BillKind::class,
2023-12-16 01:13:49 +01:00
];
2023-12-16 11:18:00 +01:00
/** @var array<int, string> */
2023-12-16 01:13:49 +01:00
public $dates = [
'sent_at',
2023-12-13 00:35:39 +01:00
];
/**
* @return HasMany<InvoicePosition>
*/
public function positions(): HasMany
{
return $this->hasMany(InvoicePosition::class);
}
public static function createForMember(Member $member): self
{
return static::create([
'to' => [
'name' => 'Familie ' . $member->lastname,
'address' => $member->address,
'zip' => $member->zip,
'location' => $member->location,
],
'greeting' => 'Liebe Familie ' . $member->lastname,
'status' => InvoiceStatus::NEW,
2023-12-16 11:18:00 +01:00
'via' => $member->bill_kind,
2023-12-17 23:29:01 +01:00
'usage' => 'Mitgliedsbeitrag für ' . $member->lastname,
]);
}
2023-12-17 00:55:31 +01:00
public static function booted(): void
{
static::deleting(function ($model) {
$model->positions()->delete();
});
}
2023-12-17 01:49:12 +01:00
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsPayment(Builder $query): Builder
{
return $query->whereIn('status', [InvoiceStatus::NEW->value, InvoiceStatus::SENT->value]);
}
2023-12-13 00:35:39 +01:00
}