Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
f5721b07 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f5721b07 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | ||
29 | /* | |
30 | * This file checks and updates the status of all pledge records for a | |
31 | * given domain using the updatePledgePaymentStatus. | |
783f84c9 | 32 | * |
6a488035 TO |
33 | * UpdatePledgeRecord.php prior to running this script. |
34 | */ | |
35 | ||
36 | require_once '../civicrm.config.php'; | |
37 | require_once 'CRM/Core/Config.php'; | |
38 | class CRM_UpdatePledgeRecord { | |
39 | function __construct() { | |
40 | $config = CRM_Core_Config::singleton(); | |
41 | // this does not return on failure | |
42 | require_once 'CRM/Utils/System.php'; | |
43 | require_once 'CRM/Utils/Hook.php'; | |
44 | ||
45 | CRM_Utils_System::authenticateScript(TRUE); | |
46 | $config->cleanURL = 1; | |
47 | ||
48 | //log the execution time of script | |
49 | CRM_Core_Error::debug_log_message('UpdatePledgeRecord.php'); | |
50 | } | |
51 | ||
52 | public function updatePledgeStatus($sendReminders = FALSE) { | |
53 | ||
54 | // *** Uncomment the next line if you want automated reminders to be sent | |
55 | // $sendReminders = true; | |
56 | ||
57 | require_once 'CRM/Contribute/PseudoConstant.php'; | |
58 | $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
59 | ||
60 | //unset statues that we never use for pledges | |
61 | foreach (array( | |
62 | 'Completed', 'Cancelled', 'Failed') as $statusKey) { | |
63 | if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) { | |
64 | unset($allStatus[$key]); | |
65 | } | |
66 | } | |
67 | ||
68 | $statusIds = implode(',', array_keys($allStatus)); | |
69 | $updateCnt = 0; | |
70 | ||
71 | $query = " | |
72 | SELECT pledge.contact_id as contact_id, | |
73 | pledge.id as pledge_id, | |
74 | pledge.amount as amount, | |
75 | payment.scheduled_date as scheduled_date, | |
76 | pledge.create_date as create_date, | |
77 | payment.id as payment_id, | |
78 | pledge.contribution_page_id as contribution_page_id, | |
79 | payment.reminder_count as reminder_count, | |
80 | pledge.max_reminders as max_reminders, | |
81 | payment.reminder_date as reminder_date, | |
82 | pledge.initial_reminder_day as initial_reminder_day, | |
83 | pledge.additional_reminder_day as additional_reminder_day, | |
84 | pledge.status_id as pledge_status, | |
85 | payment.status_id as payment_status, | |
86 | pledge.is_test as is_test, | |
87 | pledge.campaign_id as campaign_id, | |
88 | SUM(payment.scheduled_amount) as amount_due, | |
783f84c9 | 89 | ( SELECT sum(civicrm_pledge_payment.actual_amount) |
90 | FROM civicrm_pledge_payment | |
6a488035 TO |
91 | WHERE civicrm_pledge_payment.status_id = 1 |
92 | AND civicrm_pledge_payment.pledge_id = pledge.id | |
93 | ) as amount_paid | |
783f84c9 | 94 | FROM civicrm_pledge pledge, civicrm_pledge_payment payment |
6a488035 TO |
95 | WHERE pledge.id = payment.pledge_id |
96 | AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} ) | |
97 | GROUP By payment.id | |
98 | "; | |
99 | ||
100 | $dao = CRM_Core_DAO::executeQuery($query); | |
101 | ||
102 | require_once 'CRM/Contact/BAO/Contact/Utils.php'; | |
103 | require_once 'CRM/Utils/Date.php'; | |
104 | $now = date('Ymd'); | |
105 | $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = array(); | |
106 | while ($dao->fetch()) { | |
107 | $checksumValue = CRM_Contact_BAO_Contact_Utils::generateChecksum($dao->contact_id); | |
108 | ||
109 | $pledgeDetails[$dao->payment_id] = array( | |
110 | 'scheduled_date' => $dao->scheduled_date, | |
111 | 'amount_due' => $dao->amount_due, | |
112 | 'amount' => $dao->amount, | |
113 | 'amount_paid' => $dao->amount_paid, | |
114 | 'create_date' => $dao->create_date, | |
115 | 'contact_id' => $dao->contact_id, | |
116 | 'pledge_id' => $dao->pledge_id, | |
117 | 'checksumValue' => $checksumValue, | |
118 | 'contribution_page_id' => $dao->contribution_page_id, | |
119 | 'reminder_count' => $dao->reminder_count, | |
120 | 'max_reminders' => $dao->max_reminders, | |
121 | 'reminder_date' => $dao->reminder_date, | |
122 | 'initial_reminder_day' => $dao->initial_reminder_day, | |
123 | 'additional_reminder_day' => $dao->additional_reminder_day, | |
124 | 'pledge_status' => $dao->pledge_status, | |
125 | 'payment_status' => $dao->payment_status, | |
126 | 'is_test' => $dao->is_test, | |
127 | 'campaign_id' => $dao->campaign_id, | |
128 | ); | |
129 | ||
130 | $contactIds[$dao->contact_id] = $dao->contact_id; | |
131 | $pledgeStatus[$dao->pledge_id] = $dao->pledge_status; | |
132 | ||
133 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'), | |
134 | $now | |
135 | ) && $dao->payment_status != array_search('Overdue', $allStatus)) { | |
136 | $pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id; | |
137 | } | |
138 | } | |
139 | ||
140 | require_once 'CRM/Pledge/BAO/PledgePayment.php'; | |
141 | // process the updating script... | |
142 | ||
143 | foreach ($pledgePayments as $pledgeId => $paymentIds) { | |
144 | // 1. update the pledge /pledge payment status. returns new status when an update happens | |
145 | echo "<br />Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})"; | |
146 | ||
147 | $newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds, | |
148 | array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE | |
149 | ); | |
150 | if ($newStatus != $pledgeStatus[$pledgeId]) { | |
151 | echo "<br />- status updated to: {$allStatus[$newStatus]}"; | |
152 | $updateCnt += 1; | |
153 | } | |
154 | } | |
155 | ||
156 | if ($sendReminders) { | |
157 | // retrieve domain tokens | |
158 | require_once 'CRM/Core/BAO/Domain.php'; | |
159 | require_once 'CRM/Core/SelectValues.php'; | |
160 | $domain = CRM_Core_BAO_Domain::getDomain(); | |
161 | $tokens = array('domain' => array('name', 'phone', 'address', 'email'), | |
162 | 'contact' => CRM_Core_SelectValues::contactTokens(), | |
163 | ); | |
164 | ||
165 | require_once 'CRM/Utils/Token.php'; | |
166 | $domainValues = array(); | |
167 | foreach ($tokens['domain'] as $token) { | |
168 | $domainValues[$token] = CRM_Utils_Token::getDomainTokenReplacement($token, $domain); | |
169 | } | |
170 | ||
171 | //get the domain email address, since we don't carry w/ object. | |
172 | require_once 'CRM/Core/BAO/Domain.php'; | |
173 | $domainValue = CRM_Core_BAO_Domain::getNameAndEmail(); | |
174 | $domainValues['email'] = $domainValue[1]; | |
175 | ||
176 | // retrieve contact tokens | |
177 | ||
178 | // this function does NOT return Deceased contacts since we don't want to send them email | |
179 | require_once 'CRM/Utils/Token.php'; | |
180 | list($contactDetails) = CRM_Utils_Token::getTokenDetails($contactIds, | |
181 | NULL, | |
182 | FALSE, FALSE, NULL, | |
183 | $tokens, 'CRM_UpdatePledgeRecord' | |
184 | ); | |
185 | ||
186 | // assign domain values to template | |
187 | $template = CRM_Core_Smarty::singleton(); | |
188 | $template->assign('domain', $domainValues); | |
189 | ||
190 | //set receipt from | |
191 | $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>'; | |
192 | ||
193 | foreach ($pledgeDetails as $paymentId => $details) { | |
194 | if (array_key_exists($details['contact_id'], $contactDetails)) { | |
195 | $contactId = $details['contact_id']; | |
196 | $pledgerName = $contactDetails[$contactId]['display_name']; | |
197 | } | |
198 | else { | |
199 | continue; | |
200 | } | |
201 | ||
202 | if (empty($details['reminder_date'])) { | |
203 | $nextReminderDate = new DateTime($details['scheduled_date']); | |
204 | $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day"); | |
205 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
206 | } | |
207 | else { | |
208 | $nextReminderDate = new DateTime($details['reminder_date']); | |
209 | $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day"); | |
210 | $nextReminderDate = $nextReminderDate->format("Ymd"); | |
211 | } | |
212 | if (($details['reminder_count'] < $details['max_reminders']) | |
213 | && ($nextReminderDate <= $now) | |
214 | ) { | |
215 | ||
216 | $toEmail = $doNotEmail = $onHold = NULL; | |
217 | ||
218 | if (!empty($contactDetails[$contactId]['email'])) { | |
219 | $toEmail = $contactDetails[$contactId]['email']; | |
220 | } | |
221 | ||
222 | if (!empty($contactDetails[$contactId]['do_not_email'])) { | |
223 | $doNotEmail = $contactDetails[$contactId]['do_not_email']; | |
224 | } | |
225 | ||
226 | if (!empty($contactDetails[$contactId]['on_hold'])) { | |
227 | $onHold = $contactDetails[$contactId]['on_hold']; | |
228 | } | |
229 | ||
230 | // 2. send acknowledgement mail | |
231 | if ($toEmail && !($doNotEmail || $onHold)) { | |
232 | //assign value to template | |
233 | $template->assign('amount_paid', $details['amount_paid'] ? $details['amount_paid'] : 0); | |
234 | $template->assign('contact', $contactDetails[$contactId]); | |
235 | $template->assign('next_payment', $details['scheduled_date']); | |
236 | $template->assign('amount_due', $details['amount_due']); | |
237 | $template->assign('checksumValue', $details['checksumValue']); | |
238 | $template->assign('contribution_page_id', $details['contribution_page_id']); | |
239 | $template->assign('pledge_id', $details['pledge_id']); | |
240 | $template->assign('scheduled_payment_date', $details['scheduled_date']); | |
241 | $template->assign('amount', $details['amount']); | |
242 | $template->assign('create_date', $details['create_date']); | |
243 | ||
c6327d7d TO |
244 | require_once 'CRM/Core/BAO/MessageTemplate.php'; |
245 | list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate( | |
6a488035 TO |
246 | array( |
247 | 'groupName' => 'msg_tpl_workflow_pledge', | |
248 | 'valueName' => 'pledge_reminder', | |
249 | 'contactId' => $contactId, | |
250 | 'from' => $receiptFrom, | |
251 | 'toName' => $pledgerName, | |
252 | 'toEmail' => $toEmail, | |
253 | ) | |
254 | ); | |
255 | ||
256 | // 3. update pledge payment details | |
257 | if ($mailSent) { | |
258 | CRM_Pledge_BAO_PledgePayment::updateReminderDetails($paymentId); | |
259 | $activityType = 'Pledge Reminder'; | |
260 | $activityParams = array( | |
261 | 'subject' => $subject, | |
262 | 'source_contact_id' => $contactId, | |
263 | 'source_record_id' => $paymentId, | |
264 | 'assignee_contact_id' => $contactId, | |
265 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
266 | $activityType, | |
267 | 'name' | |
268 | ), | |
269 | 'activity_date_time' => CRM_Utils_Date::isoToMysql($now), | |
270 | 'due_date_time' => CRM_Utils_Date::isoToMysql($details['scheduled_date']), | |
271 | 'is_test' => $details['is_test'], | |
272 | 'status_id' => 2, | |
273 | 'campaign_id' => $details['campaign_id'], | |
274 | ); | |
275 | if (is_a(civicrm_api('activity', 'create', $activityParams), 'CRM_Core_Error')) { | |
276 | CRM_Core_Error::fatal("Failed creating Activity for acknowledgment"); | |
277 | } | |
278 | echo "<br />Payment reminder sent to: {$pledgerName} - {$toEmail}"; | |
279 | } | |
280 | } | |
281 | } | |
282 | } | |
283 | // end foreach on $pledgeDetails | |
284 | } | |
285 | // end if ( $sendReminders ) | |
286 | echo "<br />{$updateCnt} records updated."; | |
287 | } | |
288 | } | |
289 | ||
290 | $obj = new CRM_UpdatePledgeRecord(); | |
291 | echo "Updating<br />"; | |
292 | $obj->updatePledgeStatus(); | |
293 | echo "<br />Pledge records update script finished."; | |
294 | ||
295 |