Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Member_BAO_Membership extends CRM_Member_DAO_Membership { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Static field for all the membership information that we can potentially import |
6a488035 TO |
39 | * |
40 | * @var array | |
6a488035 TO |
41 | */ |
42 | static $_importableFields = NULL; | |
8ef12e64 | 43 | |
4e636a74 | 44 | static $_renewalActType = NULL; |
8ef12e64 | 45 | |
4e636a74 | 46 | static $_signupActType = NULL; |
8ef12e64 | 47 | |
bb3a214a | 48 | /** |
100fef9d | 49 | * Class constructor |
bb3a214a | 50 | * |
bb3a214a EM |
51 | * @return \CRM_Member_DAO_Membership |
52 | */ | |
53 | /** | |
bb3a214a | 54 | */ |
00be9182 | 55 | public function __construct() { |
6a488035 TO |
56 | parent::__construct(); |
57 | } | |
58 | ||
59 | /** | |
100fef9d | 60 | * Takes an associative array and creates a membership object |
6a488035 TO |
61 | * |
62 | * the function extracts all the params it needs to initialize the created | |
63 | * membership object. The params array could contain additional unused name/value | |
64 | * pairs | |
65 | * | |
b2363ea8 TO |
66 | * @param array $params |
67 | * (reference ) an assoc array of name/value pairs. | |
68 | * @param array $ids | |
69 | * The array that holds all the db ids. | |
6a488035 | 70 | * |
16b10e64 | 71 | * @return CRM_Member_BAO_Membership |
6a488035 | 72 | */ |
00be9182 | 73 | public static function add(&$params, $ids = array()) { |
a5e2b32e | 74 | $oldStatus = $oldType = NULL; |
f880fd16 EM |
75 | $params['id'] = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membership', $ids)); |
76 | if ($params['id']) { | |
77 | CRM_Utils_Hook::pre('edit', 'Membership', $params['id'], $params); | |
78 | } | |
79 | else { | |
1acc24d5 | 80 | CRM_Utils_Hook::pre('create', 'Membership', NULL, $params); |
f880fd16 EM |
81 | } |
82 | $id = $params['id']; | |
83 | // we do this after the hooks are called in case it has been altered | |
84 | if ($id) { | |
85 | $membershipObj = new CRM_Member_DAO_Membership(); | |
86 | $membershipObj->id = $id; | |
6a488035 TO |
87 | $membershipObj->find(); |
88 | while ($membershipObj->fetch()) { | |
89 | $oldStatus = $membershipObj->status_id; | |
90 | $oldType = $membershipObj->membership_type_id; | |
91 | } | |
92 | } | |
6a488035 TO |
93 | |
94 | if (array_key_exists('is_override', $params) && !$params['is_override']) { | |
95 | $params['is_override'] = 'null'; | |
96 | } | |
97 | ||
98 | $membership = new CRM_Member_BAO_Membership(); | |
99 | $membership->copyValues($params); | |
f880fd16 | 100 | $membership->id = $id; |
6a488035 TO |
101 | |
102 | $membership->save(); | |
103 | $membership->free(); | |
104 | ||
6a488035 TO |
105 | if (empty($membership->contact_id) || empty($membership->status_id)) { |
106 | // this means we are in renewal mode and are just updating the membership | |
107 | // record or this is an API update call and all fields are not present in the update record | |
108 | // however the hooks dont care and want all data CRM-7784 | |
109 | $tempMembership = new CRM_Member_DAO_Membership(); | |
110 | $tempMembership->id = $membership->id; | |
111 | $tempMembership->find(TRUE); | |
112 | $membership = $tempMembership; | |
113 | } | |
114 | ||
115 | //get the log start date. | |
116 | //it is set during renewal of membership. | |
ca58d9b9 | 117 | $logStartDate = CRM_Utils_Array::value('log_start_date', $params); |
6a488035 | 118 | $logStartDate = ($logStartDate) ? CRM_Utils_Date::isoToMysql($logStartDate) : CRM_Utils_Date::isoToMysql($membership->start_date); |
e0556ebe | 119 | $values = self::getStatusANDTypeValues($membership->id); |
6a488035 TO |
120 | |
121 | $membershipLog = array( | |
122 | 'membership_id' => $membership->id, | |
123 | 'status_id' => $membership->status_id, | |
124 | 'start_date' => $logStartDate, | |
125 | 'end_date' => CRM_Utils_Date::isoToMysql($membership->end_date), | |
126 | 'modified_date' => date('Ymd'), | |
127 | 'membership_type_id' => $values[$membership->id]['membership_type_id'], | |
128 | 'max_related' => $membership->max_related, | |
129 | ); | |
130 | ||
131 | $session = CRM_Core_Session::singleton(); | |
132 | // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id | |
133 | if ($session->get('userID')) { | |
134 | $membershipLog['modified_id'] = $session->get('userID'); | |
135 | } | |
136 | elseif (!empty($ids['userId'])) { | |
137 | $membershipLog['modified_id'] = $ids['userId']; | |
138 | } | |
139 | else { | |
140 | $membershipLog['modified_id'] = $membership->contact_id; | |
141 | } | |
142 | ||
143 | CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray); | |
144 | ||
145 | // reset the group contact cache since smart groups might be affected due to this | |
146 | CRM_Contact_BAO_GroupContactCache::remove(); | |
147 | ||
f880fd16 | 148 | if ($id) { |
6a488035 | 149 | if ($membership->status_id != $oldStatus) { |
4d63cfde | 150 | $allStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'get'); |
6a488035 TO |
151 | $activityParam = array( |
152 | 'subject' => "Status changed from {$allStatus[$oldStatus]} to {$allStatus[$membership->status_id]}", | |
153 | 'source_contact_id' => $membershipLog['modified_id'], | |
154 | 'target_contact_id' => $membership->contact_id, | |
155 | 'source_record_id' => $membership->id, | |
4d63cfde | 156 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Status'), |
6a488035 | 157 | 'status_id' => 2, |
6a488035 TO |
158 | 'priority_id' => 2, |
159 | 'activity_date_time' => date('Y-m-d H:i:s'), | |
6a488035 | 160 | ); |
4d63cfde | 161 | civicrm_api3('activity', 'create', $activityParam); |
6a488035 TO |
162 | } |
163 | if (isset($membership->membership_type_id) && $membership->membership_type_id != $oldType) { | |
4d63cfde | 164 | $membershipTypes = CRM_Member_BAO_Membership::buildOptions('membership_type_id', 'get'); |
6a488035 TO |
165 | $activityParam = array( |
166 | 'subject' => "Type changed from {$membershipTypes[$oldType]} to {$membershipTypes[$membership->membership_type_id]}", | |
167 | 'source_contact_id' => $membershipLog['modified_id'], | |
168 | 'target_contact_id' => $membership->contact_id, | |
169 | 'source_record_id' => $membership->id, | |
bd38fe3f | 170 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Change Membership Type'), |
6a488035 | 171 | 'status_id' => 2, |
6a488035 TO |
172 | 'priority_id' => 2, |
173 | 'activity_date_time' => date('Y-m-d H:i:s'), | |
6a488035 | 174 | ); |
4d63cfde | 175 | civicrm_api3('activity', 'create', $activityParam); |
6a488035 TO |
176 | } |
177 | CRM_Utils_Hook::post('edit', 'Membership', $membership->id, $membership); | |
178 | } | |
179 | else { | |
180 | CRM_Utils_Hook::post('create', 'Membership', $membership->id, $membership); | |
181 | } | |
182 | ||
183 | return $membership; | |
184 | } | |
185 | ||
186 | /** | |
187 | * Given the list of params in the params array, fetch the object | |
188 | * and store the values in the values array | |
189 | * | |
b2363ea8 TO |
190 | * @param array $params |
191 | * Input parameters to find object. | |
192 | * @param array $values | |
193 | * Output values of the object. | |
194 | * @param bool $active | |
195 | * Do you want only active memberships to. | |
6a488035 TO |
196 | * be returned |
197 | * | |
198 | * @return CRM_Member_BAO_Membership|null the found object or null | |
6a488035 | 199 | */ |
00be9182 | 200 | public static function &getValues(&$params, &$values, $active = FALSE) { |
6a488035 TO |
201 | if (empty($params)) { |
202 | return NULL; | |
203 | } | |
204 | $membership = new CRM_Member_BAO_Membership(); | |
205 | ||
206 | $membership->copyValues($params); | |
207 | $membership->find(); | |
208 | $memberships = array(); | |
209 | while ($membership->fetch()) { | |
210 | if ($active && | |
211 | (!CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', | |
e0556ebe TO |
212 | $membership->status_id, |
213 | 'is_current_member' | |
214 | )) | |
6a488035 TO |
215 | ) { |
216 | continue; | |
217 | } | |
218 | ||
219 | CRM_Core_DAO::storeValues($membership, $values[$membership->id]); | |
220 | $memberships[$membership->id] = $membership; | |
221 | } | |
222 | ||
223 | return $memberships; | |
224 | } | |
225 | ||
226 | /** | |
100fef9d | 227 | * Takes an associative array and creates a membership object |
6a488035 | 228 | * |
b2363ea8 TO |
229 | * @param array $params |
230 | * (reference ) an assoc array of name/value pairs. | |
231 | * @param array $ids | |
232 | * The array that holds all the db ids. | |
8efea814 EM |
233 | * @param bool $skipRedirect |
234 | * @param string $activityType | |
235 | * | |
236 | * @throws CRM_Core_Exception | |
6a488035 | 237 | * |
906e6120 | 238 | * @return CRM_Member_BAO_Membership|CRM_Core_Error |
6a488035 | 239 | */ |
00be9182 | 240 | public static function create(&$params, &$ids, $skipRedirect = FALSE, $activityType = 'Membership Signup') { |
6a488035 TO |
241 | // always calculate status if is_override/skipStatusCal is not true. |
242 | // giving respect to is_override during import. CRM-4012 | |
243 | ||
244 | // To skip status calculation we should use 'skipStatusCal'. | |
245 | // eg pay later membership, membership update cron CRM-3984 | |
246 | ||
8cc574cf | 247 | if (empty($params['is_override']) && empty($params['skipStatusCal'])) { |
0042ddd9 | 248 | $dates = array('start_date', 'end_date', 'join_date'); |
ca58d9b9 | 249 | $start_date = $end_date = $join_date = NULL; // declare these out of courtesy as IDEs don't pick up the setting of them below |
0042ddd9 | 250 | foreach ($dates as $date) { |
b5588874 | 251 | $$date = $params[$date] = CRM_Utils_Date::processDate(CRM_Utils_Array::value($date, $params), NULL, TRUE, 'Ymd'); |
6a488035 TO |
252 | } |
253 | ||
254 | //fix for CRM-3570, during import exclude the statuses those having is_admin = 1 | |
255 | $excludeIsAdmin = CRM_Utils_Array::value('exclude_is_admin', $params, FALSE); | |
256 | ||
257 | //CRM-3724 always skip is_admin if is_override != true. | |
8cc574cf | 258 | if (!$excludeIsAdmin && empty($params['is_override'])) { |
6a488035 TO |
259 | $excludeIsAdmin = TRUE; |
260 | } | |
261 | ||
01f6b47e | 262 | $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($start_date, $end_date, $join_date, |
5f11bbcc | 263 | 'today', $excludeIsAdmin, CRM_Utils_Array::value('membership_type_id', $params), $params |
6a488035 TO |
264 | ); |
265 | if (empty($calcStatus)) { | |
dcc4f6a7 | 266 | // Redirect the form in case of error |
267 | // @todo this redirect in the BAO layer is really bad & should be moved to the form layer | |
268 | // however since we have no idea how (if) this is triggered we can't safely move / remove it | |
269 | // NB I tried really hard to trigger this error from backoffice membership form in order to test it | |
270 | // and am convinced form validation is complete on that form WRT this error. | |
271 | $errorParams = array( | |
272 | 'message_title' => ts('No valid membership status for given dates.'), | |
273 | 'legacy_redirect_path' => 'civicrm/contact/view', | |
274 | 'legacy_redirect_query' => "reset=1&force=1&cid={$params['contact_id']}&selectedChild=member", | |
275 | ); | |
8c33a68c | 276 | throw new CRM_Core_Exception(ts('The membership cannot be saved because the status cannot be calculated.'), 0, $errorParams); |
6a488035 TO |
277 | } |
278 | $params['status_id'] = $calcStatus['id']; | |
279 | } | |
280 | ||
281 | // data cleanup only: all verifications on number of related memberships are done upstream in: | |
282 | // CRM_Member_BAO_Membership::createRelatedMemberships() | |
283 | // CRM_Contact_BAO_Relationship::relatedMemberships() | |
284 | if (isset($params['owner_membership_id'])) { | |
285 | unset($params['max_related']); | |
8efea814 EM |
286 | } |
287 | else { | |
6a488035 TO |
288 | // if membership allows related, default max_related to value in membership_type |
289 | if (!array_key_exists('max_related', $params) && !empty($params['membership_type_id'])) { | |
290 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($params['membership_type_id']); | |
291 | if (isset($membershipType['relationship_type_id'])) { | |
292 | $params['max_related'] = CRM_Utils_Array::value('max_related', $membershipType); | |
293 | } | |
294 | } | |
295 | } | |
296 | ||
297 | $transaction = new CRM_Core_Transaction(); | |
298 | ||
299 | $membership = self::add($params, $ids); | |
300 | ||
301 | if (is_a($membership, 'CRM_Core_Error')) { | |
302 | $transaction->rollback(); | |
303 | return $membership; | |
304 | } | |
305 | ||
306 | // add custom field values | |
a7488080 | 307 | if (!empty($params['custom']) && is_array($params['custom']) |
6a488035 TO |
308 | ) { |
309 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_membership', $membership->id); | |
310 | } | |
311 | ||
312 | $params['membership_id'] = $membership->id; | |
313 | if (isset($ids['membership'])) { | |
314 | $ids['contribution'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipPayment', | |
315 | $ids['membership'], | |
316 | 'contribution_id', | |
317 | 'membership_id' | |
318 | ); | |
319 | } | |
3c0201c9 | 320 | |
d5b95619 | 321 | $params['skipLineItem'] = TRUE; |
3c0201c9 | 322 | |
6a488035 | 323 | //record contribution for this membership |
8cc574cf | 324 | if (!empty($params['contribution_status_id']) && empty($params['relate_contribution_id'])) { |
8d8bd076 | 325 | $memInfo = array_merge($params, array('membership_id' => $membership->id)); |
326 | $params['contribution'] = self::recordMembershipContribution($memInfo, $ids); | |
6a488035 | 327 | } |
3c0201c9 | 328 | |
82cc6775 PN |
329 | if (!empty($params['lineItems'])) { |
330 | $params['line_item'] = $params['lineItems']; | |
331 | } | |
332 | ||
333 | //do cleanup line items if membership edit the Membership type. | |
334 | if (empty($ids['contribution']) && !empty($ids['membership'])) { | |
335 | CRM_Price_BAO_LineItem::deleteLineItems($ids['membership'], 'civicrm_membership'); | |
336 | } | |
3c0201c9 | 337 | |
82cc6775 PN |
338 | if (!empty($params['line_item']) && empty($ids['contribution'])) { |
339 | CRM_Price_BAO_LineItem::processPriceSet($membership->id, $params['line_item'], CRM_Utils_Array::value('contribution', $params)); | |
d5b95619 | 340 | } |
6a488035 TO |
341 | |
342 | //insert payment record for this membership | |
a7488080 | 343 | if (!empty($params['relate_contribution_id'])) { |
e0556ebe | 344 | CRM_Member_BAO_MembershipPayment::create(array( |
353ffa53 TO |
345 | 'membership_id' => $membership->id, |
346 | 'contribution_id' => $params['relate_contribution_id'], | |
347 | )); | |
6a488035 TO |
348 | } |
349 | ||
350 | // add activity record only during create mode and renew mode | |
351 | // also add activity if status changed CRM-3984 and CRM-2521 | |
a7488080 | 352 | if (empty($ids['membership']) || |
e0556ebe TO |
353 | $activityType == 'Membership Renewal' || !empty($params['createActivity']) |
354 | ) { | |
a7488080 | 355 | if (!empty($ids['membership'])) { |
ca58d9b9 | 356 | $data = array(); |
6a488035 TO |
357 | CRM_Core_DAO::commonRetrieveAll('CRM_Member_DAO_Membership', |
358 | 'id', | |
359 | $membership->id, | |
360 | $data, | |
361 | array('contact_id', 'membership_type_id', 'source') | |
362 | ); | |
363 | ||
364 | $membership->contact_id = $data[$membership->id]['contact_id']; | |
365 | $membership->membership_type_id = $data[$membership->id]['membership_type_id']; | |
366 | $membership->source = CRM_Utils_Array::value('source', $data[$membership->id]); | |
367 | } | |
368 | ||
369 | // since we are going to create activity record w/ | |
370 | // individual contact as a target in case of on behalf signup, | |
371 | // so get the copy of organization id, CRM-5551 | |
372 | $realMembershipContactId = $membership->contact_id; | |
373 | ||
374 | // create activity source = individual, target = org CRM-4027 | |
375 | $targetContactID = NULL; | |
a7488080 | 376 | if (!empty($params['is_for_organization'])) { |
6a488035 TO |
377 | $targetContactID = $membership->contact_id; |
378 | $membership->contact_id = CRM_Utils_Array::value('userId', $ids); | |
379 | } | |
380 | ||
381 | if (empty($membership->contact_id) && (!empty($membership->owner_membership_id))) { | |
382 | $membership->contact_id = $realMembershipContactId; | |
383 | } | |
384 | ||
a7488080 | 385 | if (!empty($ids['membership']) && $activityType != 'Membership Signup') { |
6a488035 | 386 | CRM_Activity_BAO_Activity::addActivity($membership, $activityType, $targetContactID); |
e0556ebe TO |
387 | } |
388 | elseif (empty($ids['membership'])) { | |
6a488035 TO |
389 | CRM_Activity_BAO_Activity::addActivity($membership, $activityType, $targetContactID); |
390 | } | |
391 | ||
392 | // we might created activity record w/ individual | |
393 | // contact as target so update membership object w/ | |
394 | // original organization id, CRM-5551 | |
395 | $membership->contact_id = $realMembershipContactId; | |
396 | } | |
397 | ||
398 | $transaction->commit(); | |
399 | ||
400 | self::createRelatedMemberships($params, $membership); | |
401 | ||
402 | // do not add to recent items for import, CRM-4399 | |
a7488080 | 403 | if (empty($params['skipRecentView'])) { |
6a488035 TO |
404 | $url = CRM_Utils_System::url('civicrm/contact/view/membership', |
405 | "action=view&reset=1&id={$membership->id}&cid={$membership->contact_id}&context=home" | |
406 | ); | |
e0556ebe | 407 | if (empty($membership->membership_type_id)) {// ie in an update situation |
6a488035 TO |
408 | $membership->find(TRUE); |
409 | } | |
410 | $membershipTypes = CRM_Member_PseudoConstant::membershipType(); | |
411 | $title = CRM_Contact_BAO_Contact::displayName($membership->contact_id) . ' - ' . ts('Membership Type:') . ' ' . $membershipTypes[$membership->membership_type_id]; | |
412 | ||
413 | $recentOther = array(); | |
414 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::UPDATE)) { | |
415 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
416 | "action=update&reset=1&id={$membership->id}&cid={$membership->contact_id}&context=home" | |
417 | ); | |
418 | } | |
419 | if (CRM_Core_Permission::checkActionPermission('CiviMember', CRM_Core_Action::DELETE)) { | |
420 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/membership', | |
421 | "action=delete&reset=1&id={$membership->id}&cid={$membership->contact_id}&context=home" | |
422 | ); | |
423 | } | |
424 | ||
425 | // add the recently created Membership | |
426 | CRM_Utils_Recent::add($title, | |
427 | $url, | |
428 | $membership->id, | |
429 | 'Membership', | |
430 | $membership->contact_id, | |
431 | NULL, | |
432 | $recentOther | |
433 | ); | |
434 | } | |
435 | ||
436 | return $membership; | |
437 | } | |
438 | ||
439 | /** | |
100fef9d | 440 | * Check the membership extended through relationship |
6a488035 | 441 | * |
b2363ea8 TO |
442 | * @param int $membershipId |
443 | * Membership id. | |
444 | * @param int $contactId | |
445 | * Contact id. | |
ada8a833 | 446 | * |
b2363ea8 | 447 | * @param int $action |
6a488035 | 448 | * |
608e6658 | 449 | * @return array |
a6c01b45 | 450 | * array of contact_id of all related contacts. |
6a488035 | 451 | */ |
00be9182 | 452 | public static function checkMembershipRelationship($membershipId, $contactId, $action = CRM_Core_Action::ADD) { |
6a488035 TO |
453 | $contacts = array(); |
454 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id'); | |
455 | ||
456 | $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipTypeID); | |
457 | $relationships = array(); | |
458 | if (isset($membershipType['relationship_type_id'])) { | |
459 | $relationships = CRM_Contact_BAO_Relationship::getRelationship($contactId, | |
460 | CRM_Contact_BAO_Relationship::CURRENT | |
461 | ); | |
462 | if ($action & CRM_Core_Action::UPDATE) { | |
463 | $pastRelationships = CRM_Contact_BAO_Relationship::getRelationship($contactId, | |
464 | CRM_Contact_BAO_Relationship::PAST | |
465 | ); | |
466 | $relationships = array_merge($relationships, $pastRelationships); | |
467 | } | |
468 | } | |
469 | ||
470 | if (!empty($relationships)) { | |
471 | // check for each contact relationships | |
472 | foreach ($relationships as $values) { | |
473 | //get details of the relationship type | |
474 | $relType = array('id' => $values['civicrm_relationship_type_id']); | |
475 | $relValues = array(); | |
476 | CRM_Contact_BAO_RelationshipType::retrieve($relType, $relValues); | |
477 | // Check if contact's relationship type exists in membership type | |
e0556ebe TO |
478 | $relTypeDirs = array(); |
479 | $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']); | |
6a488035 TO |
480 | $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']); |
481 | $bidirectional = FALSE; | |
482 | foreach ($relTypeIds as $key => $value) { | |
483 | $relTypeDirs[] = $value . '_' . $relDirections[$key]; | |
484 | if (in_array($value, $relType) && | |
485 | $relValues['name_a_b'] == $relValues['name_b_a'] | |
486 | ) { | |
487 | $bidirectional = TRUE; | |
488 | break; | |
489 | } | |
490 | } | |
491 | $relTypeDir = $values['civicrm_relationship_type_id'] . '_' . $values['rtype']; | |
492 | if ($bidirectional || in_array($relTypeDir, $relTypeDirs)) { | |
493 | // $values['status'] is going to have value for | |
494 | // current or past relationships. | |
495 | $contacts[$values['cid']] = $values['status']; | |
496 | } | |
497 | } | |
498 | } | |
499 | ||
500 | // Sort by contact_id ascending | |
501 | ksort($contacts); | |
502 | return $contacts; | |
503 | } | |
504 | ||
505 | /** | |
506 | * Takes a bunch of params that are needed to match certain criteria and | |
507 | * retrieves the relevant objects. We'll tweak this function to be more | |
508 | * full featured over a period of time. This is the inverse function of | |
509 | * create. It also stores all the retrieved values in the default array | |
510 | * | |
b2363ea8 TO |
511 | * @param array $params |
512 | * (reference ) an assoc array of name/value pairs. | |
513 | * @param array $defaults | |
514 | * (reference ) an assoc array to hold the name / value pairs. | |
6a488035 | 515 | * in a hierarchical manner |
8efea814 | 516 | * |
16b10e64 | 517 | * @return CRM_Member_BAO_Membership |
6a488035 | 518 | */ |
00be9182 | 519 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
520 | $membership = new CRM_Member_DAO_Membership(); |
521 | ||
522 | $membership->copyValues($params); | |
523 | ||
524 | if ($membership->find(TRUE)) { | |
525 | CRM_Core_DAO::storeValues($membership, $defaults); | |
526 | ||
527 | //get the membership status and type values. | |
528 | $statusANDType = self::getStatusANDTypeValues($membership->id); | |
529 | foreach (array( | |
e0556ebe | 530 | 'status', |
21dfd5f5 | 531 | 'membership_type', |
e0556ebe | 532 | ) as $fld) { |
6a488035 TO |
533 | $defaults[$fld] = CRM_Utils_Array::value($fld, $statusANDType[$membership->id]); |
534 | } | |
a7488080 | 535 | if (!empty($statusANDType[$membership->id]['is_current_member'])) { |
6a488035 TO |
536 | $defaults['active'] = TRUE; |
537 | } | |
538 | ||
539 | $membership->free(); | |
540 | ||
541 | return $membership; | |
542 | } | |
543 | ||
544 | return NULL; | |
545 | } | |
546 | ||
547 | /** | |
548 | * | |
c490a46a | 549 | * get membership status and membership type values |
6a488035 | 550 | * |
b2363ea8 TO |
551 | * @param int $membershipId |
552 | * Membership id of values to return. | |
6a488035 | 553 | * |
a6c01b45 | 554 | * @return array |
16b10e64 | 555 | * Array of key value pairs |
6a488035 | 556 | */ |
00be9182 | 557 | public static function getStatusANDTypeValues($membershipId) { |
6a488035 TO |
558 | $values = array(); |
559 | if (!$membershipId) { | |
560 | return $values; | |
561 | } | |
562 | $sql = ' | |
563 | SELECT membership.id as id, | |
564 | status.id as status_id, | |
565 | status.label as status, | |
566 | status.is_current_member as is_current_member, | |
567 | type.id as membership_type_id, | |
568 | type.name as membership_type, | |
569 | type.relationship_type_id as relationship_type_id | |
570 | FROM civicrm_membership membership | |
571 | INNER JOIN civicrm_membership_status status ON ( status.id = membership.status_id ) | |
572 | INNER JOIN civicrm_membership_type type ON ( type.id = membership.membership_type_id ) | |
573 | WHERE membership.id = %1'; | |
574 | $dao = CRM_Core_DAO::executeQuery($sql, array(1 => array($membershipId, 'Positive'))); | |
e0556ebe TO |
575 | $properties = array( |
576 | 'status', | |
577 | 'status_id', | |
578 | 'membership_type', | |
579 | 'membership_type_id', | |
580 | 'is_current_member', | |
21dfd5f5 | 581 | 'relationship_type_id', |
e0556ebe | 582 | ); |
6a488035 TO |
583 | while ($dao->fetch()) { |
584 | foreach ($properties as $property) { | |
585 | $values[$dao->id][$property] = $dao->$property; | |
586 | } | |
587 | } | |
588 | ||
589 | return $values; | |
590 | } | |
591 | ||
3506b6cd | 592 | /** |
100fef9d | 593 | * Delete membership. |
f5e53870 | 594 | * Wrapper for most delete calls. Use this unless you JUST want to delete related memberships w/o deleting the parent. |
3506b6cd | 595 | * |
b2363ea8 TO |
596 | * @param int $membershipId |
597 | * Membership id that needs to be deleted. | |
3506b6cd | 598 | * |
3506b6cd | 599 | * |
608e6658 | 600 | * @return int $results id of deleted Membership on success, false otherwise |
3506b6cd | 601 | */ |
00be9182 | 602 | public static function del($membershipId) { |
3506b6cd DG |
603 | //delete related first and then delete parent. |
604 | self::deleteRelatedMemberships($membershipId); | |
d824fb6e | 605 | return self::deleteMembership($membershipId); |
3506b6cd | 606 | } |
d824fb6e | 607 | |
6a488035 | 608 | /** |
100fef9d | 609 | * Delete membership. |
6a488035 | 610 | * |
b2363ea8 TO |
611 | * @param int $membershipId |
612 | * Membership id that needs to be deleted. | |
6a488035 | 613 | * |
6a488035 | 614 | * |
608e6658 | 615 | * @return int $results id of deleted Membership on success, false otherwise |
6a488035 | 616 | */ |
00be9182 | 617 | public static function deleteMembership($membershipId) { |
46d8f506 DL |
618 | // CRM-12147, retrieve membership data before we delete it for hooks |
619 | $params = array('id' => $membershipId); | |
620 | $memValues = array(); | |
621 | $memberships = self::getValues($params, $memValues); | |
3506b6cd | 622 | |
46d8f506 DL |
623 | $membership = $memberships[$membershipId]; |
624 | ||
625 | CRM_Utils_Hook::pre('delete', 'Membership', $membershipId, $memValues); | |
6a488035 TO |
626 | |
627 | $transaction = new CRM_Core_Transaction(); | |
628 | ||
629 | $results = NULL; | |
630 | //delete activity record | |
631 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); | |
632 | ||
633 | $params = array(); | |
e0556ebe TO |
634 | $deleteActivity = FALSE; |
635 | $membershipActivities = array( | |
46d8f506 DL |
636 | 'Membership Signup', |
637 | 'Membership Renewal', | |
638 | 'Change Membership Status', | |
639 | 'Change Membership Type', | |
21dfd5f5 | 640 | 'Membership Renewal Reminder', |
46d8f506 | 641 | ); |
e0556ebe | 642 | foreach ($membershipActivities as $membershipActivity) { |
6a488035 TO |
643 | $activityId = array_search($membershipActivity, $activityTypes); |
644 | if ($activityId) { | |
645 | $params['activity_type_id'][] = $activityId; | |
e0556ebe | 646 | $deleteActivity = TRUE; |
6a488035 TO |
647 | } |
648 | } | |
649 | if ($deleteActivity) { | |
650 | $params['source_record_id'] = $membershipId; | |
46d8f506 | 651 | CRM_Activity_BAO_Activity::deleteActivity($params); |
6a488035 TO |
652 | } |
653 | self::deleteMembershipPayment($membershipId); | |
654 | ||
d0fbc816 | 655 | $results = $membership->delete(); |
6a488035 TO |
656 | $transaction->commit(); |
657 | ||
658 | CRM_Utils_Hook::post('delete', 'Membership', $membership->id, $membership); | |
659 | ||
660 | // delete the recently created Membership | |
661 | $membershipRecent = array( | |
662 | 'id' => $membershipId, | |
663 | 'type' => 'Membership', | |
664 | ); | |
665 | CRM_Utils_Recent::del($membershipRecent); | |
666 | ||
667 | return $results; | |
668 | } | |
669 | ||
3506b6cd | 670 | /** |
100fef9d | 671 | * Delete related memberships |
3506b6cd DG |
672 | * |
673 | * @param int $ownerMembershipId | |
674 | * @param int $contactId | |
675 | * | |
676 | * @return null | |
3506b6cd | 677 | */ |
00be9182 | 678 | public static function deleteRelatedMemberships($ownerMembershipId, $contactId = NULL) { |
3506b6cd | 679 | if (!$ownerMembershipId && !$contactId) { |
608e6658 | 680 | return FALSE; |
3506b6cd DG |
681 | } |
682 | ||
683 | $membership = new CRM_Member_DAO_Membership(); | |
684 | $membership->owner_membership_id = $ownerMembershipId; | |
685 | ||
686 | if ($contactId) { | |
687 | $membership->contact_id = $contactId; | |
688 | } | |
689 | ||
690 | $membership->find(); | |
691 | while ($membership->fetch()) { | |
692 | //delete related first and then delete parent. | |
693 | self::deleteRelatedMemberships($membership->id); | |
694 | self::deleteMembership($membership->id); | |
695 | } | |
696 | $membership->free(); | |
697 | } | |
698 | ||
6a488035 | 699 | /** |
100fef9d | 700 | * Obtain active/inactive memberships from the list of memberships passed to it. |
6a488035 | 701 | * |
b2363ea8 TO |
702 | * @param array $memberships |
703 | * Membership records. | |
704 | * @param string $status | |
705 | * Active or inactive. | |
6a488035 | 706 | * |
a6c01b45 CW |
707 | * @return array |
708 | * array of memberships based on status | |
6a488035 | 709 | */ |
00be9182 | 710 | public static function activeMembers($memberships, $status = 'active') { |
6a488035 TO |
711 | $actives = array(); |
712 | if ($status == 'active') { | |
713 | foreach ($memberships as $f => $v) { | |
a7488080 | 714 | if (!empty($v['active'])) { |
6a488035 TO |
715 | $actives[$f] = $v; |
716 | } | |
717 | } | |
718 | return $actives; | |
719 | } | |
720 | elseif ($status == 'inactive') { | |
721 | foreach ($memberships as $f => $v) { | |
a7488080 | 722 | if (empty($v['active'])) { |
6a488035 TO |
723 | $actives[$f] = $v; |
724 | } | |
725 | } | |
726 | return $actives; | |
727 | } | |
728 | return NULL; | |
729 | } | |
730 | ||
731 | /** | |
100fef9d | 732 | * Build Membership Block in Contribution Pages |
6a488035 | 733 | * |
b2363ea8 TO |
734 | * @param CRM_Core_Form $form |
735 | * Form object. | |
736 | * @param int $pageID | |
737 | * Unused?. | |
738 | * @param int $cid | |
739 | * Contact checked for having a current membership for a particular membership. | |
740 | * @param bool $formItems | |
741 | * @param int $selectedMembershipTypeID | |
742 | * Selected membership id. | |
743 | * @param bool $thankPage | |
744 | * Thank you page. | |
e46e9a0b EM |
745 | * @param null $isTest |
746 | * | |
a6c01b45 CW |
747 | * @return bool |
748 | * Is this a separate membership payment | |
6a488035 | 749 | * |
6a488035 | 750 | */ |
608e6658 | 751 | public static function buildMembershipBlock( |
500cfe81 | 752 | &$form, |
353ffa53 TO |
753 | $pageID, |
754 | $cid, | |
755 | $formItems = FALSE, | |
756 | $selectedMembershipTypeID = NULL, | |
757 | $thankPage = FALSE, | |
758 | $isTest = NULL | |
6a488035 TO |
759 | ) { |
760 | ||
761 | $separateMembershipPayment = FALSE; | |
762 | if ($form->_membershipBlock) { | |
763 | $form->_currentMemberships = array(); | |
6a488035 | 764 | |
e0556ebe TO |
765 | $membershipBlock = $form->_membershipBlock; |
766 | $membershipTypeIds = $membershipTypes = $radio = array(); | |
6a488035 TO |
767 | $membershipPriceset = (!empty($form->_priceSetId) && $form->_useForMember) ? TRUE : FALSE; |
768 | ||
769 | $allowAutoRenewMembership = $autoRenewOption = FALSE; | |
770 | $autoRenewMembershipTypeOptions = array(); | |
771 | ||
772 | $paymentProcessor = CRM_Core_PseudoConstant::paymentProcessor(FALSE, FALSE, 'is_recur = 1'); | |
773 | ||
774 | $separateMembershipPayment = CRM_Utils_Array::value('is_separate_payment', $membershipBlock); | |
775 | ||
776 | if ($membershipPriceset) { | |
777 | foreach ($form->_priceSet['fields'] as $pField) { | |
778 | if (empty($pField['options'])) { | |
779 | continue; | |
780 | } | |
781 | foreach ($pField['options'] as $opId => $opValues) { | |
a7488080 | 782 | if (empty($opValues['membership_type_id'])) { |
6a488035 TO |
783 | continue; |
784 | } | |
785 | $membershipTypeIds[$opValues['membership_type_id']] = $opValues['membership_type_id']; | |
786 | } | |
787 | } | |
788 | } | |
a7488080 | 789 | elseif (!empty($membershipBlock['membership_types'])) { |
6a488035 TO |
790 | $membershipTypeIds = explode(',', $membershipBlock['membership_types']); |
791 | } | |
792 | ||
793 | if (!empty($membershipTypeIds)) { | |
794 | //set status message if wrong membershipType is included in membershipBlock | |
795 | if (isset($form->_mid) && !$membershipPriceset) { | |
796 | $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
797 | $form->_mid, | |
798 | 'membership_type_id' | |
799 | ); | |
800 | if (!in_array($membershipTypeID, $membershipTypeIds)) { | |
801 | CRM_Core_Session::setStatus(ts("Oops. The membership you're trying to renew appears to be invalid. Contact your site administrator if you need assistance. If you continue, you will be issued a new membership."), ts('Invalid Membership'), 'error'); | |
802 | } | |
803 | } | |
804 | ||
805 | $membershipTypeValues = self::buildMembershipTypeValues($form, $membershipTypeIds); | |
806 | $form->_membershipTypeValues = $membershipTypeValues; | |
807 | $endDate = NULL; | |
808 | foreach ($membershipTypeIds as $value) { | |
809 | $memType = $membershipTypeValues[$value]; | |
810 | if ($selectedMembershipTypeID != NULL) { | |
811 | if ($memType['id'] == $selectedMembershipTypeID) { | |
812 | $form->assign('minimum_fee', | |
813 | CRM_Utils_Array::value('minimum_fee', $memType) | |
814 | ); | |
815 | $form->assign('membership_name', $memType['name']); | |
816 | if (!$thankPage && $cid) { | |
817 | $membership = new CRM_Member_DAO_Membership(); | |
818 | $membership->contact_id = $cid; | |
819 | $membership->membership_type_id = $memType['id']; | |
820 | if ($membership->find(TRUE)) { | |
821 | $form->assign('renewal_mode', TRUE); | |
822 | $memType['current_membership'] = $membership->end_date; | |
823 | $form->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
824 | } | |
825 | } | |
826 | $membershipTypes[] = $memType; | |
827 | } | |
828 | } | |
829 | elseif ($memType['is_active']) { | |
830 | $javascriptMethod = NULL; | |
831 | $allowAutoRenewOpt = 1; | |
e0556ebe | 832 | if (is_array($form->_paymentProcessors)) { |
6a488035 TO |
833 | foreach ($form->_paymentProcessors as $id => $val) { |
834 | if (!$val['is_recur']) { | |
e0556ebe | 835 | $allowAutoRenewOpt = 0; |
6a488035 | 836 | continue; |
e0556ebe | 837 | } |
6a488035 TO |
838 | } |
839 | } | |
840 | ||
841 | $javascriptMethod = array('onclick' => "return showHideAutoRenew( this.value );"); | |
e0556ebe | 842 | $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = (int) $allowAutoRenewOpt * CRM_Utils_Array::value($value, CRM_Utils_Array::value('auto_renew', $form->_membershipBlock));; |
6a488035 TO |
843 | |
844 | if ($allowAutoRenewOpt) { | |
845 | $allowAutoRenewMembership = TRUE; | |
846 | } | |
847 | ||
848 | //add membership type. | |
849 | $radio[$memType['id']] = $form->createElement('radio', NULL, NULL, NULL, | |
850 | $memType['id'], $javascriptMethod | |
851 | ); | |
852 | if ($cid) { | |
853 | $membership = new CRM_Member_DAO_Membership(); | |
854 | $membership->contact_id = $cid; | |
855 | $membership->membership_type_id = $memType['id']; | |
856 | ||
857 | //show current membership, skip pending and cancelled membership records, | |
858 | //because we take first membership record id for renewal | |
859 | $membership->whereAdd('status_id != 5 AND status_id !=6'); | |
860 | ||
861 | if (!is_null($isTest)) { | |
862 | $membership->is_test = $isTest; | |
863 | } | |
864 | ||
865 | //CRM-4297 | |
866 | $membership->orderBy('end_date DESC'); | |
867 | ||
868 | if ($membership->find(TRUE)) { | |
869 | if (!$membership->end_date) { | |
870 | unset($radio[$memType['id']]); | |
871 | $form->assign('islifetime', TRUE); | |
872 | continue; | |
873 | } | |
874 | $form->assign('renewal_mode', TRUE); | |
875 | $form->_currentMemberships[$membership->membership_type_id] = $membership->membership_type_id; | |
876 | $memType['current_membership'] = $membership->end_date; | |
877 | if (!$endDate) { | |
878 | $endDate = $memType['current_membership']; | |
879 | $form->_defaultMemTypeId = $memType['id']; | |
880 | } | |
881 | if ($memType['current_membership'] < $endDate) { | |
882 | $endDate = $memType['current_membership']; | |
883 | $form->_defaultMemTypeId = $memType['id']; | |
884 | } | |
885 | } | |
886 | } | |
887 | $membershipTypes[] = $memType; | |
888 | } | |
889 | } | |
890 | } | |
891 | ||
892 | $form->assign('showRadio', $formItems); | |
893 | if ($formItems) { | |
894 | if (!$membershipPriceset) { | |
895 | if (!$membershipBlock['is_required']) { | |
896 | $form->assign('showRadioNoThanks', TRUE); | |
897 | $radio[''] = $form->createElement('radio', NULL, NULL, NULL, 'no_thanks', NULL); | |
898 | $form->addGroup($radio, 'selectMembership', NULL); | |
899 | } | |
900 | elseif ($membershipBlock['is_required'] && count($radio) == 1) { | |
901 | $temp = array_keys($radio); | |
902 | $form->add('hidden', 'selectMembership', $temp[0], array('id' => 'selectMembership')); | |
903 | $form->assign('singleMembership', TRUE); | |
904 | $form->assign('showRadio', FALSE); | |
905 | } | |
906 | else { | |
907 | $form->addGroup($radio, 'selectMembership', NULL); | |
908 | } | |
909 | ||
910 | $form->addRule('selectMembership', ts('Please select one of the memberships.'), 'required'); | |
911 | } | |
912 | else { | |
9da8dc8c | 913 | $autoRenewOption = CRM_Price_BAO_PriceSet::checkAutoRenewForPriceSet($form->_priceSetId); |
6a488035 TO |
914 | $form->assign('autoRenewOption', $autoRenewOption); |
915 | } | |
916 | ||
917 | if (!$form->_values['is_pay_later'] && is_array($form->_paymentProcessors) && ($allowAutoRenewMembership || $autoRenewOption)) { | |
918 | $form->addElement('checkbox', 'auto_renew', ts('Please renew my membership automatically.')); | |
919 | } | |
920 | ||
921 | } | |
922 | ||
923 | $form->assign('membershipBlock', $membershipBlock); | |
924 | $form->assign('membershipTypes', $membershipTypes); | |
925 | $form->assign('allowAutoRenewMembership', $allowAutoRenewMembership); | |
926 | $form->assign('autoRenewMembershipTypeOptions', json_encode($autoRenewMembershipTypeOptions)); | |
927 | ||
928 | //give preference to user submitted auto_renew value. | |
929 | $takeUserSubmittedAutoRenew = (!empty($_POST) || $form->isSubmitted()) ? TRUE : FALSE; | |
930 | $form->assign('takeUserSubmittedAutoRenew', $takeUserSubmittedAutoRenew); | |
931 | } | |
932 | ||
933 | return $separateMembershipPayment; | |
934 | } | |
935 | ||
936 | /** | |
100fef9d | 937 | * Return Membership Block info in Contribution Pages |
6a488035 | 938 | * |
b2363ea8 TO |
939 | * @param int $pageID |
940 | * Contribution page id. | |
e46e9a0b EM |
941 | * |
942 | * @return array|null | |
6a488035 | 943 | * |
6a488035 | 944 | */ |
00be9182 | 945 | public static function getMembershipBlock($pageID) { |
e0556ebe TO |
946 | $membershipBlock = array(); |
947 | $dao = new CRM_Member_DAO_MembershipBlock(); | |
6a488035 TO |
948 | $dao->entity_table = 'civicrm_contribution_page'; |
949 | ||
950 | $dao->entity_id = $pageID; | |
951 | $dao->is_active = 1; | |
952 | if ($dao->find(TRUE)) { | |
953 | CRM_Core_DAO::storeValues($dao, $membershipBlock); | |
a7488080 | 954 | if (!empty($membershipBlock['membership_types'])) { |
6a488035 TO |
955 | $membershipTypes = unserialize($membershipBlock['membership_types']); |
956 | if (!is_array($membershipTypes)) { | |
957 | return $membershipBlock; | |
958 | } | |
959 | $memTypes = array(); | |
960 | foreach ($membershipTypes as $key => $value) { | |
961 | $membershipBlock['auto_renew'][$key] = $value; | |
962 | $memTypes[$key] = $key; | |
963 | } | |
964 | $membershipBlock['membership_types'] = implode(',', $memTypes); | |
965 | } | |
966 | } | |
967 | else { | |
968 | return NULL; | |
969 | } | |
970 | ||
971 | return $membershipBlock; | |
972 | } | |
973 | ||
974 | /** | |
100fef9d | 975 | * Return a current membership of given contact |
c490a46a | 976 | * NB: if more than one membership meets criteria, a randomly selected one is returned. |
6a488035 | 977 | * |
b2363ea8 TO |
978 | * @param int $contactID |
979 | * Contact id. | |
980 | * @param int $memType | |
981 | * Membership type, null to retrieve all types. | |
6a488035 | 982 | * @param int $isTest |
b2363ea8 TO |
983 | * @param int $membershipId |
984 | * If provided, then determine if it is current. | |
985 | * @param bool $onlySameParentOrg | |
986 | * True if only Memberships with same parent org as the $memType wanted, false otherwise. | |
e46e9a0b EM |
987 | * |
988 | * @return array|bool | |
6a488035 | 989 | */ |
00be9182 | 990 | public static function getContactMembership($contactID, $memType, $isTest, $membershipId = NULL, $onlySameParentOrg = FALSE) { |
6a488035 TO |
991 | $dao = new CRM_Member_DAO_Membership(); |
992 | if ($membershipId) { | |
993 | $dao->id = $membershipId; | |
994 | } | |
995 | $dao->contact_id = $contactID; | |
996 | $dao->membership_type_id = $memType; | |
997 | ||
998 | //fetch proper membership record. | |
999 | if ($isTest) { | |
1000 | $dao->is_test = $isTest; | |
1001 | } | |
1002 | else { | |
1003 | $dao->whereAdd('is_test IS NULL OR is_test = 0'); | |
1004 | } | |
5624f515 | 1005 | |
6a488035 | 1006 | //avoid pending membership as current membership: CRM-3027 |
b5a62499 PN |
1007 | $statusIds[] = array_search('Pending', CRM_Member_PseudoConstant::membershipStatus()); |
1008 | if (!$membershipId) { | |
7ff60806 PN |
1009 | // CRM-15475 |
1010 | $statusIds[] = array_search( | |
4c16123d | 1011 | 'Cancelled', |
7ff60806 | 1012 | CRM_Member_PseudoConstant::membershipStatus( |
4c16123d EM |
1013 | NULL, |
1014 | " name = 'Cancelled' ", | |
1015 | 'name', | |
1016 | FALSE, | |
7ff60806 PN |
1017 | TRUE |
1018 | ) | |
1019 | ); | |
b5a62499 | 1020 | } |
e0556ebe | 1021 | $dao->whereAdd('status_id NOT IN ( ' . implode(',', $statusIds) . ')'); |
5624f515 | 1022 | |
6a488035 TO |
1023 | // order by start date to find most recent membership first, CRM-4545 |
1024 | $dao->orderBy('start_date DESC'); | |
1025 | ||
1026 | // CRM-8141 | |
1027 | if ($onlySameParentOrg && $memType) { | |
1028 | // require the same parent org as the $memType | |
1029 | $params = array('id' => $memType); | |
5901ddf9 | 1030 | $membershipType = array(); |
6a488035 TO |
1031 | if (CRM_Member_BAO_MembershipType::retrieve($params, $membershipType)) { |
1032 | $memberTypesSameParentOrg = CRM_Member_BAO_MembershipType::getMembershipTypesByOrg($membershipType['member_of_contact_id']); | |
1033 | $memberTypesSameParentOrgList = implode(',', array_keys($memberTypesSameParentOrg)); | |
1034 | $dao->whereAdd('membership_type_id IN (' . $memberTypesSameParentOrgList . ')'); | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | if ($dao->find(TRUE)) { | |
1039 | $membership = array(); | |
1040 | CRM_Core_DAO::storeValues($dao, $membership); | |
1041 | $membership['is_current_member'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', | |
1042 | $membership['status_id'], | |
1043 | 'is_current_member', 'id' | |
1044 | ); | |
1045 | return $membership; | |
1046 | } | |
1047 | ||
1048 | // CRM-8141 | |
1049 | if ($onlySameParentOrg && $memType) { | |
1050 | // see if there is a membership that has same parent as $memType but different parent than $membershipID | |
5682e889 | 1051 | if ($dao->id && CRM_Core_Permission::check('edit memberships')) { |
1052 | // CRM-10016, This is probably a backend renewal, and make sure we return the same membership thats being renewed. | |
e0556ebe | 1053 | $dao->whereAdd(); |
5682e889 | 1054 | } |
1055 | else { | |
1056 | unset($dao->id); | |
1057 | } | |
6a488035 TO |
1058 | |
1059 | unset($dao->membership_type_id); | |
1060 | if ($dao->find(TRUE)) { | |
1061 | $membership = array(); | |
1062 | CRM_Core_DAO::storeValues($dao, $membership); | |
1063 | $membership['is_current_member'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', | |
1064 | $membership['status_id'], | |
1065 | 'is_current_member', 'id' | |
1066 | ); | |
1067 | return $membership; | |
1068 | } | |
1069 | } | |
1070 | return FALSE; | |
1071 | } | |
1072 | ||
1073 | /** | |
1074 | * Combine all the importable fields from the lower levels object | |
1075 | * | |
b2363ea8 TO |
1076 | * @param string $contactType |
1077 | * Contact type. | |
1078 | * @param bool $status | |
6a488035 | 1079 | * |
a6c01b45 CW |
1080 | * @return array |
1081 | * array of importable Fields | |
6a488035 | 1082 | */ |
00be9182 | 1083 | public static function &importableFields($contactType = 'Individual', $status = TRUE) { |
6a488035 TO |
1084 | if (!self::$_importableFields) { |
1085 | if (!self::$_importableFields) { | |
1086 | self::$_importableFields = array(); | |
1087 | } | |
1088 | ||
1089 | if (!$status) { | |
1090 | $fields = array('' => array('title' => '- ' . ts('do not import') . ' -')); | |
1091 | } | |
1092 | else { | |
1093 | $fields = array('' => array('title' => '- ' . ts('Membership Fields') . ' -')); | |
1094 | } | |
1095 | ||
1096 | $tmpFields = CRM_Member_DAO_Membership::import(); | |
1097 | $contactFields = CRM_Contact_BAO_Contact::importableFields($contactType, NULL); | |
1098 | ||
1099 | // Using new Dedupe rule. | |
1100 | $ruleParams = array( | |
1101 | 'contact_type' => $contactType, | |
e0556ebe | 1102 | 'used' => 'Unsupervised', |
6a488035 TO |
1103 | ); |
1104 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
1105 | ||
1106 | $tmpContactField = array(); | |
1107 | if (is_array($fieldsArray)) { | |
1108 | foreach ($fieldsArray as $value) { | |
1109 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
1110 | $value, | |
1111 | 'id', | |
1112 | 'column_name' | |
1113 | ); | |
1114 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; | |
1115 | $tmpContactField[trim($value)] = CRM_Utils_Array::value(trim($value), $contactFields); | |
1116 | if (!$status) { | |
1117 | $title = $tmpContactField[trim($value)]['title'] . " " . ts('(match to contact)'); | |
1118 | } | |
1119 | else { | |
1120 | $title = $tmpContactField[trim($value)]['title']; | |
1121 | } | |
1122 | $tmpContactField[trim($value)]['title'] = $title; | |
1123 | } | |
1124 | } | |
1125 | $tmpContactField['external_identifier'] = $contactFields['external_identifier']; | |
1126 | $tmpContactField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " " . ts('(match to contact)'); | |
1127 | ||
1128 | $tmpFields['membership_contact_id']['title'] = $tmpFields['membership_contact_id']['title'] . " " . ts('(match to contact)');; | |
1129 | ||
1130 | $fields = array_merge($fields, $tmpContactField); | |
1131 | $fields = array_merge($fields, $tmpFields); | |
1132 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Membership')); | |
1133 | self::$_importableFields = $fields; | |
1134 | } | |
1135 | return self::$_importableFields; | |
1136 | } | |
1137 | ||
1138 | /** | |
100fef9d | 1139 | * Get all exportable fields |
6a488035 TO |
1140 | * |
1141 | * @retun array return array of all exportable fields | |
6a488035 | 1142 | */ |
00be9182 | 1143 | public static function &exportableFields() { |
6a488035 | 1144 | $expFieldMembership = CRM_Member_DAO_Membership::export(); |
6a488035 TO |
1145 | |
1146 | $expFieldsMemType = CRM_Member_DAO_MembershipType::export(); | |
e0556ebe TO |
1147 | $fields = array_merge($expFieldMembership, $expFieldsMemType); |
1148 | $fields = array_merge($fields, $expFieldMembership); | |
6a488035 | 1149 | $membershipStatus = array( |
e0556ebe TO |
1150 | 'membership_status' => array( |
1151 | 'title' => 'Membership Status', | |
6a488035 TO |
1152 | 'name' => 'membership_status', |
1153 | 'type' => CRM_Utils_Type::T_STRING, | |
1154 | 'where' => 'civicrm_membership_status.name', | |
21dfd5f5 | 1155 | ), |
e0556ebe | 1156 | ); |
6a488035 TO |
1157 | //CRM-6161 fix for customdata export |
1158 | $fields = array_merge($fields, $membershipStatus, CRM_Core_BAO_CustomField::getFieldsForImport('Membership')); | |
1159 | return $fields; | |
1160 | } | |
1161 | ||
1162 | /** | |
100fef9d | 1163 | * Get membership joins/renewals for a specified membership |
4e636a74 AH |
1164 | * type. Specifically, retrieves a count of memberships whose "Membership |
1165 | * Signup" or "Membership Renewal" activity falls in the given date range. | |
8ef12e64 | 1166 | * Dates match the pattern "yyyy-mm-dd". |
6a488035 | 1167 | * |
b2363ea8 TO |
1168 | * @param int $membershipTypeId |
1169 | * Membership type id. | |
1170 | * @param int $startDate | |
1171 | * Date on which to start counting. | |
1172 | * @param int $endDate | |
1173 | * Date on which to end counting. | |
e46e9a0b EM |
1174 | * @param bool|int $isTest if true, membership is for a test site |
1175 | * @param bool|int $isOwner if true, only retrieve membership records for owners //LCD | |
6a488035 | 1176 | * |
df8d3074 | 1177 | * @return int |
a6c01b45 | 1178 | * the number of members of type $membershipTypeId whose |
688d37c6 | 1179 | * start_date is between $startDate and $endDate |
6a488035 | 1180 | */ |
6a488035 | 1181 | public static function getMembershipStarts($membershipTypeId, $startDate, $endDate, $isTest = 0, $isOwner = 0) { |
8ef12e64 | 1182 | |
4e636a74 AH |
1183 | $testClause = 'membership.is_test = 1'; |
1184 | if (!$isTest) { | |
1185 | $testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )'; | |
1186 | } | |
8ef12e64 | 1187 | |
4e636a74 AH |
1188 | if (!self::$_signupActType || !self::$_renewalActType) { |
1189 | self::_getActTypes(); | |
1190 | } | |
8ef12e64 | 1191 | |
4e636a74 AH |
1192 | if (!self::$_signupActType || !self::$_renewalActType) { |
1193 | return 0; | |
1194 | } | |
1195 | ||
1196 | $query = " | |
1197 | SELECT COUNT(DISTINCT membership.id) as member_count | |
1198 | FROM civicrm_membership membership | |
1199 | INNER JOIN civicrm_activity activity ON (activity.source_record_id = membership.id AND activity.activity_type_id in (%1, %2)) | |
1200 | INNER JOIN civicrm_membership_status status ON ( membership.status_id = status.id AND status.is_current_member = 1 ) | |
1201 | INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND contact.is_deleted = 0 ) | |
1202 | WHERE membership.membership_type_id = %3 | |
1203 | AND activity.activity_date_time >= '$startDate' AND activity.activity_date_time <= '$endDate 23:59:59' | |
1204 | AND {$testClause}"; | |
8ef12e64 | 1205 | |
6a488035 | 1206 | $query .= ($isOwner) ? ' AND owner_membership_id IS NULL' : ''; |
4e636a74 AH |
1207 | |
1208 | $params = array( | |
1209 | 1 => array(self::$_signupActType, 'Integer'), | |
1210 | 2 => array(self::$_renewalActType, 'Integer'), | |
8ef12e64 | 1211 | 3 => array($membershipTypeId, 'Integer'), |
6a488035 | 1212 | ); |
8ef12e64 | 1213 | |
6a488035 | 1214 | $memberCount = CRM_Core_DAO::singleValueQuery($query, $params); |
e0556ebe | 1215 | return (int) $memberCount; |
6a488035 TO |
1216 | } |
1217 | ||
1218 | /** | |
100fef9d | 1219 | * Get a count of membership for a specified membership type, |
8ef12e64 | 1220 | * optionally for a specified date. The date must have the form yyyy-mm-dd. |
6a488035 TO |
1221 | * |
1222 | * If $date is omitted, this function counts as a member anyone whose | |
1223 | * membership status_id indicates they're a current member. | |
1224 | * If $date is given, this function counts as a member anyone who: | |
1225 | * -- Has a start_date before $date and end_date after $date, or | |
1226 | * -- Has a start_date before $date and is currently a member, as indicated | |
1227 | * by the the membership's status_id. | |
1228 | * The second condition takes care of records that have no end_date. These | |
1229 | * are assumed to be lifetime memberships. | |
1230 | * | |
b2363ea8 TO |
1231 | * @param int $membershipTypeId |
1232 | * Membership type id. | |
1233 | * @param string $date | |
1234 | * The date for which to retrieve the count. | |
e46e9a0b EM |
1235 | * @param bool|int $isTest if true, membership is for a test site |
1236 | * @param bool|int $isOwner if true, only retrieve membership records for owners //LCD | |
6a488035 | 1237 | * |
72b3a70c CW |
1238 | * @return int |
1239 | * the number of members of type $membershipTypeId as of $date. | |
6a488035 TO |
1240 | */ |
1241 | public static function getMembershipCount($membershipTypeId, $date = NULL, $isTest = 0, $isOwner = 0) { | |
4e636a74 AH |
1242 | if (!CRM_Utils_Rule::date($date)) { |
1243 | CRM_Core_Error::fatal(ts('Invalid date "%1" (must have form yyyy-mm-dd).', array(1 => $date))); | |
6a488035 TO |
1244 | } |
1245 | ||
e0556ebe TO |
1246 | $params = array( |
1247 | 1 => array($membershipTypeId, 'Integer'), | |
6a488035 TO |
1248 | 2 => array($isTest, 'Boolean'), |
1249 | ); | |
1250 | $query = "SELECT count(civicrm_membership.id ) as member_count | |
1251 | FROM civicrm_membership left join civicrm_membership_status on ( civicrm_membership.status_id = civicrm_membership_status.id ) | |
1252 | WHERE civicrm_membership.membership_type_id = %1 | |
1253 | AND civicrm_membership.contact_id NOT IN (SELECT id FROM civicrm_contact WHERE is_deleted = 1) | |
1254 | AND civicrm_membership.is_test = %2"; | |
1255 | if (!$date) { | |
1256 | $query .= " AND civicrm_membership_status.is_current_member = 1"; | |
1257 | } | |
1258 | else { | |
6a488035 TO |
1259 | $query .= " AND civicrm_membership.start_date <= '$date' AND civicrm_membership_status.is_current_member = 1"; |
1260 | } | |
1261 | // LCD | |
1262 | $query .= ($isOwner) ? ' AND owner_membership_id IS NULL' : ''; | |
1263 | $memberCount = CRM_Core_DAO::singleValueQuery($query, $params); | |
e0556ebe | 1264 | return (int) $memberCount; |
6a488035 TO |
1265 | } |
1266 | ||
1267 | /** | |
1268 | * Function check the status of the membership before adding membership for a contact | |
1269 | * | |
b2363ea8 TO |
1270 | * @param int $contactId |
1271 | * Contact id. | |
6a488035 | 1272 | * |
e46e9a0b | 1273 | * @return int |
6a488035 | 1274 | */ |
00be9182 | 1275 | public static function statusAvailabilty($contactId) { |
6a488035 TO |
1276 | $membership = new CRM_Member_DAO_MembershipStatus(); |
1277 | $membership->whereAdd('is_active=1'); | |
c905ba98 | 1278 | return $membership->count(); |
6a488035 TO |
1279 | } |
1280 | ||
1281 | /** | |
1282 | * Process the Memberships | |
1283 | * | |
b2363ea8 TO |
1284 | * @param array $membershipParams |
1285 | * Array of membership fields. | |
1286 | * @param int $contactID | |
1287 | * Contact id. | |
1288 | * @param CRM_Contribute_Form_Contribution_Confirm $form | |
1289 | * Confirmation form object. | |
e46e9a0b | 1290 | * |
100fef9d | 1291 | * @param array $premiumParams |
e46e9a0b EM |
1292 | * @param null $customFieldsFormatted |
1293 | * @param null $includeFieldTypes | |
6a488035 | 1294 | * |
fa9d0451 | 1295 | * @param array $membershipDetails |
705b4205 | 1296 | * |
d25e4224 | 1297 | * @param array $membershipTypeIDs |
fa9d0451 EM |
1298 | * |
1299 | * @param bool $isPaidMembership | |
d25e4224 | 1300 | * @param array $membershipID |
705b4205 | 1301 | * |
38b4a5fe EM |
1302 | * @param $isProcessSeparateMembershipTransaction |
1303 | * | |
100fef9d | 1304 | * @param int $defaultContributionTypeID |
b2363ea8 TO |
1305 | * @param array $membershipLineItems |
1306 | * Line items specific to membership payment that is separate to contribution. | |
e46e9a0b | 1307 | * @throws CRM_Core_Exception |
705b4205 | 1308 | * |
6a488035 | 1309 | * @return void |
6a488035 | 1310 | */ |
500cfe81 TO |
1311 | public static function postProcessMembership( |
1312 | $membershipParams, $contactID, &$form, $premiumParams, | |
353ffa53 TO |
1313 | $customFieldsFormatted = NULL, $includeFieldTypes = NULL, $membershipDetails, $membershipTypeIDs, $isPaidMembership, $membershipID, |
1314 | $isProcessSeparateMembershipTransaction, $defaultContributionTypeID, $membershipLineItems, $isPayLater) { | |
e0556ebe TO |
1315 | $result = $membershipContribution = NULL; |
1316 | $isTest = CRM_Utils_Array::value('is_test', $membershipParams, FALSE); | |
f5d25ee9 | 1317 | $errors = $createdMemberships = array(); |
3c0201c9 | 1318 | |
cc789d46 EM |
1319 | //@todo move this into the calling function & pass in the correct financialTypeID |
1320 | if (isset($paymentParams['financial_type'])) { | |
1321 | $financialTypeID = $paymentParams['financial_type']; | |
1322 | } | |
1323 | else { | |
1324 | $financialTypeID = $defaultContributionTypeID; | |
1325 | } | |
1326 | ||
f990d7e8 | 1327 | if (CRM_Utils_Array::value('membership_source', $form->_params)) { |
1328 | $membershipParams['contribution_source'] = $form->_params['membership_source']; | |
1329 | } | |
1330 | ||
fa9d0451 | 1331 | if ($isPaidMembership) { |
6a488035 TO |
1332 | $result = CRM_Contribute_BAO_Contribution_Utils::processConfirm($form, $membershipParams, |
1333 | $premiumParams, $contactID, | |
cc789d46 EM |
1334 | $financialTypeID, |
1335 | 'membership', | |
1336 | array(), | |
1337 | $isTest, | |
1338 | $isPayLater | |
6a488035 | 1339 | ); |
d45686b0 EM |
1340 | if (is_a($result[1], 'CRM_Core_Error')) { |
1341 | $errors[1] = CRM_Core_Error::getMessages($result[1]); | |
6a488035 | 1342 | } |
d45686b0 EM |
1343 | elseif (!empty($result[1])) { |
1344 | // Save the contribution ID so that I can be used in email receipts | |
1345 | // For example, if you need to generate a tax receipt for the donation only. | |
1346 | $form->_values['contribution_other_id'] = $result[1]->id; | |
1347 | //note that this will be over-written if we are using a separate membership transaction. Otherwise there is only one | |
1348 | $membershipContribution = $result[1]; | |
6a488035 | 1349 | } |
6a488035 | 1350 | } |
6a488035 | 1351 | |
38b4a5fe | 1352 | if ($isProcessSeparateMembershipTransaction) { |
d45686b0 | 1353 | try { |
866f6e8e | 1354 | $lineItems = $form->_lineItem = $membershipLineItems; |
5624f515 | 1355 | $membershipContribution = self::processSecondaryFinancialTransaction($contactID, $form, $membershipParams, $isTest, $membershipLineItems, CRM_Utils_Array::value('minimum_fee', $membershipDetails, 0), CRM_Utils_Array::value('financial_type_id', $membershipDetails)); |
6a488035 | 1356 | } |
d45686b0 EM |
1357 | catch (CRM_Core_Exception $e) { |
1358 | $errors[2] = $e->getMessage(); | |
1359 | $membershipContribution = NULL; | |
6a488035 TO |
1360 | } |
1361 | } | |
1362 | ||
8d65cef7 | 1363 | $membership = NULL; |
423c9872 | 1364 | if (!empty($membershipContribution) && !is_a($membershipContribution, 'CRM_Core_Error')) { |
dd9e2b05 EM |
1365 | $membershipContributionID = $membershipContribution->id; |
1366 | } | |
423c9872 C |
1367 | |
1368 | //@todo - why is this nested so deep? it seems like it could be just set on the calling function on the form layer | |
1369 | if (isset($membershipParams['onbehalf']) && !empty($membershipParams['onbehalf']['member_campaign_id'])) { | |
1370 | $form->_params['campaign_id'] = $membershipParams['onbehalf']['member_campaign_id']; | |
1371 | } | |
1372 | //@todo it should no longer be possible for it to get to this point & membership to not be an array | |
6e92ff36 | 1373 | if (is_array($membershipTypeIDs) && !empty($membershipContributionID)) { |
423c9872 | 1374 | $typesTerms = CRM_Utils_Array::value('types_terms', $membershipParams, array()); |
d25e4224 | 1375 | foreach ($membershipTypeIDs as $memType) { |
423c9872 C |
1376 | $numTerms = CRM_Utils_Array::value($memType, $typesTerms, 1); |
1377 | $createdMemberships[$memType] = self::createOrRenewMembership($membershipParams, $contactID, $customFieldsFormatted, $membershipID, $memType, $isTest, $numTerms, $membershipContribution, $form); | |
6a488035 | 1378 | } |
423c9872 C |
1379 | if ($form->_priceSetId && !empty($form->_useForMember) && !empty($form->_lineItem)) { |
1380 | foreach ($form->_lineItem[$form->_priceSetId] as & $priceFieldOp) { | |
1381 | if (!empty($priceFieldOp['membership_type_id']) && | |
1382 | isset($createdMemberships[$priceFieldOp['membership_type_id']]) | |
1383 | ) { | |
1384 | $membershipOb = $createdMemberships[$priceFieldOp['membership_type_id']]; | |
3b85fc04 PN |
1385 | $priceFieldOp['start_date'] = $membershipOb->start_date ? CRM_Utils_Date::customFormat($membershipOb->start_date, '%B %E%f, %Y') : '-'; |
1386 | $priceFieldOp['end_date'] = $membershipOb->end_date ? CRM_Utils_Date::customFormat($membershipOb->end_date, '%B %E%f, %Y') : '-'; | |
423c9872 C |
1387 | } |
1388 | else { | |
1389 | $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A'; | |
6a488035 | 1390 | } |
6a488035 | 1391 | } |
423c9872 C |
1392 | $form->_values['lineItem'] = $form->_lineItem; |
1393 | $form->assign('lineItem', $form->_lineItem); | |
6a488035 | 1394 | } |
6a488035 TO |
1395 | } |
1396 | ||
1397 | if (!empty($errors)) { | |
15c3308b | 1398 | $message = self::compileErrorMessage($errors); |
93a11cd6 | 1399 | throw new CRM_Core_Exception($message); |
6a488035 | 1400 | } |
8d65cef7 AC |
1401 | $form->_params['createdMembershipIDs'] = array(); |
1402 | ||
1403 | // CRM-7851 - Moved after processing Payment Errors | |
15c3308b | 1404 | //@todo - the reasoning for this being here seems a little outdated |
8d65cef7 AC |
1405 | foreach ($createdMemberships as $createdMembership) { |
1406 | CRM_Core_BAO_CustomValueTable::postProcess( | |
1407 | $form->_params, | |
1408 | CRM_Core_DAO::$_nullArray, | |
1409 | 'civicrm_membership', | |
1410 | $createdMembership->id, | |
1411 | 'Membership' | |
1412 | ); | |
1413 | $form->_params['createdMembershipIDs'][] = $createdMembership->id; | |
1414 | } | |
e0556ebe | 1415 | if (count($createdMemberships) == 1) { |
1f4d7bcb | 1416 | //presumably this is only relevant for exactly 1 membership |
12dac866 | 1417 | $form->_params['membershipID'] = $createdMembership->id; |
1f4d7bcb | 1418 | } |
6a488035 | 1419 | |
12dac866 | 1420 | //CRM-15232: Check if membership is created and on the basis of it use |
1421 | //membership reciept template to send payment reciept | |
1422 | if (count($createdMemberships)) { | |
1423 | $form->_values['isMembership'] = TRUE; | |
1424 | } | |
6a488035 TO |
1425 | if ($form->_contributeMode == 'notify') { |
1426 | if ($form->_values['is_monetary'] && $form->_amount > 0.0 && !$form->_params['is_pay_later']) { | |
dd9e2b05 | 1427 | // call postProcess hook before leaving |
6a488035 TO |
1428 | $form->postProcessHook(); |
1429 | // this does not return | |
1430 | $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form); | |
1431 | $payment->doTransferCheckout($form->_params, 'contribute'); | |
1432 | } | |
1433 | } | |
1434 | ||
dd9e2b05 EM |
1435 | if (isset($membershipContributionID)) { |
1436 | $form->_values['contribution_id'] = $membershipContributionID; | |
6a488035 TO |
1437 | } |
1438 | ||
1439 | // Do not send an email if Recurring transaction is done via Direct Mode | |
1440 | // Email will we sent when the IPN is received. | |
a7488080 | 1441 | if (!empty($form->_params['is_recur']) && $form->_contributeMode == 'direct') { |
a7a33651 EM |
1442 | if (!empty($membershipContribution->trxn_id)) { |
1443 | try { | |
e0556ebe | 1444 | civicrm_api3('contribution', 'completetransaction', array( |
353ffa53 TO |
1445 | 'id' => $membershipContribution->id, |
1446 | 'trxn_id' => $membershipContribution->trxn_id, | |
1447 | )); | |
a7a33651 EM |
1448 | } |
1449 | catch (CiviCRM_API3_Exception $e) { | |
1450 | // if for any reason it is already completed this will fail - e.g extensions hacking around core not completing transactions prior to CRM-15296 | |
1451 | // so let's be gentle here | |
1452 | CRM_Core_Error::debug_log_message('contribution ' . $membershipContribution->id . ' not completed with trxn_id ' . $membershipContribution->trxn_id . ' and message ' . $e->getMessage()); | |
1453 | } | |
1454 | } | |
5901ddf9 | 1455 | return; |
6a488035 TO |
1456 | } |
1457 | ||
1458 | //finally send an email receipt | |
1459 | CRM_Contribute_BAO_ContributionPage::sendMail($contactID, | |
1460 | $form->_values, | |
1461 | $isTest, FALSE, | |
1462 | $includeFieldTypes | |
1463 | ); | |
1464 | } | |
1465 | ||
cd125a40 FG |
1466 | /** |
1467 | * Function for updating a membership record's contribution_recur_id | |
1468 | * | |
c866eb5f | 1469 | * @param CRM_Member_DAO_Membership $membership |
5901ddf9 | 1470 | * @param \CRM_Contribute_BAO_Contribution|\CRM_Contribute_DAO_Contribution $contribution |
cd125a40 FG |
1471 | * |
1472 | * @return void | |
cd125a40 | 1473 | */ |
c866eb5f | 1474 | static public function updateRecurMembership(CRM_Member_DAO_Membership $membership, CRM_Contribute_BAO_Contribution $contribution) { |
cd125a40 FG |
1475 | |
1476 | if (empty($contribution->contribution_recur_id)) { | |
1477 | return; | |
1478 | } | |
1479 | ||
1480 | $params = array( | |
1481 | 1 => array($contribution->contribution_recur_id, 'Integer'), | |
1482 | 2 => array($membership->id, 'Integer'), | |
1483 | ); | |
1484 | ||
1485 | $sql = "UPDATE civicrm_membership SET contribution_recur_id = %1 WHERE id = %2"; | |
1486 | CRM_Core_DAO::executeQuery($sql, $params); | |
1487 | } | |
1488 | ||
6a488035 | 1489 | /** |
c98997b9 EM |
1490 | * @deprecated |
1491 | * A wrapper for renewing memberships from a form - including the form in the membership processing adds complexity | |
1492 | * as the forms are being forced to pretend similarity | |
1493 | * Try to call the renewMembership directly | |
e0efd2d0 | 1494 | * @todo - this form method needs to have the interaction with the form layer removed from it |
1495 | * as a BAO function. Note that the api now supports membership renewals & it is not clear this function does anything | |
1496 | * not done by the membership.create api (with a lot less unit tests) | |
1497 | * | |
6a488035 TO |
1498 | * This method will renew / create the membership depending on |
1499 | * whether the given contact has a membership or not. And will add | |
1500 | * the modified dates for membership and in the log table. | |
1501 | * | |
b2363ea8 TO |
1502 | * @param int $contactID |
1503 | * Id of the contact. | |
1504 | * @param int $membershipTypeID | |
1505 | * Id of the new membership type. | |
1506 | * @param bool $is_test | |
1507 | * If this is test contribution or live contribution. | |
1508 | * @param CRM_Core_Form $form | |
1509 | * Form object. | |
e46e9a0b | 1510 | * @param null $changeToday |
b2363ea8 TO |
1511 | * @param int $modifiedID |
1512 | * Individual contact id in case of On Behalf signup (CRM-4027 ). | |
e46e9a0b | 1513 | * @param null $customFieldsFormatted |
b2363ea8 TO |
1514 | * @param int $numRenewTerms |
1515 | * How many membership terms are being added to end date (default is 1). | |
1516 | * @param int $membershipID | |
1517 | * Membership ID, this should always be passed in & optionality should be removed. | |
fa9d0451 EM |
1518 | * |
1519 | * @throws CRM_Core_Exception | |
6a488035 | 1520 | * |
e46e9a0b | 1521 | */ |
608e6658 | 1522 | public static function renewMembershipFormWrapper( |
6a488035 TO |
1523 | $contactID, |
1524 | $membershipTypeID, | |
1525 | $is_test, | |
1526 | &$form, | |
1527 | $changeToday = NULL, | |
1528 | $modifiedID = NULL, | |
1529 | $customFieldsFormatted = NULL, | |
fa9d0451 EM |
1530 | $numRenewTerms = 1, |
1531 | $membershipID = NULL | |
6a488035 TO |
1532 | ) { |
1533 | $statusFormat = '%Y-%m-%d'; | |
e0556ebe TO |
1534 | $format = '%Y%m%d'; |
1535 | $ids = array(); | |
fa9d0451 | 1536 | //@todo would be better to make $membershipID a compulsory function param & make form layer responsible for extracting it |
e0556ebe | 1537 | if (!$membershipID && isset($form->_membershipId)) { |
fa9d0451 EM |
1538 | $membershipID = $form->_membershipId; |
1539 | } | |
6a488035 TO |
1540 | |
1541 | //get all active statuses of membership. | |
1542 | $allStatus = CRM_Member_PseudoConstant::membershipStatus(); | |
1543 | ||
1544 | $membershipTypeDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipTypeID); | |
1545 | ||
1546 | // check is it pending. - CRM-4555 | |
c98997b9 EM |
1547 | list($pending, $contributionRecurID, $changeToday, $membershipSource, $isPayLater, $campaignId) = self::extractFormValues($form, $changeToday, $membershipTypeDetails); |
1548 | list($membership, $renewalMode, $dates) = self::renewMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $allStatus, $membershipTypeDetails, $contributionRecurID, $format, $membershipSource, $ids, $statusFormat, $isPayLater, $campaignId); | |
1f4d7bcb | 1549 | $form->set('renewal_mode', $renewalMode); |
6a488035 TO |
1550 | if (!empty($dates)) { |
1551 | $form->assign('mem_start_date', | |
1552 | CRM_Utils_Date::customFormat($dates['start_date'], $format) | |
1553 | ); | |
1554 | $form->assign('mem_end_date', | |
1555 | CRM_Utils_Date::customFormat($dates['end_date'], $format) | |
1556 | ); | |
1557 | } | |
6a488035 | 1558 | return $membership; |
c98997b9 | 1559 | |
6a488035 TO |
1560 | } |
1561 | ||
1562 | /** | |
1563 | * Method to fix membership status of stale membership | |
1564 | * | |
1565 | * This method first checks if the membership is stale. If it is, | |
1566 | * then status will be updated based on existing start and end | |
1567 | * dates and log will be added for the status change. | |
1568 | * | |
b2363ea8 TO |
1569 | * @param array $currentMembership |
1570 | * Reference to the array. | |
688d37c6 CW |
1571 | * containing all values of |
1572 | * the current membership | |
b2363ea8 TO |
1573 | * @param array $changeToday |
1574 | * Array of month, day, year. | |
688d37c6 CW |
1575 | * values in case today needs |
1576 | * to be customised, null otherwise | |
6a488035 TO |
1577 | * |
1578 | * @return void | |
6a488035 | 1579 | */ |
00be9182 | 1580 | public static function fixMembershipStatusBeforeRenew(&$currentMembership, $changeToday) { |
6a488035 TO |
1581 | $today = NULL; |
1582 | if ($changeToday) { | |
1583 | $today = CRM_Utils_Date::processDate($changeToday, NULL, FALSE, 'Y-m-d'); | |
1584 | } | |
1585 | ||
1586 | $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate( | |
1587 | CRM_Utils_Array::value('start_date', $currentMembership), | |
1588 | CRM_Utils_Array::value('end_date', $currentMembership), | |
1589 | CRM_Utils_Array::value('join_date', $currentMembership), | |
1590 | $today, | |
5f11bbcc EM |
1591 | TRUE, |
1592 | $currentMembership['membership_type_id'], | |
1593 | $currentMembership | |
6a488035 TO |
1594 | ); |
1595 | ||
1596 | if (empty($status) || | |
1597 | empty($status['id']) | |
1598 | ) { | |
1599 | CRM_Core_Error::fatal(ts('Oops, it looks like there is no valid membership status corresponding to the membership start and end dates for this membership. Contact the site administrator for assistance.')); | |
1600 | } | |
1601 | ||
1602 | $currentMembership['today_date'] = $today; | |
1603 | ||
1604 | if ($status['id'] !== $currentMembership['status_id']) { | |
1605 | $memberDAO = new CRM_Member_DAO_Membership(); | |
1606 | $memberDAO->id = $currentMembership['id']; | |
1607 | $memberDAO->find(TRUE); | |
1608 | ||
1609 | $memberDAO->status_id = $status['id']; | |
1610 | $memberDAO->join_date = CRM_Utils_Date::isoToMysql($memberDAO->join_date); | |
1611 | $memberDAO->start_date = CRM_Utils_Date::isoToMysql($memberDAO->start_date); | |
1612 | $memberDAO->end_date = CRM_Utils_Date::isoToMysql($memberDAO->end_date); | |
1613 | $memberDAO->save(); | |
1614 | CRM_Core_DAO::storeValues($memberDAO, $currentMembership); | |
1615 | $memberDAO->free(); | |
1616 | ||
1617 | $currentMembership['is_current_member'] = CRM_Core_DAO::getFieldValue( | |
1618 | 'CRM_Member_DAO_MembershipStatus', | |
1619 | $currentMembership['status_id'], | |
1620 | 'is_current_member' | |
1621 | ); | |
1622 | $format = '%Y%m%d'; | |
1623 | ||
1624 | $logParams = array( | |
1625 | 'membership_id' => $currentMembership['id'], | |
1626 | 'status_id' => $status['id'], | |
1627 | 'start_date' => CRM_Utils_Date::customFormat( | |
1628 | $currentMembership['start_date'], | |
1629 | $format | |
1630 | ), | |
1631 | 'end_date' => CRM_Utils_Date::customFormat( | |
1632 | $currentMembership['end_date'], | |
1633 | $format | |
1634 | ), | |
1635 | 'modified_date' => CRM_Utils_Date::customFormat( | |
1636 | $currentMembership['today_date'], | |
1637 | $format | |
1638 | ), | |
1639 | 'membership_type_id' => $currentMembership['membership_type_id'], | |
1640 | 'max_related' => $currentMembership['max_related'], | |
1641 | ); | |
1642 | ||
1643 | $session = CRM_Core_Session::singleton(); | |
1644 | // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id | |
1645 | if ($session->get('userID')) { | |
1646 | $logParams['modified_id'] = $session->get('userID'); | |
1647 | } | |
1648 | else { | |
1649 | $logParams['modified_id'] = $currentMembership['contact_id']; | |
1650 | } | |
1651 | CRM_Member_BAO_MembershipLog::add($logParams, CRM_Core_DAO::$_nullArray); | |
1652 | } | |
1653 | } | |
1654 | ||
1655 | /** | |
100fef9d | 1656 | * Get the contribution page id from the membership record |
6a488035 | 1657 | * |
688d37c6 | 1658 | * @param int $membershipID |
6a488035 | 1659 | * |
a6c01b45 | 1660 | * @return int |
688d37c6 | 1661 | * contribution page id |
6a488035 | 1662 | */ |
00be9182 | 1663 | public static function getContributionPageId($membershipID) { |
6a488035 TO |
1664 | $query = " |
1665 | SELECT c.contribution_page_id as pageID | |
1666 | FROM civicrm_membership_payment mp, civicrm_contribution c | |
1667 | WHERE mp.contribution_id = c.id | |
1668 | AND c.contribution_page_id IS NOT NULL | |
1669 | AND mp.membership_id = " . CRM_Utils_Type::escape($membershipID, 'Integer') | |
e0556ebe | 1670 | . " ORDER BY mp.id DESC"; |
6a488035 TO |
1671 | |
1672 | return CRM_Core_DAO::singleValueQuery($query, | |
1673 | CRM_Core_DAO::$_nullArray | |
1674 | ); | |
1675 | } | |
1676 | ||
6a488035 | 1677 | /** |
100fef9d | 1678 | * Updated related memberships |
6a488035 | 1679 | * |
b2363ea8 TO |
1680 | * @param int $ownerMembershipId |
1681 | * Owner Membership Id. | |
1682 | * @param array $params | |
1683 | * Formatted array of key => value. | |
6a488035 | 1684 | */ |
00be9182 | 1685 | public static function updateRelatedMemberships($ownerMembershipId, $params) { |
6a488035 TO |
1686 | $membership = new CRM_Member_DAO_Membership(); |
1687 | $membership->owner_membership_id = $ownerMembershipId; | |
1688 | $membership->find(); | |
1689 | ||
1690 | while ($membership->fetch()) { | |
1691 | $relatedMembership = new CRM_Member_DAO_Membership(); | |
1692 | $relatedMembership->id = $membership->id; | |
1693 | $relatedMembership->copyValues($params); | |
1694 | $relatedMembership->save(); | |
1695 | $relatedMembership->free(); | |
1696 | } | |
1697 | ||
1698 | $membership->free(); | |
1699 | } | |
1700 | ||
1701 | /** | |
100fef9d | 1702 | * Get list of membership fields for profile |
6a488035 TO |
1703 | * For now we only allow custom membership fields to be in |
1704 | * profile | |
1705 | * | |
e46e9a0b | 1706 | * @param null $mode |
688d37c6 | 1707 | * FIXME: This param is ignored |
e46e9a0b | 1708 | * |
a6c01b45 CW |
1709 | * @return array |
1710 | * the list of membership fields | |
6a488035 | 1711 | */ |
00be9182 | 1712 | public static function getMembershipFields($mode = NULL) { |
6a488035 TO |
1713 | $fields = CRM_Member_DAO_Membership::export(); |
1714 | ||
6a488035 TO |
1715 | unset($fields['membership_contact_id']); |
1716 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Membership')); | |
1717 | ||
1718 | $membershipType = CRM_Member_DAO_MembershipType::export(); | |
1719 | ||
1720 | $membershipStatus = CRM_Member_DAO_MembershipStatus::export(); | |
1721 | ||
1722 | $fields = array_merge($fields, $membershipType, $membershipStatus); | |
1723 | ||
1724 | return $fields; | |
1725 | } | |
1726 | ||
1727 | /** | |
100fef9d | 1728 | * Get the sort name of a contact for a particular membership |
6a488035 | 1729 | * |
b2363ea8 TO |
1730 | * @param int $id |
1731 | * Id of the membership. | |
6a488035 | 1732 | * |
72b3a70c CW |
1733 | * @return null|string |
1734 | * sort name of the contact if found | |
6a488035 | 1735 | */ |
00be9182 | 1736 | public static function sortName($id) { |
6a488035 TO |
1737 | $id = CRM_Utils_Type::escape($id, 'Integer'); |
1738 | ||
1739 | $query = " | |
1740 | SELECT civicrm_contact.sort_name | |
1741 | FROM civicrm_membership, civicrm_contact | |
1742 | WHERE civicrm_membership.contact_id = civicrm_contact.id | |
1743 | AND civicrm_membership.id = {$id} | |
1744 | "; | |
1745 | return CRM_Core_DAO::singleValueQuery($query, CRM_Core_DAO::$_nullArray); | |
1746 | } | |
1747 | ||
1748 | /** | |
100fef9d | 1749 | * Create memberships for related contacts |
6a488035 TO |
1750 | * takes into account the maximum related memberships |
1751 | * | |
b2363ea8 TO |
1752 | * @param array $params |
1753 | * Array of key - value pairs. | |
1754 | * @param CRM_Core_DAO $dao | |
1755 | * Membership object. | |
6a488035 | 1756 | * |
72b3a70c CW |
1757 | * @return null|array |
1758 | * array of memberships if created | |
6a488035 | 1759 | */ |
00be9182 | 1760 | public static function createRelatedMemberships(&$params, &$dao, $reset = FALSE) { |
6a488035 | 1761 | static $relatedContactIds = array(); |
6d8a45ed EM |
1762 | if ($reset) { |
1763 | // not sure why a static var is in use here - we need a way to reset it from the test suite | |
1764 | $relatedContactIds = array(); | |
608e6658 | 1765 | return FALSE; |
6d8a45ed | 1766 | } |
6a488035 TO |
1767 | |
1768 | $membership = new CRM_Member_DAO_Membership(); | |
1769 | $membership->id = $dao->id; | |
1770 | ||
1771 | // required since create method doesn't return all the | |
1772 | // parameters in the returned membership object | |
1773 | if (!$membership->find(TRUE)) { | |
1774 | return; | |
1775 | } | |
1776 | $deceasedStatusId = array_search('Deceased', CRM_Member_PseudoConstant::membershipStatus()); | |
1777 | // FIXME : While updating/ renewing the | |
1778 | // membership, if the relationship is PAST then | |
1779 | // the membership of the related contact must be | |
1780 | // expired. | |
1781 | // For that, getting Membership Status for which | |
1782 | // is_current_member is 0. It works for the | |
1783 | // generated data as there is only one membership | |
1784 | // status having is_current_member = 0. | |
1785 | // But this wont work exactly if there will be | |
1786 | // more than one status having is_current_member = 0. | |
1787 | $membershipStatus = new CRM_Member_DAO_MembershipStatus(); | |
1788 | $membershipStatus->is_current_member = 0; | |
1789 | if ($membershipStatus->find(TRUE)) { | |
1790 | $expiredStatusId = $membershipStatus->id; | |
e0556ebe TO |
1791 | } |
1792 | else { | |
6a488035 TO |
1793 | $expiredStatusId = array_search('Expired', CRM_Member_PseudoConstant::membershipStatus()); |
1794 | } | |
1795 | ||
1796 | $allRelatedContacts = array(); | |
1797 | $relatedContacts = array(); | |
1798 | if (!is_a($membership, 'CRM_Core_Error')) { | |
1799 | $allRelatedContacts = CRM_Member_BAO_Membership::checkMembershipRelationship($membership->id, | |
1800 | $membership->contact_id, | |
1801 | CRM_Utils_Array::value('action', $params) | |
1802 | ); | |
1803 | } | |
1804 | ||
1805 | // check for loops. CRM-4213 | |
1806 | // remove repeated related contacts, which already inherited membership. | |
1807 | $relatedContactIds[$membership->contact_id] = TRUE; | |
1808 | foreach ($allRelatedContacts as $cid => $status) { | |
a7488080 | 1809 | if (empty($relatedContactIds[$cid])) { |
6a488035 TO |
1810 | $relatedContactIds[$cid] = TRUE; |
1811 | ||
1812 | //don't create membership again for owner contact. | |
1813 | $nestedRelationship = FALSE; | |
1814 | if ($membership->owner_membership_id) { | |
1815 | $nestedRelMembership = new CRM_Member_DAO_Membership(); | |
1816 | $nestedRelMembership->id = $membership->owner_membership_id; | |
1817 | $nestedRelMembership->contact_id = $cid; | |
1818 | $nestedRelationship = $nestedRelMembership->find(TRUE); | |
1819 | $nestedRelMembership->free(); | |
1820 | } | |
1821 | if (!$nestedRelationship) { | |
1822 | $relatedContacts[$cid] = $status; | |
1823 | } | |
1824 | } | |
1825 | } | |
1826 | ||
1827 | //lets cleanup related membership if any. | |
1828 | if (empty($relatedContacts)) { | |
3506b6cd | 1829 | self::deleteRelatedMemberships($membership->id); |
6a488035 TO |
1830 | } |
1831 | else { | |
1832 | // Edit the params array | |
1833 | unset($params['id']); | |
1834 | // Reminder should be sent only to the direct membership | |
1835 | unset($params['reminder_date']); | |
1836 | // unset the custom value ids | |
1837 | if (is_array(CRM_Utils_Array::value('custom', $params))) { | |
1838 | foreach ($params['custom'] as $k => $v) { | |
1839 | unset($params['custom'][$k]['id']); | |
1840 | } | |
1841 | } | |
1842 | if (!isset($params['membership_type_id'])) { | |
1843 | $params['membership_type_id'] = $membership->membership_type_id; | |
1844 | } | |
1845 | ||
1846 | // max_related should be set in the parent membership | |
1847 | unset($params['max_related']); | |
1848 | // Number of inherited memberships available - NULL is interpreted as unlimited, '0' as none | |
1849 | $available = ($membership->max_related == NULL ? PHP_INT_MAX : $membership->max_related); | |
1850 | $queue = array(); // will be used to queue potential memberships to be created | |
1851 | ||
1852 | foreach ($relatedContacts as $contactId => $relationshipStatus) { | |
1853 | //use existing membership record. | |
1854 | $relMembership = new CRM_Member_DAO_Membership(); | |
1855 | $relMembership->contact_id = $contactId; | |
1856 | $relMembership->owner_membership_id = $membership->id; | |
1857 | $relMemIds = array(); | |
1858 | if ($relMembership->find(TRUE)) { | |
1859 | $params['id'] = $relMemIds['membership'] = $relMembership->id; | |
1860 | } | |
1861 | $params['contact_id'] = $contactId; | |
1862 | $params['owner_membership_id'] = $membership->id; | |
1863 | ||
1864 | // set status_id as it might have been changed for | |
1865 | // past relationship | |
1866 | $params['status_id'] = $membership->status_id; | |
1867 | ||
1868 | if ($deceasedStatusId && | |
1869 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'is_deceased') | |
1870 | ) { | |
1871 | $params['status_id'] = $deceasedStatusId; | |
1872 | } | |
1873 | elseif ((CRM_Utils_Array::value('action', $params) & CRM_Core_Action::UPDATE) && | |
1874 | ($relationshipStatus == CRM_Contact_BAO_Relationship::PAST) | |
1875 | ) { | |
e0556ebe TO |
1876 | $params['status_id'] = $expiredStatusId; |
1877 | } | |
6a488035 TO |
1878 | |
1879 | //don't calculate status again in create( ); | |
1880 | $params['skipStatusCal'] = TRUE; | |
1881 | ||
1882 | //do create activity if we changed status. | |
1883 | if ($params['status_id'] != $relMembership->status_id) { | |
1884 | $params['createActivity'] = TRUE; | |
1885 | } | |
1886 | ||
1887 | // we should not created contribution record for related contacts, CRM-3371 | |
1888 | unset($params['contribution_status_id']); | |
1889 | ||
1890 | if (($params['status_id'] == $deceasedStatusId) || ($params['status_id'] == $expiredStatusId)) { | |
1891 | // related membership is not active so does not count towards maximum | |
1892 | CRM_Member_BAO_Membership::create($params, $relMemIds); | |
8efea814 EM |
1893 | } |
1894 | else { | |
6a488035 TO |
1895 | // related membership already exists, so this is just an update |
1896 | if (isset($params['id'])) { | |
1897 | if ($available > 0) { | |
e0556ebe TO |
1898 | CRM_Member_BAO_Membership::create($params, $relMemIds); |
1899 | $available--; | |
1900 | } | |
608e6658 | 1901 | else { |
1902 | // we have run out of inherited memberships, so delete extras | |
f5e53870 | 1903 | self::deleteMembership($params['id']); |
6a488035 | 1904 | } |
e0556ebe TO |
1905 | // we need to first check if there will remain inherited memberships, so queue it up |
1906 | } | |
1907 | else { | |
6a488035 TO |
1908 | $queue[] = $params; |
1909 | } | |
1910 | } | |
1911 | } | |
1912 | // now go over the queue and create any available related memberships | |
1913 | reset($queue); | |
1914 | while (($available > 0) && ($params = each($queue))) { | |
1915 | CRM_Member_BAO_Membership::create($params['value'], $relMemIds); | |
e0556ebe | 1916 | $available--; |
6a488035 TO |
1917 | } |
1918 | } | |
1919 | } | |
1920 | ||
1921 | /** | |
1922 | * Delete the record that are associated with this Membership Payment | |
1923 | * | |
b2363ea8 | 1924 | * @param int $membershipId |
6a488035 | 1925 | * |
608e6658 | 1926 | * @return object |
1927 | * $membershipPayment deleted membership payment object | |
6a488035 | 1928 | */ |
00be9182 | 1929 | public static function deleteMembershipPayment($membershipId) { |
6a488035 | 1930 | |
608e6658 | 1931 | $membershipPayment = new CRM_Member_DAO_MembershipPayment(); |
1932 | $membershipPayment->membership_id = $membershipId; | |
1933 | $membershipPayment->find(); | |
6a488035 | 1934 | |
608e6658 | 1935 | while ($membershipPayment->fetch()) { |
1936 | CRM_Contribute_BAO_Contribution::deleteContribution($membershipPayment->contribution_id); | |
1937 | CRM_Utils_Hook::pre('delete', 'MembershipPayment', $membershipPayment->id, $membershipPayment); | |
6a488035 | 1938 | $membesrshipPayment->delete(); |
608e6658 | 1939 | CRM_Utils_Hook::post('delete', 'MembershipPayment', $membershipPayment->id, $membershipPayment); |
6a488035 | 1940 | } |
608e6658 | 1941 | return $membershipPayment; |
6a488035 TO |
1942 | } |
1943 | ||
bb3a214a | 1944 | /** |
c490a46a | 1945 | * @param CRM_Core_Form $form |
100fef9d | 1946 | * @param int $membershipTypeID |
bb3a214a EM |
1947 | * |
1948 | * @return array | |
1949 | */ | |
00be9182 | 1950 | public static function &buildMembershipTypeValues(&$form, $membershipTypeID = NULL) { |
e0556ebe | 1951 | $whereClause = " WHERE domain_id = " . CRM_Core_Config::domainID(); |
6a488035 TO |
1952 | |
1953 | if (is_array($membershipTypeID)) { | |
1954 | $allIDs = implode(',', $membershipTypeID); | |
1955 | $whereClause .= " AND id IN ( $allIDs )"; | |
1956 | } | |
1957 | elseif (is_numeric($membershipTypeID) && | |
1958 | $membershipTypeID > 0 | |
1959 | ) { | |
1960 | $whereClause .= " AND id = $membershipTypeID"; | |
1961 | } | |
1962 | ||
1963 | $query = " | |
1964 | SELECT * | |
1965 | FROM civicrm_membership_type | |
1966 | $whereClause; | |
1967 | "; | |
1968 | $dao = CRM_Core_DAO::executeQuery($query); | |
1969 | ||
1970 | $membershipTypeValues = array(); | |
1971 | $membershipTypeFields = array( | |
e0556ebe TO |
1972 | 'id', |
1973 | 'minimum_fee', | |
1974 | 'name', | |
1975 | 'is_active', | |
1976 | 'description', | |
1977 | 'financial_type_id', | |
1978 | 'auto_renew', | |
1979 | 'member_of_contact_id', | |
1980 | 'relationship_type_id', | |
1981 | 'relationship_direction', | |
1982 | 'max_related', | |
6a488035 TO |
1983 | ); |
1984 | ||
1985 | while ($dao->fetch()) { | |
1986 | $membershipTypeValues[$dao->id] = array(); | |
1987 | foreach ($membershipTypeFields as $mtField) { | |
1988 | $membershipTypeValues[$dao->id][$mtField] = $dao->$mtField; | |
1989 | } | |
1990 | } | |
1991 | $dao->free(); | |
1992 | ||
1993 | CRM_Utils_Hook::membershipTypeValues($form, $membershipTypeValues); | |
1994 | ||
1995 | if (is_numeric($membershipTypeID) && | |
1996 | $membershipTypeID > 0 | |
1997 | ) { | |
1998 | return $membershipTypeValues[$membershipTypeID]; | |
1999 | } | |
2000 | else { | |
2001 | return $membershipTypeValues; | |
2002 | } | |
2003 | } | |
2004 | ||
2005 | /** | |
100fef9d | 2006 | * Get membership record count for a Contact |
6a488035 | 2007 | * |
c490a46a | 2008 | * @param int $contactID |
b2363ea8 | 2009 | * @param bool $activeOnly |
6a488035 | 2010 | * |
c490a46a | 2011 | * @return null|string |
6a488035 | 2012 | */ |
00be9182 | 2013 | public static function getContactMembershipCount($contactID, $activeOnly = FALSE) { |
6a488035 | 2014 | $select = "SELECT count(*) FROM civicrm_membership "; |
e0556ebe | 2015 | $where = "WHERE civicrm_membership.contact_id = {$contactID} AND civicrm_membership.is_test = 0 "; |
6a488035 TO |
2016 | |
2017 | // CRM-6627, all status below 3 (active, pending, grace) are considered active | |
2018 | if ($activeOnly) { | |
2019 | $select .= " INNER JOIN civicrm_membership_status ON civicrm_membership.status_id = civicrm_membership_status.id "; | |
e0556ebe | 2020 | $where .= " and civicrm_membership_status.is_current_member = 1"; |
6a488035 TO |
2021 | } |
2022 | ||
2023 | $query = $select . $where; | |
2024 | return CRM_Core_DAO::singleValueQuery($query); | |
2025 | } | |
2026 | ||
2027 | /** | |
100fef9d | 2028 | * Check whether payment processor supports |
6a488035 TO |
2029 | * cancellation of membership subscription |
2030 | * | |
b2363ea8 TO |
2031 | * @param int $mid |
2032 | * Membership id. | |
6a488035 | 2033 | * |
e46e9a0b EM |
2034 | * @param bool $isNotCancelled |
2035 | * | |
608e6658 | 2036 | * @return bool |
6a488035 | 2037 | */ |
00be9182 | 2038 | public static function isCancelSubscriptionSupported($mid, $isNotCancelled = TRUE) { |
6a488035 TO |
2039 | $cacheKeyString = "$mid"; |
2040 | $cacheKeyString .= $isNotCancelled ? '_1' : '_0'; | |
2041 | ||
2042 | static $supportsCancel = array(); | |
2043 | ||
2044 | if (!array_key_exists($cacheKeyString, $supportsCancel)) { | |
2045 | $supportsCancel[$cacheKeyString] = FALSE; | |
2046 | $isCancelled = FALSE; | |
2047 | ||
2048 | if ($isNotCancelled) { | |
2049 | $isCancelled = self::isSubscriptionCancelled($mid); | |
2050 | } | |
2051 | ||
2052 | $paymentObject = CRM_Financial_BAO_PaymentProcessor::getProcessorForEntity($mid, 'membership', 'obj'); | |
2053 | if (!empty($paymentObject)) { | |
2054 | $supportsCancel[$cacheKeyString] = $paymentObject->isSupported('cancelSubscription') && !$isCancelled; | |
2055 | } | |
2056 | } | |
2057 | return $supportsCancel[$cacheKeyString]; | |
2058 | } | |
2059 | ||
2060 | /** | |
100fef9d | 2061 | * Check whether subscription is already cancelled |
6a488035 | 2062 | * |
b2363ea8 TO |
2063 | * @param int $mid |
2064 | * Membership id. | |
6a488035 | 2065 | * |
a6c01b45 CW |
2066 | * @return string |
2067 | * contribution status | |
6a488035 | 2068 | */ |
00be9182 | 2069 | public static function isSubscriptionCancelled($mid) { |
6a488035 TO |
2070 | $sql = " |
2071 | SELECT cr.contribution_status_id | |
2072 | FROM civicrm_contribution_recur cr | |
2073 | LEFT JOIN civicrm_membership mem ON ( cr.id = mem.contribution_recur_id ) | |
2074 | WHERE mem.id = %1 LIMIT 1"; | |
e0556ebe | 2075 | $params = array(1 => array($mid, 'Integer')); |
6a488035 | 2076 | $statusId = CRM_Core_DAO::singleValueQuery($sql, $params); |
e0556ebe | 2077 | $status = CRM_Contribute_PseudoConstant::contributionStatus($statusId, 'name'); |
6a488035 TO |
2078 | if ($status == 'Cancelled') { |
2079 | return TRUE; | |
2080 | } | |
2081 | return FALSE; | |
2082 | } | |
2083 | ||
2084 | /** | |
100fef9d | 2085 | * Get membership joins for a specified membership |
6a488035 | 2086 | * type. Specifically, retrieves a count of still current memberships whose |
4e636a74 | 2087 | * join_date and start_date are within a specified date range. Dates match |
8ef12e64 | 2088 | * the pattern "yyyy-mm-dd". |
6a488035 | 2089 | * |
b2363ea8 TO |
2090 | * @param int $membershipTypeId |
2091 | * Membership type id. | |
2092 | * @param int $startDate | |
2093 | * Date on which to start counting. | |
2094 | * @param int $endDate | |
2095 | * Date on which to end counting. | |
e46e9a0b | 2096 | * @param bool|int $isTest if true, membership is for a test site |
6a488035 | 2097 | * |
72b3a70c CW |
2098 | * @return int |
2099 | * the number of members of type $membershipTypeId | |
2100 | * whose join_date is between $startDate and $endDate and | |
2101 | * whose start_date is between $startDate and $endDate | |
6a488035 | 2102 | */ |
00be9182 | 2103 | public static function getMembershipJoins($membershipTypeId, $startDate, $endDate, $isTest = 0) { |
6a488035 TO |
2104 | $testClause = 'membership.is_test = 1'; |
2105 | if (!$isTest) { | |
2106 | $testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )'; | |
2107 | } | |
4e636a74 AH |
2108 | if (!self::$_signupActType) { |
2109 | self::_getActTypes(); | |
2110 | } | |
8ef12e64 | 2111 | |
4e636a74 AH |
2112 | if (!self::$_signupActType) { |
2113 | return 0; | |
2114 | } | |
6a488035 TO |
2115 | |
2116 | $query = " | |
8ef12e64 | 2117 | SELECT COUNT(DISTINCT membership.id) as member_count |
6a488035 | 2118 | FROM civicrm_membership membership |
8ef12e64 | 2119 | INNER JOIN civicrm_activity activity ON (activity.source_record_id = membership.id AND activity.activity_type_id = %1) |
6a488035 | 2120 | INNER JOIN civicrm_membership_status status ON ( membership.status_id = status.id AND status.is_current_member = 1 ) |
4e636a74 AH |
2121 | INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND contact.is_deleted = 0 ) |
2122 | WHERE membership.membership_type_id = %2 | |
2123 | AND activity.activity_date_time >= '$startDate' AND activity.activity_date_time <= '$endDate 23:59:59' | |
6a488035 TO |
2124 | AND {$testClause}"; |
2125 | ||
4e636a74 AH |
2126 | $params = array( |
2127 | 1 => array(self::$_signupActType, 'Integer'), | |
2128 | 2 => array($membershipTypeId, 'Integer'), | |
2129 | ); | |
2130 | ||
6a488035 TO |
2131 | $memberCount = CRM_Core_DAO::singleValueQuery($query, $params); |
2132 | ||
e0556ebe | 2133 | return (int) $memberCount; |
6a488035 TO |
2134 | } |
2135 | ||
2136 | /** | |
100fef9d | 2137 | * Get membership renewals for a specified membership |
4e636a74 | 2138 | * type. Specifically, retrieves a count of still current memberships |
8ef12e64 | 2139 | * whose join_date is before and start_date is within a specified date |
4e636a74 | 2140 | * range. Dates match the pattern "yyyy-mm-dd". |
6a488035 | 2141 | * |
b2363ea8 TO |
2142 | * @param int $membershipTypeId |
2143 | * Membership type id. | |
2144 | * @param int $startDate | |
2145 | * Date on which to start counting. | |
2146 | * @param int $endDate | |
2147 | * Date on which to end counting. | |
e46e9a0b | 2148 | * @param bool|int $isTest if true, membership is for a test site |
6a488035 | 2149 | * |
df8d3074 | 2150 | * @return int |
a6c01b45 | 2151 | * returns the number of members of type $membershipTypeId |
688d37c6 CW |
2152 | * whose join_date is before $startDate and |
2153 | * whose start_date is between $startDate and $endDate | |
6a488035 | 2154 | */ |
00be9182 | 2155 | public static function getMembershipRenewals($membershipTypeId, $startDate, $endDate, $isTest = 0) { |
6a488035 TO |
2156 | $testClause = 'membership.is_test = 1'; |
2157 | if (!$isTest) { | |
2158 | $testClause = '( membership.is_test IS NULL OR membership.is_test = 0 )'; | |
2159 | } | |
4e636a74 AH |
2160 | if (!self::$_renewalActType) { |
2161 | self::_getActTypes(); | |
2162 | } | |
8ef12e64 | 2163 | |
4e636a74 AH |
2164 | if (!self::$_renewalActType) { |
2165 | return 0; | |
2166 | } | |
6a488035 TO |
2167 | |
2168 | $query = " | |
8ef12e64 | 2169 | SELECT COUNT(DISTINCT membership.id) as member_count |
6a488035 | 2170 | FROM civicrm_membership membership |
4e636a74 | 2171 | INNER JOIN civicrm_activity activity ON (activity.source_record_id = membership.id AND activity.activity_type_id = %1) |
6a488035 TO |
2172 | INNER JOIN civicrm_membership_status status ON ( membership.status_id = status.id AND status.is_current_member = 1 ) |
2173 | INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND contact.is_deleted = 0 ) | |
4e636a74 | 2174 | WHERE membership.membership_type_id = %2 |
8ef12e64 | 2175 | AND activity.activity_date_time >= '$startDate' AND activity.activity_date_time <= '$endDate 23:59:59' |
6a488035 TO |
2176 | AND {$testClause}"; |
2177 | ||
4e636a74 AH |
2178 | $params = array( |
2179 | 1 => array(self::$_renewalActType, 'Integer'), | |
2180 | 2 => array($membershipTypeId, 'Integer'), | |
2181 | ); | |
6a488035 TO |
2182 | $memberCount = CRM_Core_DAO::singleValueQuery($query, $params); |
2183 | ||
e0556ebe | 2184 | return (int) $memberCount; |
6a488035 TO |
2185 | } |
2186 | ||
7273a245 EM |
2187 | /** |
2188 | * Where a second separate financial transaction is supported we will process it here | |
236a2274 | 2189 | * |
100fef9d | 2190 | * @param int $contactID |
5624f515 | 2191 | * @param CRM_Contribute_Form_Contribution_Confirm $form |
100fef9d | 2192 | * @param array $tempParams |
7273a245 | 2193 | * @param $isTest |
100fef9d | 2194 | * @param array $lineItems |
d25e4224 | 2195 | * @param $minimumFee |
100fef9d | 2196 | * @param int $financialTypeID |
d25e4224 | 2197 | * |
5624f515 | 2198 | * @throws CRM_Core_Exception |
d25e4224 | 2199 | * @throws Exception |
236a2274 | 2200 | * @return CRM_Contribute_BAO_Contribution |
7273a245 | 2201 | */ |
5624f515 | 2202 | public static function processSecondaryFinancialTransaction($contactID, &$form, $tempParams, $isTest, $lineItems, $minimumFee, $financialTypeID) { |
2883c2b2 EM |
2203 | $financialType = new CRM_Financial_DAO_FinancialType(); |
2204 | $financialType->id = $financialTypeID; | |
2205 | if (!$financialType->find(TRUE)) { | |
7273a245 EM |
2206 | CRM_Core_Error::fatal(ts("Could not find a system table")); |
2207 | } | |
2208 | $tempParams['amount'] = $minimumFee; | |
2209 | $tempParams['invoiceID'] = md5(uniqid(rand(), TRUE)); | |
2210 | ||
2211 | $result = NULL; | |
2212 | if ($form->_values['is_monetary'] && !$form->_params['is_pay_later'] && $minimumFee > 0.0) { | |
2213 | $payment = CRM_Core_Payment::singleton($form->_mode, $form->_paymentProcessor, $form); | |
2214 | ||
2215 | if ($form->_contributeMode == 'express') { | |
2216 | $result = $payment->doExpressCheckout($tempParams); | |
a19183c1 EM |
2217 | if (is_a($result, 'CRM_Core_Error')) { |
2218 | throw new CRM_Core_Exception(CRM_Core_Error::getMessages($result)); | |
2219 | } | |
7273a245 EM |
2220 | } |
2221 | else { | |
a19183c1 | 2222 | $result = $payment->doPayment($tempParams, 'contribute'); |
7273a245 EM |
2223 | } |
2224 | } | |
2225 | ||
a19183c1 EM |
2226 | //assign receive date when separate membership payment |
2227 | //and contribution amount not selected. | |
2228 | if ($form->_amount == 0) { | |
2229 | $now = date('YmdHis'); | |
2230 | $form->_params['receive_date'] = $now; | |
2231 | $receiveDate = CRM_Utils_Date::mysqlToIso($now); | |
2232 | $form->set('params', $form->_params); | |
2233 | $form->assign('receive_date', $receiveDate); | |
7273a245 | 2234 | } |
7273a245 | 2235 | |
a19183c1 EM |
2236 | $form->set('membership_trx_id', $result['trxn_id']); |
2237 | $form->set('membership_amount', $minimumFee); | |
2238 | ||
2239 | $form->assign('membership_trx_id', $result['trxn_id']); | |
2240 | $form->assign('membership_amount', $minimumFee); | |
2241 | ||
2242 | // we don't need to create the user twice, so lets disable cms_create_account | |
2243 | // irrespective of the value, CRM-2888 | |
2244 | $tempParams['cms_create_account'] = 0; | |
2245 | ||
2246 | $pending = $form->_params['is_pay_later'] ? (($minimumFee > 0.0) ? TRUE : FALSE) : FALSE; | |
2247 | ||
2248 | //set this variable as we are not creating pledge for | |
2249 | //separate membership payment contribution. | |
2250 | //so for differentiating membership contribution from | |
2251 | //main contribution. | |
2252 | $form->_params['separate_membership_payment'] = 1; | |
2253 | $membershipContribution = CRM_Contribute_Form_Contribution_Confirm::processContribution($form, | |
2254 | $tempParams, | |
2255 | $result, | |
2256 | $contactID, | |
2883c2b2 | 2257 | $financialType, |
a19183c1 EM |
2258 | $pending, |
2259 | TRUE, | |
2260 | $isTest, | |
2261 | $lineItems | |
2262 | ); | |
2263 | return $membershipContribution; | |
7273a245 EM |
2264 | } |
2265 | ||
0547ad7d EM |
2266 | /** |
2267 | * Create linkages between membership & contribution - note this is the wrong place for this code but this is a | |
2268 | * refactoring step. This should be BAO functionality | |
2269 | * @param $membership | |
2270 | * @param $membershipContribution | |
2271 | */ | |
2272 | public static function linkMembershipPayment($membership, $membershipContribution) { | |
e0556ebe | 2273 | CRM_Member_BAO_MembershipPayment::create(array( |
353ffa53 TO |
2274 | 'membership_id' => $membership->id, |
2275 | 'contribution_id' => $membershipContribution->id, | |
2276 | )); | |
0547ad7d EM |
2277 | } |
2278 | ||
8dde2f13 | 2279 | /** |
100fef9d CW |
2280 | * @param array $membershipParams |
2281 | * @param int $contactID | |
8dde2f13 | 2282 | * @param $customFieldsFormatted |
100fef9d | 2283 | * @param int $membershipID |
8dde2f13 EM |
2284 | * @param $memType |
2285 | * @param $isTest | |
2286 | * @param $numTerms | |
2287 | * @param $membershipContribution | |
c490a46a | 2288 | * @param CRM_Core_Form $form |
8dde2f13 EM |
2289 | * |
2290 | * @return array | |
2291 | */ | |
e0556ebe | 2292 | public static function createOrRenewMembership($membershipParams, $contactID, $customFieldsFormatted, $membershipID, $memType, $isTest, $numTerms, $membershipContribution, &$form) { |
c98997b9 | 2293 | $membership = self::renewMembershipFormWrapper($contactID, $memType, |
8dde2f13 EM |
2294 | $isTest, $form, NULL, |
2295 | CRM_Utils_Array::value('cms_contactID', $membershipParams), | |
2296 | $customFieldsFormatted, $numTerms, | |
2297 | $membershipID | |
2298 | ); | |
2299 | ||
8dde2f13 | 2300 | if (!empty($membershipContribution)) { |
423c9872 C |
2301 | // update recurring id for membership record |
2302 | self::updateRecurMembership($membership, $membershipContribution); | |
2303 | ||
8dde2f13 EM |
2304 | self::linkMembershipPayment($membership, $membershipContribution); |
2305 | } | |
2306 | return $membership; | |
2307 | } | |
2308 | ||
15c3308b EM |
2309 | /** |
2310 | * Turn array of errors into message string | |
2311 | * | |
2312 | * @param array $errors | |
2313 | * | |
15c3308b EM |
2314 | * @return string |
2315 | */ | |
e0556ebe TO |
2316 | public static function compileErrorMessage($errors) { |
2317 | foreach ($errors as $error) { | |
15c3308b EM |
2318 | if (is_string($error)) { |
2319 | $message[] = $error; | |
2320 | } | |
2321 | } | |
2322 | return ts('Payment Processor Error message') . ': ' . implode('<br/>', $message); | |
2323 | } | |
2324 | ||
c98997b9 EM |
2325 | /** |
2326 | * Extract relevant values from the form so we can separate form logic from BAO logcis | |
688d37c6 | 2327 | * |
c490a46a | 2328 | * @param CRM_Core_Form $form |
c98997b9 EM |
2329 | * @param $changeToday |
2330 | * @param $membershipTypeDetails | |
2331 | * | |
2332 | * @return array | |
2333 | */ | |
e0556ebe | 2334 | public static function extractFormValues($form, $changeToday, $membershipTypeDetails) { |
c98997b9 EM |
2335 | $pending = FALSE; |
2336 | //@todo this is a BAO function & should not inspect the form - the form should do this | |
2337 | // & pass required params to the BAO | |
2338 | if (CRM_Utils_Array::value('minimum_fee', $membershipTypeDetails) > 0.0) { | |
2339 | if (((isset($form->_contributeMode) && $form->_contributeMode == 'notify') || !empty($form->_params['is_pay_later']) || | |
2340 | (!empty($form->_params['is_recur']) && $form->_contributeMode == 'direct' | |
2341 | ) | |
2342 | ) && | |
2343 | (($form->_values['is_monetary'] && $form->_amount > 0.0) || !empty($form->_params['separate_membership_payment']) || | |
2344 | CRM_Utils_Array::value('record_contribution', $form->_params) | |
2345 | ) | |
2346 | ) { | |
2347 | $pending = TRUE; | |
2348 | } | |
2349 | } | |
2350 | $contributionRecurID = isset($form->_params['contributionRecurID']) ? $form->_params['contributionRecurID'] : NULL; | |
2351 | ||
2352 | //we renew expired membership, CRM-6277 | |
2353 | if (!$changeToday) { | |
2354 | if ($form->get('renewalDate')) { | |
2355 | $changeToday = $form->get('renewalDate'); | |
2356 | } | |
2357 | elseif (get_class($form) == 'CRM_Contribute_Form_Contribution_Confirm') { | |
2358 | $changeToday = date('YmdHis'); | |
2359 | } | |
2360 | } | |
2361 | ||
c352e9fc | 2362 | $membershipSource = NULL; |
c98997b9 EM |
2363 | if (!empty($form->_params['membership_source'])) { |
2364 | $membershipSource = $form->_params['membership_source']; | |
2365 | } | |
2366 | elseif (isset($form->_values['title']) && !empty($form->_values['title'])) { | |
2367 | $membershipSource = ts('Online Contribution:') . ' ' . $form->_values['title']; | |
2368 | } | |
c352e9fc | 2369 | $isPayLater = NULL; |
e0556ebe | 2370 | if (isset($form->_params)) { |
c352e9fc C |
2371 | $isPayLater = CRM_Utils_Array::value('is_pay_later', $form->_params); |
2372 | } | |
c98997b9 EM |
2373 | $campaignId = NULL; |
2374 | if (isset($form->_values) && is_array($form->_values) && !empty($form->_values)) { | |
2375 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_params); | |
2376 | if (!array_key_exists('campaign_id', $form->_params)) { | |
2377 | $campaignId = CRM_Utils_Array::value('campaign_id', $form->_values); | |
2378 | } | |
2379 | } | |
2380 | return array($pending, $contributionRecurID, $changeToday, $membershipSource, $isPayLater, $campaignId); | |
2381 | } | |
2382 | ||
2383 | /** | |
b2363ea8 | 2384 | * @param int $contactID |
100fef9d | 2385 | * @param int $membershipTypeID |
c98997b9 EM |
2386 | * @param bool $is_test |
2387 | * @param $changeToday | |
b2363ea8 | 2388 | * @param int $modifiedID |
c98997b9 EM |
2389 | * @param $customFieldsFormatted |
2390 | * @param $numRenewTerms | |
100fef9d | 2391 | * @param int $membershipID |
c98997b9 EM |
2392 | * @param $pending |
2393 | * @param $allStatus | |
2394 | * @param array $membershipTypeDetails | |
b2363ea8 | 2395 | * @param int $contributionRecurID |
c98997b9 EM |
2396 | * @param $format |
2397 | * @param $membershipSource | |
2398 | * @param $ids | |
2399 | * @param $statusFormat | |
2400 | * @param $isPayLater | |
b2363ea8 | 2401 | * @param int $campaignId |
c98997b9 EM |
2402 | * |
2403 | * @throws CRM_Core_Exception | |
2404 | * @return array | |
2405 | */ | |
2406 | public static function renewMembership($contactID, $membershipTypeID, $is_test, $changeToday, $modifiedID, $customFieldsFormatted, $numRenewTerms, $membershipID, $pending, $allStatus, $membershipTypeDetails, $contributionRecurID, $format, $membershipSource, $ids, $statusFormat, $isPayLater, $campaignId) { | |
2407 | $renewalMode = $updateStatusId = FALSE; | |
2408 | $dates = array(); | |
d81c67a2 | 2409 | // CRM-7297 - allow membership type to be be changed during renewal so long as the parent org of new membershipType |
c98997b9 EM |
2410 | // is the same as the parent org of an existing membership of the contact |
2411 | $currentMembership = CRM_Member_BAO_Membership::getContactMembership($contactID, $membershipTypeID, | |
2412 | $is_test, $membershipID, TRUE | |
2413 | ); | |
2414 | if ($currentMembership) { | |
2415 | $activityType = 'Membership Renewal'; | |
2416 | $renewalMode = TRUE; | |
2417 | ||
2418 | // Do NOT do anything. | |
2419 | //1. membership with status : PENDING/CANCELLED (CRM-2395) | |
2420 | //2. Paylater/IPN renew. CRM-4556. | |
e0556ebe TO |
2421 | if ($pending || in_array($currentMembership['status_id'], array( |
2422 | array_search('Pending', $allStatus), | |
7ff60806 PN |
2423 | // CRM-15475 |
2424 | array_search('Cancelled', CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE)), | |
c98997b9 EM |
2425 | )) |
2426 | ) { | |
2427 | $membership = new CRM_Member_DAO_Membership(); | |
2428 | $membership->id = $currentMembership['id']; | |
2429 | $membership->find(TRUE); | |
2430 | ||
2431 | // CRM-8141 create a membership_log entry so that we will know the membership_type_id to change to when payment completed | |
2432 | $format = '%Y%m%d'; | |
2433 | // note that we are logging the requested new membership_type_id that may be different than current membership_type_id | |
2434 | // it will be used when payment is received to update the membership_type_id to what was paid for | |
2435 | $logParams = array( | |
2436 | 'membership_id' => $membership->id, | |
2437 | 'status_id' => $membership->status_id, | |
2438 | 'start_date' => CRM_Utils_Date::customFormat( | |
2439 | $membership->start_date, | |
2440 | $format | |
2441 | ), | |
2442 | 'end_date' => CRM_Utils_Date::customFormat( | |
2443 | $membership->end_date, | |
2444 | $format | |
2445 | ), | |
2446 | 'modified_date' => CRM_Utils_Date::customFormat( | |
2447 | date('Ymd'), | |
2448 | $format | |
2449 | ), | |
2450 | 'membership_type_id' => $membershipTypeID, | |
ef0abb4c | 2451 | 'max_related' => !empty($membershipTypeDetails['max_related']) ? $membershipTypeDetails['max_related'] : NULL, |
c98997b9 EM |
2452 | ); |
2453 | $session = CRM_Core_Session::singleton(); | |
2454 | // If we have an authenticated session, set modified_id to that user's contact_id, else set to membership.contact_id | |
2455 | if ($session->get('userID')) { | |
2456 | $logParams['modified_id'] = $session->get('userID'); | |
2457 | } | |
2458 | else { | |
2459 | $logParams['modified_id'] = $membership->contact_id; | |
2460 | } | |
2461 | CRM_Member_BAO_MembershipLog::add($logParams, CRM_Core_DAO::$_nullArray); | |
2462 | ||
2463 | if (!empty($contributionRecurID)) { | |
2464 | CRM_Core_DAO::setFieldValue('CRM_Member_DAO_Membership', $membership->id, | |
2465 | 'contribution_recur_id', $contributionRecurID | |
2466 | ); | |
2467 | } | |
2468 | ||
f82cf0ed | 2469 | return array($membership, $renewalMode, $dates); |
c98997b9 EM |
2470 | } |
2471 | ||
2472 | // Check and fix the membership if it is STALE | |
2473 | self::fixMembershipStatusBeforeRenew($currentMembership, $changeToday); | |
2474 | ||
2475 | // Now Renew the membership | |
2476 | if (!$currentMembership['is_current_member']) { | |
2477 | // membership is not CURRENT | |
2478 | ||
2479 | // CRM-7297 Membership Upsell - calculate dates based on new membership type | |
2480 | $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($currentMembership['id'], | |
2481 | $changeToday, | |
2482 | $membershipTypeID, | |
2483 | $numRenewTerms | |
2484 | ); | |
2485 | ||
2486 | $currentMembership['join_date'] = CRM_Utils_Date::customFormat($currentMembership['join_date'], $format); | |
2487 | $currentMembership['start_date'] = CRM_Utils_Array::value('start_date', $dates); | |
2488 | $currentMembership['end_date'] = CRM_Utils_Array::value('end_date', $dates); | |
2489 | $currentMembership['is_test'] = $is_test; | |
2490 | ||
2491 | if (!empty($membershipSource)) { | |
2492 | $currentMembership['source'] = $membershipSource; | |
2493 | } | |
2494 | else { | |
2495 | $currentMembership['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
2496 | $currentMembership['id'], | |
2497 | 'source' | |
2498 | ); | |
2499 | } | |
2500 | ||
2501 | if (!empty($currentMembership['id'])) { | |
2502 | $ids['membership'] = $currentMembership['id']; | |
2503 | } | |
2504 | $memParams = $currentMembership; | |
2505 | $memParams['membership_type_id'] = $membershipTypeID; | |
2506 | ||
2507 | //set the log start date. | |
2508 | $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); | |
2509 | } | |
2510 | else { | |
2511 | ||
2512 | // CURRENT Membership | |
2513 | $membership = new CRM_Member_DAO_Membership(); | |
2514 | $membership->id = $currentMembership['id']; | |
2515 | $membership->find(TRUE); | |
2516 | // CRM-7297 Membership Upsell - calculate dates based on new membership type | |
2517 | $dates = CRM_Member_BAO_MembershipType::getRenewalDatesForMembershipType($membership->id, | |
2518 | $changeToday, | |
2519 | $membershipTypeID, | |
2520 | $numRenewTerms | |
2521 | ); | |
2522 | ||
2523 | // Insert renewed dates for CURRENT membership | |
2524 | $memParams = array(); | |
2525 | $memParams['join_date'] = CRM_Utils_Date::isoToMysql($membership->join_date); | |
2526 | $memParams['start_date'] = CRM_Utils_Date::isoToMysql($membership->start_date); | |
2527 | $memParams['end_date'] = CRM_Utils_Array::value('end_date', $dates); | |
2528 | $memParams['membership_type_id'] = $membershipTypeID; | |
2529 | ||
2530 | //set the log start date. | |
2531 | $memParams['log_start_date'] = CRM_Utils_Date::customFormat($dates['log_start_date'], $format); | |
2532 | if (empty($membership->source)) { | |
2533 | if (!empty($membershipSource)) { | |
2534 | $memParams['source'] = $membershipSource; | |
2535 | } | |
2536 | else { | |
2537 | $memParams['source'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', | |
2538 | $currentMembership['id'], | |
2539 | 'source' | |
2540 | ); | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | if (!empty($currentMembership['id'])) { | |
2545 | $ids['membership'] = $currentMembership['id']; | |
2546 | } | |
2547 | } | |
2548 | //CRM-4555 | |
2549 | if ($pending) { | |
2550 | $updateStatusId = array_search('Pending', $allStatus); | |
2551 | } | |
2552 | } | |
2553 | else { | |
2554 | // NEW Membership | |
2555 | ||
2556 | $activityType = 'Membership Signup'; | |
2557 | $memParams = array( | |
2558 | 'contact_id' => $contactID, | |
2559 | 'membership_type_id' => $membershipTypeID, | |
2560 | ); | |
2561 | ||
2562 | if (!$pending) { | |
2563 | $dates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($membershipTypeID, NULL, NULL, NULL, $numRenewTerms); | |
2564 | ||
2565 | $memParams['join_date'] = CRM_Utils_Array::value('join_date', $dates); | |
2566 | $memParams['start_date'] = CRM_Utils_Array::value('start_date', $dates); | |
2567 | $memParams['end_date'] = CRM_Utils_Array::value('end_date', $dates); | |
2568 | ||
2569 | $status = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate(CRM_Utils_Date::customFormat($dates['start_date'], | |
2570 | $statusFormat | |
2571 | ), | |
2572 | CRM_Utils_Date::customFormat($dates['end_date'], | |
2573 | $statusFormat | |
2574 | ), | |
2575 | CRM_Utils_Date::customFormat($dates['join_date'], | |
2576 | $statusFormat | |
2577 | ), | |
2578 | 'today', | |
2579 | TRUE, | |
2580 | $membershipTypeID, | |
2581 | $memParams | |
2582 | ); | |
2583 | $updateStatusId = CRM_Utils_Array::value('id', $status); | |
2584 | } | |
2585 | else { | |
2586 | // if IPN/Pay-Later set status to: PENDING | |
2587 | $updateStatusId = array_search('Pending', $allStatus); | |
2588 | } | |
2589 | ||
2590 | if (!empty($membershipSource)) { | |
2591 | $memParams['source'] = $membershipSource; | |
2592 | } | |
2593 | $memParams['contribution_recur_id'] = $contributionRecurID; | |
2594 | ||
2595 | $memParams['is_test'] = $is_test; | |
2596 | $memParams['is_pay_later'] = $isPayLater; | |
2597 | } | |
2598 | ||
2599 | //CRM-4555 | |
2600 | //if we decided status here and want to skip status | |
2601 | //calculation in create( ); then need to pass 'skipStatusCal'. | |
2602 | if ($updateStatusId) { | |
2603 | $memParams['status_id'] = $updateStatusId; | |
2604 | $memParams['skipStatusCal'] = TRUE; | |
2605 | } | |
2606 | ||
2607 | //since we are renewing, | |
2608 | //make status override false. | |
2609 | $memParams['is_override'] = FALSE; | |
2610 | ||
2611 | //CRM-4027, create log w/ individual contact. | |
2612 | if ($modifiedID) { | |
2613 | $ids['userId'] = $modifiedID; | |
2614 | $memParams['is_for_organization'] = TRUE; | |
2615 | } | |
2616 | else { | |
2617 | $ids['userId'] = $contactID; | |
2618 | } | |
2619 | ||
2620 | //inherit campaign from contrib page. | |
2621 | if (isset($campaignId)) { | |
2622 | $memParams['campaign_id'] = $campaignId; | |
2623 | } | |
2624 | ||
2625 | $memParams['custom'] = $customFieldsFormatted; | |
2626 | $membership = self::create($memParams, $ids, FALSE, $activityType); | |
2627 | ||
2628 | // not sure why this statement is here, seems quite odd :( - Lobo: 12/26/2010 | |
2629 | // related to: http://forum.civicrm.org/index.php/topic,11416.msg49072.html#msg49072 | |
2630 | $membership->find(TRUE); | |
2631 | ||
2632 | return array($membership, $renewalMode, $dates); | |
2633 | } | |
2634 | ||
6a488035 | 2635 | /** |
100fef9d | 2636 | * Process price set and line items. |
6a488035 | 2637 | * |
100fef9d | 2638 | * @param int $membershipId |
e46e9a0b EM |
2639 | * @param $lineItem |
2640 | * | |
355ba699 | 2641 | * @return void |
6a488035 | 2642 | */ |
00be9182 | 2643 | public function processPriceSet($membershipId, $lineItem) { |
6a488035 TO |
2644 | //FIXME : need to move this too |
2645 | if (!$membershipId || !is_array($lineItem) | |
ca58d9b9 | 2646 | || CRM_Utils_System::isNull($lineItem) |
6a488035 TO |
2647 | ) { |
2648 | return; | |
2649 | } | |
2650 | ||
2651 | foreach ($lineItem as $priceSetId => $values) { | |
2652 | if (!$priceSetId) { | |
2653 | continue; | |
2654 | } | |
2655 | foreach ($values as $line) { | |
2656 | $line['entity_table'] = 'civicrm_membership'; | |
2657 | $line['entity_id'] = $membershipId; | |
2658 | CRM_Price_BAO_LineItem::create($line); | |
2659 | } | |
2660 | } | |
2661 | } | |
2662 | ||
2663 | /** | |
100fef9d | 2664 | * Retrieve the contribution id for the associated Membership id |
ca266339 | 2665 | * @todo we should get this off the line item |
6a488035 | 2666 | * |
b2363ea8 TO |
2667 | * @param int $membershipId |
2668 | * Membership id. | |
6a488035 | 2669 | * |
df8d3074 | 2670 | * @return int |
a6c01b45 | 2671 | * contribution id |
6a488035 | 2672 | */ |
00be9182 | 2673 | public static function getMembershipContributionId($membershipId) { |
6a488035 | 2674 | |
ca266339 EM |
2675 | $membershipPayment = new CRM_Member_DAO_MembershipPayment(); |
2676 | $membershipPayment->membership_id = $membershipId; | |
2677 | if ($membershipPayment->find(TRUE)) { | |
2678 | return $membershipPayment->contribution_id; | |
6a488035 TO |
2679 | } |
2680 | return NULL; | |
2681 | } | |
2682 | ||
2683 | /** | |
e0556ebe TO |
2684 | * The function checks and updates the status of all membership records for a given domain using the |
2685 | * calc_membership_status and update_contact_membership APIs. | |
2686 | * | |
2687 | * IMPORTANT: | |
2688 | * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3. | |
2689 | * | |
a6c01b45 | 2690 | * @return array |
e0556ebe | 2691 | */ |
00be9182 | 2692 | public static function updateAllMembershipStatus() { |
6a488035 TO |
2693 | |
2694 | //get all active statuses of membership, CRM-3984 | |
e0556ebe TO |
2695 | $allStatus = CRM_Member_PseudoConstant::membershipStatus(); |
2696 | $statusLabels = CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'label'); | |
2697 | $allTypes = CRM_Member_PseudoConstant::membershipType(); | |
6a488035 | 2698 | |
85b6e9a2 | 2699 | // get only memberships with active membership types |
6a488035 TO |
2700 | $query = " |
2701 | SELECT civicrm_membership.id as membership_id, | |
2702 | civicrm_membership.is_override as is_override, | |
2703 | civicrm_membership.membership_type_id as membership_type_id, | |
2704 | civicrm_membership.status_id as status_id, | |
2705 | civicrm_membership.join_date as join_date, | |
2706 | civicrm_membership.start_date as start_date, | |
2707 | civicrm_membership.end_date as end_date, | |
2708 | civicrm_membership.source as source, | |
2709 | civicrm_contact.id as contact_id, | |
2710 | civicrm_contact.is_deceased as is_deceased, | |
2711 | civicrm_membership.owner_membership_id as owner_membership_id, | |
2712 | civicrm_membership.contribution_recur_id as recur_id | |
2713 | FROM civicrm_membership | |
2714 | INNER JOIN civicrm_contact ON ( civicrm_membership.contact_id = civicrm_contact.id ) | |
85b6e9a2 DL |
2715 | INNER JOIN civicrm_membership_type ON |
2716 | (civicrm_membership.membership_type_id = civicrm_membership_type.id AND civicrm_membership_type.is_active = 1) | |
6a488035 TO |
2717 | WHERE civicrm_membership.is_test = 0"; |
2718 | ||
2719 | $params = array(); | |
2720 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
2721 | ||
e0556ebe TO |
2722 | $processCount = 0; |
2723 | $updateCount = 0; | |
6a488035 TO |
2724 | |
2725 | $smarty = CRM_Core_Smarty::singleton(); | |
2726 | ||
2727 | while ($dao->fetch()) { | |
2728 | // echo "."; | |
2729 | $processCount++; | |
2730 | ||
6a488035 TO |
2731 | // Put common parameters into array for easy access |
2732 | $memberParams = array( | |
2733 | 'id' => $dao->membership_id, | |
2734 | 'status_id' => $dao->status_id, | |
2735 | 'contact_id' => $dao->contact_id, | |
2736 | 'membership_type_id' => $dao->membership_type_id, | |
2737 | 'membership_type' => $allTypes[$dao->membership_type_id], | |
2738 | 'join_date' => $dao->join_date, | |
2739 | 'start_date' => $dao->start_date, | |
2740 | 'end_date' => $dao->end_date, | |
2741 | 'source' => $dao->source, | |
2742 | 'skipStatusCal' => TRUE, | |
2743 | 'skipRecentView' => TRUE, | |
2744 | ); | |
2745 | ||
2746 | $smarty->assign_by_ref('memberParams', $memberParams); | |
2747 | ||
2748 | //update membership record to Deceased if contact is deceased | |
2749 | if ($dao->is_deceased) { | |
2750 | // check for 'Deceased' membership status, CRM-5636 | |
2751 | $deceaseStatusId = array_search('Deceased', $allStatus); | |
2752 | if (!$deceaseStatusId) { | |
2753 | CRM_Core_Error::fatal(ts("Deceased Membership status is missing or not active. <a href='%1'>Click here to check</a>.", array(1 => CRM_Utils_System::url('civicrm/admin/member/membershipStatus', 'reset=1')))); | |
2754 | } | |
2755 | ||
2756 | //process only when status change. | |
2757 | if ($dao->status_id != $deceaseStatusId) { | |
2758 | //take all params that need to save. | |
2759 | $deceasedMembership = $memberParams; | |
2760 | $deceasedMembership['status_id'] = $deceaseStatusId; | |
2761 | $deceasedMembership['createActivity'] = TRUE; | |
2762 | $deceasedMembership['version'] = 3; | |
2763 | ||
2764 | //since there is change in status. | |
2765 | $statusChange = array('status_id' => $deceaseStatusId); | |
2766 | $smarty->append_by_ref('memberParams', $statusChange, TRUE); | |
68cf2de1 | 2767 | unset( |
2768 | $deceasedMembership['contact_id'], | |
2769 | $deceasedMembership['membership_type_id'], | |
2770 | $deceasedMembership['membership_type'], | |
2771 | $deceasedMembership['join_date'], | |
2772 | $deceasedMembership['start_date'], | |
2773 | $deceasedMembership['end_date'], | |
2774 | $deceasedMembership['source'] | |
2775 | ); | |
6a488035 TO |
2776 | |
2777 | //process membership record. | |
2778 | civicrm_api('membership', 'create', $deceasedMembership); | |
2779 | } | |
2780 | continue; | |
2781 | } | |
2782 | ||
2783 | //we fetch related, since we need to check for deceased | |
2784 | //now further processing is handle w/ main membership record. | |
2785 | if ($dao->owner_membership_id) { | |
2786 | continue; | |
2787 | } | |
2788 | ||
2789 | //update membership records where status is NOT - Pending OR Cancelled. | |
2790 | //as well as membership is not override. | |
2791 | //skipping Expired membership records -> reduced extra processing( kiran ) | |
2792 | if (!$dao->is_override && | |
e0556ebe TO |
2793 | !in_array($dao->status_id, array( |
2794 | array_search('Pending', $allStatus), | |
2795 | // CRM-15475 | |
2796 | array_search( | |
2797 | 'Cancelled', | |
2798 | CRM_Member_PseudoConstant::membershipStatus(NULL, " name = 'Cancelled' ", 'name', FALSE, TRUE) | |
2799 | ), | |
2800 | array_search('Expired', $allStatus), | |
2801 | )) | |
6a488035 TO |
2802 | ) { |
2803 | ||
2804 | // CRM-7248: added excludeIsAdmin param to the following fn call to prevent moving to admin statuses | |
2805 | //get the membership status as per id. | |
2806 | $newStatus = civicrm_api('membership_status', 'calc', | |
2807 | array( | |
e0556ebe TO |
2808 | 'membership_id' => $dao->membership_id, |
2809 | 'version' => 3, | |
21dfd5f5 | 2810 | 'ignore_admin_only' => FALSE, |
e0556ebe | 2811 | ), TRUE |
6a488035 TO |
2812 | ); |
2813 | $statusId = CRM_Utils_Array::value('id', $newStatus); | |
2814 | ||
2815 | //process only when status change. | |
2816 | if ($statusId && | |
2817 | $statusId != $dao->status_id | |
2818 | ) { | |
2819 | //take all params that need to save. | |
2820 | $memParams = $memberParams; | |
2821 | $memParams['status_id'] = $statusId; | |
2822 | $memParams['createActivity'] = TRUE; | |
2823 | $memParams['version'] = 3; | |
2824 | ||
4daee127 | 2825 | // Unset columns which should remain unchanged from their current saved |
2826 | // values. This avoids race condition in which these values may have | |
2827 | // been changed by other processes. | |
2828 | unset( | |
2829 | $memParams['contact_id'], | |
2830 | $memParams['membership_type_id'], | |
2831 | $memParams['membership_type'], | |
2832 | $memParams['join_date'], | |
2833 | $memParams['start_date'], | |
2834 | $memParams['end_date'], | |
2835 | $memParams['source'] | |
2836 | ); | |
6a488035 TO |
2837 | //since there is change in status. |
2838 | $statusChange = array('status_id' => $statusId); | |
2839 | $smarty->append_by_ref('memberParams', $statusChange, TRUE); | |
2840 | ||
2841 | //process member record. | |
2842 | civicrm_api('membership', 'create', $memParams); | |
2843 | $updateCount++; | |
2844 | } | |
2845 | } | |
6a488035 TO |
2846 | } |
2847 | $result['is_error'] = 0; | |
e0556ebe | 2848 | $result['messages'] = ts('Processed %1 membership records. Updated %2 records.', array( |
353ffa53 TO |
2849 | 1 => $processCount, |
2850 | 2 => $updateCount, | |
2851 | )); | |
6a488035 TO |
2852 | return $result; |
2853 | } | |
2854 | ||
e46e9a0b | 2855 | /** |
688d37c6 | 2856 | * Returns the membership types for a particular contact |
6a488035 TO |
2857 | * who has lifetime membership without end date. |
2858 | * | |
100fef9d | 2859 | * @param int $contactID |
e46e9a0b EM |
2860 | * @param bool $isTest |
2861 | * @param bool $onlyLifeTime | |
6a488035 | 2862 | * |
e46e9a0b | 2863 | * @return array |
6a488035 | 2864 | */ |
00be9182 | 2865 | public static function getAllContactMembership($contactID, $isTest = FALSE, $onlyLifeTime = FALSE) { |
6a488035 TO |
2866 | $contactMembershipType = array(); |
2867 | if (!$contactID) { | |
2868 | return $contactMembershipType; | |
2869 | } | |
2870 | ||
e0556ebe | 2871 | $dao = new CRM_Member_DAO_Membership(); |
6a488035 TO |
2872 | $dao->contact_id = $contactID; |
2873 | $pendingStatusId = array_search('Pending', CRM_Member_PseudoConstant::membershipStatus()); | |
2874 | $dao->whereAdd("status_id != $pendingStatusId"); | |
2875 | ||
2876 | if ($isTest) { | |
2877 | $dao->is_test = $isTest; | |
2878 | } | |
2879 | else { | |
2880 | $dao->whereAdd('is_test IS NULL OR is_test = 0'); | |
2881 | } | |
2882 | ||
2883 | if ($onlyLifeTime) { | |
2884 | $dao->whereAdd('end_date IS NULL'); | |
2885 | } | |
2886 | ||
2887 | $dao->find(); | |
2888 | while ($dao->fetch()) { | |
2889 | $membership = array(); | |
2890 | CRM_Core_DAO::storeValues($dao, $membership); | |
2891 | $contactMembershipType[$dao->membership_type_id] = $membership; | |
2892 | } | |
2893 | return $contactMembershipType; | |
2894 | } | |
2895 | ||
2896 | /** | |
100fef9d | 2897 | * Record contribution record associated with membership |
6a488035 | 2898 | * |
b2363ea8 TO |
2899 | * @param array $params |
2900 | * Array of submitted params. | |
2901 | * @param array $ids | |
2902 | * (param in process of being removed - try to use params) array of ids. | |
6a488035 | 2903 | * |
02af3683 | 2904 | * @return CRM_Contribute_BAO_Contribution |
6a488035 | 2905 | */ |
e0556ebe | 2906 | public static function recordMembershipContribution(&$params, $ids = array()) { |
d824fb6e | 2907 | $membershipId = $params['membership_id']; |
6a488035 TO |
2908 | $contributionParams = array(); |
2909 | $config = CRM_Core_Config::singleton(); | |
2910 | $contributionParams['currency'] = $config->defaultCurrency; | |
2911 | $contributionParams['receipt_date'] = (CRM_Utils_Array::value('receipt_date', $params)) ? $params['receipt_date'] : 'null'; | |
2912 | $contributionParams['source'] = CRM_Utils_Array::value('contribution_source', $params); | |
6a488035 | 2913 | $contributionParams['non_deductible_amount'] = 'null'; |
dcb032dc | 2914 | $contributionParams['payment_processor'] = CRM_Utils_Array::value('payment_processor_id', $params); |
d80dbc14 | 2915 | $contributionSoftParams = CRM_Utils_Array::value('soft_credit', $params); |
6a488035 | 2916 | $recordContribution = array( |
e0556ebe TO |
2917 | 'contact_id', |
2918 | 'total_amount', | |
2919 | 'receive_date', | |
2920 | 'financial_type_id', | |
2921 | 'payment_instrument_id', | |
2922 | 'trxn_id', | |
2923 | 'invoice_id', | |
2924 | 'is_test', | |
2925 | 'contribution_status_id', | |
2926 | 'check_number', | |
2927 | 'campaign_id', | |
2928 | 'is_pay_later', | |
2929 | 'membership_id', | |
2930 | 'tax_amount', | |
21dfd5f5 | 2931 | 'skipLineItem', |
6a488035 TO |
2932 | ); |
2933 | foreach ($recordContribution as $f) { | |
2934 | $contributionParams[$f] = CRM_Utils_Array::value($f, $params); | |
2935 | } | |
2936 | ||
2937 | // make entry in batch entity batch table | |
a7488080 | 2938 | if (!empty($params['batch_id'])) { |
6a488035 TO |
2939 | $contributionParams['batch_id'] = $params['batch_id']; |
2940 | } | |
2941 | ||
91ef9be0 | 2942 | if (!empty($params['contribution_contact_id'])) { |
2943 | // deal with possibility of a different person paying for contribution | |
2944 | $contributionParams['contact_id'] = $params['contribution_contact_id']; | |
2945 | } | |
2946 | ||
a7488080 | 2947 | if (!empty($params['processPriceSet']) && |
6a488035 TO |
2948 | !empty($params['lineItems']) |
2949 | ) { | |
2950 | $contributionParams['line_item'] = CRM_Utils_Array::value('lineItems', $params, NULL); | |
2951 | } | |
2952 | ||
2953 | $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams, $ids); | |
2954 | ||
c206647d | 2955 | //CRM-13981, create new soft-credit record as to record payment from different person for this membership |
d80dbc14 | 2956 | if (!empty($contributionSoftParams)) { |
9e1854a1 | 2957 | if (!empty($params['batch_id'])) { |
2958 | foreach ($contributionSoftParams as $contributionSoft) { | |
2959 | $contributionSoft['contribution_id'] = $contribution->id; | |
2960 | $contributionSoft['currency'] = $contribution->currency; | |
2961 | CRM_Contribute_BAO_ContributionSoft::add($contributionSoft); | |
2962 | } | |
2963 | } | |
2964 | else { | |
e0556ebe TO |
2965 | $contributionSoftParams['contribution_id'] = $contribution->id; |
2966 | $contributionSoftParams['currency'] = $contribution->currency; | |
2967 | $contributionSoftParams['amount'] = $contribution->total_amount; | |
2968 | CRM_Contribute_BAO_ContributionSoft::add($contributionSoftParams); | |
a54e2c55 | 2969 | } |
d80dbc14 | 2970 | } |
2971 | ||
6a488035 TO |
2972 | // store contribution id |
2973 | $params['contribution_id'] = $contribution->id; | |
2974 | ||
6a488035 | 2975 | //insert payment record for this membership |
8cc574cf | 2976 | if (empty($ids['contribution']) || !empty($params['is_recur'])) { |
e0556ebe | 2977 | CRM_Member_BAO_MembershipPayment::create(array( |
353ffa53 TO |
2978 | 'membership_id' => $membershipId, |
2979 | 'contribution_id' => $contribution->id, | |
2980 | )); | |
6a488035 TO |
2981 | } |
2982 | return $contribution; | |
2983 | } | |
2984 | ||
2985 | /** | |
100fef9d | 2986 | * Record line items for default membership |
6a488035 | 2987 | * |
c490a46a | 2988 | * @param CRM_Core_Form $qf |
b2363ea8 TO |
2989 | * @param array $membershipType |
2990 | * Array with membership type and organization. | |
c490a46a | 2991 | * @param int $priceSetId |
e46e9a0b | 2992 | * |
6a488035 | 2993 | */ |
00be9182 | 2994 | public static function createLineItems(&$qf, $membershipType, &$priceSetId) { |
9da8dc8c | 2995 | $qf->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name'); |
6a488035 | 2996 | if ($priceSetId) { |
9da8dc8c | 2997 | $qf->_priceSet = $priceSets = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); |
6a488035 TO |
2998 | } |
2999 | $editedFieldParams = array( | |
3000 | 'price_set_id' => $priceSetId, | |
3001 | 'name' => $membershipType[0], | |
3002 | ); | |
3003 | $editedResults = array(); | |
9da8dc8c | 3004 | CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); |
6a488035 TO |
3005 | |
3006 | if (!empty($editedResults)) { | |
3007 | unset($qf->_priceSet['fields']); | |
3008 | $qf->_priceSet['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']]; | |
3009 | unset($qf->_priceSet['fields'][$editedResults['id']]['options']); | |
3010 | $fid = $editedResults['id']; | |
3011 | $editedFieldParams = array( | |
3012 | 'price_field_id' => $editedResults['id'], | |
3013 | 'membership_type_id' => $membershipType[1], | |
3014 | ); | |
3015 | $editedResults = array(); | |
9da8dc8c | 3016 | CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); |
6a488035 | 3017 | $qf->_priceSet['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']]; |
a7488080 | 3018 | if (!empty($qf->_params['total_amount'])) { |
6a488035 TO |
3019 | $qf->_priceSet['fields'][$fid]['options'][$editedResults['id']]['amount'] = $qf->_params['total_amount']; |
3020 | } | |
3021 | } | |
3022 | ||
3023 | $fieldID = key($qf->_priceSet['fields']); | |
3024 | $qf->_params['price_' . $fieldID] = CRM_Utils_Array::value('id', $editedResults); | |
3025 | } | |
8ef12e64 | 3026 | |
e46e9a0b EM |
3027 | /** |
3028 | * @todo document me - I seem a bit out of date.... | |
3029 | */ | |
00be9182 | 3030 | public static function _getActTypes() { |
4e636a74 AH |
3031 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); |
3032 | self::$_renewalActType = CRM_Utils_Array::key('Membership Renewal', $activityTypes); | |
3033 | self::$_signupActType = CRM_Utils_Array::key('Membership Signup', $activityTypes); | |
3034 | } | |
5624f515 | 3035 | |
b5a62499 PN |
3036 | /** |
3037 | * Get all Cancelled Membership(s) for a contact | |
3038 | * | |
b2363ea8 TO |
3039 | * @param int $contactID |
3040 | * Contact id. | |
3041 | * @param bool $isTest | |
3042 | * Mode of payment. | |
b5a62499 | 3043 | * |
a6c01b45 | 3044 | * @return array |
16b10e64 | 3045 | * Array of membership type |
b5a62499 | 3046 | */ |
00be9182 | 3047 | public static function getContactsCancelledMembership($contactID, $isTest = FALSE) { |
b5a62499 PN |
3048 | if (!$contactID) { |
3049 | return array(); | |
5624f515 | 3050 | } |
b5a62499 PN |
3051 | $query = 'SELECT membership_type_id FROM civicrm_membership WHERE contact_id = %1 AND status_id = %2 AND is_test = %3'; |
3052 | $queryParams = array( | |
3053 | 1 => array($contactID, 'Integer'), | |
7ff60806 PN |
3054 | 2 => array( |
3055 | // CRM-15475 | |
3056 | array_search( | |
4c16123d | 3057 | 'Cancelled', |
7ff60806 | 3058 | CRM_Member_PseudoConstant::membershipStatus( |
4c16123d EM |
3059 | NULL, |
3060 | " name = 'Cancelled' ", | |
3061 | 'name', | |
3062 | FALSE, | |
7ff60806 PN |
3063 | TRUE |
3064 | ) | |
4c16123d | 3065 | ), |
21dfd5f5 | 3066 | 'Integer', |
7ff60806 | 3067 | ), |
b5a62499 PN |
3068 | 3 => array($isTest, 'Boolean'), |
3069 | ); | |
5624f515 | 3070 | |
b5a62499 PN |
3071 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); |
3072 | $cancelledMembershipIds = array(); | |
3073 | while ($dao->fetch()) { | |
3074 | $cancelledMembershipIds[] = $dao->membership_type_id; | |
3075 | } | |
3076 | return $cancelledMembershipIds; | |
3077 | } | |
96025800 | 3078 | |
6a488035 | 3079 | } |