1 <?php
2
3 namespace Objednavky;
4
5 use Nette\Application\UI;
6 use Nette\Utils\Html;
7 use Nette\Database\Table\Selection;
8 use Kdyby\BootstrapFormRenderer\BootstrapRenderer;
9 use PLus\Orders\Repositories;
10 use PLus\Orders\Factory;
11
12 13 14 15 16
17 class InvestmentPresenter extends BasePresenter {
18
19
20 public $orderRepository;
21
22
23 public $areaRepository;
24
25
26 public $ordertypeRepository;
27
28
29 public $realizationRepository;
30
31
32 public $timelineRepository;
33
34
35 public $itemorderRepository;
36
37
38 public $itemareastateRepository;
39
40
41 public $unituserRepository;
42
43
44 public $unitRepository;
45
46
47 public $userRepository;
48
49
50 public $documentFactory;
51
52
53 public $documenttypeRepository;
54
55
56 public $documentRepository;
57
58
59 public $departmentRepository;
60
61
62 public $chiefRepository;
63
64
65 public $traderRepository;
66
67
68 public $managerRepository;
69
70
71 public $justifyRepository;
72
73
74 public $cfgRepository;
75
76
77 public $parameters;
78
79
80 public $investmentRepository;
81
82
83 public $investmentchatRepository;
84
85
86 public $mailerCfg;
87
88
89
90 91 92 93 94 95
96 function createNewInvestment($backlink) {
97
98 $user = $this->getUser();
99 $maxsize = $this->parameters->getMaxSizeUpload();
100 $maxsizeToHuman = $this->parameters->formatBytes($maxsize);
101 $form = new UI\Form();
102 $form->setRenderer(new BootstrapRenderer());
103
104
105 $form->getElementPrototype()->class = 'form-horizontal';
106
107 $order = $form->addContainer('order');
108
109 $order->addSelect('ns', 'Nákladové středisko:', $this->unitRepository->getPairUserUnit($this->getUser()->getId()))
110 ->setPrompt('Vyberte NS ...')
111 ->setRequired()
112 ->setAttribute('class', 'form-control2');
113
114 $order->addText('name', 'Předmět objednávky:')
115 ->setRequired('Zadejte prosím předmět objednávky')
116 ->addRule(UI\Form::MAX_LENGTH, 'Maximální délka je %d znaků.', 255)
117 ->setAttribute('class', 'form-control');
118
119
120 $order->addSelect('ordertype_id', 'Typ objednávky:', $this->ordertypeRepository->getPairData())
121 ->setPrompt('Vyberte typ objednávky ...')
122 ->setAttribute('class', 'form-control2');
123 $order->addSelect('realization_id', 'Způsob realizace:', $this->realizationRepository->getPairData())
124 ->setPrompt('Vyberte způsob realizace ...')
125 ->setAttribute('class', 'form-control2');
126
127
128 $order->addText('price', 'Očekávaná cena:')
129 ->setRequired('Zadejte předpokládanou cenu investice')
130 ->addRule(UI\Form::FLOAT, 'Očekávaná cena musí být číslo')
131 ->addRule(UI\Form::MIN, 'Minimální hodnota investice je 20.000,- Kč. Pro nižší částku použijte formulář neinvestiční objednávky.',20000)
132 ->setAttribute('class', 'form-control')
133 ->setAttribute('type', 'addon');
134
135
136 $order->addTbDatePicker('deadline','Datum realizace do:',NULL,10)
137 ->setRequired()
138 ->setAttribute('data-date-format', 'DD.MM.YYYY')
139 ->setAttribute('class', 'form-control')
140 ->setAttribute('type', 'addon')
141 ->setAttribute('readonly');
142
143 $order->addSelect('justify_id', 'Odůvodnění:', $this->justifyRepository->getPairDataNotOne() + $this->justifyRepository->getPairDataOne())
144 ->setPrompt('Vyberte odůvodnění ...')
145 ->setRequired()
146 ->setAttribute('class', 'form-control2');
147
148
149 $order->addHidden('creator');
150
151
152 $item = $form->addContainer('item');
153
154 $item->addTextArea('description', 'Detail odůvodnění:', NULL, 4)
155 ->setRequired()
156 ->setAttribute('class', 'form-control');
157
158 $document = $form->addContainer('document');
159
160 $document->addSelect('documenttype_id', 'Typ dokumentu:', array(0 => '') + $this->documenttypeRepository->getPairData())
161 ->setAttribute('class', 'form-control2');
162 $document->addUpload('files', 'Soubor:', TRUE)
163 ->addRule(UI\Form::MAX_FILE_SIZE, 'Maximální povolená velikost souboru je '.$maxsizeToHuman.'.', $maxsize );
164
165
166
167 $investment = $form->addContainer('investment');
168
169 $investment->addRadioList('health', 'Požadavky na ZP:', array('1' => 'Ano', '2' => 'Ne', '0' => 'Není vyžadováno'))
170 ->setDefaultValue('0')
171 ->getSeparatorPrototype()
172 ->setName(NULL);
173
174 $investment->addTextArea('dhealth', 'Popis:', NULL, 5)
175 ->setAttribute('class', 'form-control')
176 ->addConditionOn($investment['health'], UI\Form::IS_IN, array(1,2))
177 ->setRequired('Je požadováno vyplnění položky Popis požadavku na ZP v části Doplňující informace.');
178
179 $investment->addRadioList('human', 'Personální vybavení:', array('1' => 'Ano', '2' => 'Ne', '0' => 'Není vyžadováno'))
180 ->setDefaultValue('0')
181 ->getSeparatorPrototype()
182 ->setName(NULL);
183
184 $investment->addTextArea('dhuman', 'Popis:', NULL, 5)
185 ->setAttribute('class', 'form-control')
186 ->addConditionOn($investment['human'], UI\Form::IS_IN, array(1,2))
187 ->setRequired('Je požadováno vyplnění položky Popis požadavku na personální vybavení v části Doplňující informace.');
188
189 $investment->addRadioList('technical', 'Zdravotnická tech.:', array('1' => 'Ano', '2' => 'Ne', '0' => 'Není vyžadováno'))
190 ->setDefaultValue('0')
191 ->getSeparatorPrototype()
192 ->setName(NULL);
193
194 $investment->addTextArea('dtechnical', 'Popis:', NULL, 5)
195 ->setAttribute('class', 'form-control')
196 ->addConditionOn($investment['technical'], UI\Form::IS_IN, array(1,2))
197 ->setRequired('Je požadováno vyplnění položky Popis požadavku na technického vybavení v části Doplňující informace.');
198
199 $investment->addRadioList('construct', 'Stavební úpravy:', array('1' => 'Ano', '2' => 'Ne', '0' => 'Není vyžadováno'))
200 ->setDefaultValue('0')
201 ->getSeparatorPrototype()
202 ->setName(NULL);
203
204 $investment->addTextArea('dconstruct', 'Popis:', NULL, 5)
205 ->setAttribute('class', 'form-control')
206 ->addConditionOn($investment['construct'], UI\Form::IS_IN, array(1,2))
207 ->setRequired('Je požadováno vyplnění položky Popis požadavku na stavební úpravy v části Doplňující informace.');
208
209
210
211 $form->addSubmit('saving', 'Odeslat objednávku')
212 ->setAttribute('class', 'btn btn-primary');
213
214 $form->addSubmit('back')
215 ->setAttribute('class', 'btn btn-default')
216 ->getControlPrototype()
217 ->setName('a')
218 ->href($this->link('Order:sent'))
219
220 ->add(Html::el('span', ' Storno'));
221
222
223 return $form;
224 }
225
226
227 228 229
230 public function actionCreate() {
231
232 $deadline = new \Nette\Utils\DateTime();
233 $deadline->add(new \DateInterval('P90D'));
234 $creator = $this->userRepository->findById($this->getUser()->id);
235 $this['newInvestment']['order']->setDefaults(array( 'price' => 0,
236 'deadline' => $deadline,
237 'creator' => $creator->firstname." ".$creator->lastname
238 ));
239
240 $this->template->maxsizedoc = $this->parameters->formatBytes($this->parameters->getMaxSizeUpload());
241 }
242
243
244 245 246 247 248
249 protected function createComponentNewInvestment() {
250
251 $session = $this->getSession()->getSection('history');
252
253 if (isset($session->secnewborder)) {
254 $backlink = $session->secnewborder;
255 } else {
256 $backlink = $this->link('default');
257 }
258
259 $form = $this->createNewInvestment($backlink);
260 $form->onSuccess[] = $this->newInvestmentSubmitted;
261
262 return $form;
263 }
264
265
266 public function newInvestmentSubmitted($form) {
267
268 try {
269
270 $fdata = $form['order']->getValues();
271 $user_id = $fdata->ns;
272 $author_id = $this->getUser()->getId();
273
274 $data = new \Nette\Utils\ArrayHash();
275 $data->name = $fdata->name;
276 $data->ordertype_id = $fdata->ordertype_id;
277 $data->realization_id = $fdata->realization_id;
278 $data->price = $fdata->price;
279 $data->deadline = $fdata->deadline;
280 $data->justify_id = $fdata->justify_id;
281 $data->creator = $fdata->creator? $fdata->creator : NULL;
282 $data->user_id = $user_id;
283 $data->created = date('Y-m-d H:i:s');
284 $data->departmentflag = 1;
285 $data->chiefflag = 1;
286 $data->traderdownflag = 1;
287 $data->isinvestment = 1;
288
289 $neworder = $this->orderRepository->insertData($data);
290 $idorder = $neworder->id;
291
292
293 $this->timelineRepository->insertData(array(
294 'order_id' => $idorder,
295 'user_id' => $user_id,
296 'event_id' => 1,
297 'created' => date('Y-m-d H:i:s')
298 ));
299
300
301 $idata = $form['item']->getValues();
302 $dataitem = new \Nette\Utils\ArrayHash();
303 $dataitem->name = $fdata->name;
304 $dataitem->number = 1;
305 $dataitem->order_id = $idorder;
306 $dataitem->description = $idata->description? $idata->description : NULL;
307 $dataitem->dimenzion_id = 1;
308
309 $newitem = $this->itemorderRepository->insertData($dataitem);
310 $iditemorder = $newitem->id;
311
312
313 $sumfiles = 0;
314 $filedatas = $form['document']->getValues();
315
316 try {
317 foreach ($filedatas->files as $file) {
318 if ($file->isOk()) {
319 $doc = new \Nette\Utils\ArrayHash();
320 $doc->name = $file->getName();
321 $doc->filename = $file->getName();
322 $doc->documenttype_id = ($filedatas->documenttype_id == 0? NULL: $filedatas->documenttype_id);
323 $doc->mime = $file->getContentType();
324 $doc->data = $file->getContents();
325 $doc->lastmod = date('Y-m-d H:i:s');
326 $doc->user_id = $user_id;
327 $doc->order_id = $idorder;
328
329 $this->documentRepository->insertData($doc);
330
331 $sumfiles = $sumfiles + 1;
332 }
333 }
334
335 if ($sumfiles > 0) {
336
337 $this->timelineRepository->insertData(array(
338 'order_id' => $idorder,
339 'user_id' => $user_id,
340 'event_id' => 10,
341 'created' => date('Y-m-d H:i:s')
342 ));
343 }
344 } catch ( \PDOException $e) {
345 $this->flashMessage("Soubor se nepodařilo uložit! ". $e->getMessage(), "error");
346 }
347
348
349 $this->timelineRepository->insertData(array(
350 'order_id' => $idorder,
351 'user_id' => $user_id,
352 'event_id' => 11,
353 'created' => date('Y-m-d H:i:s')
354 ));
355
356
357
358 $this->chiefRepository->insertData(array(
359 'approved' => 1,
360 'rejected' => NULL,
361 'lastmod' => date('Y-m-d H:i:s'),
362 'order_id' => $idorder,
363 'user_id' => $author_id,
364 'description' => '~ systémový zápis (investice) ~'
365 ));
366
367
368 $this->timelineRepository->insertData(array(
369 'order_id' => $idorder,
370 'user_id' => $author_id,
371 'event_id' => 2,
372 'created' => date('Y-m-d H:i:s')
373 ));
374
375
376
377 $datai = $form['investment']->getValues();
378 $di = new \Nette\Utils\ArrayHash();
379 $di->order_id = $idorder;
380 $di->healthreq = $datai->health;
381 $di->humanres = $datai->human;
382 $di->construction = $datai->construct;
383 $di->technicalsupp = $datai->technical;
384 $di->user_id = $author_id;
385
386 $newinvestment = $this->investmentRepository->insertData($di);
387 $idinvestment = $newinvestment->id;
388
389
390 if($datai->health != 0) {
391 $this->investmentchatRepository->insertData(array(
392 'text' => $datai->dhealth,
393 'created' => date('Y-m-d H:i:s'),
394 'investment_id' => $idinvestment,
395 'user_id' => $author_id,
396 'ichattype_id' => 1
397 ));
398 }
399 if($datai->human != 0) {
400 $this->investmentchatRepository->insertData(array(
401 'text' => $datai->dhuman,
402 'created' => date('Y-m-d H:i:s'),
403 'investment_id' => $idinvestment,
404 'user_id' => $author_id,
405 'ichattype_id' => 2
406 ));
407 }
408 if($datai->technical != 0) {
409 $this->investmentchatRepository->insertData(array(
410 'text' => $datai->dtechnical,
411 'created' => date('Y-m-d H:i:s'),
412 'investment_id' => $idinvestment,
413 'user_id' => $author_id,
414 'ichattype_id' => 3
415 ));
416 }
417 if($datai->construct != 0) {
418 $this->investmentchatRepository->insertData(array(
419 'text' => $datai->dconstruct,
420 'created' => date('Y-m-d H:i:s'),
421 'investment_id' => $idinvestment,
422 'user_id' => $author_id,
423 'ichattype_id' => 4
424 ));
425 }
426
427
428 $areas = $this->areaRepository->getInvestmentArea();
429 foreach ($areas as $key => $value) {
430 $this->itemorderRepository->assignArea($value, $iditemorder);
431 }
432
433 foreach ($areas as $key => $value) {
434 $this->itemareastateRepository->addAreaState($iditemorder, $value, $author_id);
435 }
436
437
438 $this->timelineRepository->insertData(array(
439 'order_id' => $idorder,
440 'user_id' => $author_id,
441 'event_id' => 14,
442 'created' => date('Y-m-d H:i:s')
443 ));
444
445 $this->timelineRepository->insertData(array(
446 'order_id' => $idorder,
447 'user_id' => $author_id,
448 'event_id' => 5,
449 'created' => date('Y-m-d H:i:s')
450 ));
451
452
453
454 $uri = $this->getHttpRequest()->getUrl();
455 $from = $this->parameters->getFrom();
456 $support = $this->parameters->getSupport();
457 $to = $this->userRepository->getTraderEmail();
458 $subject = 'Investiční objednávka - přiřadit odbornou oblast';
459 $params = array(
460 'order_id' => $idorder,
461 'contact' => $support,
462 'url' => $uri->hostUrl.'/trader&execution=view&id='.$idorder,
463 'investment' => 1
464 );
465 $template = __DIR__ . '/../templates/Mailer/emailToTrader.latte';
466 $image = __DIR__ . '/../../www/img/logo32x32.gif';
467 $mailer = $this->mailerCfg->getMailConfig();
468
469 MailerPresenter::sendMail($from, $to, $subject, $params, $template, $image, $mailer);
470
471
472 $to = $this->userRepository->getExpertEmail($areas);
473 $subject = 'Objednávka k odbornému posouzení';
474 $params = array(
475 'order_id' => $idorder,
476 'contact' => $support,
477 'url' => $uri->hostUrl.'/expert&execution=view&id='.$idorder
478 );
479 $template = __DIR__ . '/../templates/Mailer/emailToExpert.latte';
480 $image = __DIR__ . '/../../www/img/logo32x32.gif';
481 $mailer = $this->mailerCfg->getMailConfig();
482
483 MailerPresenter::sendMail($from, $to, $subject, $params, $template, $image, $mailer);
484
485
486
487 $this->flashMessage("Záznam byl uložen.", 'success');
488
489 $session = $this->getSession()->getSection('history');
490 if (isset($session->secnewinvestment)) {
491 $backlink = $session->secnewinvestment;
492 } else {
493 $backlink = $this->link('Order:sent');
494 }
495 $this->redirectUrl($backlink);
496
497 } catch (\PDOException $ex) {
498 $this->flashMessage("Chyba při uložení do DB. ".$ex->getMessage(), 'error');
499 }
500 }
501
502
503 }
504