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
|
|
|
|
2023-12-17 22:33:29 +01:00
|
|
|
use App\Invoice\Models\Invoice;
|
2022-11-07 16:18:11 +01:00
|
|
|
use App\Payment\Payment;
|
|
|
|
use Exception;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Zoomyboy\Tex\Document;
|
|
|
|
use Zoomyboy\Tex\Engine;
|
|
|
|
use Zoomyboy\Tex\Template;
|
|
|
|
|
2023-12-17 22:33:29 +01:00
|
|
|
abstract class InvoiceDocument extends Document
|
2022-11-07 16:18:11 +01:00
|
|
|
{
|
|
|
|
abstract public function getSubject(): string;
|
|
|
|
abstract public function view(): string;
|
2023-11-30 23:54:16 +01:00
|
|
|
abstract public function afterSingle(Payment $payment): void;
|
2022-11-07 16:18:11 +01:00
|
|
|
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
|
|
|
|
|
|
|
public string $until;
|
2023-11-30 23:54:16 +01:00
|
|
|
public string $filename;
|
2022-11-07 16:18:11 +01:00
|
|
|
|
|
|
|
/**
|
2023-11-30 23:54:16 +01:00
|
|
|
* @param array<string, string> $positions
|
2022-11-07 16:18:11 +01:00
|
|
|
*/
|
2023-11-30 23:54:16 +01:00
|
|
|
public function __construct(
|
2023-12-17 22:33:29 +01:00
|
|
|
public string $toName,
|
|
|
|
public string $toAddress,
|
|
|
|
public string $toZip,
|
|
|
|
public string $toLocation,
|
|
|
|
public string $greeting,
|
2023-11-30 23:54:16 +01:00
|
|
|
public array $positions,
|
|
|
|
public string $usage,
|
|
|
|
) {
|
2022-11-07 16:18:11 +01:00
|
|
|
$this->until = now()->addWeeks(2)->format('d.m.Y');
|
2023-12-17 22:33:29 +01:00
|
|
|
$this->filename = Str::slug("{$this->getSubject()} für {$toName}");
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 22:33:29 +01:00
|
|
|
public static function fromInvoice(Invoice $invoice): self
|
2022-11-07 16:18:11 +01:00
|
|
|
{
|
2023-11-30 23:54:16 +01:00
|
|
|
return static::withoutMagicalCreationFrom([
|
2023-12-17 22:33:29 +01:00
|
|
|
'toName' => $invoice->to['name'],
|
|
|
|
'toAddress' => $invoice->to['address'],
|
|
|
|
'toZip' => $invoice->to['zip'],
|
|
|
|
'toLocation' => $invoice->to['location'],
|
|
|
|
'greeting' => $invoice->greeting,
|
|
|
|
'positions' => static::renderPositions($invoice),
|
|
|
|
'usage' => $invoice->usage,
|
2023-11-30 23:54:16 +01:00
|
|
|
]);
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
2023-11-30 23:54:16 +01:00
|
|
|
public function settings(): InvoiceSettings
|
2022-11-07 16:18:11 +01:00
|
|
|
{
|
2023-11-30 23:54:16 +01:00
|
|
|
return app(InvoiceSettings::class);
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-11-30 23:54:16 +01:00
|
|
|
throw_unless($this->email, Exception::class, 'Cannot get Recipient. Mail not set.');
|
2022-11-07 16:18:11 +01:00
|
|
|
|
2023-11-30 23:54:16 +01:00
|
|
|
return new MailRecipient($this->email, $this->familyName);
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|
2022-11-07 16:42:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return view-string
|
|
|
|
*/
|
|
|
|
public function mailView(): string
|
|
|
|
{
|
2023-11-30 23:54:16 +01:00
|
|
|
$view = 'mail.payment.' . Str::snake(class_basename($this));
|
2022-11-07 16:42:25 +01:00
|
|
|
|
2023-11-30 23:54:16 +01:00
|
|
|
throw_unless(view()->exists($view), Exception::class, 'Mail view ' . $view . ' existiert nicht.');
|
2022-11-07 16:42:25 +01:00
|
|
|
|
|
|
|
return $view;
|
|
|
|
}
|
2023-11-30 23:54:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, string>
|
|
|
|
*/
|
2023-12-17 22:33:29 +01:00
|
|
|
public static function renderPositions(Invoice $invoice): array
|
2023-11-30 23:54:16 +01:00
|
|
|
{
|
2023-12-17 22:33:29 +01:00
|
|
|
return $invoice->positions->mapWithKeys(fn ($position) => [$position->description => static::number($position->price)])->toArray();
|
2023-11-30 23:54:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function number(int $number): string
|
|
|
|
{
|
|
|
|
return number_format($number / 100, 2, '.', '');
|
|
|
|
}
|
2022-11-07 16:18:11 +01:00
|
|
|
}
|