]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/TransactionsController.php
094381001b50084604ad70cef08a83d8d0e3ed4c
[galette.git] / galette / lib / Galette / Controllers / Crud / TransactionsController.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Galette transactions controller
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Controllers
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-08
36 */
37
38 namespace Galette\Controllers\Crud;
39
40 use Galette\Controllers\CrudController;
41 use Slim\Http\Request;
42 use Slim\Http\Response;
43 use Galette\Entity\Adherent;
44 use Galette\Entity\Contribution;
45 use Galette\Entity\Transaction;
46 use Galette\Repository\Contributions;
47 use Galette\Repository\Members;
48 use Galette\Repository\Transactions;
49 use Analog\Analog;
50
51 /**
52 * Galette transactions controller
53 *
54 * @category Controllers
55 * @name TransactionsController
56 * @package Galette
57 * @author Johan Cwiklinski <johan@x-tnd.be>
58 * @copyright 2020 The Galette Team
59 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
60 * @link http://galette.tuxfamily.org
61 * @since Available since 0.9.4dev - 2020-05-02
62 */
63
64 class TransactionsController extends ContributionsController
65 {
66 // CRUD - Create
67
68 /**
69 * Add page
70 *
71 * @param Request $request PSR Request
72 * @param Response $response PSR Response
73 * @param array $args Request arguments
74 *
75 * @return Response
76 */
77 public function add(Request $request, Response $response, array $args = []): Response
78 {
79 return $this->edit($request, $response, $args);
80 }
81
82 /**
83 * Add action
84 *
85 * @param Request $request PSR Request
86 * @param Response $response PSR Response
87 * @param array $args Request arguments
88 *
89 * @return Response
90 */
91 public function doAdd(Request $request, Response $response, array $args = []): Response
92 {
93 return $this->doEdit($request, $response, $args);
94 }
95
96 // /CRUD - Create
97 // CRUD - Read
98
99 //ContributionsController manages both lists and filter
100
101 // /CRUD - Read
102 // CRUD - Update
103
104 /**
105 * Edit page
106 *
107 * @param Request $request PSR Request
108 * @param Response $response PSR Response
109 * @param array $args Request arguments
110 *
111 * @return Response
112 */
113 public function edit(Request $request, Response $response, array $args = []): Response
114 {
115 $trans = null;
116
117 if ($this->session->transaction !== null) {
118 $trans = $this->session->transaction;
119 $this->session->transaction = null;
120 } else {
121 $trans = new Transaction($this->zdb, $this->login);
122 }
123
124 $action = $args['action'];
125 $trans_id = null;
126 if (isset($args['id'])) {
127 $trans_id = $args['id'];
128 }
129
130 if ($action === 'edit' && $trans_id === null) {
131 throw new \RuntimeException(
132 _T("Transaction ID cannot ben null calling edit route!")
133 );
134 } elseif ($action === 'add' && $trans_id !== null) {
135 return $response
136 ->withStatus(301)
137 ->withHeader('Location', $this->router->pathFor('transaction', ['action' => 'add']));
138 }
139
140 $transaction['trans_id'] = $trans_id;
141 $transaction['trans_amount'] = get_numeric_form_value("trans_amount", '');
142 $transaction['trans_date'] = get_form_value("trans_date", '');
143 $transaction['trans_desc'] = get_form_value("trans_desc", '');
144 $transaction['id_adh'] = get_numeric_form_value("id_adh", '');
145
146 // flagging required fields
147 $required = array(
148 'trans_amount' => 1,
149 'trans_date' => 1,
150 'trans_desc' => 1,
151 'id_adh' => 1
152 );
153 $disabled = array();
154
155 if ($action === 'edit') {
156 // initialize transactions structure with database values
157 $trans->load($trans_id);
158 if ($trans->id == '') {
159 //not possible to load transaction, exit
160 throw new \RuntimeException('Transaction does not exists!');
161 }
162 }
163
164 // template variable declaration
165 $title = _T("Transaction");
166 if ($action === 'edit') {
167 $title .= ' (' . _T("modification") . ')';
168 } else {
169 $title .= ' (' . _T("creation") . ')';
170 }
171
172 $params = [
173 'page_title' => $title,
174 'required' => $required,
175 'data' => $transaction, //TODO: remove
176 'transaction' => $trans
177 ];
178
179 if ($trans->id != '') {
180 $contribs = new Contributions($this->zdb, $this->login);
181 $params['contribs'] = $contribs->getListFromTransaction($trans->id);
182 }
183
184 // members
185 $m = new Members();
186 $members = $m->getSelectizedMembers(
187 $this->zdb,
188 $trans->member > 0 ? $trans->member : null
189 );
190
191 $params['members'] = [
192 'filters' => $m->getFilters(),
193 'count' => $m->getCount()
194 ];
195 $params['autocomplete'] = true;
196
197 if (count($members)) {
198 $params['members']['list'] = $members;
199 }
200
201 // display page
202 $this->view->render(
203 $response,
204 'ajouter_transaction.tpl',
205 $params
206 );
207 return $response;
208 }
209
210 /**
211 * Edit action
212 *
213 * @param Request $request PSR Request
214 * @param Response $response PSR Response
215 * @param array $args Request arguments
216 *
217 * @return Response
218 */
219 public function doEdit(Request $request, Response $response, array $args = []): Response
220 {
221 $post = $request->getParsedBody();
222 $trans = new Transaction($this->zdb, $this->login);
223
224 $action = $args['action'];
225 $trans_id = null;
226 if (isset($args['id'])) {
227 $trans_id = $args['id'];
228 }
229
230 if ($action === 'edit' && $trans_id === null) {
231 throw new \RuntimeException(
232 _T("Transaction ID cannot ben null calling edit route!")
233 );
234 } elseif ($action === 'add' && $trans_id !== null) {
235 throw new \RuntimeException(
236 _T("Transaction ID cannot ben set while adding!")
237 );
238 }
239
240 $transaction['trans_id'] = $trans_id;
241 $transaction['trans_amount'] = $post['trans_amount'];
242 $transaction['trans_date'] = $post['trans_date'];
243 $transaction['trans_desc'] = $post['trans_desc'];
244 $transaction['id_adh'] = $post['id_adh'];
245
246 // flagging required fields
247 $required = array(
248 'trans_amount' => 1,
249 'trans_date' => 1,
250 'trans_desc' => 1,
251 'id_adh' => 1
252 );
253 $disabled = array();
254
255 if ($action === 'edit') {
256 // initialize transactions structure with database values
257 $trans->load($trans_id);
258 if ($trans->id == '') {
259 //not possible to load transaction, exit
260 throw new \RuntimeException('Transaction does not exists!');
261 }
262 }
263
264 $error_detected = [];
265 // regular fields
266 $valid = $trans->check($_POST, $required, $disabled);
267 if ($valid !== true) {
268 $error_detected = array_merge($error_detected, $valid);
269 }
270
271 if (count($error_detected) == 0) {
272 //all goes well, we can proceed
273 $new = false;
274 if ($trans->id == '') {
275 $new = true;
276 }
277
278 $store = $trans->store($this->history);
279 if ($store === true) {
280 //transaction has been stored :)
281 if ($new) {
282 $transaction['trans_id'] = $trans->id;
283 }
284 } else {
285 //something went wrong :'(
286 $error_detected[] = _T("An error occurred while storing the transaction.");
287 }
288 }
289
290 if (count($error_detected) == 0) {
291 if ($trans->getMissingAmount() > 0) {
292 $rparams = [
293 'type' => $post['contrib_type']
294 ];
295
296 if (isset($trans->member)) {
297 $rparams['id_adh'] = $trans->member;
298 }
299
300 return $response
301 ->withStatus(301)
302 ->withHeader(
303 'Location',
304 $this->router->pathFor(
305 'addContribution',
306 $rparams
307 ) . '?' . Transaction::PK . '=' . $trans->id .
308 '&' . Adherent::PK . '=' . $trans->member
309 );
310 } else {
311 //report success
312 $this->flash->addMessage(
313 'success_detected',
314 _T("Transaction has been successfully stored")
315 );
316
317 //get back to transactions list
318 return $response
319 ->withStatus(301)
320 ->withHeader(
321 'Location',
322 $this->router->pathFor('contributions', ['type' => 'transactions'])
323 );
324 }
325 } else {
326 //something went wrong.
327 //store entity in session
328 $this->session->transaction = $trans;
329
330 //report errors
331 foreach ($error_detected as $error) {
332 $this->flash->addMessage(
333 'error_detected',
334 $error
335 );
336 }
337
338 //redirect to calling action
339 return $response
340 ->withStatus(301)
341 ->withHeader('Location', $this->router->pathFor('transaction', $args));
342 }
343
344 return $response
345 ->withStatus(301)
346 ->withHeader('Location', $this->router->pathFor('contributions', ['type' => 'transactions']));
347 }
348
349 /**
350 * Attach action
351 *
352 * @param Request $request PSR Request
353 * @param Response $response PSR Response
354 * @param array $args Request arguments
355 *
356 * @return Response
357 */
358 public function attach(Request $request, Response $response, array $args = []): Response
359 {
360 if (!Contribution::setTransactionPart($this->zdb, $args['id'], $args['cid'])) {
361 $this->flash->addMessage(
362 'error_detected',
363 _T("Unable to attach contribution to transaction")
364 );
365 } else {
366 $this->flash->addMessage(
367 'success_detected',
368 _T("Contribution has been successfully attached to current transaction")
369 );
370 }
371
372 return $response
373 ->withStatus(301)
374 ->withHeader('Location', $this->router->pathFor(
375 'transaction',
376 ['action' => 'edit', 'id' => $args['id']]
377 ));
378 }
379
380 /**
381 * Attach action
382 *
383 * @param Request $request PSR Request
384 * @param Response $response PSR Response
385 * @param array $args Request arguments
386 *
387 * @return Response
388 */
389 public function detach(Request $request, Response $response, array $args = []): Response
390 {
391 if (!Contribution::unsetTransactionPart($this->zdb, $this->login, $args['id'], $args['cid'])) {
392 $this->flash->addMessage(
393 'error_detected',
394 _T("Unable to detach contribution from transaction")
395 );
396 } else {
397 $this->flash->addMessage(
398 'success_detected',
399 _T("Contribution has been successfully detached from current transaction")
400 );
401 }
402
403 return $response
404 ->withStatus(301)
405 ->withHeader('Location', $this->router->pathFor(
406 'transaction',
407 ['action' => 'edit', 'id' => $args['id']]
408 ));
409 }
410
411 // /CRUD - Update
412 // CRUD - Delete
413
414 //all inherited
415
416 // /CRUD - Delete
417 // /CRUD
418 }