3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Contributions CSV exports
10 * Copyright © 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/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2019 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.6-dev - 2021-11-07
42 use Galette\Core\Login
;
43 use Galette\Core\Authentication
;
44 use Galette\Entity\Adherent
;
45 use Galette\Entity\Contribution
;
46 use Galette\Entity\ContributionsTypes
;
47 use Galette\Repository\Contributions
;
48 use Galette\Filters\ContributionsList
;
49 use Galette\Repository\PaymentTypes
;
52 * Contributions CSV exports
57 * @author Johan Cwiklinski <johan@x-tnd.be>
58 * @copyright 2021 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
61 * @since Available since 0.9.6-dev - 2021-11-07
64 class ContributionsCsv
extends CsvOut
70 private $members_fields;
71 private $fields_config;
78 * @param Db $zdb Db instance
79 * @param Login $login Login instance
80 * @param string $type One of 'contributions' or 'transactions'
82 public function __construct(Db
$zdb, Login
$login, string $type)
84 $this->filename
= 'filtered_' . $type . 'list.csv';
85 $this->path
= self
::DEFAULT_DIRECTORY
. $this->filename
;
87 $this->login
= $login;
89 parent
::__construct();
95 * @param ContributionsList $filters Current filters
99 public function exportContributions(ContributionsList
$filters)
101 $class = '\\Galette\\Entity\\' . ucwords(trim($this->type
, 's'));
102 $contrib = new $class($this->zdb
, $this->login
);
104 $fields = $contrib->fields
;
106 unset($fields['duree_mois_cotis']);
109 foreach ($fields as $k => $f) {
110 $label = $f['label'];
111 if (isset($f['cotlabel'])) {
112 $label = $f['cotlabel'] . ' / ' . $label;
117 $contributions = new Contributions($this->zdb
, $this->login
, $filters);
118 $contributions_list = $contributions->getArrayList($filters->selected
);
120 $ptypes = PaymentTypes
::getAll();
121 $ctype = new ContributionsTypes($this->zdb
);
123 foreach ($contributions_list as &$contribution) {
124 /** @var ArrayObject $contribution */
125 if (isset($contribution->type_paiement_cotis
)) {
126 //add textual payment type
127 $contribution->type_paiement_cotis
= $ptypes[$contribution->type_paiement_cotis
];
131 $contribution->id_type_cotis
= $ctype->getLabel($contribution->id_type_cotis
);
134 if (isset($contribution->date
)) {
136 $contribution->date
!= ''
137 && $contribution->date
!= '1901-01-01'
139 $date = new DateTime($contribution->date
);
140 $contribution->date
= $date->format(__("Y-m-d"));
142 $contribution->date
= '';
146 if (isset($contribution->date_debut_cotis
)) {
148 $contribution->date_debut_cotis
!= ''
149 && $contribution->date_debut_cotis
!= '1901-01-01'
151 $date = new DateTime($contribution->date_debut_cotis
);
152 $contribution->date_debut_cotis
= $date->format(__("Y-m-d"));
154 $contribution->date_debut_cotis
= '';
158 if (isset($contribution->date_fin_cotis
)) {
160 $contribution->date_fin_cotis
!= ''
161 && $contribution->date_fin_cotis
!= '1901-01-01'
163 $date = new DateTime($contribution->date_fin_cotis
);
164 $contribution->date_fin_cotis
= $date->format(__("Y-m-d"));
166 $contribution->date_fin_cotis
= '';
171 if (isset($contribution->{Adherent
::PK
})) {
172 $contribution->{Adherent
::PK
} = Adherent
::getSName($this->zdb
, $contribution->{Adherent
::PK
});
176 $fp = fopen($this->path
, 'w');
180 self
::DEFAULT_SEPARATOR
,
190 * Get file path on disk
194 public function getPath()
204 public function getFileName()
206 return $this->filename
;