]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Controllers/Crud/TransactionsController.php
e5062d11cbdc6133649d90966dd21c161c805f4e
[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 * @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 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 if ($trans->getMissingAmount() > 0) {
272 $rparams = [
273 'type' => $post['contrib_type']
274 ];
275
276 if (isset($trans->member)) {
277 $rparams['id_adh'] = $trans->member;
278 }
279
280 return $response
281 ->withStatus(301)
282 ->withHeader(
283 'Location',
284 $this->router->pathFor(
285 'addContribution',
286 $rparams
287 ) . '?' . Transaction::PK . '=' . $trans->id .
288 '&' . Adherent::PK . '=' . $trans->member
289 );
290 } else {
291 //report success
292 $this->flash->addMessage(
293 'success_detected',
294 _T("Transaction has been successfully stored")
295 );
296
297 //get back to transactions list
298 return $response
299 ->withStatus(301)
300 ->withHeader(
301 'Location',
302 $this->router->pathFor('contributions', ['type' => 'transactions'])
303 );
304 }
305 } else {
306 //something went wrong.
307 //store entity in session
308 $this->session->transaction = $trans;
309
310 //report errors
311 foreach ($error_detected as $error) {
312 $this->flash->addMessage(
313 'error_detected',
314 $error
315 );
316 }
317
318 $args = [];
319 if ($trans_id !== null) {
320 $args['id'] = $id;
321 }
322 //redirect to calling action
323 return $response
324 ->withStatus(301)
325 ->withHeader(
326 'Location',
327 $this->router->pathFor(
328 ($action == 'add' ? 'addTransaction' : 'editTransaction'),
329 $args
330 )
331 );
332 }
333 }
334
335 /**
336 * Attach action
337 *
338 * @param Request $request PSR Request
339 * @param Response $response PSR Response
340 * @param integer $id Transaction id
341 * @param integer $cid Contribution id
342 *
343 * @return Response
344 */
345 public function attach(Request $request, Response $response, int $id = null, int $cid = null): Response
346 {
347 if (!Contribution::setTransactionPart($this->zdb, $id, $cid)) {
348 $this->flash->addMessage(
349 'error_detected',
350 _T("Unable to attach contribution to transaction")
351 );
352 } else {
353 $this->flash->addMessage(
354 'success_detected',
355 _T("Contribution has been successfully attached to current transaction")
356 );
357 }
358
359 return $response
360 ->withStatus(301)
361 ->withHeader('Location', $this->router->pathFor(
362 'editTransaction',
363 ['id' => $id]
364 ));
365 }
366
367 /**
368 * Attach action
369 *
370 * @param Request $request PSR Request
371 * @param Response $response PSR Response
372 * @param integer $id Transaction id
373 * @param integer $cid Contribution id
374 *
375 * @return Response
376 */
377 public function detach(Request $request, Response $response, int $id = null, int $cid = null): Response
378 {
379 if (!Contribution::unsetTransactionPart($this->zdb, $this->login, $id, $cid)) {
380 $this->flash->addMessage(
381 'error_detected',
382 _T("Unable to detach contribution from transaction")
383 );
384 } else {
385 $this->flash->addMessage(
386 'success_detected',
387 _T("Contribution has been successfully detached from current transaction")
388 );
389 }
390
391 return $response
392 ->withStatus(301)
393 ->withHeader('Location', $this->router->pathFor(
394 'editTransaction',
395 ['id' => $id]
396 ));
397 }
398
399 // /CRUD - Update
400 // CRUD - Delete
401
402 //all inherited
403
404 // /CRUD - Delete
405 // /CRUD
406 }