]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/TransactionsList.php
477a87d8fab4ac0a44ef9d5d6818aaf1707c9e01
[galette.git] / galette / lib / Galette / Filters / TransactionsList.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\Filters;
23
24 use Throwable;
25 use Analog\Analog;
26 use Galette\Core\Pagination;
27
28 /**
29 * Transactions lists filters and paginator
30 *
31 * @author Johan Cwiklinski <johan@x-tnd.be>
32 *
33 * @property ?string $start_date_filter
34 * @property ?string $end_date_filter
35 * @property ?integer $filtre_cotis_adh
36 * @property integer|false $filtre_cotis_children
37 * @property string $rstart_date_filter
38 * @property string $rend_date_filter
39 * @property ?integer $max_amount
40 */
41
42 class TransactionsList extends Pagination
43 {
44 public const ORDERBY_DATE = 0;
45 public const ORDERBY_MEMBER = 3;
46 public const ORDERBY_AMOUNT = 5;
47 public const ORDERBY_PAYMENT_TYPE = 7;
48 public const ORDERBY_ID = 8;
49
50 //filters
51 private ?string $start_date_filter = null;
52 private ?string $end_date_filter = null;
53 private ?int $filtre_cotis_adh = null;
54 private int|false $filtre_cotis_children = false;
55 private ?int $max_amount = null;
56
57 /** @var array<string> */
58 protected array $list_fields = array(
59 'start_date_filter',
60 'end_date_filter',
61 'filtre_cotis_adh',
62 'filtre_cotis_children'
63 );
64
65 /** @var array<string> */
66 protected array $virtuals_list_fields = array(
67 'rstart_date_filter',
68 'rend_date_filter'
69 );
70
71 /**
72 * Default constructor
73 */
74 public function __construct()
75 {
76 $this->reinit();
77 }
78
79 /**
80 * Returns the field we want to default set order to
81 *
82 * @return int|string
83 */
84 protected function getDefaultOrder(): int|string
85 {
86 return self::ORDERBY_DATE;
87 }
88
89 /**
90 * Reinit default parameters
91 *
92 * @return void
93 */
94 public function reinit(): void
95 {
96 parent::reinit();
97 $this->start_date_filter = null;
98 $this->end_date_filter = null;
99 $this->filtre_cotis_adh = null;
100 $this->filtre_cotis_children = false;
101 }
102
103 /**
104 * Global getter method
105 *
106 * @param string $name name of the property we want to retrieve
107 *
108 * @return mixed the called property
109 */
110 public function __get(string $name)
111 {
112 Analog::log(
113 '[TransactionsList] Getting property `' . $name . '`',
114 Analog::DEBUG
115 );
116
117 if (in_array($name, $this->pagination_fields)) {
118 return parent::__get($name);
119 } else {
120 if (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
121 switch ($name) {
122 case 'start_date_filter':
123 case 'end_date_filter':
124 if ($this->$name === null) {
125 return $this->$name;
126 }
127 try {
128 $d = \DateTime::createFromFormat(__("Y-m-d"), $this->$name);
129 if ($d === false) {
130 //try with non localized date
131 $d = \DateTime::createFromFormat("Y-m-d", $this->$name);
132 if ($d === false) {
133 throw new \Exception('Incorrect format');
134 }
135 }
136 return $d->format(__("Y-m-d"));
137 } catch (Throwable $e) {
138 //oops, we've got a bad date :/
139 Analog::log(
140 'Bad date (' . $this->$name . ') | ' .
141 $e->getMessage(),
142 Analog::INFO
143 );
144 return $this->$name;
145 }
146 case 'rstart_date_filter':
147 case 'rend_date_filter':
148 //same as above, but raw format
149 $rname = substr($name, 1);
150 return $this->$rname;
151 default:
152 return $this->$name;
153 }
154 } else {
155 Analog::log(
156 '[TransactionsList] Unable to get property `' . $name . '`',
157 Analog::WARNING
158 );
159 }
160 }
161 }
162
163 /**
164 * Global isset method
165 * Required for twig to access properties via __get
166 *
167 * @param string $name name of the property we want to retrieve
168 *
169 * @return bool
170 */
171 public function __isset(string $name): bool
172 {
173 if (in_array($name, $this->pagination_fields)) {
174 return true;
175 } elseif (in_array($name, $this->list_fields) || in_array($name, $this->virtuals_list_fields)) {
176 return true;
177 }
178
179 return false;
180 }
181
182 /**
183 * Global setter method
184 *
185 * @param string $name name of the property we want to assign a value to
186 * @param mixed $value a relevant value for the property
187 *
188 * @return void
189 */
190 public function __set(string $name, $value): void
191 {
192 if (in_array($name, $this->pagination_fields)) {
193 parent::__set($name, $value);
194 } else {
195 Analog::log(
196 '[TransactionsList] Setting property `' . $name . '`',
197 Analog::DEBUG
198 );
199
200 switch ($name) {
201 case 'start_date_filter':
202 case 'end_date_filter':
203 try {
204 if ($value !== '') {
205 $y = \DateTime::createFromFormat(__("Y"), $value);
206 if ($y !== false) {
207 $month = 1;
208 $day = 1;
209 if ($name === 'end_date_filter') {
210 $month = 12;
211 $day = 31;
212 }
213 $y->setDate(
214 (int)$y->format('Y'),
215 $month,
216 $day
217 );
218 $this->$name = $y->format('Y-m-d');
219 }
220
221 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
222 if ($y === false && $ym !== false) {
223 $day = 1;
224 if ($name === 'end_date_filter') {
225 $day = (int)$ym->format('t');
226 }
227 $ym->setDate(
228 (int)$ym->format('Y'),
229 (int)$ym->format('m'),
230 $day
231 );
232 $this->$name = $ym->format('Y-m-d');
233 }
234
235 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
236 if ($y === false && $ym === false && $d !== false) {
237 $this->$name = $d->format('Y-m-d');
238 }
239
240 if ($y === false && $ym === false && $d === false) {
241 $formats = array(
242 __("Y"),
243 __("Y-m"),
244 __("Y-m-d"),
245 );
246
247 $field = null;
248 if ($name === 'start_date_filter') {
249 $field = _T("start date filter");
250 }
251 if ($name === 'end_date_filter') {
252 $field = _T("end date filter");
253 }
254
255 throw new \Exception(
256 sprintf(
257 //TRANS: %1$s is field name, %2$s is list of known date formats
258 _T('Unknown date format for %1$s.<br/>Know formats are: %2$s'),
259 $field,
260 implode(', ', $formats)
261 )
262 );
263 }
264 } else {
265 $this->$name = null;
266 }
267 } catch (Throwable $e) {
268 Analog::log(
269 'Wrong date format. field: ' . $name .
270 ', value: ' . $value . ', expected fmt: ' .
271 __("Y-m-d") . ' | ' . $e->getMessage(),
272 Analog::INFO
273 );
274 throw $e;
275 }
276 break;
277 default:
278 $this->$name = $value;
279 break;
280 }
281 }
282 }
283 }