1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Investform module for webcms2. |
5
|
|
|
* Copyright (c) @see LICENSE |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace FrontendModule\InvestformModule; |
9
|
|
|
|
10
|
|
|
use Nette\Forms\Form; |
11
|
|
|
use WebCMS\InvestformModule\Entity\Investment; |
12
|
|
|
use WebCMS\InvestformModule\Entity\Address; |
13
|
|
|
use WebCMS\InvestformModule\Common\PdfPrinter; |
14
|
|
|
use WebCMS\InvestformModule\Common\EmailSender; |
15
|
|
|
use WebCMS\InvestformModule\Entity\Businessman; |
16
|
|
|
use Nette\Mail\Message; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Description of InvestformPresenter |
20
|
|
|
* |
21
|
|
|
* @author Tomas Voslar <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class InvestformPresenter extends BasePresenter |
24
|
|
|
{ |
25
|
|
|
private $id; |
26
|
|
|
|
27
|
|
|
/* \Nette\Http\Session */ |
28
|
|
|
public $businessmanSession; |
29
|
|
|
|
30
|
|
|
protected function startup() |
31
|
|
|
{ |
32
|
|
|
parent::startup(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function beforeRender() |
36
|
|
|
{ |
37
|
|
|
parent::beforeRender(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function createComponentForm($name) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
$form = $this->createForm('form-submit', 'default', null); |
43
|
|
|
|
44
|
|
|
$form->addText('name', 'Name')->setRequired('Name is mandatory.'); |
45
|
|
|
$form->addText('lastname', 'Lastname')->setRequired('Lastname is mandatory.'); |
46
|
|
|
$form->addText('street', 'Street')->setRequired('Street is mandatory.'); |
47
|
|
|
$form->addText('date', 'Date')->setRequired('Date is mandatory.'); |
48
|
|
|
$form->addText('postcode', 'Postcode') |
49
|
|
|
->setRequired('Postcode is mandatory.') |
50
|
|
|
->addRule(Form::PATTERN, 'Postcode must contain 5 numbers.', '([0-9]\s*){5}'); |
51
|
|
|
$form->addText('city', 'City')->setRequired('City is mandatory.'); |
52
|
|
|
$form->addText('phone', 'Phone')->setRequired('Phone is mandatory.'); |
53
|
|
|
$form->addText('email', 'Email') |
54
|
|
|
->addRule(Form::EMAIL, 'This email is not valid.') |
55
|
|
|
->setRequired('Email is mandatory.'); |
56
|
|
|
$form->addCheckbox('invest', 'Invest'); |
57
|
|
|
$form->addText('company', 'Company') |
58
|
|
|
->addConditionOn($form['invest'], Form::EQUAL, true) |
59
|
|
|
->addRule(Form::FILLED, 'Company name is madatory.'); |
60
|
|
|
$form->addText('registrationNumber', 'Registration number') |
61
|
|
|
->addConditionOn($form['invest'], Form::EQUAL, true) |
62
|
|
|
->addRule(Form::FILLED, 'Registration number is mandatory.'); |
63
|
|
|
$form->addText('bankAccountPrefix', 'Bank account') |
64
|
|
|
->addRule(callback($this, 'validateBankAccount'), "Bank acccount prefix is not valid`."); |
65
|
|
|
$form->addText('bankAccount', 'Bank account') |
66
|
|
|
->setRequired('Bank account is mandatory.'); |
67
|
|
|
// ->addRule(callback($this, 'validateBankAccount'), "Bank acccount is not valid`."); |
68
|
|
|
$form->addSelect('investmentAmount', 'Investment amount', $this->amountItems) |
69
|
|
|
->setRequired('Amount of investment is mandatory.'); |
70
|
|
|
$form->addSelect('investmentLength', 'Investment length', array(3 => 'Tříletý', 5 => 'Pětiletý')) |
71
|
|
|
->setRequired('Investment length is mandatory.'); |
72
|
|
|
|
73
|
|
|
$form->addSubmit('send', 'Send'); |
74
|
|
|
|
75
|
|
|
$form->onSuccess[] = callback($this, 'formSubmitted'); |
76
|
|
|
|
77
|
|
|
return $form; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function createComponentStep2Form($name) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$parameters = $this->getParameter('parameters'); |
83
|
|
|
$form = $this->createForm('step2Form-submit', 'default', null, array( |
84
|
|
|
'2', |
85
|
|
|
'hash' => $parameters[1] |
86
|
|
|
)); |
87
|
|
|
|
88
|
|
|
$investment = $this->em->getRepository('WebCMS\InvestformModule\Entity\Investment')->findOneByHash($parameters[1]); |
89
|
|
|
$companyNumber = $investment->getRegistrationNumber(); |
90
|
|
|
if (empty($companyNumber)) { |
91
|
|
|
$form->addText('birthdateNumber', 'Birthdate number') |
92
|
|
|
->setRequired('Birthdate number is mandatory.') |
93
|
|
|
->addRule(callback($this, 'validateBirthdateNumber'), "Birthdate can contain just numbers."); |
94
|
|
|
} else { |
95
|
|
|
$form->addHidden('birthdateNumber'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (isset($this->businessmanSession->id)) { |
99
|
|
|
|
100
|
|
|
$form->addHidden('pin', $this->businessmanSession->id); |
101
|
|
|
|
102
|
|
|
$form['pin']->getControlPrototype()->setClass('session'); |
103
|
|
|
} else { |
104
|
|
|
$form->addText('pin', 'Pin number'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
$form->addCheckbox('postalAddress', 'Postal address'); |
109
|
|
|
$form->addText('name', 'Name') |
110
|
|
|
->addConditionOn($form['postalAddress'], Form::EQUAL, true) |
111
|
|
|
->addRule(Form::FILLED, 'Name is mandatory.'); |
112
|
|
|
$form->addText('lastname', 'Lastname') |
113
|
|
|
->addConditionOn($form['postalAddress'], Form::EQUAL, true) |
114
|
|
|
->addRule(Form::FILLED, 'Lastname is mandatory.'); |
115
|
|
|
$form->addText('street', 'Street') |
116
|
|
|
->addConditionOn($form['postalAddress'], Form::EQUAL, true) |
117
|
|
|
->addRule(Form::FILLED, 'Street is mandatory.'); |
118
|
|
|
$form->addText('postcode', 'Postcode') |
119
|
|
|
->addConditionOn($form['postalAddress'], Form::EQUAL, true) |
120
|
|
|
->addRule(Form::PATTERN, 'Postcode must contain 5 numbers.', '([0-9]\s*){5}') |
121
|
|
|
->addRule(Form::FILLED, 'Postcode is mandatory.'); |
122
|
|
|
$form->addText('city', 'City') |
123
|
|
|
->addConditionOn($form['postalAddress'], Form::EQUAL, true) |
124
|
|
|
->addRule(Form::FILLED, 'City is mandatory.'); |
125
|
|
|
$form->addHidden('idUser')->setDefaultValue($this->id); |
126
|
|
|
|
127
|
|
|
$form->addSubmit('send', 'Send'); |
128
|
|
|
|
129
|
|
|
$form->onSuccess[] = callback($this, 'step2formSubmitted'); |
130
|
|
|
|
131
|
|
|
return $form; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function validateBirthdateNumber($control) |
135
|
|
|
{ |
136
|
|
|
$rc = $control->getValue(); |
137
|
|
|
|
138
|
|
|
if (!preg_match('#^\s*(\d\d)(\d\d)(\d\d)[ /]*(\d\d\d)(\d?)\s*$#', $rc, $matches)) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
list(, $year, $month, $day, $ext, $c) = $matches; |
143
|
|
|
|
144
|
|
|
if ($c === '') { |
145
|
|
|
return $year < 54; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$mod = ($year . $month . $day . $ext) % 11; |
149
|
|
|
if ($mod === 10) $mod = 0; |
150
|
|
|
if ($mod !== (int) $c) { |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$year += $year < 54 ? 2000 : 1900; |
155
|
|
|
|
156
|
|
|
if ($month > 70 && $year > 2003) $month -= 70; |
157
|
|
|
elseif ($month > 50) $month -= 50; |
158
|
|
|
elseif ($month > 20 && $year > 2003) $month -= 20; |
159
|
|
|
|
160
|
|
|
if (!checkdate($month, $day, $year)) { |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function validateBankAccount($control) |
168
|
|
|
{ |
169
|
|
|
$number = $control->getValue(); |
170
|
|
|
|
171
|
|
|
$parts = explode('/', $number); |
172
|
|
|
$baNumber = $parts[0]; |
173
|
|
|
|
174
|
|
|
$parts = explode('-', $baNumber); |
175
|
|
|
|
176
|
|
|
foreach ($parts as $part) { |
177
|
|
|
if (!$this->validatePart($part)) { |
178
|
|
|
return false; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* A B C D E F G H I J |
187
|
|
|
* _________________________ |
188
|
|
|
* s 6 3 7 9 10 5 8 4 2 1 |
189
|
|
|
* n 10 9 8 7 6 5 4 3 2 1 |
190
|
|
|
* |
191
|
|
|
* S = J *1 I *2 H *4 G *8 F*5 E *10 D*9 C*7 B*3 A *6 |
192
|
|
|
* |
193
|
|
|
* @param [type] $part [description] |
|
|
|
|
194
|
|
|
* @return [type] [description] |
|
|
|
|
195
|
|
|
*/ |
196
|
|
|
private function validatePart($part) |
197
|
|
|
{ |
198
|
|
|
$scales = array(1, 2, 4, 8, 5, 10, 9, 7, 3, 6); |
199
|
|
|
$toValidate = array_reverse(str_split($part)); |
200
|
|
|
|
201
|
|
|
$controlSum = 0; |
202
|
|
|
for ($i=0; $i < count($toValidate); $i++) { |
|
|
|
|
203
|
|
|
$controlSum += $toValidate[$i] * $scales[$i]; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $controlSum % 11 === 0; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function actionDefault($id) |
|
|
|
|
210
|
|
|
{ |
211
|
|
|
$parameters = $this->getParameter(); |
212
|
|
|
$parameters = $parameters['parameters']; |
213
|
|
|
|
214
|
|
|
$this->businessmanSession = $this->getSession('businessman'); |
215
|
|
|
|
216
|
|
|
if (array_key_exists(0, $parameters) && $parameters[0] === '2') { |
217
|
|
|
|
218
|
|
|
if (isset($parameters[1])) { |
219
|
|
|
$hash = $parameters[1]; |
220
|
|
|
|
221
|
|
|
$investment = $this->em->getRepository('\WebCMS\InvestformModule\Entity\Investment')->findOneByHash($hash); |
222
|
|
|
$this->id = $investment->getId(); |
223
|
|
|
|
224
|
|
|
$this->template->setFile(APP_DIR . '/templates/investform-module/Investform/step2.latte'); |
|
|
|
|
225
|
|
|
} else { |
226
|
|
|
$this->template->setFile(APP_DIR . '/templates/investform-module/Investform/contactLater.latte'); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
} else if(array_key_exists(0, $parameters) && $parameters[0] === 'final') { |
231
|
|
|
$this->template->setFile(APP_DIR . '/templates/investform-module/Investform/final.latte'); |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$this->template->form = $this->createComponentForm('form'); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function formSubmitted($form) |
238
|
|
|
{ |
239
|
|
|
|
240
|
|
|
$values = $form->getValues(); |
241
|
|
|
|
242
|
|
|
if ($this->getUser()->isLoggedIn()) { |
243
|
|
|
|
244
|
|
|
$address = new Address; |
245
|
|
|
$address->setName($values->name); |
246
|
|
|
$address->setLastname($values->lastname); |
247
|
|
|
$address->setStreet($values->street); |
248
|
|
|
$address->setPostcode($values->postcode); |
249
|
|
|
$address->setCity($values->city); |
250
|
|
|
|
251
|
|
|
$this->em->persist($address); |
252
|
|
|
|
253
|
|
|
$investment = new Investment; |
254
|
|
|
$investment->setPhone($values->phone); |
255
|
|
|
$investment->setEmail($values->email); |
256
|
|
|
$investment->setInvestmentDate(new \Datetime(date('Y-m-d', strtotime($values->date)))); |
257
|
|
|
$investment->setInvestment($values->investmentAmount); |
258
|
|
|
$investment->setInvestmentLength($values->investmentLength); |
259
|
|
|
$investment->setRegistrationNumber($values->registrationNumber); |
260
|
|
|
$investment->setCompany($values->company); |
261
|
|
|
$investment->setAddress($address); |
262
|
|
|
|
263
|
|
|
$investment->setContractSend(false); |
264
|
|
|
$investment->setContractPaid(false); |
265
|
|
|
$investment->setContractClosed(false); |
266
|
|
|
$investment->setClientContacted(false); |
267
|
|
|
|
268
|
|
|
$values->bankAccount = ltrim($values->bankAccount, '0'); |
269
|
|
|
|
270
|
|
|
if (!empty($values->bankAccountPrefix)) { |
271
|
|
|
$bankAccount = str_replace('_', '', $values->bankAccountPrefix).'-'.str_replace('_', '', $values->bankAccount); |
272
|
|
|
} else { |
273
|
|
|
$bankAccount = str_replace('_', '', $values->bankAccount); |
274
|
|
|
} |
275
|
|
|
$investment->setBankAccount($bankAccount); |
276
|
|
|
|
277
|
|
|
if (isset($this->businessmanSession->id)) { |
278
|
|
|
$businessman = $this->em->getRepository('WebCMS\InvestformModule\Entity\Businessman')->find($this->businessmanSession->id); |
279
|
|
|
$investment->setBusinessman($businessman); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$this->em->persist($investment); |
283
|
|
|
$this->em->flush(); |
284
|
|
|
$investment->getHash(); |
285
|
|
|
$this->em->flush(); |
286
|
|
|
|
287
|
|
|
$this->sendPdf($investment, 'form'); |
288
|
|
|
|
289
|
|
|
} else { |
290
|
|
|
$infoEmail = $this->settings->get('Info email', \WebCMS\Settings::SECTION_BASIC, 'text')->getValue(); |
|
|
|
|
291
|
|
|
if (!empty($infoEmail)) { |
292
|
|
|
|
293
|
|
|
$mailBody = '<h3>Zájem o investici</h3>'; |
294
|
|
|
$mailBody .= '<hr><br />'; |
295
|
|
|
$mailBody .= '<h4>Kontaktní údaje</h4>'; |
296
|
|
|
$mailBody .= '<p>'; |
297
|
|
|
$mailBody .= $values->name.' '.$values->lastname.'<br />'; |
298
|
|
|
$mailBody .= $values->street.', '.$values->postcode.' '.$values->city.'<br />'; |
299
|
|
|
$mailBody .= $values->phone.', '.$values->email.'<br />'; |
300
|
|
|
$mailBody .= '</p><br />'; |
301
|
|
|
$mailBody .= '<h4>Zadaná investice - údaje</h4>'; |
302
|
|
|
$mailBody .= '<p>'; |
303
|
|
|
$mailBody .= 'Výše investice: '.$values->investmentAmount.'<br />'; |
304
|
|
|
$mailBody .= 'Varianta dluhopisu: '.$values->investmentLength.'<br />'; |
305
|
|
|
$mailBody .= '</p>'; |
306
|
|
|
|
307
|
|
|
$mail = new Message; |
308
|
|
|
$mail->setFrom($infoEmail) |
309
|
|
|
->addTo($infoEmail) |
310
|
|
|
->setSubject('Zájem o investici - '.$values->name.' '.$values->lastname) |
311
|
|
|
->setHTMLBody($mailBody); |
312
|
|
|
|
313
|
|
|
$mail->send(); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
|
318
|
|
|
|
319
|
|
|
if ($this->getUser()->isLoggedIn()) { |
320
|
|
|
|
321
|
|
|
$this->redirect('default', array( |
322
|
|
|
'path' => $this->actualPage->getPath(), |
323
|
|
|
'abbr' => $this->abbr, |
324
|
|
|
'parameters' => array( |
325
|
|
|
'2', |
326
|
|
|
'hash' => $investment->getHash() |
|
|
|
|
327
|
|
|
) |
328
|
|
|
)); |
329
|
|
|
|
330
|
|
|
} else { |
331
|
|
|
|
332
|
|
|
$this->redirect('default', array( |
333
|
|
|
'path' => $this->actualPage->getPath(), |
334
|
|
|
'abbr' => $this->abbr, |
335
|
|
|
'parameters' => array( |
336
|
|
|
'2' |
337
|
|
|
) |
338
|
|
|
)); |
339
|
|
|
|
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
|
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function step2formSubmitted($form) |
346
|
|
|
{ |
347
|
|
|
$values = $form->getValues(); |
348
|
|
|
|
349
|
|
|
$investment = $this->em->getRepository('WebCMS\InvestformModule\Entity\Investment')->find($values->idUser); |
350
|
|
|
$investment->setBirthdateNumber($values->birthdateNumber); |
351
|
|
|
|
352
|
|
|
if (isset($this->businessmanSession->id)) { |
353
|
|
|
$businessman = $this->em->getRepository('WebCMS\InvestformModule\Entity\Businessman')->find($this->businessmanSession->id); |
354
|
|
|
$investment->setBusinessman($businessman); |
355
|
|
|
} else { |
356
|
|
|
//check if businessman exists |
357
|
|
|
$businessman = $this->em->getRepository('WebCMS\InvestformModule\Entity\Businessman')->findOneBy(array( |
358
|
|
|
'businessId' => $values->pin |
359
|
|
|
)); |
360
|
|
|
if ($businessman) { |
361
|
|
|
$investment->setBusinessman($businessman); |
362
|
|
|
} |
363
|
|
|
$investment->setPin($values->pin); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
$investment->setContractSend(true); |
367
|
|
|
|
368
|
|
|
if ($values->postalAddress) { |
369
|
|
|
$address = new Address; |
370
|
|
|
$address->setName($values->name); |
371
|
|
|
$address->setLastname($values->lastname); |
372
|
|
|
$address->setStreet($values->street); |
373
|
|
|
$address->setPostcode($values->postcode); |
374
|
|
|
$address->setCity($values->city); |
375
|
|
|
|
376
|
|
|
$this->em->persist($address); |
377
|
|
|
|
378
|
|
|
$investment->setPostalAddress($address); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
$this->sendPdf($investment, 'contract'); |
382
|
|
|
$this->em->flush(); |
383
|
|
|
|
384
|
|
|
$this->redirect('default', array( |
385
|
|
|
'path' => $this->actualPage->getPath(), |
386
|
|
|
'abbr' => $this->abbr, |
387
|
|
|
'parameters' => array( |
388
|
|
|
'final' |
389
|
|
|
) |
390
|
|
|
)); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function handlegetNetIncome($amount, $length, $date) |
394
|
|
|
{ |
395
|
|
|
|
396
|
|
|
$from = strtotime($date); |
397
|
|
|
if ($length == 5) { |
398
|
|
|
$to = strtotime('2020-11-30'); |
399
|
|
|
} else { |
400
|
|
|
$to = strtotime('2017-10-30'); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
$length = ($to - $from) / 60 / 60 / 24 / 365; |
404
|
|
|
|
405
|
|
|
$fvoa = new \WebCMS\InvestformModule\Common\FutureValueOfAnnuityCalculator($amount, $length); |
406
|
|
|
|
407
|
|
|
$this->payload->profit = \WebCMS\Helpers\SystemHelper::price($fvoa->getTotalProfit(), '%.0n'); |
|
|
|
|
408
|
|
|
$this->sendPayload(); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
public function sendPdf($investment, $type) |
412
|
|
|
{ |
413
|
|
|
$emailSender = new EmailSender($this->settings, $investment, $type); |
414
|
|
|
$emailSender->send(); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
public function renderDefault($id) |
418
|
|
|
{ |
419
|
|
|
$this->template->id = $id; |
|
|
|
|
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.