adrema/app/Invoice/Models/Invoice.php

125 lines
3.2 KiB
PHP
Raw Normal View History

2023-12-13 00:35:39 +01:00
<?php
namespace App\Invoice\Models;
2023-12-18 01:15:16 +01:00
use App\Invoice\BillDocument;
2023-12-16 11:18:00 +01:00
use App\Invoice\BillKind;
use App\Invoice\Enums\InvoiceStatus;
2023-12-18 01:15:16 +01:00
use App\Invoice\InvoiceDocument;
2023-12-18 02:17:31 +01:00
use App\Invoice\RememberDocument;
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;
2023-12-18 01:15:16 +01:00
use stdClass;
2023-12-13 00:35:39 +01:00
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-18 02:17:31 +01:00
'last_remembered_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-18 01:56:58 +01:00
'mail_email' => $member->email_parents ?: $member->email,
]);
}
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-18 01:15:16 +01:00
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsBill(Builder $query): Builder
{
return $query->where('status', InvoiceStatus::NEW);
}
2023-12-18 02:17:31 +01:00
/**
* @param Builder<self> $query
*
* @return Builder<self>
*/
public function scopeWhereNeedsRemember(Builder $query): Builder
{
return $query->where('status', InvoiceStatus::SENT)->whereNotNull('sent_at')->where(function ($query) {
return $query->orWhere('last_remembered_at', '<=', now()->subMonths(3))
->orWhereNull('last_remembered_at');
});
}
2023-12-18 01:15:16 +01:00
public function getMailRecipient(): stdClass
{
return (object) [
'email' => $this->mail_email,
2023-12-18 01:56:58 +01:00
'name' => $this->to['name']
2023-12-18 01:15:16 +01:00
];
}
public function sent(InvoiceDocument $document): void
{
if (is_a($document, BillDocument::class)) {
$this->update([
'sent_at' => now(),
'status' => InvoiceStatus::SENT,
]);
}
2023-12-18 02:17:31 +01:00
if (is_a($document, RememberDocument::class)) {
$this->update([
'last_remembered_at' => now(),
'status' => InvoiceStatus::SENT,
]);
}
2023-12-18 01:15:16 +01:00
}
2023-12-13 00:35:39 +01:00
}