Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
85.48% |
106 / 124 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
ProcessQueue | |
85.48% |
106 / 124 |
|
85.71% |
6 / 7 |
20.10 | |
0.00% |
0 / 1 |
readResponse | |
100.00% |
30 / 30 |
|
100.00% |
1 / 1 |
5 | |||
getValidatedForm | |
100.00% |
42 / 42 |
|
100.00% |
1 / 1 |
1 | |||
readSelectedProcessWithWaitingnumber | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getProcess | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
writeQueuedProcess | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
isOpened | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
4 | |||
printProcessResponse | |
33.33% |
9 / 27 |
|
0.00% |
0 / 1 |
8.74 |
1 | <?php |
2 | |
3 | /** |
4 | * |
5 | * @package Zmsadmin |
6 | * @copyright BerlinOnline Stadtportal GmbH & Co. KG |
7 | * |
8 | */ |
9 | |
10 | namespace BO\Zmsadmin; |
11 | |
12 | use BO\Mellon\Condition; |
13 | use BO\Slim\Render; |
14 | use BO\Zmsadmin\Helper\MailTemplateArrayProvider; |
15 | use BO\Zmsentities\Client; |
16 | use BO\Zmsentities\Collection\ProcessList; |
17 | use BO\Zmsentities\Config; |
18 | use BO\Zmsentities\Helper\Messaging; |
19 | use BO\Zmsentities\Validator\ProcessValidator; |
20 | use BO\Zmsentities\Process as Entity; |
21 | use BO\Zmsadmin\Helper\AppointmentFormHelper; |
22 | use Psr\Http\Message\RequestInterface; |
23 | use Psr\Http\Message\ResponseInterface; |
24 | |
25 | /** |
26 | * Queue a process from appointment formular without appointment |
27 | */ |
28 | class ProcessQueue extends BaseController |
29 | { |
30 | /** |
31 | * @SuppressWarnings(Param) |
32 | */ |
33 | public function readResponse( |
34 | RequestInterface $request, |
35 | ResponseInterface $response, |
36 | array $args |
37 | ) { |
38 | $workstation = \App::$http->readGetResult('/workstation/', ['resolveReferences' => 3])->getEntity(); |
39 | |
40 | $validator = $request->getAttribute('validator'); |
41 | $selectedProcessId = $validator->getParameter('selectedprocess')->isNumber()->getValue(); |
42 | |
43 | if ($selectedProcessId) { |
44 | $process = $this->readSelectedProcessWithWaitingnumber($selectedProcessId); |
45 | |
46 | if ($process && $validator->getParameter('print')->isNumber()->getValue()) { |
47 | return $this->printProcessResponse( |
48 | $response, |
49 | $process, |
50 | $validator->getParameter('printType')->isString()->getValue(), |
51 | $workstation->scope['provider']['id'] |
52 | ); |
53 | } |
54 | } |
55 | |
56 | $input = $request->getParams(); |
57 | $scope = AppointmentFormHelper::readSelectedScope($request, $workstation); |
58 | $process = $this->getProcess($input, $scope); |
59 | $validatedForm = static::getValidatedForm($validator, $process); |
60 | if ($validatedForm['failed']) { |
61 | return \BO\Slim\Render::withJson( |
62 | $response, |
63 | $validatedForm |
64 | ); |
65 | } |
66 | |
67 | $process = $this->writeQueuedProcess($input, $process); |
68 | return \BO\Slim\Render::withHtml( |
69 | $response, |
70 | 'element/helper/messageHandler.twig', |
71 | array( |
72 | 'selectedprocess' => $process, |
73 | 'success' => 'process_queued' |
74 | ) |
75 | ); |
76 | } |
77 | |
78 | public static function getValidatedForm($validator, $process) |
79 | { |
80 | $processValidator = new ProcessValidator($process); |
81 | $delegatedProcess = $processValidator->getDelegatedProcess(); |
82 | $processValidator |
83 | ->validateName( |
84 | $validator->getParameter('familyName'), |
85 | $delegatedProcess->setter('clients', 0, 'familyName') |
86 | ) |
87 | ->validateMail( |
88 | $validator->getParameter('email'), |
89 | $delegatedProcess->setter('clients', 0, 'email') |
90 | ) |
91 | ->validateTelephone( |
92 | $validator->getParameter('telephone'), |
93 | $delegatedProcess->setter('clients', 0, 'telephone') |
94 | ) |
95 | ->validateSurvey( |
96 | $validator->getParameter('surveyAccepted'), |
97 | $delegatedProcess->setter('clients', 0, 'surveyAccepted') |
98 | ) |
99 | ->validateText( |
100 | $validator->getParameter('amendment'), |
101 | $delegatedProcess->setter('amendment') |
102 | ) |
103 | ->validateText( |
104 | $validator->getParameter('customTextfield'), |
105 | $delegatedProcess->setter('customTextfield') |
106 | ) |
107 | ->validateReminderTimestamp( |
108 | $validator->getParameter('headsUpTime'), |
109 | $delegatedProcess->setter('reminderTimestamp'), |
110 | new Condition( |
111 | $validator->getParameter('sendReminder')->isNumber()->isNotEqualTo(1) |
112 | ) |
113 | ) |
114 | |
115 | ; |
116 | $processValidator->getCollection()->addValid( |
117 | $validator->getParameter('sendConfirmation')->isNumber(), |
118 | $validator->getParameter('sendReminder')->isNumber() |
119 | ); |
120 | |
121 | $form = $processValidator->getCollection()->getStatus(null, true); |
122 | $form['failed'] = $processValidator->getCollection()->hasFailed(); |
123 | return $form; |
124 | } |
125 | |
126 | protected function readSelectedProcessWithWaitingnumber($selectedProcessId) |
127 | { |
128 | $result = null; |
129 | if ($selectedProcessId) { |
130 | $result = \App::$http->readGetResult('/process/' . $selectedProcessId . '/')->getEntity(); |
131 | } |
132 | return $result; |
133 | } |
134 | |
135 | protected function getProcess($input, $scope) |
136 | { |
137 | $process = new \BO\Zmsentities\Process(); |
138 | $notice = (! $this->isOpened($scope)) ? 'Außerhalb der Öffnungszeiten gebucht! ' : ''; |
139 | return $process->withUpdatedData($input, \App::$now, $scope, $notice); |
140 | } |
141 | |
142 | protected function writeQueuedProcess($input, $process) |
143 | { |
144 | $process = \App::$http->readPostResult('/workstation/process/waitingnumber/', $process)->getEntity(); |
145 | AppointmentFormHelper::updateMailAndNotification($input, $process); |
146 | return $process; |
147 | } |
148 | |
149 | protected function isOpened($scope) |
150 | { |
151 | if ($scope->getResolveLevel() < 1) { |
152 | $scope = \App::$http->readGetResult('/scope/' . $scope->getId() . '/', [ |
153 | 'resolveReferences' => 1, |
154 | 'gql' => Helper\GraphDefaults::getScope() |
155 | ]) |
156 | ->getEntity(); |
157 | } |
158 | try { |
159 | $isOpened = \App::$http |
160 | ->readGetResult('/scope/' . $scope->getId() . '/availability/', ['resolveReferences' => 0]) |
161 | ->getCollection() |
162 | ->withScope($scope) |
163 | ->isOpened(\App::$now); |
164 | } catch (\BO\Zmsclient\Exception $exception) { |
165 | if ($exception->template == 'BO\\Zmsapi\\Exception\\Availability\\AvailabilityNotFound') { |
166 | $isOpened = false; |
167 | } |
168 | } |
169 | return $isOpened; |
170 | } |
171 | |
172 | private function printProcessResponse( |
173 | ResponseInterface $response, |
174 | Entity $process, |
175 | ?string $printType = null, |
176 | ?int $providerId = null |
177 | ): ResponseInterface { |
178 | if ($printType === 'mail') { |
179 | $mergedMailTemplates = \App::$http->readGetResult('/merged-mailtemplates/' . $providerId . '/') |
180 | ->getCollection(); |
181 | |
182 | $templates = []; |
183 | |
184 | foreach ($mergedMailTemplates as $template) { |
185 | $templates[$template->name] = $template->value; |
186 | }; |
187 | |
188 | $templateProvider = new MailTemplateArrayProvider(); |
189 | $templateProvider->setTemplates($templates); |
190 | |
191 | $config = new \BO\Zmsentities\Config(); |
192 | |
193 | $mail = (new \BO\Zmsentities\Mail()) |
194 | ->setTemplateProvider($templateProvider) |
195 | ->toResolvedEntity($process, $config, 'appointment'); |
196 | |
197 | return \BO\Slim\Render::withHtml( |
198 | $response, |
199 | 'page/printAppointmentMail.twig', |
200 | [ |
201 | 'render' => $mail->getHtmlPart() |
202 | ] |
203 | ); |
204 | } |
205 | |
206 | return \BO\Slim\Render::withHtml( |
207 | $response, |
208 | 'page/printWaitingNumber.twig', |
209 | array( |
210 | 'title' => ($process->isWithAppointment()) ? 'Vorgangsnummer drucken' : 'Wartenummer drucken', |
211 | 'process' => $process |
212 | ) |
213 | ); |
214 | } |
215 | } |