*/ private Collection $positions; private int $year; private ?Subscription $subscription = null; public function __construct(private Member $member) {} /** * @param Collection $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; } }