4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
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.
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.
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/>.
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
;
35 * Contributions CSV exports
37 * @author Johan Cwiklinski <johan@x-tnd.be>
40 class ContributionsCsv
extends CsvOut
42 private string $filename;
51 * @param Db $zdb Db instance
52 * @param Login $login Login instance
53 * @param string $type One of 'contributions' or 'transactions'
55 public function __construct(Db
$zdb, Login
$login, string $type)
57 $this->filename
= 'filtered_' . $type . 'list.csv';
58 $this->path
= self
::DEFAULT_DIRECTORY
. $this->filename
;
60 $this->login
= $login;
62 parent
::__construct();
68 * @param ContributionsList $filters Current filters
72 public function exportContributions(ContributionsList
$filters)
74 $class = '\\Galette\\Entity\\' . ucwords(trim($this->type
, 's'));
75 $contrib = new $class($this->zdb
, $this->login
);
77 $fields = $contrib->fields
;
79 unset($fields['duree_mois_cotis']);
82 foreach ($fields as $k => $f) {
84 if (isset($f['cotlabel'])) {
85 $label = $f['cotlabel'] . ' / ' . $label;
90 $contributions = new Contributions($this->zdb
, $this->login
, $filters);
91 $contributions_list = $contributions->getArrayList($filters->selected
);
93 $ptypes = PaymentTypes
::getAll();
94 $ctype = new ContributionsTypes($this->zdb
);
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
];
104 $contribution->id_type_cotis
= $ctype->getLabel($contribution->id_type_cotis
);
107 if (isset($contribution->date
)) {
109 $contribution->date
!= ''
110 && $contribution->date
!= '1901-01-01'
112 $date = new DateTime($contribution->date
);
113 $contribution->date
= $date->format(__("Y-m-d"));
115 $contribution->date
= '';
119 if (isset($contribution->date_debut_cotis
)) {
121 $contribution->date_debut_cotis
!= ''
122 && $contribution->date_debut_cotis
!= '1901-01-01'
124 $date = new DateTime($contribution->date_debut_cotis
);
125 $contribution->date_debut_cotis
= $date->format(__("Y-m-d"));
127 $contribution->date_debut_cotis
= '';
131 if (isset($contribution->date_fin_cotis
)) {
133 $contribution->date_fin_cotis
!= ''
134 && $contribution->date_fin_cotis
!= '1901-01-01'
136 $date = new DateTime($contribution->date_fin_cotis
);
137 $contribution->date_fin_cotis
= $date->format(__("Y-m-d"));
139 $contribution->date_fin_cotis
= '';
144 if (isset($contribution->{Adherent
::PK
})) {
145 $contribution->{Adherent
::PK
} = Adherent
::getSName($this->zdb
, $contribution->{Adherent
::PK
});
149 $fp = fopen($this->path
, 'w');
153 self
::DEFAULT_SEPARATOR
,
163 * Get file path on disk
167 public function getPath()
177 public function getFileName()
179 return $this->filename
;