adrema/app/Invoice/Models/Invoice.php

167 lines
4.7 KiB
PHP
Raw Permalink 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;
use App\Invoice\InvoiceSettings;
2023-12-18 02:17:31 +01:00
use App\Invoice\RememberDocument;
2024-05-14 01:29:39 +02:00
use App\Invoice\Scopes\InvoiceFilterScope;
use App\Member\Member;
2024-02-10 01:42:51 +01:00
use App\Payment\Subscription;
2024-09-22 17:32:29 +02:00
use Database\Factories\Invoice\Models\InvoiceFactory;
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;
2024-12-15 16:05:38 +01:00
use Laravel\Scout\Searchable;
2023-12-18 01:15:16 +01:00
use stdClass;
2023-12-13 00:35:39 +01:00
class Invoice extends Model
{
2024-09-22 17:32:29 +02:00
/** @use HasFactory<InvoiceFactory> */
2023-12-13 00:35:39 +01:00
use HasFactory;
2024-12-15 16:05:38 +01:00
use Searchable;
2023-12-13 00:35:39 +01:00
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,
2024-09-21 17:46:07 +02:00
'sent_at' => 'datetime',
'last_remembered_at' => 'datetime',
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, ?int $weeks = null): Builder
2023-12-18 02:17:31 +01:00
{
$weeks = $weeks ?: app(InvoiceSettings::class)->rememberWeeks;
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()->subWeeks($weeks));
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
}
2024-12-15 16:05:38 +01:00
/**
* Get the indexable data array for the model.
*
* @return array<string, mixed>
*/
public function toSearchableArray(): array
{
return [
'to' => implode(', ', $this->to),
'usage' => $this->usage,
'greeting' => $this->greeting,
'mail_email' => $this->mail_email,
'status' => $this->status->value,
];
}
2023-12-13 00:35:39 +01:00
}