adrema/app/Invoice/Page.php

77 lines
2.2 KiB
PHP
Raw Permalink Normal View History

2022-11-07 16:18:11 +01:00
<?php
2023-04-18 22:08:45 +02:00
namespace App\Invoice;
2022-11-07 16:18:11 +01:00
use App\Member\Member;
use App\Payment\Payment;
2023-02-17 18:57:11 +01:00
use Illuminate\Support\Collection;
2022-11-07 16:18:11 +01:00
class Page
{
/**
2023-02-17 18:57:11 +01:00
* @var Collection<int, Member>
2022-11-07 16:18:11 +01:00
*/
private Collection $members;
public string $familyName;
public string $singleName;
public string $address;
public string $zip;
public string $location;
public string $usage;
public ?string $email;
/**
* @var array<string, string>
*/
public array $positions;
/**
2023-02-17 18:57:11 +01:00
* @param Collection<int, Member> $members
2022-11-07 16:18:11 +01:00
*/
public function __construct(Collection $members)
{
$this->members = $members;
$this->familyName = $this->members->first()->lastname;
$this->singleName = $members->first()->lastname;
$this->address = $members->first()->address;
$this->zip = $members->first()->zip;
$this->location = $members->first()->location;
$this->email = $members->first()->email_parents ?: $members->first()->email;
$this->positions = $this->getPositions();
$this->usage = "Mitgliedsbeitrag für {$this->familyName}";
}
/**
* @return array<string, string>
*/
public function getPositions(): array
{
2022-12-14 00:23:03 +01:00
/** @var array<string, string> */
$result = [];
2022-11-07 16:18:11 +01:00
2022-12-14 00:23:03 +01:00
foreach ($this->getPayments() as $payment) {
if ($payment->subscription->split) {
foreach ($payment->subscription->children as $child) {
$result["{$payment->subscription->name} ({$child->name}) {$payment->nr} für {$payment->member->firstname} {$payment->member->lastname}"] = $this->number($child->amount);
}
} else {
$result["{$payment->subscription->name} {$payment->nr} für {$payment->member->firstname} {$payment->member->lastname}"] = $this->number($payment->subscription->getAmount());
}
}
return $result;
2022-11-07 16:18:11 +01:00
}
/**
2023-02-17 18:57:11 +01:00
* @return Collection<int, Payment>
2022-11-07 16:18:11 +01:00
*/
2023-02-17 18:57:11 +01:00
public function getPayments(): Collection
2022-11-07 16:18:11 +01:00
{
return $this->members->pluck('payments')->flatten(1);
}
public function number(int $number): string
{
return number_format($number / 100, 2, '.', '');
}
}