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