3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2011-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 Repository
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2011-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.7dev - 2011-07-31
37 namespace Galette\Repository
;
41 use Laminas\Db\Sql\Expression
;
42 use Galette\Entity\Transaction
;
43 use Galette\Entity\Adherent
;
45 use Galette\Core\Login
;
46 use Galette\Core\History
;
47 use Galette\Filters\TransactionsList
;
50 * Transactions class for galette
53 * @category Repository
56 * @author Johan Cwiklinski <johan@x-tnd.be>
57 * @copyright 2011-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
63 public const TABLE
= Transaction
::TABLE
;
64 public const PK
= Transaction
::PK
;
66 private $count = null;
74 * @param Db $zdb Database
75 * @param Login $login Login
76 * @param TransactionsList $filters Filtering
78 public function __construct(Db
$zdb, Login
$login, $filters = null)
81 $this->login
= $login;
83 if ($filters === null) {
84 $this->filters
= new TransactionsList();
86 $this->filters
= $filters;
91 * Get transactions list
93 * @param bool $as_trans return the results as an array of
95 * @param array $fields field(s) name(s) to get. Should be a string or
96 * an array. If null, all fields will be returned
97 * @param boolean $count true if we want to count members
99 * @return Transaction[]|ResultSet
101 public function getList($as_trans = false, $fields = null, $count = true)
104 $select = $this->buildSelect($fields, $count);
105 $this->filters
->setLimits($select);
107 $transactions = array();
108 $results = $this->zdb
->execute($select);
110 foreach ($results as $row) {
111 $transactions[] = new Transaction($this->zdb
, $this->login
, $row);
114 $transactions = $results;
116 return $transactions;
117 } catch (Throwable
$e) {
119 'Cannot list transactions | ' . $e->getMessage(),
127 * Builds the SELECT statement
129 * @param array $fields fields list to retrieve
130 * @param bool $count true if we want to count members
131 * (not applicable from static calls), defaults to false
133 * @return string SELECT statement
135 private function buildSelect($fields, $count = false)
138 $fieldsList = ($fields != null)
139 ?
((!is_array($fields) ||
count($fields) < 1) ?
(array)'*'
140 : implode(', ', $fields)) : (array)'*';
142 $select = $this->zdb
->select(self
::TABLE
, 't');
152 array('a' => PREFIX_DB
. Adherent
::TABLE
),
153 't.' . Adherent
::PK
. '=' . 'a.' . Adherent
::PK
,
154 array('nom_adh', 'prenom_adh')
157 $this->buildWhereClause($select);
158 $select->order(self
::buildOrderClause());
161 $this->proceedCount($select);
165 } catch (Throwable
$e) {
167 'Cannot build SELECT clause for transactions | ' . $e->getMessage(),
175 * Count transactions from the query
177 * @param Select $select Original select
181 private function proceedCount($select)
184 $countSelect = clone $select;
185 $countSelect->reset($countSelect::COLUMNS
);
186 $countSelect->reset($countSelect::ORDER
);
187 $countSelect->reset($countSelect::JOINS
);
188 $countSelect->columns(
190 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
194 $results = $this->zdb
->execute($countSelect);
195 $result = $results->current();
198 $this->count
= $result->$k;
199 if ($this->count
> 0) {
200 $this->filters
->setCounter($this->count
);
202 } catch (Throwable
$e) {
204 'Cannot count transactions | ' . $e->getMessage(),
212 * Builds the order clause
214 * @return string SQL ORDER clause
216 private function buildOrderClause()
220 switch ($this->filters
->orderby
) {
221 case TransactionsList
::ORDERBY_ID
:
222 $order[] = Transaction
::PK
. ' ' . $this->filters
->ordered
;
224 case TransactionsList
::ORDERBY_DATE
:
225 $order[] = 'trans_date' . ' ' . $this->filters
->ordered
;
227 case TransactionsList
::ORDERBY_MEMBER
:
228 $order[] = 'nom_adh' . ' ' . $this->filters
->ordered
;
229 $order[] = 'prenom_adh' . ' ' . $this->filters
->ordered
;
231 case TransactionsList
::ORDERBY_AMOUNT
:
232 $order[] = 'trans_amount' . ' ' . $this->filters
->ordered
;
235 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->ordered
;
243 * Builds where clause, for filtering on simple list mode
245 * @param Select $select Original select
247 * @return string SQL WHERE clause
249 private function buildWhereClause($select)
252 if ($this->filters
->start_date_filter
!= null) {
253 $d = new \
DateTime($this->filters
->start_date_filter
);
254 $select->where
->greaterThanOrEqualTo(
260 if ($this->filters
->end_date_filter
!= null) {
261 $d = new \
DateTime($this->filters
->end_date_filter
);
262 $select->where
->lessThanOrEqualTo(
268 $member_clause = null;
269 if ($this->filters
->filtre_cotis_adh
!= null) {
270 $member_clause = [$this->filters
->filtre_cotis_adh
];
271 if (!$this->login
->isAdmin() && !$this->login
->isStaff() && $this->filters
->filtre_cotis_adh
!= $this->login
->id
) {
272 $member = new Adherent(
274 (int)$this->filters
->filtre_cotis_adh
,
283 !$member->hasParent() ||
284 $member->hasParent() && $member->parent
->id
!= $this->login
->id
287 'Trying to display transactions for member #' . $member->id
.
288 ' without appropriate ACLs',
291 $member_clause = [$this->login
->id
];
294 } elseif ($this->filters
->filtre_cotis_children
!== false) {
295 $member_clause = [$this->login
->id
];
296 $member = new Adherent(
298 (int)$this->filters
->filtre_cotis_children
,
306 foreach ($member->children
as $child) {
307 $member_clause[] = $child->id
;
309 } elseif (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
310 $member_clause = $this->login
->id
;
313 if ($member_clause !== null) {
316 't.' . Adherent
::PK
=> $member_clause
320 } catch (Throwable
$e) {
322 __METHOD__
. ' | ' . $e->getMessage(),
329 * Get count for current query
333 public function getCount()
339 * Remove specified transactions
341 * @param interger|array $ids Transactions identifiers to delete
342 * @param History $hist History
346 public function remove($ids, History
$hist)
349 if (is_numeric($ids)) {
350 //we've got only one identifier
356 if (is_array($list)) {
359 $this->zdb
->connection
->beginTransaction();
361 $select = $this->zdb
->select(self
::TABLE
);
362 $select->where
->in(self
::PK
, $list);
364 $results = $this->zdb
->execute($select);
365 foreach ($results as $transaction) {
366 $c = new Transaction($this->zdb
, $this->login
, $transaction);
367 $res = $c->remove($hist, false);
368 if ($res === false) {
369 throw new \
Exception();
372 $this->zdb
->connection
->commit();
374 "Transactions deleted (" . print_r($list, true) . ')'
377 } catch (Throwable
$e) {
378 $this->zdb
->connection
->rollBack();
380 'An error occurred trying to remove transactions | ' .
387 //not numeric and not an array: incorrect.
389 'Asking to remove transaction, but without providing ' .
390 'an array or a single numeric value.',