Add pdf to

This commit is contained in:
philipp lang 2021-07-17 15:16:29 +02:00
parent 3406cf22f9
commit c2e49909c7
4 changed files with 92 additions and 37 deletions

View File

@ -2,12 +2,14 @@
namespace App\Pdf; namespace App\Pdf;
use App\Payment\Payment;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class BillType implements PdfRepository class BillType extends Repository implements PdfRepository
{ {
public string $filename; public string $filename;
public Collection $pages;
public function __construct(Collection $pages) public function __construct(Collection $pages)
{ {
@ -41,4 +43,36 @@ class BillType implements PdfRepository
return 'default'; return 'default';
} }
public function getPositions(Collection $page): array
{
$memberIds = $page->pluck('id')->toArray();
$payments = Payment::whereIn('member_id', $memberIds)->whereNeedsBill()->get();
return $payments->mapWithKeys(function (Payment $payment) {
$key = "Beitrag für {$payment->nr} ({$payment->subscription->name})";
return [$key => $this->number($payment->subscription->amount)];
})->toArray();
}
public function getFamilyName(Collection $page): string
{
return $page->first()->lastname;
}
public function getAddress(Collection $page): string
{
return $page->first()->address;
}
public function getZip(Collection $page): string
{
return $page->first()->zip;
}
public function getLocation(Collection $page): string
{
return $page->first()->location;
}
} }

View File

@ -17,4 +17,14 @@ interface PdfRepository
public function getTemplate(): string; public function getTemplate(): string;
public function getPositions(Collection $page): array;
public function getFamilyName(Collection $page): string;
public function getAddress(Collection $page): string;
public function getZip(Collection $page): string;
public function getLocation(Collection $page): string;
} }

View File

@ -1,18 +1,21 @@
\documentclass[silvaletter,12pt]{scrlttr2} \documentclass[silvaletter,12pt]{scrlttr2}
\setkomavar{subject}{Rechnung} \setkomavar{subject}{<<< $data->getSubject() >>>}
\begin{document} \begin{document}
\begin{letter}{Familie Charnay\\Junkerstr 4\\42699 Solingen} @foreach($data->pages as $page)
\begin{letter}{Familie <<< $data->getFamilyName($page) >>>\\<<< $data->getAddress($page) >>>\\<<< $data->getZip($page) >>> <<< $data->getLocation($page) >>>}
\sffamily \sffamily
\gdef\TotalHT{0} \gdef\TotalHT{0}
\opening{Liebe Familie Charnay,} \opening{Liebe Familie <<< $data->getFamilyName($page) >>>,}
Hiermit stellen wir Ihnen den aktuellen Mitgliedsbeitrag für den \usekomavar*{fromname} und die DPSG in Rechnung. Dieser setzt sich wie folgt zusammen: Hiermit stellen wir Ihnen den aktuellen Mitgliedsbeitrag für den \usekomavar*{fromname} und die DPSG in Rechnung. Dieser setzt sich wie folgt zusammen:
\begin{center} \begin{center}
\begin{tabular}{@{}p{0.5\textwidth}|r} \begin{tabular}{@{}p{0.5\textwidth}|r}
\product{Beitrag FE 2021 für Noah Charnay (1. HJ)}{25.00} @foreach($data->getPositions($page) as $desc => $price)
\product{<<< $desc >>>}{<<< $price >>>}
@endforeach
\hline \hline
\textbf{Gesamt} & \textbf{\numprint[€]{\TotalHT}} \\ \textbf{Gesamt} & \textbf{\numprint[€]{\TotalHT}} \\
\end{tabular} \end{tabular}
@ -33,6 +36,6 @@
\closing{Viele Grüße \\ Nils Montag $\cdot$ Kassenwart} \closing{Viele Grüße \\ Nils Montag $\cdot$ Kassenwart}
\end{letter} \end{letter}
@endforeach
\end{document} \end{document}

View File

@ -50,12 +50,18 @@ class GenerateTest extends TestCase
'members' => [ 'members' => [
[ [
'factory' => fn (MemberFactory $member): MemberFactory => $member 'factory' => fn (MemberFactory $member): MemberFactory => $member
->state(['firstname' => '::firstname::', 'lastname' => '::lastname::']), ->state([
'firstname' => '::firstname::',
'lastname' => '::lastname::',
'address' => '::street::',
'zip' => '::zip::',
'location' => '::location::',
]),
'payments' => [ 'payments' => [
fn (PaymentFactory $payment): PaymentFactory => $payment fn (PaymentFactory $payment): PaymentFactory => $payment
->notPaid() ->notPaid()
->nr('1995') ->nr('1995')
->subscription('::subName::', 1200), ->subscription('::subName::', 1500),
], ],
], ],
], ],
@ -63,40 +69,15 @@ class GenerateTest extends TestCase
'type' => BillType::class, 'type' => BillType::class,
'filename' => 'rechnung-fur-firstname-lastname.pdf', 'filename' => 'rechnung-fur-firstname-lastname.pdf',
'output' => [ 'output' => [
'12.00', 'Rechnung',
'Familie ::lastname::', '15.00',
'Beitrag für 1995 (::subName::)',
'Familie ::lastname::\\\\::street::\\\\::zip:: ::location::',
], ],
], ],
]; ];
} }
/** @dataProvider generatorProvider */
public function testItGeneratesAPdf(
array $members,
callable $urlCallable,
string $type,
?string $filename = null,
?array $output = null
): void {
$this->withoutExceptionHandling();
$this->login();
$members = $this->setupMembers($members);
$urlId = call_user_func($urlCallable, $members);
$response = $this->call('GET', "/member/{$urlId}/pdf", [
'type' => $type,
]);
if ($filename === null) {
$response->assertStatus(204);
return;
}
$this->assertEquals('application/pdf', $response->headers->get('content-type'));
$this->assertEquals('inline; filename="' . $filename . '"', $response->headers->get('content-disposition'));
}
/** @dataProvider generatorProvider */ /** @dataProvider generatorProvider */
public function testItGeneratesTheLayout( public function testItGeneratesTheLayout(
array $members, array $members,
@ -126,6 +107,33 @@ class GenerateTest extends TestCase
} }
} }
/** @dataProvider generatorProvider */
public function testItGeneratesAPdf(
array $members,
callable $urlCallable,
string $type,
?string $filename = null,
?array $output = null
): void {
$this->withoutExceptionHandling();
$this->login();
$members = $this->setupMembers($members);
$urlId = call_user_func($urlCallable, $members);
$response = $this->call('GET', "/member/{$urlId}/pdf", [
'type' => $type,
]);
if ($filename === null) {
$response->assertStatus(204);
return;
}
$this->assertEquals('application/pdf', $response->headers->get('content-type'));
$this->assertEquals('inline; filename="' . $filename . '"', $response->headers->get('content-disposition'));
}
private function setupMembers(array $members): Collection private function setupMembers(array $members): Collection
{ {
return collect($members)->map(function (array $member): Member { return collect($members)->map(function (array $member): Member {