3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 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-2014 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-2014 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
89 private function loadToRemind($zdb, $type)
91 $this->toremind
= array();
92 $select = $zdb->select(Members
::TABLE
, 'a');
93 $select->columns([Members
::PK
, 'date_echeance']);
95 array('r' => PREFIX_DB
. self
::TABLE
),
96 'a.' . Members
::PK
. '=r.reminder_dest',
98 'last_reminder' => new Expression('MAX(reminder_date)'),
99 'reminder_type' => new Expression('MAX(reminder_type)')
103 array('p' => PREFIX_DB
. Members
::TABLE
),
104 'a.parent_id=p.' . Members
::PK
,
107 )->where('(a.email_adh != \'\' OR p.email_adh != \'\')')
108 ->where('a.activite_adh=true')
109 ->where('a.bool_exempt_adh=false');
111 if ($type === Reminder
::LATE
) {
112 $select->where
->LessThan(
114 date('Y-m-d', time())
117 $now = new \
DateTime();
118 $duedate = new \
DateTime();
119 $duedate->modify('+1 month');
120 $select->where
->greaterThanOrEqualTo(
122 $now->format('Y-m-d')
125 $duedate->format('Y-m-d')
129 $select->group('a.id_adh');
131 $results = $zdb->execute($select);
133 foreach ($results as $r) {
134 if ($r->reminder_type
< $type) {
135 //sent impending, but is now late. reset last remind.
136 $r->reminder_type
= $type;
137 $r->last_reminder
= '';
140 if ($r->reminder_type
=== null ||
(int)$r->reminder_type
=== $type) {
141 $date_checked = false;
143 $due_date = new \
DateTime($r->date_echeance
);
144 $now = new \
DateTime();
147 case Reminder
::IMPENDING
:
148 //reminders 30 days and 7 days before
149 $first = clone $due_date;
150 $second = clone $due_date;
151 $first->modify('-1 month');
152 $second->modify('-7 day');
153 if ($now >= $first ||
$now >= $second) {
154 if ($r->last_reminder
== '') {
155 $date_checked = true;
157 $last_reminder = new \
DateTime($r->last_reminder
);
158 if ($now >= $second && $second > $last_reminder) {
159 $date_checked = true;
165 //reminders 30 days and 60 days after
166 $first = clone $due_date;
167 $second = clone $due_date;
168 $first->modify('1 month');
169 $second->modify('2 month');
170 if ($now >= $second ||
$now >= $first) {
171 if ($r->last_reminder
=== null ||
$r->last_reminder
== '') {
172 $date_checked = true;
174 $last_reminder = new \
DateTime($r->last_reminder
);
175 if ($now >= $second && $second > $last_reminder) {
176 $date_checked = true;
185 $this->toremind
[] = $r->$pk;
189 'Reminder does not suits current requested type ' .
198 * Get the list of reminders
200 * @param Db $zdb Database instance
201 * @param boolean $nomail Get reminders for members who do not have email address
205 public function getList($zdb, $nomail = false)
207 $this->types
= array();
208 $this->reminders
= array();
211 foreach ($this->selected
as $s) {
212 $this->loadToRemind($zdb, $s);
214 if (count($this->toremind
) > 0) {
217 $members = $m->getArrayList(
226 $this->types
[$s] = $members;
230 if (is_array($this->types
)) {
231 foreach ($this->types
as $type => $members) {
233 if (is_array($members)) {
234 foreach ($members as $member) {
235 $reminder = new Reminder();
236 $reminder->type
= $type;
237 $reminder->dest
= $member;
239 $this->reminders
[] = $reminder;
244 return $this->reminders
;