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