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