]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/ScheduledPaymentsCsv.php
Add scheduled payments feature
[galette.git] / galette / lib / Galette / IO / ScheduledPaymentsCsv.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\ScheduledPayment;
30 use Galette\Filters\ScheduledPaymentsList;
31 use Galette\Repository\PaymentTypes;
32 use Galette\Repository\ScheduledPayments;
33
34 /**
35 * Contributions CSV exports
36 *
37 * @author Johan Cwiklinski <johan@x-tnd.be>
38 */
39
40 class ScheduledPaymentsCsv extends CsvOut
41 {
42 private string $filename;
43 private string $path;
44 private Db $zdb;
45 private Login $login;
46
47 /**
48 * Default constructor
49 *
50 * @param Db $zdb Db instance
51 * @param Login $login Login instance
52 */
53 public function __construct(Db $zdb, Login $login)
54 {
55 $this->filename = 'filtered_shceduledpaymentslist.csv';
56 $this->path = self::DEFAULT_DIRECTORY . $this->filename;
57 $this->zdb = $zdb;
58 $this->login = $login;
59 parent::__construct();
60 }
61
62 /**
63 * Export members CSV
64 *
65 * @param ScheduledPaymentsList $filters Current filters
66 *
67 * @return void
68 */
69 public function exportScheduledPayments(ScheduledPaymentsList $filters)
70 {
71 $scheduled = new ScheduledPayment($this->zdb);
72 $fields = $scheduled->getFields();
73 $labels = array();
74
75 foreach ($fields as $k => $f) {
76 $label = $f['label'];
77 $labels[] = $label;
78 }
79
80 $scheduleds = new ScheduledPayments($this->zdb, $this->login, $filters);
81 $scheduled_list = $scheduleds->getArrayList($filters->selected);
82 $ptypes = PaymentTypes::getAll(false);
83
84 foreach ($scheduled_list as &$scheduled) {
85 /** @var ArrayObject<string, int|string> $scheduled */
86 if (isset($scheduled->id_paymenttype)) {
87 //add textual payment type
88 $scheduled->id_paymenttype = $ptypes[$scheduled->id_paymenttype];
89 }
90
91 //handle dates
92 if (isset($scheduled->date)) {
93 if (
94 $scheduled->date != ''
95 && $scheduled->date != '1901-01-01'
96 ) {
97 $date = new DateTime($scheduled->date);
98 $scheduled->date = $date->format(__("Y-m-d"));
99 } else {
100 $scheduled->date = '';
101 }
102 }
103
104 if (isset($scheduled->scheduled_date)) {
105 if (
106 $scheduled->scheduled_date != ''
107 && $scheduled->scheduled_date != '1901-01-01'
108 ) {
109 $date = new DateTime($scheduled->scheduled_date);
110 $scheduled->scheduled_date = $date->format(__("Y-m-d"));
111 } else {
112 $scheduled->scheduled_date = '';
113 }
114 }
115
116 //member name
117 if (isset($scheduled->{Adherent::PK})) {
118 $scheduled->{Adherent::PK} = Adherent::getSName($this->zdb, $scheduled->{Adherent::PK});
119 }
120 }
121
122 $fp = fopen($this->path, 'w');
123 if ($fp) {
124 $this->export(
125 $scheduled_list,
126 self::DEFAULT_SEPARATOR,
127 self::DEFAULT_QUOTE,
128 $labels,
129 $fp
130 );
131 fclose($fp);
132 }
133 }
134
135 /**
136 * Get file path on disk
137 *
138 * @return string
139 */
140 public function getPath()
141 {
142 return $this->path;
143 }
144
145 /**
146 * Get file name
147 *
148 * @return string
149 */
150 public function getFileName()
151 {
152 return $this->filename;
153 }
154 }