Add MemberInvoiceFactory
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
5143a69f52
commit
2640622685
|
|
@ -37,7 +37,10 @@ class MassStoreAction
|
|||
->groupBy(fn($member) => "{$member->bill_kind->value}{$member->lastname}{$member->address}{$member->zip}{$member->location}");
|
||||
|
||||
foreach ($memberGroup as $members) {
|
||||
$invoice = Invoice::createForMember($members->first(), $members, $year);
|
||||
$invoice = $members->first()->getInvoiceFactory()
|
||||
->withFamilyMembers($members)
|
||||
->year($year)
|
||||
->getInvoice();
|
||||
$invoice->save();
|
||||
$invoice->positions()->createMany($invoice->positions->toArray());
|
||||
$invoices->push($invoice->fresh('positions'));
|
||||
|
|
|
|||
|
|
@ -30,7 +30,11 @@ class MemberNewInvoiceAction
|
|||
*/
|
||||
public function handle(Member $member, Subscription $subscription, int $year): array
|
||||
{
|
||||
$invoice = Invoice::createForMember($member, collect([$member]), $year, $subscription);
|
||||
$invoice = $member->first()->getInvoiceFactory()
|
||||
->forSingleMember()
|
||||
->year($year)
|
||||
->withSubscription($subscription)
|
||||
->getInvoice();
|
||||
|
||||
return [
|
||||
...$invoice->getAttributes(),
|
||||
|
|
|
|||
|
|
@ -9,14 +9,11 @@ use App\Invoice\Enums\InvoiceStatus;
|
|||
use App\Invoice\InvoiceDocument;
|
||||
use App\Invoice\InvoiceSettings;
|
||||
use App\Invoice\RememberDocument;
|
||||
use App\Member\Member;
|
||||
use App\Payment\Subscription;
|
||||
use Database\Factories\Invoice\Models\InvoiceFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
use Laravel\Scout\Searchable;
|
||||
|
||||
class Invoice extends Model
|
||||
|
|
@ -43,42 +40,6 @@ class Invoice extends Model
|
|||
return $this->hasMany(InvoicePosition::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Member> $members
|
||||
*/
|
||||
public static function createForMember(Member $member, Collection $members, int $year, ?Subscription $subscription = null): self
|
||||
{
|
||||
$invoice = new self([
|
||||
'to' => [
|
||||
'name' => 'Familie ' . $member->lastname,
|
||||
'address' => $member->address,
|
||||
'zip' => $member->zip,
|
||||
'location' => $member->location,
|
||||
'greeting' => 'Liebe Familie ' . $member->lastname,
|
||||
'email' => $member->email_parents ?: $member->email,
|
||||
],
|
||||
'status' => InvoiceStatus::NEW,
|
||||
'via' => $member->bill_kind,
|
||||
'usage' => 'Mitgliedsbeitrag für ' . $member->lastname,
|
||||
]);
|
||||
|
||||
$positions = collect([]);
|
||||
foreach ($members as $member) {
|
||||
$memberSubscription = $subscription ?: $member->subscription;
|
||||
foreach ($memberSubscription->children as $child) {
|
||||
$positions->push([
|
||||
'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $year),
|
||||
'price' => $child->amount,
|
||||
'member_id' => $member->id,
|
||||
'id' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
$invoice->setRelation('positions', $positions);
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
|
||||
public static function booted(): void
|
||||
{
|
||||
static::deleting(function ($model) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace App\Member\Factories;
|
||||
|
||||
use App\Invoice\Enums\InvoiceStatus;
|
||||
use App\Invoice\Models\Invoice;
|
||||
use App\Member\Member;
|
||||
use App\Payment\Subscription;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class MemberInvoiceFactory
|
||||
{
|
||||
|
||||
/** @var Collection<int, Member> */
|
||||
private Collection $positions;
|
||||
|
||||
private int $year;
|
||||
|
||||
private ?Subscription $subscription = null;
|
||||
|
||||
public function __construct(private Member $member) {}
|
||||
|
||||
/**
|
||||
* @param Collection<int, Member> $members
|
||||
*/
|
||||
public function withFamilyMembers(Collection $members): self
|
||||
{
|
||||
$this->positions = $members;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function forSingleMember(): self
|
||||
{
|
||||
$this->positions = collect([$this->member]);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function year(int $year): self
|
||||
{
|
||||
$this->year = $year;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function withSubscription(Subscription $subscription): self
|
||||
{
|
||||
$this->subscription = $subscription;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getInvoice(): Invoice
|
||||
{
|
||||
$invoice = new Invoice([
|
||||
'to' => [
|
||||
'name' => 'Familie ' . $this->member->lastname,
|
||||
'address' => $this->member->address,
|
||||
'zip' => $this->member->zip,
|
||||
'location' => $this->member->location,
|
||||
'greeting' => 'Liebe Familie ' . $this->member->lastname,
|
||||
'email' => $this->member->email_parents ?: $this->member->email,
|
||||
],
|
||||
'status' => InvoiceStatus::NEW,
|
||||
'via' => $this->member->bill_kind,
|
||||
'usage' => 'Mitgliedsbeitrag für ' . $this->member->lastname,
|
||||
]);
|
||||
|
||||
$positions = collect([]);
|
||||
foreach ($this->positions as $member) {
|
||||
$memberSubscription = $this->subscription ?: $member->subscription;
|
||||
foreach ($memberSubscription->children as $child) {
|
||||
$positions->push([
|
||||
'description' => str($child->name)->replace('{name}', $member->firstname . ' ' . $member->lastname)->replace('{year}', (string) $this->year),
|
||||
'price' => $child->amount,
|
||||
'member_id' => $member->id,
|
||||
'id' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
$invoice->setRelation('positions', $positions);
|
||||
|
||||
return $invoice;
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ use App\Gender;
|
|||
use App\Group;
|
||||
use App\Invoice\BillKind;
|
||||
use App\Invoice\Models\InvoicePosition;
|
||||
use App\Member\Factories\MemberInvoiceFactory;
|
||||
use App\Nami\HasNamiField;
|
||||
use App\Nationality;
|
||||
use App\Payment\Subscription;
|
||||
|
|
@ -385,6 +386,10 @@ class Member extends Model implements Geolocatable, Preventable
|
|||
return $query->where('bill_kind', '!=', null)->where('subscription_id', '!=', null);
|
||||
}
|
||||
|
||||
public function getInvoiceFactory(): MemberInvoiceFactory {
|
||||
return new MemberInvoiceFactory($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in New Issue