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