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