Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 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 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Contribute_BAO_ContributionRecur extends CRM_Contribute_DAO_ContributionRecur { | |
36 | ||
37 | /** | |
ca26cb52 | 38 | * function to create recurring contribution |
6a488035 TO |
39 | * |
40 | * @param array $params (reference ) an assoc array of name/value pairs | |
41 | * | |
42 | * @return object activity contact object | |
43 | * @access public | |
44 | * | |
45 | */ | |
46 | static function create(&$params) { | |
47 | return self::add($params); | |
48 | } | |
49 | ||
50 | /** | |
51 | * takes an associative array and creates a contribution object | |
52 | * | |
53 | * the function extract all the params it needs to initialize the create a | |
54 | * contribution object. the params array could contain additional unused name/value | |
55 | * pairs | |
56 | * | |
fd31fa4c EM |
57 | * @param array $params (reference ) an assoc array of name/value pairs |
58 | * | |
59 | * @internal param array $ids the array that holds all the db ids | |
6a488035 TO |
60 | * |
61 | * @return object CRM_Contribute_BAO_Contribution object | |
62 | * @access public | |
63 | * @static | |
64 | * @todo move hook calls / extended logic to create - requires changing calls to call create not add | |
65 | */ | |
66 | static function add(&$params) { | |
a7488080 | 67 | if (!empty($params['id'])) { |
6a488035 TO |
68 | CRM_Utils_Hook::pre('edit', 'ContributionRecur', $params['id'], $params); |
69 | } | |
70 | else { | |
71 | CRM_Utils_Hook::pre('create', 'ContributionRecur', NULL, $params); | |
72 | } | |
73 | ||
74 | // make sure we're not creating a new recurring contribution with the same transaction ID | |
75 | // or invoice ID as an existing recurring contribution | |
76 | $duplicates = array(); | |
77 | if (self::checkDuplicate($params, $duplicates)) { | |
78 | $error = CRM_Core_Error::singleton(); | |
79 | $d = implode(', ', $duplicates); | |
80 | $error->push(CRM_Core_Error::DUPLICATE_CONTRIBUTION, | |
81 | 'Fatal', | |
82 | array($d), | |
83 | "Found matching recurring contribution(s): $d" | |
84 | ); | |
85 | return $error; | |
86 | } | |
87 | ||
88 | $recurring = new CRM_Contribute_BAO_ContributionRecur(); | |
89 | $recurring->copyValues($params); | |
90 | $recurring->id = CRM_Utils_Array::value('id', $params); | |
91 | ||
92 | // set currency for CRM-1496 | |
93 | if (!isset($recurring->currency)) { | |
94 | $config = CRM_Core_Config::singleton(); | |
95 | $recurring->currency = $config->defaultCurrency; | |
96 | } | |
97 | $result = $recurring->save(); | |
98 | ||
a7488080 | 99 | if (!empty($params['id'])) { |
6a488035 TO |
100 | CRM_Utils_Hook::post('edit', 'ContributionRecur', $recurring->id, $recurring); |
101 | } | |
102 | else { | |
103 | CRM_Utils_Hook::post('create', 'ContributionRecur', $recurring->id, $recurring); | |
104 | } | |
105 | ||
106 | return $result; | |
107 | } | |
108 | ||
109 | /** | |
110 | * Check if there is a recurring contribution with the same trxn_id or invoice_id | |
111 | * | |
112 | * @param array $params (reference ) an assoc array of name/value pairs | |
113 | * @param array $duplicates (reference ) store ids of duplicate contribs | |
114 | * | |
115 | * @return boolean true if duplicate, false otherwise | |
116 | * @access public | |
117 | * static | |
118 | */ | |
119 | static function checkDuplicate($params, &$duplicates) { | |
120 | $id = CRM_Utils_Array::value('id', $params); | |
121 | $trxn_id = CRM_Utils_Array::value('trxn_id', $params); | |
122 | $invoice_id = CRM_Utils_Array::value('invoice_id', $params); | |
123 | ||
124 | $clause = array(); | |
125 | $params = array(); | |
126 | ||
127 | if ($trxn_id) { | |
128 | $clause[] = "trxn_id = %1"; | |
129 | $params[1] = array($trxn_id, 'String'); | |
130 | } | |
131 | ||
132 | if ($invoice_id) { | |
133 | $clause[] = "invoice_id = %2"; | |
134 | $params[2] = array($invoice_id, 'String'); | |
135 | } | |
136 | ||
137 | if (empty($clause)) { | |
138 | return FALSE; | |
139 | } | |
140 | ||
141 | $clause = implode(' OR ', $clause); | |
142 | if ($id) { | |
143 | $clause = "( $clause ) AND id != %3"; | |
144 | $params[3] = array($id, 'Integer'); | |
145 | } | |
146 | ||
147 | $query = "SELECT id FROM civicrm_contribution_recur WHERE $clause"; | |
148 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
149 | $result = FALSE; | |
150 | while ($dao->fetch()) { | |
151 | $duplicates[] = $dao->id; | |
152 | $result = TRUE; | |
153 | } | |
154 | return $result; | |
155 | } | |
156 | ||
186c9c17 EM |
157 | /** |
158 | * @param $id | |
159 | * @param $mode | |
160 | * | |
161 | * @return array|null | |
162 | */ | |
6a488035 TO |
163 | static function getPaymentProcessor($id, $mode) { |
164 | //FIX ME: | |
165 | $sql = " | |
166 | SELECT r.payment_processor_id | |
167 | FROM civicrm_contribution_recur r | |
168 | WHERE r.id = %1"; | |
169 | $params = array(1 => array($id, 'Integer')); | |
170 | $paymentProcessorID = &CRM_Core_DAO::singleValueQuery($sql, | |
171 | $params | |
172 | ); | |
173 | if (!$paymentProcessorID) { | |
174 | return NULL; | |
175 | } | |
176 | ||
177 | return CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID, $mode); | |
178 | } | |
179 | ||
180 | /** | |
181 | * Function to get the number of installment done/completed for each recurring contribution | |
182 | * | |
183 | * @param array $ids (reference ) an array of recurring contribution ids | |
184 | * | |
185 | * @return array $totalCount an array of recurring ids count | |
186 | * @access public | |
187 | * static | |
188 | */ | |
189 | static function getCount(&$ids) { | |
190 | $recurID = implode(',', $ids); | |
191 | $totalCount = array(); | |
192 | ||
d5828a02 | 193 | $query = " |
6a488035 TO |
194 | SELECT contribution_recur_id, count( contribution_recur_id ) as commpleted |
195 | FROM civicrm_contribution | |
196 | WHERE contribution_recur_id IN ( {$recurID}) AND is_test = 0 | |
197 | GROUP BY contribution_recur_id"; | |
198 | ||
199 | $res = CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray); | |
200 | ||
201 | while ($res->fetch()) { | |
202 | $totalCount[$res->contribution_recur_id] = $res->commpleted; | |
203 | } | |
204 | return $totalCount; | |
205 | } | |
206 | ||
207 | /** | |
208 | * Delete Recurring contribution. | |
209 | * | |
da6b46f4 EM |
210 | * @param $recurId |
211 | * | |
ca26cb52 | 212 | * @return bool |
6a488035 TO |
213 | * @access public |
214 | * @static | |
215 | */ | |
216 | static function deleteRecurContribution($recurId) { | |
217 | $result = FALSE; | |
218 | if (!$recurId) { | |
219 | return $result; | |
220 | } | |
221 | ||
222 | $recur = new CRM_Contribute_DAO_ContributionRecur(); | |
223 | $recur->id = $recurId; | |
224 | $result = $recur->delete(); | |
225 | ||
226 | return $result; | |
227 | } | |
228 | ||
229 | /** | |
230 | * Cancel Recurring contribution. | |
231 | * | |
dd244018 EM |
232 | * @param integer $recurId recur contribution id. |
233 | * @param array $objects an array of objects that is to be cancelled like | |
6a488035 TO |
234 | * contribution, membership, event. At least contribution object is a must. |
235 | * | |
dd244018 EM |
236 | * @param array $activityParams |
237 | * | |
ca26cb52 | 238 | * @return bool |
6a488035 TO |
239 | * @access public |
240 | * @static | |
241 | */ | |
242 | static function cancelRecurContribution($recurId, $objects, $activityParams = array()) { | |
243 | if (!$recurId) { | |
244 | return FALSE; | |
245 | } | |
246 | ||
247 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
248 | $canceledId = array_search('Cancelled', $contributionStatus); | |
249 | $recur = new CRM_Contribute_DAO_ContributionRecur(); | |
250 | $recur->id = $recurId; | |
251 | $recur->whereAdd("contribution_status_id != $canceledId"); | |
252 | ||
253 | if ($recur->find(TRUE)) { | |
254 | $transaction = new CRM_Core_Transaction(); | |
255 | $recur->contribution_status_id = $canceledId; | |
256 | $recur->start_date = CRM_Utils_Date::isoToMysql($recur->start_date); | |
257 | $recur->create_date = CRM_Utils_Date::isoToMysql($recur->create_date); | |
258 | $recur->modified_date = CRM_Utils_Date::isoToMysql($recur->modified_date); | |
259 | $recur->cancel_date = date('YmdHis'); | |
260 | $recur->save(); | |
261 | ||
262 | $dao = CRM_Contribute_BAO_ContributionRecur::getSubscriptionDetails($recurId); | |
bbf58b03 | 263 | if ($dao && $dao->recur_id) { |
6a488035 TO |
264 | $details = CRM_Utils_Array::value('details', $activityParams); |
265 | if ($dao->auto_renew && $dao->membership_id) { | |
266 | // its auto-renewal membership mode | |
267 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
268 | $membershipType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $dao->membership_id, 'membership_type_id'); | |
269 | $membershipType = CRM_Utils_Array::value($membershipType, $membershipTypes); | |
270 | $details .= ' | |
271 | <br/>' . ts('Automatic renewal of %1 membership cancelled.', array(1 => $membershipType)); | |
272 | } | |
273 | else { | |
274 | $details .= ' | |
275 | <br/>' . ts('The recurring contribution of %1, every %2 %3 has been cancelled.', array( | |
276 | 1 => $dao->amount, | |
277 | 2 => $dao->frequency_interval, | |
278 | 3 => $dao->frequency_unit | |
279 | )); | |
280 | } | |
281 | $activityParams = array( | |
282 | 'source_contact_id' => $dao->contact_id, | |
283 | 'source_record_id' => CRM_Utils_Array::value('source_record_id', $activityParams), | |
284 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
285 | 'Cancel Recurring Contribution', | |
286 | 'name' | |
287 | ), | |
288 | 'subject' => CRM_Utils_Array::value('subject', $activityParams, ts('Recurring contribution cancelled')), | |
289 | 'details' => $details, | |
290 | 'activity_date_time' => date('YmdHis'), | |
291 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', | |
292 | 'Completed', | |
293 | 'name' | |
294 | ), | |
295 | ); | |
296 | $session = CRM_Core_Session::singleton(); | |
297 | $cid = $session->get('userID'); | |
298 | if ($cid) { | |
299 | $activityParams['target_contact_id'][] = $activityParams['source_contact_id']; | |
300 | $activityParams['source_contact_id'] = $cid; | |
301 | } | |
302 | CRM_Activity_BAO_Activity::create($activityParams); | |
303 | } | |
304 | ||
305 | // if there are associated objects, cancel them as well | |
306 | if ($objects == CRM_Core_DAO::$_nullObject) { | |
307 | $transaction->commit(); | |
308 | return TRUE; | |
309 | } | |
310 | else { | |
311 | $baseIPN = new CRM_Core_Payment_BaseIPN(); | |
312 | return $baseIPN->cancelled($objects, $transaction); | |
313 | } | |
314 | } | |
315 | else { | |
316 | // if already cancelled, return true | |
317 | $recur->whereAdd(); | |
318 | $recur->whereAdd("contribution_status_id = $canceledId"); | |
319 | if ($recur->find(TRUE)) { | |
320 | return TRUE; | |
321 | } | |
322 | } | |
323 | ||
324 | return FALSE; | |
325 | } | |
326 | ||
327 | /** | |
328 | * Function to get list of recurring contribution of contact Ids | |
329 | * | |
330 | * @param int $contactId Contact ID | |
331 | * | |
ca26cb52 | 332 | * @return array list of recurring contribution fields |
6a488035 TO |
333 | * |
334 | * @access public | |
335 | * @static | |
336 | */ | |
337 | static function getRecurContributions($contactId) { | |
338 | $params = array(); | |
339 | $recurDAO = new CRM_Contribute_DAO_ContributionRecur(); | |
340 | $recurDAO->contact_id = $contactId; | |
341 | $recurDAO->find(); | |
342 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); | |
343 | ||
344 | while ($recurDAO->fetch()) { | |
345 | $params[$recurDAO->id]['id'] = $recurDAO->id; | |
346 | $params[$recurDAO->id]['contactId'] = $recurDAO->contact_id; | |
347 | $params[$recurDAO->id]['start_date'] = $recurDAO->start_date; | |
348 | $params[$recurDAO->id]['end_date'] = $recurDAO->end_date; | |
0d4bfc19 | 349 | $params[$recurDAO->id]['next_sched_contribution_date'] = $recurDAO->next_sched_contribution_date; |
6a488035 TO |
350 | $params[$recurDAO->id]['amount'] = $recurDAO->amount; |
351 | $params[$recurDAO->id]['currency'] = $recurDAO->currency; | |
352 | $params[$recurDAO->id]['frequency_unit'] = $recurDAO->frequency_unit; | |
353 | $params[$recurDAO->id]['frequency_interval'] = $recurDAO->frequency_interval; | |
354 | $params[$recurDAO->id]['installments'] = $recurDAO->installments; | |
355 | $params[$recurDAO->id]['contribution_status_id'] = $recurDAO->contribution_status_id; | |
356 | $params[$recurDAO->id]['contribution_status'] = CRM_Utils_Array::value($recurDAO->contribution_status_id, $contributionStatus); | |
357 | $params[$recurDAO->id]['is_test'] = $recurDAO->is_test; | |
358 | $params[$recurDAO->id]['payment_processor_id'] = $recurDAO->payment_processor_id; | |
359 | } | |
360 | ||
361 | return $params; | |
362 | } | |
363 | ||
186c9c17 EM |
364 | /** |
365 | * @param $entityID | |
366 | * @param string $entity | |
367 | * | |
368 | * @return null|Object | |
369 | */ | |
6a488035 TO |
370 | static function getSubscriptionDetails($entityID, $entity = 'recur') { |
371 | $sql = " | |
d5828a02 DL |
372 | SELECT rec.id as recur_id, |
373 | rec.processor_id as subscription_id, | |
6a488035 TO |
374 | rec.frequency_interval, |
375 | rec.installments, | |
376 | rec.frequency_unit, | |
377 | rec.amount, | |
378 | rec.is_test, | |
379 | rec.auto_renew, | |
380 | rec.currency, | |
d5828a02 | 381 | con.id as contribution_id, |
6a488035 | 382 | con.contribution_page_id, |
f0a7bcb5 | 383 | rec.contact_id, |
6a488035 TO |
384 | mp.membership_id"; |
385 | ||
386 | if ($entity == 'recur') { | |
387 | $sql .= " | |
d5828a02 | 388 | FROM civicrm_contribution_recur rec |
f0a7bcb5 | 389 | LEFT JOIN civicrm_contribution con ON ( con.contribution_recur_id = rec.id ) |
6a488035 TO |
390 | LEFT JOIN civicrm_membership_payment mp ON ( mp.contribution_id = con.id ) |
391 | WHERE rec.id = %1 | |
392 | GROUP BY rec.id"; | |
393 | } | |
394 | elseif ($entity == 'contribution') { | |
395 | $sql .= " | |
396 | FROM civicrm_contribution con | |
d5828a02 | 397 | INNER JOIN civicrm_contribution_recur rec ON ( con.contribution_recur_id = rec.id ) |
6a488035 TO |
398 | LEFT JOIN civicrm_membership_payment mp ON ( mp.contribution_id = con.id ) |
399 | WHERE con.id = %1"; | |
400 | } | |
401 | elseif ($entity == 'membership') { | |
402 | $sql .= " | |
d5828a02 DL |
403 | FROM civicrm_membership_payment mp |
404 | INNER JOIN civicrm_membership mem ON ( mp.membership_id = mem.id ) | |
6a488035 TO |
405 | INNER JOIN civicrm_contribution_recur rec ON ( mem.contribution_recur_id = rec.id ) |
406 | INNER JOIN civicrm_contribution con ON ( con.id = mp.contribution_id ) | |
407 | WHERE mp.membership_id = %1"; | |
408 | } | |
409 | ||
410 | $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($entityID, 'Integer'))); | |
411 | if ($dao->fetch()) { | |
412 | return $dao; | |
413 | } | |
d5828a02 DL |
414 | else { |
415 | return CRM_Core_DAO::$_nullObject; | |
416 | } | |
6a488035 TO |
417 | } |
418 | ||
419 | static function setSubscriptionContext() { | |
420 | // handle context redirection for subscription url | |
421 | $session = CRM_Core_Session::singleton(); | |
422 | if ($session->get('userID')) { | |
423 | $url = FALSE; | |
424 | $cid = CRM_Utils_Request::retrieve('cid', 'Integer', $this, FALSE); | |
425 | $mid = CRM_Utils_Request::retrieve('mid', 'Integer', $this, FALSE); | |
426 | $qfkey = CRM_Utils_Request::retrieve('key', 'String', $this, FALSE); | |
427 | $context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE); | |
428 | if ($cid) { | |
429 | switch ($context) { | |
430 | case 'contribution': | |
431 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
432 | "reset=1&selectedChild=contribute&cid={$cid}" | |
433 | ); | |
434 | break; | |
435 | ||
436 | case 'membership': | |
437 | $url = CRM_Utils_System::url('civicrm/contact/view', | |
438 | "reset=1&selectedChild=member&cid={$cid}" | |
439 | ); | |
440 | break; | |
441 | ||
442 | case 'dashboard': | |
443 | $url = CRM_Utils_System::url('civicrm/user', "reset=1&id={$cid}"); | |
444 | break; | |
445 | } | |
446 | } | |
447 | if ($mid) { | |
448 | switch ($context) { | |
449 | case 'dashboard': | |
450 | $url = CRM_Utils_System::url('civicrm/member', "force=1&context={$context}&key={$qfkey}"); | |
451 | break; | |
452 | ||
453 | case 'search': | |
454 | $url = CRM_Utils_System::url('civicrm/member/search', "force=1&context={$context}&key={$qfkey}"); | |
455 | break; | |
456 | } | |
457 | } | |
458 | if ($url) { | |
459 | $session->pushUserContext($url); | |
460 | } | |
461 | } | |
462 | } | |
463 | } |