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