3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2010-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 2010-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 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7dev - 2010-03-11
37 namespace Galette\Repository
;
40 use Laminas\Db\Sql\Expression
;
42 use Galette\Core\Login
;
43 use Galette\Core\History
;
44 use Galette\Entity\Contribution
;
45 use Galette\Entity\Adherent
;
46 use Galette\Entity\Transaction
;
47 use Galette\Entity\ContributionsTypes
;
48 use Galette\Filters\ContributionsList
;
51 * Contributions class for galette
54 * @category Repository
57 * @author Johan Cwiklinski <johan@x-tnd.be>
58 * @copyright 2009-2014 The Galette Team
59 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
60 * @link http://galette.tuxfamily.org
64 public const TABLE
= Contribution
::TABLE
;
65 public const PK
= Contribution
::PK
;
67 private $count = null;
74 * @param Db $zdb Database
75 * @param Login $login Login
76 * @param ContributionsList $filters Filtering
78 public function __construct(Db
$zdb, Login
$login, $filters = null)
81 $this->login
= $login;
83 if ($filters === null) {
84 $this->filters
= new ContributionsList();
86 $this->filters
= $filters;
91 * Get contributions list for a specific transaction
93 * @param int $trans_id Transaction identifier
95 * @return Contribution[]
97 public function getListFromTransaction($trans_id)
99 $this->filters
->from_transaction
= $trans_id;
100 return $this->getList(true);
104 * Get contributions list
106 * @param bool $as_contrib return the results as an array of
107 * Contribution object.
108 * @param array $fields field(s) name(s) to get. Should be a string or
109 * an array. If null, all fields will be
111 * @param boolean $count true if we want to count members
113 * @return Contribution[]|ResultSet
115 public function getList($as_contrib = false, $fields = null, $count = true)
118 $select = $this->buildSelect($fields, $count);
120 $this->filters
->setLimits($select);
122 $contributions = array();
123 $results = $this->zdb
->execute($select);
125 foreach ($results as $row) {
126 $contributions[] = new Contribution($this->zdb
, $this->login
, $row);
129 $contributions = $results;
131 return $contributions;
132 } catch (\Exception
$e) {
134 'Cannot list contributions | ' . $e->getMessage(),
142 * Builds the SELECT statement
144 * @param array $fields fields list to retrieve
145 * @param bool $count true if we want to count members
146 * (not applicable from static calls), defaults to false
148 * @return string SELECT statement
150 private function buildSelect($fields, $count = false)
153 $fieldsList = ($fields != null)
154 ?
((!is_array($fields) ||
count($fields) < 1) ?
(array)'*'
155 : implode(', ', $fields)) : (array)'*';
157 $select = $this->zdb
->select(self
::TABLE
, 'a');
158 $select->columns($fieldsList);
161 array('p' => PREFIX_DB
. Adherent
::TABLE
),
162 'a.' . Adherent
::PK
. '= p.' . Adherent
::PK
165 $this->buildWhereClause($select);
166 $select->order(self
::buildOrderClause());
168 $this->calculateSum($select);
171 $this->proceedCount($select);
175 } catch (\Exception
$e) {
177 'Cannot build SELECT clause for contributions | ' . $e->getMessage(),
185 * Count contributions from the query
187 * @param Select $select Original select
191 private function proceedCount($select)
194 $countSelect = clone $select;
195 $countSelect->reset($countSelect::COLUMNS
);
196 $countSelect->reset($countSelect::JOINS
);
197 $countSelect->reset($countSelect::ORDER
);
198 $countSelect->columns(
200 self
::PK
=> new Expression('COUNT(' . self
::PK
. ')')
204 $results = $this->zdb
->execute($countSelect);
205 $result = $results->current();
208 $this->count
= $result->$k;
210 if ($this->count
> 0) {
211 $this->filters
->setCounter($this->count
);
213 } catch (\Exception
$e) {
215 'Cannot count contributions | ' . $e->getMessage(),
223 * Calculate sum of all selected contributions
225 * @param Select $select Original select
229 private function calculateSum($select)
232 $sumSelect = clone $select;
233 $sumSelect->reset($sumSelect::COLUMNS
);
234 $sumSelect->reset($sumSelect::JOINS
);
235 $sumSelect->reset($sumSelect::ORDER
);
238 'contribsum' => new Expression('SUM(montant_cotis)')
242 $results = $this->zdb
->execute($sumSelect);
243 $result = $results->current();
245 $this->sum
= round($result->contribsum
, 2);
246 } catch (\Exception
$e) {
248 'Cannot calculate contributions sum | ' . $e->getMessage(),
256 * Builds the order clause
258 * @return string SQL ORDER clause
260 private function buildOrderClause()
264 switch ($this->filters
->orderby
) {
265 case ContributionsList
::ORDERBY_DATE
:
266 $order[] = 'date_enreg ' . $this->filters
->ordered
;
268 case ContributionsList
::ORDERBY_BEGIN_DATE
:
269 $order[] = 'date_debut_cotis ' . $this->filters
->ordered
;
271 case ContributionsList
::ORDERBY_END_DATE
:
272 $order[] = 'date_fin_cotis ' . $this->filters
->ordered
;
274 case ContributionsList
::ORDERBY_MEMBER
:
275 $order[] = 'nom_adh ' . $this->filters
->ordered
;
276 $order[] = 'prenom_adh ' . $this->filters
->ordered
;
278 case ContributionsList
::ORDERBY_TYPE
:
279 $order[] = ContributionsTypes
::PK
;
281 case ContributionsList
::ORDERBY_AMOUNT
:
282 $order[] = 'montant_cotis ' . $this->filters
->ordered
;
285 Hum... I really do not know how to sort a query with a value that
286 is calculated code side :/
287 case ContributionsList::ORDERBY_DURATION:
289 case ContributionsList
::ORDERBY_PAYMENT_TYPE
:
290 $order[] = 'type_paiement_cotis ' . $this->ordered
;
293 $order[] = $this->filters
->orderby
. ' ' . $this->filters
->ordered
;
301 * Builds where clause, for filtering on simple list mode
303 * @param Select $select Original select
305 * @return string SQL WHERE clause
307 private function buildWhereClause($select)
309 $field = 'date_debut_cotis';
311 switch ($this->filters
->date_field
) {
312 case ContributionsList
::DATE_RECORD
:
313 $field = 'date_enreg';
315 case ContributionsList
::DATE_END
:
316 $field = 'date_fin_cotis';
318 case ContributionsList
::DATE_BEGIN
:
320 $field = 'date_debut_cotis';
325 if ($this->filters
->start_date_filter
!= null) {
326 $d = new \
DateTime($this->filters
->start_date_filter
);
327 $select->where
->greaterThanOrEqualTo(
333 if ($this->filters
->end_date_filter
!= null) {
334 $d = new \
DateTime($this->filters
->end_date_filter
);
335 $select->where
->lessThanOrEqualTo(
341 if ($this->filters
->payment_type_filter
!== null) {
342 $select->where
->equalTo(
343 'type_paiement_cotis',
344 $this->filters
->payment_type_filter
348 if ($this->filters
->from_transaction
!== false) {
349 $select->where
->equalTo(
351 $this->filters
->from_transaction
355 if ($this->filters
->max_amount
!== null && is_int($this->filters
->max_amount
)) {
357 '(montant_cotis <= ' . $this->filters
->max_amount
.
358 ' OR montant_cotis IS NULL)'
362 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
363 //non staff members can only view their own contributions
366 'a.' . Adherent
::PK
=> $this->login
->id
369 } elseif ($this->filters
->filtre_cotis_adh
!= null) {
371 'a.' . Adherent
::PK
. ' = ' . $this->filters
->filtre_cotis_adh
374 if ($this->filters
->filtre_transactions
=== true) {
375 $select->where('a.trans_id IS NULL');
377 } catch (\Exception
$e) {
379 __METHOD__
. ' | ' . $e->getMessage(),
386 * Get count for current query
390 public function getCount()
400 public function getSum()
406 * Remove specified contributions
408 * @param integer|array $ids Contributions identifiers to delete
409 * @param History $hist History
410 * @param boolean $transaction True to begin a database transaction
414 public function remove($ids, History
$hist, $transaction = true)
417 if (is_numeric($ids)) {
418 //we've got only one identifier
424 if (is_array($list)) {
428 $this->zdb
->connection
->beginTransaction();
430 $select = $this->zdb
->select(self
::TABLE
);
431 $select->where
->in(self
::PK
, $list);
432 $contributions = $this->zdb
->execute($select);
433 foreach ($contributions as $contribution) {
434 $c = new Contribution($this->zdb
, $this->login
, $contribution);
435 $res = $c->remove(false);
436 if ($res === false) {
437 throw new \
Exception();
441 $this->zdb
->connection
->commit();
446 print_r($list, true),
447 _T("Contributions deleted (%list)")
451 } catch (\Exception
$e) {
453 $this->zdb
->connection
->rollBack();
456 'An error occurred trying to remove contributions | ' .
463 //not numeric and not an array: incorrect.
465 'Asking to remove contribution, but without providing an array or a single numeric value.',