]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/ContributionsCsv.php
9ea57e13f49388a80cd8cabf6f102a7c6ffa83c3
[galette.git] / galette / lib / Galette / IO / ContributionsCsv.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\IO;
23
24 use ArrayObject;
25 use DateTime;
26 use Galette\Core\Db;
27 use Galette\Core\Login;
28 use Galette\Entity\Adherent;
29 use Galette\Entity\ContributionsTypes;
30 use Galette\Repository\Contributions;
31 use Galette\Filters\ContributionsList;
32 use Galette\Repository\PaymentTypes;
33
34 /**
35 * Contributions CSV exports
36 *
37 * @author Johan Cwiklinski <johan@x-tnd.be>
38 */
39
40 class ContributionsCsv extends CsvOut
41 {
42 private string $filename;
43 private string $path;
44 private Db $zdb;
45 private Login $login;
46 private string $type;
47
48 /**
49 * Default constructor
50 *
51 * @param Db $zdb Db instance
52 * @param Login $login Login instance
53 * @param string $type One of 'contributions' or 'transactions'
54 */
55 public function __construct(Db $zdb, Login $login, string $type)
56 {
57 $this->filename = 'filtered_' . $type . 'list.csv';
58 $this->path = self::DEFAULT_DIRECTORY . $this->filename;
59 $this->zdb = $zdb;
60 $this->login = $login;
61 $this->type = $type;
62 parent::__construct();
63 }
64
65 /**
66 * Export members CSV
67 *
68 * @param ContributionsList $filters Current filters
69 *
70 * @return void
71 */
72 public function exportContributions(ContributionsList $filters)
73 {
74 $class = '\\Galette\\Entity\\' . ucwords(trim($this->type, 's'));
75 $contrib = new $class($this->zdb, $this->login);
76
77 $fields = $contrib->fields;
78 //not a real data
79 unset($fields['duree_mois_cotis']);
80 $labels = array();
81
82 foreach ($fields as $k => $f) {
83 $label = $f['label'];
84 if (isset($f['cotlabel'])) {
85 $label = $f['cotlabel'] . ' / ' . $label;
86 }
87 $labels[] = $label;
88 }
89
90 $contributions = new Contributions($this->zdb, $this->login, $filters);
91 $contributions_list = $contributions->getArrayList($filters->selected);
92
93 $ptypes = PaymentTypes::getAll();
94 $ctype = new ContributionsTypes($this->zdb);
95
96 foreach ($contributions_list as &$contribution) {
97 /** @var ArrayObject<string, int|string> $contribution */
98 if (isset($contribution->type_paiement_cotis)) {
99 //add textual payment type
100 $contribution->type_paiement_cotis = $ptypes[$contribution->type_paiement_cotis];
101 }
102
103 //add textual type
104 $contribution->id_type_cotis = $ctype->getLabel($contribution->id_type_cotis);
105
106 //handle dates
107 if (isset($contribution->date)) {
108 if (
109 $contribution->date != ''
110 && $contribution->date != '1901-01-01'
111 ) {
112 $date = new DateTime($contribution->date);
113 $contribution->date = $date->format(__("Y-m-d"));
114 } else {
115 $contribution->date = '';
116 }
117 }
118
119 if (isset($contribution->date_debut_cotis)) {
120 if (
121 $contribution->date_debut_cotis != ''
122 && $contribution->date_debut_cotis != '1901-01-01'
123 ) {
124 $date = new DateTime($contribution->date_debut_cotis);
125 $contribution->date_debut_cotis = $date->format(__("Y-m-d"));
126 } else {
127 $contribution->date_debut_cotis = '';
128 }
129 }
130
131 if (isset($contribution->date_fin_cotis)) {
132 if (
133 $contribution->date_fin_cotis != ''
134 && $contribution->date_fin_cotis != '1901-01-01'
135 ) {
136 $date = new DateTime($contribution->date_fin_cotis);
137 $contribution->date_fin_cotis = $date->format(__("Y-m-d"));
138 } else {
139 $contribution->date_fin_cotis = '';
140 }
141 }
142
143 //member name
144 if (isset($contribution->{Adherent::PK})) {
145 $contribution->{Adherent::PK} = Adherent::getSName($this->zdb, $contribution->{Adherent::PK});
146 }
147 }
148
149 $fp = fopen($this->path, 'w');
150 if ($fp) {
151 $this->export(
152 $contributions_list,
153 self::DEFAULT_SEPARATOR,
154 self::DEFAULT_QUOTE,
155 $labels,
156 $fp
157 );
158 fclose($fp);
159 }
160 }
161
162 /**
163 * Get file path on disk
164 *
165 * @return string
166 */
167 public function getPath()
168 {
169 return $this->path;
170 }
171
172 /**
173 * Get file name
174 *
175 * @return string
176 */
177 public function getFileName()
178 {
179 return $this->filename;
180 }
181 }