3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2021 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2021 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.7.5dev - 2013-02-13
37 namespace Galette\Repository
;
39 use Galette\Entity\Reminder
;
40 use Galette\Filters\MembersList
;
42 use Laminas\Db\Sql\Expression
;
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2013-2021 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7.5dev - 2013-02-13
59 public const TABLE
= 'reminders';
60 public const PK
= 'reminder_id';
70 * @param array $selected Selected types for sending
72 public function __construct($selected = null)
74 if (isset($selected) && is_array($selected)) {
75 $this->selected
= array_map('intval', $selected);
77 $this->selected
= array(Reminder
::IMPENDING
, Reminder
::LATE
);
84 * @param Db $zdb Database instance
85 * @param string $type Reminder type
86 * @param boolean $nomail Get reminders for members who do not have email address
90 private function loadToRemind($zdb, $type, $nomail = false)
92 $this->toremind
= array();
93 $select = $zdb->select(Members
::TABLE
, 'a');
94 $select->columns([Members
::PK
, 'date_echeance']);
96 array('r' => PREFIX_DB
. self
::TABLE
),
97 'a.' . Members
::PK
. '=r.reminder_dest',
99 'last_reminder' => new Expression('MAX(reminder_date)'),
100 'reminder_type' => new Expression('MAX(reminder_type)')
104 array('p' => PREFIX_DB
. Members
::TABLE
),
105 'a.parent_id=p.' . Members
::PK
,
110 if ($nomail === false) {
111 //per default, limit to members who have an email address
112 $select->where('(a.email_adh != \'\' OR p.email_adh != \'\')');
114 $select->where('(a.email_adh = \'\' OR a.email_adh IS NULL) AND (p.email_adh = \'\' OR p.email_adh IS NULL)');
117 $select->where('a.activite_adh=true')
118 ->where('a.bool_exempt_adh=false');
120 if ($type === Reminder
::LATE
) {
121 $select->where
->LessThan(
123 date('Y-m-d', time())
126 $now = new \
DateTime();
127 $duedate = new \
DateTime();
128 $duedate->modify('+1 month');
129 $select->where
->greaterThanOrEqualTo(
131 $now->format('Y-m-d')
134 $duedate->format('Y-m-d')
138 $select->group('a.id_adh');
140 $results = $zdb->execute($select);
142 foreach ($results as $r) {
143 if ($r->reminder_type
< $type) {
144 //sent impending, but is now late. reset last remind.
145 $r->reminder_type
= $type;
146 $r->last_reminder
= '';
149 if ($r->reminder_type
=== null ||
(int)$r->reminder_type
=== $type) {
150 $date_checked = false;
152 $due_date = new \
DateTime($r->date_echeance
);
153 $now = new \
DateTime();
156 case Reminder
::IMPENDING
:
157 //reminders 30 days and 7 days before
158 $first = clone $due_date;
159 $second = clone $due_date;
160 $first->modify('-1 month');
161 $second->modify('-7 day');
162 if ($now >= $first ||
$now >= $second) {
163 if ($r->last_reminder
== '') {
164 $date_checked = true;
166 $last_reminder = new \
DateTime($r->last_reminder
);
167 if ($now >= $second && $second > $last_reminder) {
168 $date_checked = true;
174 //reminders 30 days and 60 days after
175 $first = clone $due_date;
176 $second = clone $due_date;
177 $first->modify('1 month');
178 $second->modify('2 month');
179 if ($now >= $second ||
$now >= $first) {
180 if ($r->last_reminder
=== null ||
$r->last_reminder
== '') {
181 $date_checked = true;
183 $last_reminder = new \
DateTime($r->last_reminder
);
184 if ($now >= $second && $second > $last_reminder) {
185 $date_checked = true;
194 $this->toremind
[] = $r->$pk;
198 'Reminder does not suits current requested type ' .
207 * Get the list of reminders
209 * @param Db $zdb Database instance
210 * @param boolean $nomail Get reminders for members who do not have email address
214 public function getList($zdb, $nomail = false)
216 $this->types
= array();
217 $this->reminders
= array();
220 foreach ($this->selected
as $s) {
221 $this->loadToRemind($zdb, $s, $nomail);
223 if (count($this->toremind
) > 0) {
226 $members = $m->getArrayList(
235 $this->types
[$s] = $members;
239 if (is_array($this->types
)) {
240 foreach ($this->types
as $type => $members) {
242 if (is_array($members)) {
243 foreach ($members as $member) {
244 $reminder = new Reminder();
245 $reminder->type
= $type;
246 $reminder->dest
= $member;
248 $this->reminders
[] = $reminder;
253 return $this->reminders
;