3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2011-2014 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-2014 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.7dev - 2011-07-31
38 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-2014 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 const TABLE
= Transaction
::TABLE
;
64 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 (\Exception
$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 (\Exception
$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 (\Exception
$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_DATE
:
222 $order[] = 'trans_date' . ' ' . $this->filters
->ordered
;
224 case TransactionsList
::ORDERBY_MEMBER
:
225 $order[] = 'nom_adh' . ' ' . $this->filters
->ordered
;
226 $order[] = 'prenom_adh' . ' ' . $this->filters
->ordered
;
228 case TransactionsList
::ORDERBY_AMOUNT
:
229 $order[] = 'trans_amount' . ' ' . $this->filters
->ordered
;
232 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->ordered
;
240 * Builds where clause, for filtering on simple list mode
242 * @param Select $select Original select
244 * @return string SQL WHERE clause
246 private function buildWhereClause($select)
249 if ($this->filters
->start_date_filter
!= null) {
250 $d = new \
DateTime($this->filters
->start_date_filter
);
251 $select->where
->greaterThanOrEqualTo(
257 if ($this->filters
->end_date_filter
!= null) {
258 $d = new \
DateTime($this->filters
->end_date_filter
);
259 $select->where
->lessThanOrEqualTo(
265 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
266 //non staff members can only view their own transactions
267 $select->where('t.' . Adherent
::PK
. ' = ' . $this->login
->id
);
268 } elseif ($this->filters
->filtre_cotis_adh
!= null) {
270 't.' . Adherent
::PK
. ' = ' . $this->filters
->filtre_cotis_adh
273 } catch (\Exception
$e) {
275 __METHOD__
. ' | ' . $e->getMessage(),
282 * Get count for current query
286 public function getCount()
292 * Remove specified transactions
294 * @param interger|array $ids Transactions identifiers to delete
295 * @param History $hist History
299 public function remove($ids, History
$hist)
302 if (is_numeric($ids)) {
303 //we've got only one identifier
309 if (is_array($list)) {
312 $this->zdb
->connection
->beginTransaction();
314 $select = $this->zdb
->select(self
::TABLE
);
315 $select->where
->in(self
::PK
, $list);
317 $results = $this->zdb
->execute($select);
318 foreach ($results as $transaction) {
319 $c = new Transaction($this->zdb
, $this->login
, $transaction);
320 $res = $c->remove($hist, false);
321 if ($res === false) {
322 throw new \Exception
;
325 $this->zdb
->connection
->commit();
327 "Transactions deleted (" . print_r($list, true) . ')'
330 } catch (\Exception
$e) {
331 $this->zdb
->connection
->rollBack();
333 'An error occurred trying to remove transactions | ' .
340 //not numeric and not an array: incorrect.
342 'Asking to remove transaction, but without providing ' .
343 'an array or a single numeric value.',