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