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