]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/TransactionsController.php
Rework getSelectizedMembers to take care of group managers
[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 $this->login,
177 $trans->member > 0 ? $trans->member : null
178 );
179
180 $params['members'] = [
181 'filters' => $m->getFilters(),
182 'count' => $m->getCount()
183 ];
184 $params['autocomplete'] = true;
185
186 if (count($members)) {
187 $params['members']['list'] = $members;
188 }
189
190 // display page
191 $this->view->render(
192 $response,
193 'ajouter_transaction.tpl',
194 $params
195 );
196 return $response;
197 }
198
199 /**
200 * Edit action
201 *
202 * @param Request $request PSR Request
203 * @param Response $response PSR Response
204 * @param integer $id Transaction id
205 * @param string $type Transaction type
206 *
207 * @return Response
208 */
209 public function doEdit(Request $request, Response $response, int $id = null, $type = null): Response
210 {
211 $post = $request->getParsedBody();
212 $trans = new Transaction($this->zdb, $this->login);
213
214 $action = 'add';
215 $trans_id = null;
216 if ($id !== null) {
217 $action = 'edit';
218 $trans_id = $id;
219 }
220
221 $transaction['trans_id'] = $trans_id;
222 $transaction['trans_amount'] = $post['trans_amount'];
223 $transaction['trans_date'] = $post['trans_date'];
224 $transaction['trans_desc'] = $post['trans_desc'];
225 $transaction['id_adh'] = $post['id_adh'];
226
227 // flagging required fields
228 $required = array(
229 'trans_amount' => 1,
230 'trans_date' => 1,
231 'trans_desc' => 1,
232 'id_adh' => 1
233 );
234 $disabled = array();
235
236 if ($action === 'edit') {
237 // initialize transactions structure with database values
238 $trans->load($trans_id);
239 if ($trans->id == '') {
240 //not possible to load transaction, exit
241 throw new \RuntimeException('Transaction does not exists!');
242 }
243 }
244
245 $error_detected = [];
246 // regular fields
247 $valid = $trans->check($_POST, $required, $disabled);
248 if ($valid !== true) {
249 $error_detected = array_merge($error_detected, $valid);
250 }
251
252 if (count($error_detected) == 0) {
253 //all goes well, we can proceed
254 $new = false;
255 if ($trans->id == '') {
256 $new = true;
257 }
258
259 $store = $trans->store($this->history);
260 if ($store === true) {
261 //transaction has been stored :)
262 if ($new) {
263 $transaction['trans_id'] = $trans->id;
264 }
265 } else {
266 //something went wrong :'(
267 $error_detected[] = _T("An error occurred while storing the transaction.");
268 }
269 }
270
271 if (count($error_detected) === 0) {
272 $files_res = $trans->handleFiles($_FILES);
273 if (is_array($files_res)) {
274 $error_detected = array_merge($error_detected, $files_res);
275 }
276 }
277
278 if (count($error_detected) == 0) {
279 if (isset($post['contrib_type']) && $trans->getMissingAmount() > 0) {
280 $rparams = [
281 'type' => $post['contrib_type']
282 ];
283
284 if (isset($trans->member)) {
285 $rparams['id_adh'] = $trans->member;
286 }
287
288 return $response
289 ->withStatus(301)
290 ->withHeader(
291 'Location',
292 $this->router->pathFor(
293 'addContribution',
294 $rparams
295 ) . '?' . Transaction::PK . '=' . $trans->id .
296 '&' . Adherent::PK . '=' . $trans->member
297 );
298 } else {
299 //report success
300 $this->flash->addMessage(
301 'success_detected',
302 _T("Transaction has been successfully stored")
303 );
304
305 //get back to transactions list
306 return $response
307 ->withStatus(301)
308 ->withHeader(
309 'Location',
310 $this->router->pathFor('contributions', ['type' => 'transactions'])
311 );
312 }
313 } else {
314 //something went wrong.
315 //store entity in session
316 $this->session->transaction = $trans;
317
318 //report errors
319 foreach ($error_detected as $error) {
320 $this->flash->addMessage(
321 'error_detected',
322 $error
323 );
324 }
325
326 $args = [];
327 if ($trans_id !== null) {
328 $args['id'] = $id;
329 }
330 //redirect to calling action
331 return $response
332 ->withStatus(301)
333 ->withHeader(
334 'Location',
335 $this->router->pathFor(
336 ($action == 'add' ? 'addTransaction' : 'editTransaction'),
337 $args
338 )
339 );
340 }
341 }
342
343 /**
344 * Attach action
345 *
346 * @param Request $request PSR Request
347 * @param Response $response PSR Response
348 * @param integer $id Transaction id
349 * @param integer $cid Contribution id
350 *
351 * @return Response
352 */
353 public function attach(Request $request, Response $response, int $id = null, int $cid = null): Response
354 {
355 if (!Contribution::setTransactionPart($this->zdb, $id, $cid)) {
356 $this->flash->addMessage(
357 'error_detected',
358 _T("Unable to attach contribution to transaction")
359 );
360 } else {
361 $this->flash->addMessage(
362 'success_detected',
363 _T("Contribution has been successfully attached to current transaction")
364 );
365 }
366
367 return $response
368 ->withStatus(301)
369 ->withHeader('Location', $this->router->pathFor(
370 'editTransaction',
371 ['id' => $id]
372 ));
373 }
374
375 /**
376 * Attach action
377 *
378 * @param Request $request PSR Request
379 * @param Response $response PSR Response
380 * @param integer $id Transaction id
381 * @param integer $cid Contribution id
382 *
383 * @return Response
384 */
385 public function detach(Request $request, Response $response, int $id = null, int $cid = null): Response
386 {
387 if (!Contribution::unsetTransactionPart($this->zdb, $this->login, $id, $cid)) {
388 $this->flash->addMessage(
389 'error_detected',
390 _T("Unable to detach contribution from transaction")
391 );
392 } else {
393 $this->flash->addMessage(
394 'success_detected',
395 _T("Contribution has been successfully detached from current transaction")
396 );
397 }
398
399 return $response
400 ->withStatus(301)
401 ->withHeader('Location', $this->router->pathFor(
402 'editTransaction',
403 ['id' => $id]
404 ));
405 }
406
407 // /CRUD - Update
408 // CRUD - Delete
409
410 //all inherited
411
412 // /CRUD - Delete
413 // /CRUD
414 }