Completed
Push — master ( 09f22c...e47e1f )
by
unknown
08:36
created

PdfPrinter::printPdfContract()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 45
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 10
Bugs 3 Features 5
Metric Value
c 10
b 3
f 5
dl 0
loc 45
rs 5.3846
cc 8
eloc 33
nc 48
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Investform module for webcms2.
5
 * Copyright (c) @see LICENSE
6
 */
7
8
namespace WebCMS\InvestformModule\Common;
9
10
require(APP_DIR . '/fpdm/fpdm.php');
11
12
use Nette\Templating\FileTemplate;
13
14
/**
15
 * 
16
 */
17
class PdfPrinter
18
{
19
	private $investment;
20
21
	public function __construct($investment)
22
	{
23
		$this->investment = $investment;
24
	}
25
26
	public function printPdfForm($response = false)
27
	{
28
		$fvoa = new FutureValueOfAnnuityCalculator($this->investment->getInvestment(), $this->investment->getRealInvestmentLength());
29
		
30
		$templatePath = APP_DIR . '/../zajistenainvestice-kalkulace.pdf';
31
		$length = $this->investment->getInvestmentLength();
32
33
		$company = $this->investment->getCompany();
34
		$name = $this->investment->getAddress()->getName() . ' ' . $this->investment->getAddress()->getLastname() . (!empty($company) ? ' / ' . $company : '');
35
		$fieldData = array(
36
			'name' => $name,
37
		    'investmentAmount' => number_format($this->investment->getInvestment(), 0, ",", ".") . ',- Kč',
38
		    'investmentAmountGraph' => number_format($this->investment->getInvestment(), 0, ",", ".") . ',- Kč',
39
		    'address' => $this->investment->getAddress()->getAddressString(),
40
		    'bankAccountNumber' => $this->investment->getBankAccount(),
41
		    'email' => $this->investment->getEmail(),
42
		    'telephoneNumber' => $this->investment->getPhone(),
43
		    'investmentLength' => ($length == '3' ? 'tříletý' : 'pětiletý'), // TODO move to settings
44
		    'incomeAfterTaxes' => number_format($fvoa->getTotalProfit(), 0, ",", ".") . ',- Kč',
45
		    'incomeBeforeTaxes' => number_format($fvoa->getTotalProfit(), 0, ",", ".") . ',- Kč'
46
		);
47
48
		return $this->processPdf($response, $templatePath, $fieldData, $this->investment, $this->investment->getHash());
49
	}
50
51
	public function printPdfContract($response = false, $investmentDate = '')
52
	{
53
		$fvoa = new FutureValueOfAnnuityCalculator($this->investment->getInvestment(), $this->investment->getRealInvestmentLength());
54
55
		$oldcontract = '';
56
		$paymentBankAccount = '2110773767/2700';
57
		if ($investmentDate && $this->investment->getInvestmentLength() == 5) {
58
			$investmentDt = $investmentDate;
59
			$oldcontractDt = new \DateTime('2015-11-30');
60
61
			if ($investmentDt < $oldcontractDt) {
62
				$oldcontract = '_old';
63
				$paymentBankAccount = '2110773767/2700';
64
			} else {
65
				$paymentBankAccount = '2112420631/2700';
66
			}
67
		}
68
		
69
		$templatePath = APP_DIR . "/../zajistenainvestice-smlouva_{$this->investment->getInvestmentLength()}lety-dluhopis{$oldcontract}.pdf";
70
		$bNumber = $this->investment->getBirthdateNumber();
71
		$postalAddress = ($this->investment->getPostalAddress() ? $this->investment->getPostalAddress()->getName() . ' ' . $this->investment->getPostalAddress()->getLastname() . ', ' . $this->investment->getPostalAddress()->getAddressString() : '-');
72
73
		$company = $this->investment->getCompany();
74
		$name = $this->investment->getAddress()->getName() . ' ' . $this->investment->getAddress()->getLastname() . (!empty($company) ? ' / ' . $company : '');
75
		$id = (!empty($bNumber) ? str_replace('/', '', $bNumber) : $this->investment->getRegistrationNumber());
76
77
		$businnesId = ($this->investment->getBusinessman() ? $this->investment->getBusinessman()->getBusinessId() : $this->investment->getPin());
78
79
		$fieldData = array(
80
		    'name' => $name,
81
		    'identificationNumber' => $id,
82
		    'address' => $this->investment->getAddress()->getAddressString(),
83
		    'mailingAddress' => $postalAddress,
84
		    'bankAccountNumber' => $this->investment->getBankAccount(),
85
		    'email' => $this->investment->getEmail(),
86
		    'paymentAmount' => number_format($fvoa->getPurchaseAmount(), 0, ',', '.') . ',- Kč',
87
		    'paymentBankAccount' => $paymentBankAccount, // TODO move to settings
88
		    'telephoneNumber' => $this->investment->getPhone(),
89
		    'paymentVariableSymbol' => $id,
90
			'amountOfBonds' => $this->investment->getInvestment() / 100000, // TODO move to settings
91
			'pin' => $businnesId
92
		);
93
94
		return $this->processPdf($response, $templatePath, $fieldData, $this->investment, $this->investment->getContractHash());
95
	}
96
97
	public function savePdfToZip($subfolder)
98
	{
99
		//TODO refactor to one function
100
		$fvoa = new FutureValueOfAnnuityCalculator($this->investment->getInvestment(), $this->investment->getRealInvestmentLength());
101
		
102
		$templatePath = APP_DIR . "/../zajistenainvestice-smlouva_{$this->investment->getInvestmentLength()}lety-dluhopis.pdf";
103
		$bNumber = $this->investment->getBirthdateNumber();
104
		$postalAddress = ($this->investment->getPostalAddress() ? $this->investment->getPostalAddress()->getName() . ' ' . $this->investment->getPostalAddress()->getLastname() . ', ' . $this->investment->getPostalAddress()->getAddressString() : '-');
105
106
		$company = $this->investment->getCompany();
107
		$name = $this->investment->getAddress()->getName() . ' ' . $this->investment->getAddress()->getLastname() . (!empty($company) ? ' / ' . $company : '');
108
		$id = (!empty($bNumber) ? str_replace('/', '', $bNumber) : $this->investment->getRegistrationNumber());
109
		$businnesId = ($this->investment->getBusinessman() ? $this->investment->getBusinessman()->getBusinessId() : $this->investment->getPin());
110
		$fieldData = array(
111
		    'name' => $name,
112
		    'identificationNumber' => $id,
113
		    'address' => $this->investment->getAddress()->getAddressString(),
114
		    'mailingAddress' => $postalAddress,
115
		    'bankAccountNumber' => $this->investment->getBankAccount(),
116
		    'email' => $this->investment->getEmail(),
117
		    'paymentAmount' => number_format($fvoa->getPurchaseAmount(), 0, ',', '.') . ',- Kč',
118
		    'paymentBankAccount' => '2110773767/2700', // TODO move to settings
119
		    'telephoneNumber' => $this->investment->getPhone(),
120
		    'paymentVariableSymbol' => $id,
121
			'amountOfBonds' => $this->investment->getInvestment() / 100000, // TODO move to settings
122
			'pin' => $businnesId
123
		);
124
125
		$pdf = new \FPDM($templatePath);
126
		$pdf->Load($fieldData, true); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
127
		$pdf->Merge();
128
129
		$contractPath = WWW_DIR . '/upload/contracts/' . $subfolder;
130
		if (!file_exists($contractPath)) {
131
			mkdir($contractPath);
132
		}
133
134
		$output = $this->getPdfContent($pdf);
135
		file_put_contents($contractPath . '/' . $this->investment->getContractHash() . '.pdf', $output);
136
137
	}
138
139
	private function processPdf($response, $templatePath, $fieldData, $investment, $hash)
0 ignored issues
show
Unused Code introduced by
The parameter $investment is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
140
	{
141
		$pdf = new \FPDM($templatePath);
142
		$pdf->Load($fieldData, true); // second parameter: false if field values are in ISO-8859-1, true if UTF-8
143
		$pdf->Merge();
144
145
		$contractPath = WWW_DIR . '/upload/contracts';
146
		if (!file_exists($contractPath)) {
147
			mkdir($contractPath);
148
		}
149
150
		$output = $this->getPdfContent($pdf);
151
		file_put_contents($contractPath . '/' . $hash . '.pdf', $output);
152
153
		if ($response) {
154
			header('Content-type: application/pdf');
155
			header('Content-Disposition: inline; filename="smlouva.pdf"');
156
			header('Content-Transfer-Encoding: binary');
157
			header('Content-Length: ' . filesize($contractPath . '/' . $hash . '.pdf'));
158
			header('Accept-Ranges: bytes');
159
160
			echo $output;
161
162
			die();
163
		} else {
164
			return $output;
165
		}
166
	}
167
168
	private function getPdfContent($pdf)
169
	{
170
		ob_start();
171
		
172
		$pdf->Output();
173
174
		$pdf = ob_get_contents();
175
		ob_clean();
176
177
		return $pdf;
178
	}
179
180
181
}
182