3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * This file checks and updates the status of all pledge records for a
31 * given domain using the updatePledgePaymentStatus.
33 * UpdatePledgeRecord.php prior to running this script.
36 require_once '../civicrm.config.php';
37 require_once 'CRM/Core/Config.php';
40 * Class CRM_UpdatePledgeRecord
42 class CRM_UpdatePledgeRecord
{
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';
52 CRM_Utils_System
::authenticateScript(TRUE);
53 $config->cleanURL
= 1;
55 //log the execution time of script
56 CRM_Core_Error
::debug_log_message('UpdatePledgeRecord.php');
60 * @param bool $sendReminders
64 public function updatePledgeStatus($sendReminders = FALSE) {
66 // *** Uncomment the next line if you want automated reminders to be sent
67 // $sendReminders = true;
69 require_once 'CRM/Contribute/PseudoConstant.php';
70 $allStatus = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
72 //unset statues that we never use for pledges
74 'Completed', 'Cancelled', 'Failed') as $statusKey) {
75 if ($key = CRM_Utils_Array
::key($statusKey, $allStatus)) {
76 unset($allStatus[$key]);
80 $statusIds = implode(',', array_keys($allStatus));
84 SELECT 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,
101 ( SELECT sum(civicrm_pledge_payment.actual_amount)
102 FROM civicrm_pledge_payment
103 WHERE civicrm_pledge_payment.status_id = 1
104 AND civicrm_pledge_payment.pledge_id = pledge.id
106 FROM civicrm_pledge pledge, civicrm_pledge_payment payment
107 WHERE pledge.id = payment.pledge_id
108 AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} )
112 $dao = CRM_Core_DAO
::executeQuery($query);
114 require_once 'CRM/Contact/BAO/Contact/Utils.php';
115 require_once 'CRM/Utils/Date.php';
117 $pledgeDetails = $contactIds = $pledgePayments = $pledgeStatus = array();
118 while ($dao->fetch()) {
119 $checksumValue = CRM_Contact_BAO_Contact_Utils
::generateChecksum($dao->contact_id
);
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
,
142 $contactIds[$dao->contact_id
] = $dao->contact_id
;
143 $pledgeStatus[$dao->pledge_id
] = $dao->pledge_status
;
145 if (CRM_Utils_Date
::overdue(CRM_Utils_Date
::customFormat($dao->scheduled_date
, '%Y%m%d'),
147 ) && $dao->payment_status
!= array_search('Overdue', $allStatus)) {
148 $pledgePayments[$dao->pledge_id
][$dao->payment_id
] = $dao->payment_id
;
152 require_once 'CRM/Pledge/BAO/PledgePayment.php';
153 // process the updating script...
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]]})";
159 $newStatus = CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId, $paymentIds,
160 array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE
162 if ($newStatus != $pledgeStatus[$pledgeId]) {
163 echo "<br />- status updated to: {$allStatus[$newStatus]}";
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();
173 $tokens = array('domain' => array('name', 'phone', 'address', 'email'),
174 'contact' => CRM_Core_SelectValues
::contactTokens(),
177 require_once 'CRM/Utils/Token.php';
178 $domainValues = array();
179 foreach ($tokens['domain'] as $token) {
180 $domainValues[$token] = CRM_Utils_Token
::getDomainTokenReplacement($token, $domain);
183 //get the domain email address, since we don't carry w/ object.
184 require_once 'CRM/Core/BAO/Domain.php';
185 $domainValue = CRM_Core_BAO_Domain
::getNameAndEmail();
186 $domainValues['email'] = $domainValue[1];
188 // retrieve contact tokens
190 // this function does NOT return Deceased contacts since we don't want to send them email
191 require_once 'CRM/Utils/Token.php';
192 list($contactDetails) = CRM_Utils_Token
::getTokenDetails($contactIds,
195 $tokens, 'CRM_UpdatePledgeRecord'
198 // assign domain values to template
199 $template = CRM_Core_Smarty
::singleton();
200 $template->assign('domain', $domainValues);
203 $receiptFrom = '"' . $domainValues['name'] . '" <' . $domainValues['email'] . '>';
205 foreach ($pledgeDetails as $paymentId => $details) {
206 if (array_key_exists($details['contact_id'], $contactDetails)) {
207 $contactId = $details['contact_id'];
208 $pledgerName = $contactDetails[$contactId]['display_name'];
214 if (empty($details['reminder_date'])) {
215 $nextReminderDate = new DateTime($details['scheduled_date']);
216 $nextReminderDate->modify("-" . $details['initial_reminder_day'] . "day");
217 $nextReminderDate = $nextReminderDate->format("Ymd");
220 $nextReminderDate = new DateTime($details['reminder_date']);
221 $nextReminderDate->modify("+" . $details['additional_reminder_day'] . "day");
222 $nextReminderDate = $nextReminderDate->format("Ymd");
224 if (($details['reminder_count'] < $details['max_reminders'])
225 && ($nextReminderDate <= $now)
228 $toEmail = $doNotEmail = $onHold = NULL;
230 if (!empty($contactDetails[$contactId]['email'])) {
231 $toEmail = $contactDetails[$contactId]['email'];
234 if (!empty($contactDetails[$contactId]['do_not_email'])) {
235 $doNotEmail = $contactDetails[$contactId]['do_not_email'];
238 if (!empty($contactDetails[$contactId]['on_hold'])) {
239 $onHold = $contactDetails[$contactId]['on_hold'];
242 // 2. send acknowledgement mail
243 if ($toEmail && !($doNotEmail ||
$onHold)) {
244 //assign value to template
245 $template->assign('amount_paid', $details['amount_paid'] ?
$details['amount_paid'] : 0);
246 $template->assign('contact', $contactDetails[$contactId]);
247 $template->assign('next_payment', $details['scheduled_date']);
248 $template->assign('amount_due', $details['amount_due']);
249 $template->assign('checksumValue', $details['checksumValue']);
250 $template->assign('contribution_page_id', $details['contribution_page_id']);
251 $template->assign('pledge_id', $details['pledge_id']);
252 $template->assign('scheduled_payment_date', $details['scheduled_date']);
253 $template->assign('amount', $details['amount']);
254 $template->assign('create_date', $details['create_date']);
256 require_once 'CRM/Core/BAO/MessageTemplate.php';
257 list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate(
259 'groupName' => 'msg_tpl_workflow_pledge',
260 'valueName' => 'pledge_reminder',
261 'contactId' => $contactId,
262 'from' => $receiptFrom,
263 'toName' => $pledgerName,
264 'toEmail' => $toEmail,
268 // 3. update pledge payment details
270 CRM_Pledge_BAO_PledgePayment
::updateReminderDetails($paymentId);
271 $activityType = 'Pledge Reminder';
272 $activityParams = array(
273 'subject' => $subject,
274 'source_contact_id' => $contactId,
275 'source_record_id' => $paymentId,
276 'assignee_contact_id' => $contactId,
277 'activity_type_id' => CRM_Core_OptionGroup
::getValue('activity_type',
281 'activity_date_time' => CRM_Utils_Date
::isoToMysql($now),
282 'due_date_time' => CRM_Utils_Date
::isoToMysql($details['scheduled_date']),
283 'is_test' => $details['is_test'],
285 'campaign_id' => $details['campaign_id'],
287 if (is_a(civicrm_api('activity', 'create', $activityParams), 'CRM_Core_Error')) {
288 CRM_Core_Error
::fatal("Failed creating Activity for acknowledgment");
290 echo "<br />Payment reminder sent to: {$pledgerName} - {$toEmail}";
295 // end foreach on $pledgeDetails
297 // end if ( $sendReminders )
298 echo "<br />{$updateCnt} records updated.";
302 $obj = new CRM_UpdatePledgeRecord();
303 echo "Updating<br />";
304 $obj->updatePledgeStatus();
305 echo "<br />Pledge records update script finished.";