-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathofflinerecurring_legacy.php
More file actions
262 lines (218 loc) · 10.1 KB
/
offlinerecurring_legacy.php
File metadata and controls
262 lines (218 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* CiviCRM Offline Recurring Extension
* Legacy version - Civi < 4.5
* @package uk.co.circleinteractive.offlinerecurring
*/
/**
* Implementation of hook_civicrm_pageRun
*/
function offlinerecurring_civicrm_pageRun(&$page) {
if ($page->getVar('_name') == 'CRM_Contribute_Page_Tab') {
$contact_id = CRM_Utils_Array::value('cid', $_GET, '');
// modified - andyw@circle, 19/07/2013
// show all recurring payments
// recurring record generated by the extension is allowed to be edited in hook
// see offlinerecurring_civicrm_alterRecurringContributionsParams
$query = "
SELECT * FROM civicrm_contribution_recur ccr
WHERE contact_id = %1
";
$dao = CRM_Core_DAO::executeQuery($query, array(1 => array($contact_id, 'String')));
$recurArray = array();
if (_offlinerecurring_getCRMVersion() >= 4.4)
$dao->next_sched_contribution = $dao->next_sched_contribution_date;
$membershipIdExists = FALSE;
// Check if membership is linked with the recur record and allowed to be moved to different membership
// NOTE: 'membership_id' is not in 'civicrm_contribution_recur' table by default
if(CRM_Core_DAO::checkFieldExists('civicrm_contribution_recur', 'membership_id')) {
$membershipIdExists = TRUE;
$page->assign('membershipIdExists', $membershipIdExists);
}
while ($dao->fetch()) {
$recurParams = array(
'id' => $dao->id,
'amount' => CRM_Utils_Money::format($dao->amount),
'frequency_unit' => $dao->frequency_unit,
'frequency_interval' => $dao->frequency_interval,
'start_date' => $dao->start_date,
'next_sched_contribution' => $dao->next_sched_contribution,
'payment_processor_id' => $dao->payment_processor_id,
'payment_instrument_id' => $dao->payment_instrument_id,
'enable_edit' => 0,
);
if($membershipIdExists == TRUE) {
$recurParams['membership_name'] = '';
if (!empty($dao->membership_id)) {
// get membership details, if linked to the recurring record
require_once 'api/api.php';
$result = civicrm_api('membership', 'get', array('version' => 3, 'id' => $dao->membership_id));
$recurParams['membership_name'] = $result['values'][$dao->membership_id]['membership_name'];
}
}
// Allow $recurParams to be modified via hook, before displayed
// This will allow 'edit' link to be enabled for certain 'payment instruments' or 'payment processor'
// 'edit' link is disabled for all recurring contributions by default
require_once 'Recurring/Utils/Hook.php';
Recurring_Utils_Hook::alterRecurringContributionParams( $recurParams );
$recurArray[$dao->id] = $recurParams;
}
//for contribution tabular View
$buildTabularView = CRM_Utils_Array::value('showtable', $_GET, false);
$page->assign('buildTabularView', $buildTabularView);
if ($buildTabularView)
return;
$page->assign('recurArray', $recurArray);
$page->assign('recurArrayCount', count($recurArray));
}
}
/*
* Implementation of hook_civicrm_alterRecurringContributionParams
*
* Hook implemented to enable editing of recurring record
* so that implementors can decide which recurring records can be allowed to be edited
*/
function offlinerecurring_civicrm_alterRecurringContributionParams( &$params ) {
if (empty($params['id'])) {
return;
}
$query = "SELECT * FROM civicrm_contribution_recur_offline WHERE recur_id = %1";
$dao = CRM_Core_DAO::executeQuery($query, array(1 => array($params['id'], 'Integer')));
if ($dao->fetch()) {
$params['enable_edit'] = 1;
}
}
# cron job converted from standalone cron script to job api call
# todo: really need to rewrite this using the ContributionRecur api - that api didn't
# exist when the extension was first written
function civicrm_api3_job_process_offline_recurring_payments($params) {
$config = &CRM_Core_Config::singleton();
$debug = false;
//$dtCurrentDay = date("Ymd", mktime(0, 0, 0, date("m") , date("d") , date("Y")));
//$dtCurrentDayStart = $dtCurrentDay."000000";
//$dtCurrentDayEnd = $dtCurrentDay."235959";
// 7 day lookback - prevents contributions stopping if cron fails to run for up to 7 days
$searchStart = date('Y-m-d H:i:s', strtotime('today') - (7 * 86400));
$searchEnd = date('Y-m-d H:i:s', strtotime('today') + 86399);
// Select the recurring payment, where current date is equal to next scheduled date
$sql = "
SELECT * FROM civicrm_contribution_recur ccr
INNER JOIN civicrm_contribution_recur_offline ccro ON ccro.recur_id = ccr.id
WHERE (ccr.end_date IS NULL OR ccr.end_date > NOW())
AND ccr.next_sched_contribution >= %1
AND ccr.next_sched_contribution <= %2
";
if (_offlinerecurring_getCRMVersion() >= 4.4)
$sql = str_replace('next_sched_contribution', 'next_sched_contribution_date', $sql);
$dao = CRM_Core_DAO::executeQuery($sql, array(
1 => array($searchStart, 'String'),
2 => array($searchEnd, 'String')
)
);
$counter = 0;
$errors = 0;
$output = array();
while($dao->fetch()) {
$contact_id = $dao->contact_id;
$hash = md5(uniqid(rand(), true));
$total_amount = $dao->amount;
$contribution_recur_id = $dao->id;
$contribution_type_id = 1;
$source = "Offline Recurring Contribution";
$receive_date = date("YmdHis");
$contribution_status_id = 2; // Set to pending, must complete manually
$payment_instrument_id = 3;
require_once 'api/api.php';
$result = civicrm_api('contribution', 'create',
array(
'version' => 3,
'contact_id' => $contact_id,
'receive_date' => $receive_date,
'total_amount' => $total_amount,
'payment_instrument_id' => $payment_instrument_id,
'trxn_id' => $hash,
'invoice_id' => $hash,
'source' => $source,
'contribution_status_id' => $contribution_status_id,
'contribution_type_id' => $contribution_type_id,
'contribution_recur_id' => $contribution_recur_id,
//'contribution_page_id' => $entity_id
)
);
if ($result['is_error']) {
$output[] = $result['error_message'];
++$errors;
++$counter;
continue;
} else {
$contribution = reset($result['values']);
$contribution_id = $contribution['id'];
$output[] = ts('Created contribution record for contact id %1', array(1 => $contact_id));
}
//$mem_end_date = $member_dao->end_date;
$next_sched_contribution = _offlinerecurring_getCRMVersion() >= 4.4 ?
$dao->next_sched_contribution_date : $dao->next_sched_contribution;
$temp_date = strtotime($next_sched_contribution);
$next_collectionDate = strtotime ("+$dao->frequency_interval $dao->frequency_unit", $temp_date);
$next_collectionDate = date('YmdHis', $next_collectionDate);
$sql = "
UPDATE civicrm_contribution_recur
SET next_sched_contribution = %1
WHERE id = %2
";
if (_offlinerecurring_getCRMVersion() >= 4.4)
$sql = str_replace('next_sched_contribution', 'next_sched_contribution_date', $sql);
CRM_Core_DAO::executeQuery($sql, array(
1 => array($next_collectionDate, 'String'),
2 => array($dao->id, 'Integer')
)
);
$result = civicrm_api('activity', 'create',
array(
'version' => 3,
'activity_type_id' => 6,
'source_record_id' => $contribution_id,
'source_contact_id' => $contact_id,
'assignee_contact_id' => $contact_id,
'subject' => "Offline Recurring Contribution - " . $total_amount,
'status_id' => 2,
'activity_date_time' => date("YmdHis"),
)
);
if ($result['is_error']) {
$output[] = ts(
'An error occurred while creating activity record for contact id %1: %2',
array(
1 => $contact_id,
2 => $result['error_message']
)
);
++$errors;
} else {
$output[] = ts('Created activity record for contact id %1', array(1 => $contact_id));
}
++$counter;
}
// If errors ..
if ($errors)
return civicrm_api3_create_error(
ts("Completed, but with %1 errors. %2 records processed.",
array(
1 => $errors,
2 => $counter
)
) . "<br />" . implode("<br />", $output)
);
// If no errors and records processed ..
if ($counter)
return civicrm_api3_create_success(
ts(
'%1 contribution record(s) were processed.',
array(
1 => $counter
)
) . "<br />" . implode("<br />", $output)
);
// No records processed
return civicrm_api3_create_success(ts('No contribution records were processed.'));
}