]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/ContributionsCsv.php
3dcc806019485474bf4410181cb41f7b459c22a0
[galette.git] / galette / lib / Galette / IO / ContributionsCsv.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Contributions CSV exports
7 *
8 * PHP version 5
9 *
10 * Copyright © 2021 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 IO
28 * @package Galette
29 *
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
35 */
36
37 namespace Galette\IO;
38
39 use ArrayObject;
40 use DateTime;
41 use Galette\Core\Db;
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;
50
51 /**
52 * Contributions CSV exports
53 *
54 * @category IO
55 * @name Csv
56 * @package Galette
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
62 */
63
64 class ContributionsCsv extends CsvOut
65 {
66 private $filename;
67 private $path;
68 private $zdb;
69 private $login;
70 private $members_fields;
71 private $fields_config;
72 private $filters;
73 private $type;
74
75 /**
76 * Default constructor
77 *
78 * @param Db $zdb Db instance
79 * @param Login $login Login instance
80 * @param string $type One of 'contributions' or 'transactions'
81 */
82 public function __construct(Db $zdb, Login $login, string $type)
83 {
84 $this->filename = 'filtered_' . $type . 'list.csv';
85 $this->path = self::DEFAULT_DIRECTORY . $this->filename;
86 $this->zdb = $zdb;
87 $this->login = $login;
88 $this->type = $type;
89 parent::__construct();
90 }
91
92 /**
93 * Export members CSV
94 *
95 * @param ContributionsList $filters Current filters
96 *
97 * @return void
98 */
99 public function exportContributions(ContributionsList $filters)
100 {
101 $class = '\\Galette\\Entity\\' . ucwords(trim($this->type, 's'));
102 $contrib = new $class($this->zdb, $this->login);
103
104 $fields = $contrib->fields;
105 //not a real data
106 unset($fields['duree_mois_cotis']);
107 $labels = array();
108
109 foreach ($fields as $k => $f) {
110 $label = $f['label'];
111 if (isset($f['cotlabel'])) {
112 $label = $f['cotlabel'] . ' / ' . $label;
113 }
114 $labels[] = $label;
115 }
116
117 $contributions = new Contributions($this->zdb, $this->login, $filters);
118 $contributions_list = $contributions->getArrayList($filters->selected);
119
120 $ptypes = PaymentTypes::getAll();
121 $ctype = new ContributionsTypes($this->zdb);
122
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];
128 }
129
130 //add textual type
131 $contribution->id_type_cotis = $ctype->getLabel($contribution->id_type_cotis);
132
133 //handle dates
134 if (isset($contribution->date)) {
135 if (
136 $contribution->date != ''
137 && $contribution->date != '1901-01-01'
138 ) {
139 $date = new DateTime($contribution->date);
140 $contribution->date = $date->format(__("Y-m-d"));
141 } else {
142 $contribution->date = '';
143 }
144 }
145
146 if (isset($contribution->date_debut_cotis)) {
147 if (
148 $contribution->date_debut_cotis != ''
149 && $contribution->date_debut_cotis != '1901-01-01'
150 ) {
151 $date = new DateTime($contribution->date_debut_cotis);
152 $contribution->date_debut_cotis = $date->format(__("Y-m-d"));
153 } else {
154 $contribution->date_debut_cotis = '';
155 }
156 }
157
158 if (isset($contribution->date_fin_cotis)) {
159 if (
160 $contribution->date_fin_cotis != ''
161 && $contribution->date_fin_cotis != '1901-01-01'
162 ) {
163 $date = new DateTime($contribution->date_fin_cotis);
164 $contribution->date_fin_cotis = $date->format(__("Y-m-d"));
165 } else {
166 $contribution->date_fin_cotis = '';
167 }
168 }
169
170 //member name
171 if (isset($contribution->{Adherent::PK})) {
172 $contribution->{Adherent::PK} = Adherent::getSName($this->zdb, $contribution->{Adherent::PK});
173 }
174 }
175
176 $fp = fopen($this->path, 'w');
177 if ($fp) {
178 $this->export(
179 $contributions_list,
180 self::DEFAULT_SEPARATOR,
181 self::DEFAULT_QUOTE,
182 $labels,
183 $fp
184 );
185 fclose($fp);
186 }
187 }
188
189 /**
190 * Get file path on disk
191 *
192 * @return string
193 */
194 public function getPath()
195 {
196 return $this->path;
197 }
198
199 /**
200 * Get file name
201 *
202 * @return string
203 */
204 public function getFileName()
205 {
206 return $this->filename;
207 }
208 }