Passed
Push — master ( df9557...e5317d )
by Jan
04:48 queued 11s
created

PaymentOrderPDFGenerator::generatePDF()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 37
nc 3
nop 1
dl 0
loc 55
rs 8.7057
c 2
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\PDF;
20
21
use App\Entity\PaymentOrder;
22
use IntlDateFormatter;
23
use LogicException;
24
use TCPDF;
25
26
/**
27
 * This service generates a PDF document describing the payment order.
28
 */
29
class PaymentOrderPDFGenerator
30
{
31
    /**
32
     * Generates a PDF from the given PaymentOrder.
33
     * The raw PDF content is returned as string.
34
     */
35
    public function generatePDF(PaymentOrder $paymentOrder): string
36
    {
37
        if (null === $paymentOrder->getDepartment()) {
38
            throw new LogicException('$paymentOrder must have an associated department!');
39
        }
40
41
        $pdf = new SturaPDF();
42
        $pdf->setAuthor('StuRa FSU Jena');
43
        $pdf->setTitle('Zahlungsauftrag #'.$paymentOrder->getId());
44
        $pdf->setSubject('Zahlungsauftrag');
45
        $pdf->SetAutoPageBreak(false);
46
47
        $pdf->AddPage();
48
49
        $pdf->setY(80);
50
        $pdf->setMargins(25, 10);
51
52
        $pdf->writeHTML('<h1>Zahlungsauftrag #'.$paymentOrder->getId().'</h1><br>');
53
54
        $this->writeRow($pdf, 'Name des Zahlungsempfängers', $paymentOrder->getFullName());
55
        $this->writeRow($pdf, 'Struktur / Organisation', $paymentOrder->getDepartment()->getName());
56
        $this->writeRow($pdf, 'Projektbezeichnung', $paymentOrder->getProjectName());
57
        $this->writeRow($pdf, 'Betrag', $paymentOrder->getAmountString().' €');
58
        $this->writeRow($pdf, 'Mittelfreigabe / Finanzantrag', !empty($paymentOrder->getFundingId()) ? $paymentOrder->getFundingId() : '<i>Nicht angegeben</i>');
59
        $this->writeRow($pdf, 'FSR-Kom Antrag', $paymentOrder->isFsrKomResolution() ? 'Ja' : 'Nein');
60
        $formatter = new IntlDateFormatter('de_DE', IntlDateFormatter::MEDIUM, IntlDateFormatter::NONE);
61
        $this->writeRow($pdf, 'Beschlussdatum', null === $paymentOrder->getResolutionDate() ? '<i>Nicht angegeben</i>' : $formatter->format($paymentOrder->getResolutionDate()));
62
63
        $pdf->Ln();
64
65
        $this->writeRow($pdf, 'Kontoinhaber*in', $paymentOrder->getBankInfo()->getAccountOwner());
66
        $this->writeRow($pdf, 'Straße/Nr.', $paymentOrder->getBankInfo()->getStreet());
67
        $this->writeRow($pdf, 'PLZ/Ort', $paymentOrder->getBankInfo()->getZipCode().' '.$paymentOrder->getBankInfo()->getCity());
68
        $this->writeRow($pdf, 'IBAN', $paymentOrder->getBankInfo()->getIban());
69
        $this->writeRow($pdf, 'BIC', $paymentOrder->getBankInfo()->getBic());
70
        $this->writeRow($pdf, 'Bank', $paymentOrder->getBankInfo()->getBankName());
71
        $this->writeRow($pdf, 'Verwendungszweck', $paymentOrder->getBankInfo()->getReference());
72
73
        $pdf->Ln();
74
        $formatter = new IntlDateFormatter('de-DE', IntlDateFormatter::MEDIUM, IntlDateFormatter::MEDIUM);
75
        $this->writeRow($pdf, 'Einreichungsdatum', $formatter->format($paymentOrder->getCreationDate()));
76
77
        $pdf->Ln(15);
78
        $pdf->writeHTML('Dieses Dokument muss <i>ausgedruckt</i> und <i>unterschrieben</i> werden und wird dann zusammen mit den Belegen abgeheftet
79
                und mit dem Jahresabschluss beim StuRa abgegeben!');
80
        $pdf->writeHTML('Mit meiner Unterschrift erkläre ich, dass die Angaben hier korrekt sind und ich alle Belege vorliegen habe.');
81
82
        if ($paymentOrder->getDepartment()->isFSR()) {
83
            $pdf->Ln(20);
84
            $this->addSignatureField($pdf, 'Datum, Unterschrift FSR Verantwortliche', false);
85
        }
86
87
        //$pdf->MultiCell(0,0, 'Name:', 0, 'L', )
88
89
        return $pdf->Output('doc.pdf', 'S');
90
    }
91
92
    private function addSignatureField(TCPDF $pdf, string $content, bool $ln = true, string $align = 'L'): void
93
    {
94
        $pdf->writeHTML('_____________________________________________<br><small>'.$content.'</small>', $ln, false, false, false, $align);
95
    }
96
97
    private function writeRow(TCPDF $pdf, string $property, string $value): void
98
    {
99
        $pdf->MultiCell(80, 5, '<b>'.$property.':</b>', 0, 'L', 0, 0, '', '', true, 0, true);
100
        $pdf->MultiCell(0, 5, $value, 0, 'L', 0, 1, '', '', true, 0, true);
101
    }
102
}
103