3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Galette transactions controller
10 * Copyright © 2020-2021 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-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
37 namespace Galette\Controllers\Crud
;
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
;
51 * Galette transactions controller
53 * @category Controllers
54 * @name TransactionsController
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
63 class TransactionsController
extends ContributionsController
70 * @param Request $request PSR Request
71 * @param Response $response PSR Response
72 * @param string $type Contribution type
76 public function add(Request
$request, Response
$response, string $type = null): Response
78 return $this->edit($request, $response, null, 'add');
84 * @param Request $request PSR Request
85 * @param Response $response PSR Response
86 * @param string $type Contribution type
90 public function doAdd(Request
$request, Response
$response, string $type = null): Response
92 return $this->doEdit($request, $response, $type);
98 //ContributionsController manages both lists and filter
106 * @param Request $request PSR Request
107 * @param Response $response PSR Response
108 * @param integer $id Transaction id
109 * @param string $action Action
113 public function edit(Request
$request, Response
$response, int $id = null, $action = 'edit'): 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
);
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", '');
135 // flagging required fields
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!');
152 // template variable declaration
153 $title = _T("Transaction");
154 if ($action === 'edit') {
155 $title .= ' (' . _T("modification") . ')';
157 $title .= ' (' . _T("creation") . ')';
161 'page_title' => $title,
162 'required' => $required,
163 'data' => $transaction, //TODO: remove
164 'transaction' => $trans
167 if ($trans->id
!= '') {
168 $contribs = new Contributions($this->zdb
, $this->login
);
169 $params['contribs'] = $contribs->getListFromTransaction($trans->id
);
174 $members = $m->getSelectizedMembers(
177 $trans->member
> 0 ?
$trans->member
: null
180 $params['members'] = [
181 'filters' => $m->getFilters(),
182 'count' => $m->getCount()
184 $params['autocomplete'] = true;
186 if (count($members)) {
187 $params['members']['list'] = $members;
193 'ajouter_transaction.tpl',
202 * @param Request $request PSR Request
203 * @param Response $response PSR Response
204 * @param integer $id Transaction id
205 * @param string $type Transaction type
209 public function doEdit(Request
$request, Response
$response, int $id = null, $type = null): Response
211 $post = $request->getParsedBody();
212 $trans = new Transaction($this->zdb
, $this->login
);
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'];
227 // flagging required fields
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!');
245 $error_detected = [];
247 $valid = $trans->check($_POST, $required, $disabled);
248 if ($valid !== true) {
249 $error_detected = array_merge($error_detected, $valid);
252 if (count($error_detected) == 0) {
253 //all goes well, we can proceed
255 if ($trans->id
== '') {
259 $store = $trans->store($this->history
);
260 if ($store === true) {
261 //transaction has been stored :)
263 $transaction['trans_id'] = $trans->id
;
266 //something went wrong :'(
267 $error_detected[] = _T("An error occurred while storing the transaction.");
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);
278 if (count($error_detected) == 0) {
279 if (isset($post['contrib_type']) && $trans->getMissingAmount() > 0) {
281 'type' => $post['contrib_type']
284 if (isset($trans->member
)) {
285 $rparams['id_adh'] = $trans->member
;
292 $this->router
->pathFor(
295 ) . '?' . Transaction
::PK
. '=' . $trans->id
.
296 '&' . Adherent
::PK
. '=' . $trans->member
300 $this->flash
->addMessage(
302 _T("Transaction has been successfully stored")
305 //get back to transactions list
310 $this->router
->pathFor('contributions', ['type' => 'transactions'])
314 //something went wrong.
315 //store entity in session
316 $this->session
->transaction
= $trans;
319 foreach ($error_detected as $error) {
320 $this->flash
->addMessage(
327 if ($trans_id !== null) {
330 //redirect to calling action
335 $this->router
->pathFor(
336 ($action == 'add' ?
'addTransaction' : 'editTransaction'),
346 * @param Request $request PSR Request
347 * @param Response $response PSR Response
348 * @param integer $id Transaction id
349 * @param integer $cid Contribution id
353 public function attach(Request
$request, Response
$response, int $id = null, int $cid = null): Response
355 if (!Contribution
::setTransactionPart($this->zdb
, $id, $cid)) {
356 $this->flash
->addMessage(
358 _T("Unable to attach contribution to transaction")
361 $this->flash
->addMessage(
363 _T("Contribution has been successfully attached to current transaction")
369 ->withHeader('Location', $this->router
->pathFor(
378 * @param Request $request PSR Request
379 * @param Response $response PSR Response
380 * @param integer $id Transaction id
381 * @param integer $cid Contribution id
385 public function detach(Request
$request, Response
$response, int $id = null, int $cid = null): Response
387 if (!Contribution
::unsetTransactionPart($this->zdb
, $this->login
, $id, $cid)) {
388 $this->flash
->addMessage(
390 _T("Unable to detach contribution from transaction")
393 $this->flash
->addMessage(
395 _T("Contribution has been successfully detached from current transaction")
401 ->withHeader('Location', $this->router
->pathFor(