3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette transactions controller
10 * Copyright © 2020 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
27 * @category Controllers
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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.9.4dev - 2020-05-08
38 namespace Galette\Controllers\Crud
;
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
;
52 * Galette transactions controller
54 * @category Controllers
55 * @name TransactionsController
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
64 class TransactionsController
extends ContributionsController
71 * @param Request $request PSR Request
72 * @param Response $response PSR Response
73 * @param array $args Request arguments
77 public function add(Request
$request, Response
$response, array $args = []): Response
79 return $this->edit($request, $response, $args);
85 * @param Request $request PSR Request
86 * @param Response $response PSR Response
87 * @param array $args Request arguments
91 public function doAdd(Request
$request, Response
$response, array $args = []): Response
93 return $this->doEdit($request, $response, $args);
99 //ContributionsController manages both lists and filter
107 * @param Request $request PSR Request
108 * @param Response $response PSR Response
109 * @param array $args Request arguments
113 public function edit(Request
$request, Response
$response, array $args = []): Response
117 if ($this->session
->transaction
!== null) {
118 $trans = $this->session
->transaction
;
119 $this->session
->transaction
= null;
121 $trans = new Transaction($this->zdb
, $this->login
);
124 $action = $args['action'];
126 if (isset($args['id'])) {
127 $trans_id = $args['id'];
130 if ($action === 'edit' && $trans_id === null) {
131 throw new \
RuntimeException(
132 _T("Transaction ID cannot ben null calling edit route!")
134 } elseif ($action === 'add' && $trans_id !== null) {
137 ->withHeader('Location', $this->router
->pathFor('transaction', ['action' => 'add']));
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", '');
146 // flagging required fields
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!');
164 // template variable declaration
165 $title = _T("Transaction");
166 if ($action === 'edit') {
167 $title .= ' (' . _T("modification") . ')';
169 $title .= ' (' . _T("creation") . ')';
173 'page_title' => $title,
174 'required' => $required,
175 'data' => $transaction, //TODO: remove
176 'transaction' => $trans
179 if ($trans->id
!= '') {
180 $contribs = new Contributions($this->zdb
, $this->login
);
181 $params['contribs'] = $contribs->getListFromTransaction($trans->id
);
186 $members = $m->getSelectizedMembers(
188 $trans->member
> 0 ?
$trans->member
: null
191 $params['members'] = [
192 'filters' => $m->getFilters(),
193 'count' => $m->getCount()
195 $params['autocomplete'] = true;
197 if (count($members)) {
198 $params['members']['list'] = $members;
204 'ajouter_transaction.tpl',
213 * @param Request $request PSR Request
214 * @param Response $response PSR Response
215 * @param array $args Request arguments
219 public function doEdit(Request
$request, Response
$response, array $args = []): Response
221 $post = $request->getParsedBody();
222 $trans = new Transaction($this->zdb
, $this->login
);
224 $action = $args['action'];
226 if (isset($args['id'])) {
227 $trans_id = $args['id'];
230 if ($action === 'edit' && $trans_id === null) {
231 throw new \
RuntimeException(
232 _T("Transaction ID cannot ben null calling edit route!")
234 } elseif ($action === 'add' && $trans_id !== null) {
235 throw new \
RuntimeException(
236 _T("Transaction ID cannot ben set while adding!")
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'];
246 // flagging required fields
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!');
264 $error_detected = [];
266 $valid = $trans->check($_POST, $required, $disabled);
267 if ($valid !== true) {
268 $error_detected = array_merge($error_detected, $valid);
271 if (count($error_detected) == 0) {
272 //all goes well, we can proceed
274 if ($trans->id
== '') {
278 $store = $trans->store($this->history
);
279 if ($store === true) {
280 //transaction has been stored :)
282 $transaction['trans_id'] = $trans->id
;
285 //something went wrong :'(
286 $error_detected[] = _T("An error occurred while storing the transaction.");
290 if (count($error_detected) == 0) {
291 if ($trans->getMissingAmount() > 0) {
293 'type' => $post['contrib_type']
296 if (isset($trans->member
)) {
297 $rparams['id_adh'] = $trans->member
;
304 $this->router
->pathFor(
307 ) . '?' . Transaction
::PK
. '=' . $trans->id
.
308 '&' . Adherent
::PK
. '=' . $trans->member
312 $this->flash
->addMessage(
314 _T("Transaction has been successfully stored")
317 //get back to transactions list
322 $this->router
->pathFor('contributions', ['type' => 'transactions'])
326 //something went wrong.
327 //store entity in session
328 $this->session
->transaction
= $trans;
331 foreach ($error_detected as $error) {
332 $this->flash
->addMessage(
338 //redirect to calling action
341 ->withHeader('Location', $this->router
->pathFor('transaction', $args));
346 ->withHeader('Location', $this->router
->pathFor('contributions', ['type' => 'transactions']));
352 * @param Request $request PSR Request
353 * @param Response $response PSR Response
354 * @param array $args Request arguments
358 public function attach(Request
$request, Response
$response, array $args = []): Response
360 if (!Contribution
::setTransactionPart($this->zdb
, $args['id'], $args['cid'])) {
361 $this->flash
->addMessage(
363 _T("Unable to attach contribution to transaction")
366 $this->flash
->addMessage(
368 _T("Contribution has been successfully attached to current transaction")
374 ->withHeader('Location', $this->router
->pathFor(
376 ['action' => 'edit', 'id' => $args['id']]
383 * @param Request $request PSR Request
384 * @param Response $response PSR Response
385 * @param array $args Request arguments
389 public function detach(Request
$request, Response
$response, array $args = []): Response
391 if (!Contribution
::unsetTransactionPart($this->zdb
, $this->login
, $args['id'], $args['cid'])) {
392 $this->flash
->addMessage(
394 _T("Unable to detach contribution from transaction")
397 $this->flash
->addMessage(
399 _T("Contribution has been successfully detached from current transaction")
405 ->withHeader('Location', $this->router
->pathFor(
407 ['action' => 'edit', 'id' => $args['id']]