adrema/app/Invoice/Models/Invoice.php

148 lines
4.0 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;
2024-02-10 01:42:51 +01:00
use App\Payment\Subscription;
2023-12-17 01:49:12 +01:00
use Illuminate\Database\Eloquent\Builder;
2024-02-10 00:41:06 +01:00
use Illuminate\Database\Eloquent\Collection;
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);
}
2024-02-10 00:41:06 +01:00
/**
* @param Collection<int, Member> $members
*/
2024-02-10 01:42:51 +01:00
public static function createForMember(Member $member, Collection $members, int $year, Subscription $subscription = null): self
{
2024-02-10 01:42:51 +01:00
$subscription = $subscription ?: $member->subscription;
2024-02-10 01:17:24 +01:00
$invoice = new self([
'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,
]);
2024-02-10 00:41:06 +01:00
$positions = collect([]);
foreach ($members as $member) {
2024-02-10 01:42:51 +01:00
foreach ($subscription->children as $child) {
2024-02-10 00:41:06 +01:00
$positions->push([
'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $year),
'price' => $child->amount,
'member_id' => $member->id,
2024-02-11 20:21:33 +01:00
'id' => null,
2024-02-10 00:41:06 +01:00
]);
}
}
$invoice->setRelation('positions', $positions);
return $invoice;
}
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
{
2023-12-19 02:00:42 +01:00
return $query
->where('status', InvoiceStatus::SENT)
->whereNotNull('sent_at')
->whereNotNull('last_remembered_at')
->where('last_remembered_at', '<=', now()->subMonths(3));
2023-12-18 02:17:31 +01:00
}
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-19 02:00:42 +01:00
'last_remembered_at' => now(),
2023-12-18 01:15:16 +01:00
]);
}
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
}