]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Repository/Transactions.php
Scrutinizer Auto-Fixes (#59)
[galette.git] / galette / lib / Galette / Repository / Transactions.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Transactions class
7 *
8 * PHP version 5
9 *
10 * Copyright © 2011-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Repository
28 * @package Galette
29 *
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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2011-07-31
36 */
37
38 namespace Galette\Repository;
39
40 use Analog\Analog;
41 use Laminas\Db\Sql\Expression;
42 use Galette\Entity\Transaction;
43 use Galette\Entity\Adherent;
44 use Galette\Core\Db;
45 use Galette\Core\Login;
46 use Galette\Core\History;
47 use Galette\Filters\TransactionsList;
48
49 /**
50 * Transactions class for galette
51 *
52 * @name Transactions
53 * @category Repository
54 * @package Galette
55 *
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
60 */
61 class Transactions
62 {
63 const TABLE = Transaction::TABLE;
64 const PK = Transaction::PK;
65
66 private $count = null;
67 private $zdb;
68 private $login;
69 private $filters;
70
71 /**
72 * Default constructor
73 *
74 * @param Db $zdb Database
75 * @param Login $login Login
76 * @param TransactionsList $filters Filtering
77 */
78 public function __construct(Db $zdb, Login $login, $filters = null)
79 {
80 $this->zdb = $zdb;
81 $this->login = $login;
82
83 if ($filters === null) {
84 $this->filters = new TransactionsList();
85 } else {
86 $this->filters = $filters;
87 }
88 }
89
90 /**
91 * Get transactions list
92 *
93 * @param bool $as_trans return the results as an array of
94 * Transaction object.
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
98 *
99 * @return Transaction[]|ResultSet
100 */
101 public function getList($as_trans = false, $fields = null, $count = true)
102 {
103 try {
104 $select = $this->buildSelect($fields, $count);
105 $this->filters->setLimits($select);
106
107 $transactions = array();
108 $results = $this->zdb->execute($select);
109 if ($as_trans) {
110 foreach ($results as $row) {
111 $transactions[] = new Transaction($this->zdb, $this->login, $row);
112 }
113 } else {
114 $transactions = $results;
115 }
116 return $transactions;
117 } catch (\Exception $e) {
118 Analog::log(
119 'Cannot list transactions | ' . $e->getMessage(),
120 Analog::WARNING
121 );
122 return false;
123 }
124 }
125
126 /**
127 * Builds the SELECT statement
128 *
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
132 *
133 * @return string SELECT statement
134 */
135 private function buildSelect($fields, $count = false)
136 {
137 try {
138 $fieldsList = ($fields != null)
139 ? ((!is_array($fields) || count($fields) < 1) ? (array)'*'
140 : implode(', ', $fields)) : (array)'*';
141
142 $select = $this->zdb->select(self::TABLE, 't');
143 $select->columns(
144 array(
145 'trans_date',
146 'trans_id',
147 'trans_desc',
148 'id_adh',
149 'trans_amount'
150 )
151 )->join(
152 array('a' => PREFIX_DB . Adherent::TABLE),
153 't.' . Adherent::PK . '=' . 'a.' . Adherent::PK,
154 array('nom_adh', 'prenom_adh')
155 );
156
157 $this->buildWhereClause($select);
158 $select->order(self::buildOrderClause());
159
160 if ($count) {
161 $this->proceedCount($select);
162 }
163
164 return $select;
165 } catch (\Exception $e) {
166 Analog::log(
167 'Cannot build SELECT clause for transactions | ' . $e->getMessage(),
168 Analog::WARNING
169 );
170 return false;
171 }
172 }
173
174 /**
175 * Count transactions from the query
176 *
177 * @param Select $select Original select
178 *
179 * @return void
180 */
181 private function proceedCount($select)
182 {
183 try {
184 $countSelect = clone $select;
185 $countSelect->reset($countSelect::COLUMNS);
186 $countSelect->reset($countSelect::ORDER);
187 $countSelect->reset($countSelect::JOINS);
188 $countSelect->columns(
189 array(
190 self::PK => new Expression('COUNT(' . self::PK . ')')
191 )
192 );
193
194 $results = $this->zdb->execute($countSelect);
195 $result = $results->current();
196
197 $k = self::PK;
198 $this->count = $result->$k;
199 if ($this->count > 0) {
200 $this->filters->setCounter($this->count);
201 }
202 } catch (\Exception $e) {
203 Analog::log(
204 'Cannot count transactions | ' . $e->getMessage(),
205 Analog::WARNING
206 );
207 return false;
208 }
209 }
210
211 /**
212 * Builds the order clause
213 *
214 * @return string SQL ORDER clause
215 */
216 private function buildOrderClause()
217 {
218 $order = array();
219
220 switch ($this->filters->orderby) {
221 case TransactionsList::ORDERBY_DATE:
222 $order[] = 'trans_date' . ' ' . $this->filters->ordered;
223 break;
224 case TransactionsList::ORDERBY_MEMBER:
225 $order[] = 'nom_adh' . ' ' . $this->filters->ordered;
226 $order[] = 'prenom_adh' . ' ' . $this->filters->ordered;
227 break;
228 case TransactionsList::ORDERBY_AMOUNT:
229 $order[] = 'trans_amount' . ' ' . $this->filters->ordered;
230 break;
231 default:
232 $order[] = $this->filters->orderby . ' ' . $this->filters->ordered;
233 break;
234 }
235
236 return $order;
237 }
238
239 /**
240 * Builds where clause, for filtering on simple list mode
241 *
242 * @param Select $select Original select
243 *
244 * @return string SQL WHERE clause
245 */
246 private function buildWhereClause($select)
247 {
248 try {
249 if ($this->filters->start_date_filter != null) {
250 $d = new \DateTime($this->filters->start_date_filter);
251 $select->where->greaterThanOrEqualTo(
252 'trans_date',
253 $d->format('Y-m-d')
254 );
255 }
256
257 if ($this->filters->end_date_filter != null) {
258 $d = new \DateTime($this->filters->end_date_filter);
259 $select->where->lessThanOrEqualTo(
260 'trans_date',
261 $d->format('Y-m-d')
262 );
263 }
264
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) {
269 $select->where(
270 't.' . Adherent::PK . ' = ' . $this->filters->filtre_cotis_adh
271 );
272 }
273 } catch (\Exception $e) {
274 Analog::log(
275 __METHOD__ . ' | ' . $e->getMessage(),
276 Analog::WARNING
277 );
278 }
279 }
280
281 /**
282 * Get count for current query
283 *
284 * @return int
285 */
286 public function getCount()
287 {
288 return $this->count;
289 }
290
291 /**
292 * Remove specified transactions
293 *
294 * @param interger|array $ids Transactions identifiers to delete
295 * @param History $hist History
296 *
297 * @return boolean
298 */
299 public function remove($ids, History $hist)
300 {
301 $list = array();
302 if (is_numeric($ids)) {
303 //we've got only one identifier
304 $list[] = $ids;
305 } else {
306 $list = $ids;
307 }
308
309 if (is_array($list)) {
310 $res = true;
311 try {
312 $this->zdb->connection->beginTransaction();
313
314 $select = $this->zdb->select(self::TABLE);
315 $select->where->in(self::PK, $list);
316
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;
323 }
324 }
325 $this->zdb->connection->commit();
326 $hist->add(
327 "Transactions deleted (" . print_r($list, true) . ')'
328 );
329 return true;
330 } catch (\Exception $e) {
331 $this->zdb->connection->rollBack();
332 Analog::log(
333 'An error occurred trying to remove transactions | ' .
334 $e->getMessage(),
335 Analog::ERROR
336 );
337 return false;
338 }
339 } else {
340 //not numeric and not an array: incorrect.
341 Analog::log(
342 'Asking to remove transaction, but without providing ' .
343 'an array or a single numeric value.',
344 Analog::WARNING
345 );
346 return false;
347 }
348 }
349 }