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(
176 $trans->member
> 0 ?
$trans->member
: null
179 $params['members'] = [
180 'filters' => $m->getFilters(),
181 'count' => $m->getCount()
183 $params['autocomplete'] = true;
185 if (count($members)) {
186 $params['members']['list'] = $members;
192 'ajouter_transaction.tpl',
201 * @param Request $request PSR Request
202 * @param Response $response PSR Response
203 * @param integer $id Transaction id
204 * @param string $type Transaction type
208 public function doEdit(Request
$request, Response
$response, int $id = null, $type = null): Response
210 $post = $request->getParsedBody();
211 $trans = new Transaction($this->zdb
, $this->login
);
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'];
226 // flagging required fields
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!');
244 $error_detected = [];
246 $valid = $trans->check($_POST, $required, $disabled);
247 if ($valid !== true) {
248 $error_detected = array_merge($error_detected, $valid);
251 if (count($error_detected) == 0) {
252 //all goes well, we can proceed
254 if ($trans->id
== '') {
258 $store = $trans->store($this->history
);
259 if ($store === true) {
260 //transaction has been stored :)
262 $transaction['trans_id'] = $trans->id
;
265 //something went wrong :'(
266 $error_detected[] = _T("An error occurred while storing the transaction.");
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);
277 if (count($error_detected) == 0) {
278 if (isset($post['contrib_type']) && $trans->getMissingAmount() > 0) {
280 'type' => $post['contrib_type']
283 if (isset($trans->member
)) {
284 $rparams['id_adh'] = $trans->member
;
291 $this->router
->pathFor(
294 ) . '?' . Transaction
::PK
. '=' . $trans->id
.
295 '&' . Adherent
::PK
. '=' . $trans->member
299 $this->flash
->addMessage(
301 _T("Transaction has been successfully stored")
304 //get back to transactions list
309 $this->router
->pathFor('contributions', ['type' => 'transactions'])
313 //something went wrong.
314 //store entity in session
315 $this->session
->transaction
= $trans;
318 foreach ($error_detected as $error) {
319 $this->flash
->addMessage(
326 if ($trans_id !== null) {
329 //redirect to calling action
334 $this->router
->pathFor(
335 ($action == 'add' ?
'addTransaction' : 'editTransaction'),
345 * @param Request $request PSR Request
346 * @param Response $response PSR Response
347 * @param integer $id Transaction id
348 * @param integer $cid Contribution id
352 public function attach(Request
$request, Response
$response, int $id = null, int $cid = null): Response
354 if (!Contribution
::setTransactionPart($this->zdb
, $id, $cid)) {
355 $this->flash
->addMessage(
357 _T("Unable to attach contribution to transaction")
360 $this->flash
->addMessage(
362 _T("Contribution has been successfully attached to current transaction")
368 ->withHeader('Location', $this->router
->pathFor(
377 * @param Request $request PSR Request
378 * @param Response $response PSR Response
379 * @param integer $id Transaction id
380 * @param integer $cid Contribution id
384 public function detach(Request
$request, Response
$response, int $id = null, int $cid = null): Response
386 if (!Contribution
::unsetTransactionPart($this->zdb
, $this->login
, $id, $cid)) {
387 $this->flash
->addMessage(
389 _T("Unable to detach contribution from transaction")
392 $this->flash
->addMessage(
394 _T("Contribution has been successfully detached from current transaction")
400 ->withHeader('Location', $this->router
->pathFor(