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\Payment\Payment;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Exception;
|
|
|
|
use Generator;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Zoomyboy\Tex\Document;
|
|
|
|
use Zoomyboy\Tex\Engine;
|
|
|
|
use Zoomyboy\Tex\Template;
|
|
|
|
|
2023-04-18 22:08:45 +02:00
|
|
|
abstract class Invoice extends Document
|
2022-11-07 16:18:11 +01:00
|
|
|
{
|
|
|
|
abstract public function getSubject(): string;
|
|
|
|
|
|
|
|
abstract public function view(): string;
|
|
|
|
|
|
|
|
abstract public function linkLabel(): string;
|
|
|
|
|
2022-12-07 00:40:53 +01:00
|
|
|
abstract public static function sendAllLabel(): string;
|
2022-11-07 16:18:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param HasMany<Payment> $query
|
|
|
|
*
|
|
|
|
* @return HasMany<Payment>
|
|
|
|
*/
|
|
|
|
abstract public static function paymentsQuery(HasMany $query): HasMany;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
2022-12-07 00:40:53 +01:00
|
|
|
abstract public static function getDescription(): array;
|
2022-11-07 16:18:11 +01:00
|
|
|
|
|
|
|
abstract public function afterSingle(Payment $payment): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Collection<int, Page>
|
|
|
|
*/
|
|
|
|
public Collection $pages;
|
|
|
|
public string $subject;
|
|
|
|
protected string $filename;
|
|
|
|
public string $until;
|
2023-04-18 22:08:45 +02:00
|
|
|
public InvoiceSettings $settings;
|
2022-11-07 16:18:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Collection<int, Page> $pages
|
|
|
|
*/
|
|
|
|
public function __construct(Collection $pages)
|
|
|
|
{
|
|
|
|
$this->pages = $pages;
|
|
|
|
$this->subject = $this->getSubject();
|
|
|
|
$this->until = now()->addWeeks(2)->format('d.m.Y');
|
|
|
|
$this->setFilename(Str::slug("{$this->getSubject()} für {$pages->first()?->familyName}"));
|
2023-04-18 22:08:45 +02:00
|
|
|
$this->settings = app(InvoiceSettings::class);
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function number(int $number): string
|
|
|
|
{
|
|
|
|
return number_format($number / 100, 2, '.', '');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUntil(): Carbon
|
|
|
|
{
|
|
|
|
return now()->addWeeks(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEngine(): Engine
|
|
|
|
{
|
2023-07-18 14:18:44 +02:00
|
|
|
return Engine::PDFLATEX;
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function basename(): string
|
|
|
|
{
|
|
|
|
return $this->filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function template(): Template
|
|
|
|
{
|
|
|
|
return Template::make('tex.templates.default');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFilename(string $filename): self
|
|
|
|
{
|
|
|
|
$this->filename = $filename;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRecipient(): MailRecipient
|
|
|
|
{
|
|
|
|
if (!$this->pages->first()?->email) {
|
|
|
|
throw new Exception('Cannot get Recipient. Mail not set.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MailRecipient($this->pages->first()->email, $this->pages->first()->familyName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function allPayments(): Generator
|
|
|
|
{
|
|
|
|
foreach ($this->pages as $page) {
|
|
|
|
foreach ($page->getPayments() as $payment) {
|
|
|
|
yield $payment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 16:42:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return view-string
|
|
|
|
*/
|
|
|
|
public function mailView(): string
|
|
|
|
{
|
|
|
|
$view = 'mail.payment.'.Str::snake(class_basename($this));
|
|
|
|
|
|
|
|
if (!view()->exists($view)) {
|
|
|
|
throw new Exception('Mail view '.$view.' existiert nicht.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $view;
|
|
|
|
}
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|