Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { | |
36 | ||
37 | /** | |
100fef9d | 38 | * Static holder for the default LT |
6a488035 TO |
39 | */ |
40 | static $_defaultMembershipType = NULL; | |
41 | ||
42 | static $_membershipTypeInfo = array(); | |
43 | ||
44 | /** | |
100fef9d | 45 | * Class constructor |
6a488035 TO |
46 | */ |
47 | function __construct() { | |
48 | parent::__construct(); | |
49 | } | |
50 | ||
51 | /** | |
c490a46a | 52 | * Fetch object based on array of properties |
6a488035 TO |
53 | * |
54 | * @param array $params (reference ) an assoc array of name/value pairs | |
55 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
56 | * | |
c490a46a | 57 | * @return CRM_Member_BAO_MembershipType object |
6a488035 TO |
58 | * @access public |
59 | * @static | |
60 | */ | |
61 | static function retrieve(&$params, &$defaults) { | |
62 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
63 | $membershipType->copyValues($params); | |
64 | if ($membershipType->find(TRUE)) { | |
65 | CRM_Core_DAO::storeValues($membershipType, $defaults); | |
66 | return $membershipType; | |
67 | } | |
68 | return NULL; | |
69 | } | |
70 | ||
71 | /** | |
100fef9d | 72 | * Update the is_active flag in the db |
6a488035 TO |
73 | * |
74 | * @param int $id id of the database record | |
75 | * @param boolean $is_active value we want to set the is_active field | |
76 | * | |
77 | * @return Object DAO object on sucess, null otherwise | |
78 | * @static | |
79 | */ | |
80 | static function setIsActive($id, $is_active) { | |
81 | return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipType', $id, 'is_active', $is_active); | |
82 | } | |
83 | ||
84 | /** | |
100fef9d | 85 | * Add the membership types |
6a488035 TO |
86 | * |
87 | * @param array $params reference array contains the values submitted by the form | |
36e3b794 | 88 | * @param array $ids array contains the id (deprecated) |
6a488035 TO |
89 | * |
90 | * @access public | |
91 | * @static | |
92 | * | |
93 | * @return object | |
94 | */ | |
36e3b794 EM |
95 | static function add(&$params, $ids = array()) { |
96 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('membershipType', $ids)); | |
97 | if (!$id) { | |
98 | if (!isset($params['is_active'])) { | |
99 | // do we need this? | |
100 | $params['is_active'] = FALSE; | |
101 | } | |
102 | if (!isset($params['domain_id'])) { | |
103 | $params['domain_id'] = CRM_Core_Config::domainID(); | |
104 | } | |
105 | } | |
6a488035 TO |
106 | |
107 | // action is taken depending upon the mode | |
108 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
6a488035 | 109 | $membershipType->copyValues($params); |
36e3b794 | 110 | $membershipType->id = $id; |
6a488035 | 111 | |
301db4f8 | 112 | // $previousID is the old organization id for membership type i.e 'member_of_contact_id'. This is used when an organization is changed. |
6a488035 | 113 | $previousID = NULL; |
806e9b71 EM |
114 | if ($id) { |
115 | $previousID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $id, 'member_of_contact_id'); | |
6a488035 TO |
116 | } |
117 | ||
118 | $membershipType->save(); | |
806e9b71 EM |
119 | if ($id) { |
120 | // on update we may need to retrieve some details for the price field function - otherwise we get e-notices on attempts to retrieve | |
121 | // name etc - the presence of previous id tells us this is an update | |
122 | $params = array_merge(civicrm_api3('membership_type', 'getsingle', array('id' => $membershipType->id)), $params); | |
123 | } | |
301db4f8 | 124 | self::createMembershipPriceField($params, $previousID, $membershipType->id); |
6a488035 | 125 | // update all price field value for quick config when membership type is set CRM-11718 |
36e3b794 EM |
126 | if ($id) { |
127 | self::updateAllPriceFieldValue($id, $params); | |
6a488035 TO |
128 | } |
129 | ||
130 | return $membershipType; | |
131 | } | |
132 | ||
133 | /** | |
100fef9d | 134 | * Delete membership Types |
6a488035 TO |
135 | * |
136 | * @param int $membershipTypeId | |
77b97be7 EM |
137 | * |
138 | * @throws CRM_Core_Exception | |
139 | * @return bool|mixed | |
6a488035 TO |
140 | * @static |
141 | */ | |
8c33a68c | 142 | static function del($membershipTypeId) { |
6a488035 TO |
143 | //check dependencies |
144 | $check = FALSE; | |
145 | $status = array(); | |
146 | $dependancy = array( | |
147 | 'Membership' => 'membership_type_id', | |
148 | 'MembershipBlock' => 'membership_type_default', | |
149 | ); | |
150 | ||
151 | foreach ($dependancy as $name => $field) { | |
4d5c2eb5 | 152 | $baoString = 'CRM_Member_BAO_' . $name; |
153 | $dao = new $baoString(); | |
6a488035 TO |
154 | $dao->$field = $membershipTypeId; |
155 | if ($dao->find(TRUE)) { | |
156 | $check = TRUE; | |
157 | $status[] = $name; | |
158 | } | |
159 | } | |
160 | if ($check) { | |
6a488035 TO |
161 | $cnt = 1; |
162 | $message = ts('This membership type cannot be deleted due to following reason(s):'); | |
163 | if (in_array('Membership', $status)) { | |
d6d6bc96 DG |
164 | $findMembersURL = CRM_Utils_System::url('civicrm/member/search', 'reset=1'); |
165 | $deleteURL = CRM_Utils_System::url('civicrm/contact/search/advanced', 'reset=1'); | |
166 | $message .= '<br/>' . ts('%3. There are some contacts who have this membership type assigned to them. Search for contacts with this membership type from <a href=\'%1\'>Find Members</a>. If you are still getting this message after deleting these memberships, there may be contacts in the Trash (deleted) with this membership type. Try using <a href="%2">Advanced Search</a> and checking "Search in Trash".', array(1 => $findMembersURL, 2 => $deleteURL, 3 => $cnt)); | |
6a488035 TO |
167 | $cnt++; |
168 | } | |
169 | ||
170 | if (in_array('MembershipBlock', $status)) { | |
171 | $deleteURL = CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'); | |
dcc4f6a7 | 172 | $message .= ts('%2. This Membership Type is used in an <a href=\'%1\'>Online Contribution page</a>. Uncheck this membership type in the Memberships tab.', array(1 => $deleteURL, 2 => $cnt)); |
173 | throw new CRM_Core_Exception($message); | |
6a488035 TO |
174 | } |
175 | } | |
dcc4f6a7 | 176 | CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipType', $membershipTypeId); |
6a488035 TO |
177 | //delete from membership Type table |
178 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
179 | $membershipType->id = $membershipTypeId; | |
180 | ||
181 | //fix for membership type delete api | |
182 | $result = FALSE; | |
183 | if ($membershipType->find(TRUE)) { | |
8c33a68c | 184 | return $membershipType->delete(); |
6a488035 TO |
185 | } |
186 | ||
187 | return $result; | |
188 | } | |
189 | ||
190 | /** | |
100fef9d | 191 | * Convert membership Type's 'start day' & 'rollover day' to human readable formats. |
6a488035 TO |
192 | * |
193 | * @param array $membershipType an array of membershipType-details. | |
194 | * @static | |
195 | */ | |
196 | static function convertDayFormat(&$membershipType) { | |
197 | $periodDays = array( | |
198 | 'fixed_period_start_day', | |
199 | 'fixed_period_rollover_day', | |
200 | ); | |
201 | foreach ($membershipType as $id => $details) { | |
202 | foreach ($periodDays as $pDay) { | |
a7488080 | 203 | if (!empty($details[$pDay])) { |
6a488035 TO |
204 | if ($details[$pDay] > 31) { |
205 | $month = substr($details[$pDay], 0, strlen($details[$pDay]) - 2); | |
206 | $day = substr($details[$pDay], -2); | |
207 | $monthMap = array( | |
208 | '1' => 'Jan', | |
209 | '2' => 'Feb', | |
210 | '3' => 'Mar', | |
211 | '4' => 'Apr', | |
212 | '5' => 'May', | |
213 | '6' => 'Jun', | |
214 | '7' => 'Jul', | |
215 | '8' => 'Aug', | |
216 | '9' => 'Sep', | |
217 | '10' => 'Oct', | |
218 | '11' => 'Nov', | |
219 | '12' => 'Dec', | |
220 | ); | |
221 | $membershipType[$id][$pDay] = $monthMap[$month] . ' ' . $day; | |
222 | } | |
223 | else { | |
224 | $membershipType[$id][$pDay] = $details[$pDay]; | |
225 | } | |
226 | } | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | /** | |
100fef9d | 232 | * Get membership Types |
6a488035 | 233 | * |
77b97be7 EM |
234 | * @param bool $public |
235 | * | |
236 | * @return array | |
6a488035 TO |
237 | * @static |
238 | */ | |
239 | static function getMembershipTypes($public = TRUE) { | |
240 | $membershipTypes = array(); | |
241 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
242 | $membershipType->is_active = 1; | |
243 | if ($public) { | |
244 | $membershipType->visibility = 'Public'; | |
245 | } | |
246 | $membershipType->orderBy(' weight'); | |
247 | $membershipType->find(); | |
248 | while ($membershipType->fetch()) { | |
249 | $membershipTypes[$membershipType->id] = $membershipType->name; | |
250 | } | |
251 | $membershipType->free(); | |
252 | return $membershipTypes; | |
253 | } | |
254 | ||
255 | /** | |
100fef9d | 256 | * Get membership Type Details |
6a488035 TO |
257 | * |
258 | * @param int $membershipTypeId | |
77b97be7 EM |
259 | * |
260 | * @return array|null | |
6a488035 TO |
261 | * @static |
262 | */ | |
263 | static function getMembershipTypeDetails($membershipTypeId) { | |
264 | $membershipTypeDetails = array(); | |
265 | ||
266 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
267 | $membershipType->is_active = 1; | |
268 | $membershipType->id = $membershipTypeId; | |
269 | if ($membershipType->find(TRUE)) { | |
270 | CRM_Core_DAO::storeValues($membershipType, $membershipTypeDetails); | |
271 | $membershipType->free(); | |
272 | return $membershipTypeDetails; | |
273 | } | |
274 | else { | |
275 | return NULL; | |
276 | } | |
277 | } | |
278 | ||
279 | /** | |
100fef9d | 280 | * Calculate start date and end date for new membership |
6a488035 | 281 | * |
da6b46f4 | 282 | * @param int $membershipTypeId membership type id |
6a488035 TO |
283 | * @param date $joinDate member since ( in mysql date format ) |
284 | * @param date $startDate start date ( in mysql date format ) | |
da6b46f4 EM |
285 | * @param null $endDate |
286 | * @param int $numRenewTerms how many membership terms are being added to end date (default is 1) | |
6a488035 TO |
287 | * |
288 | * @return array associated array with start date, end date and join date for the membership | |
289 | * @static | |
290 | */ | |
291 | public static function getDatesForMembershipType($membershipTypeId, $joinDate = NULL, $startDate = NULL, $endDate = NULL, $numRenewTerms = 1) { | |
292 | $membershipTypeDetails = self::getMembershipTypeDetails($membershipTypeId); | |
293 | ||
294 | // convert all dates to 'Y-m-d' format. | |
295 | foreach (array( | |
296 | 'joinDate', 'startDate', 'endDate') as $dateParam) { | |
297 | if (!empty($$dateParam)) { | |
298 | $$dateParam = CRM_Utils_Date::processDate($$dateParam, NULL, FALSE, 'Y-m-d'); | |
299 | } | |
300 | } | |
301 | if (!$joinDate) { | |
302 | $joinDate = date('Y-m-d'); | |
303 | } | |
304 | $actualStartDate = $joinDate; | |
305 | if ($startDate) { | |
306 | $actualStartDate = $startDate; | |
307 | } | |
308 | ||
309 | $fixed_period_rollover = FALSE; | |
310 | if (CRM_Utils_Array::value('period_type', $membershipTypeDetails) == 'rolling') { | |
311 | if (!$startDate) { | |
312 | $startDate = $joinDate; | |
313 | } | |
314 | $actualStartDate = $startDate; | |
315 | } | |
316 | elseif (CRM_Utils_Array::value('period_type', $membershipTypeDetails) == 'fixed') { | |
5ae96933 DG |
317 | // calculate start date |
318 | // if !$startDate then use $joinDate | |
319 | $toDay = explode('-', (empty($startDate) ? $joinDate : $startDate)); | |
6a488035 TO |
320 | $year = $toDay[0]; |
321 | $month = $toDay[1]; | |
f7660a39 | 322 | $day = $toDay[2]; |
6a488035 TO |
323 | |
324 | if ($membershipTypeDetails['duration_unit'] == 'year') { | |
325 | ||
326 | //get start fixed day | |
327 | $startMonth = substr($membershipTypeDetails['fixed_period_start_day'], 0, | |
328 | strlen($membershipTypeDetails['fixed_period_start_day']) - 2 | |
329 | ); | |
330 | $startDay = substr($membershipTypeDetails['fixed_period_start_day'], -2); | |
331 | ||
f7660a39 DG |
332 | if (date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year)) <= date('Y-m-d', mktime(0, 0, 0, $month, $day, $year))) { |
333 | $fixedStartDate = date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year)); | |
334 | } | |
335 | else { | |
336 | $fixedStartDate = date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year - 1)); | |
337 | } | |
6a488035 TO |
338 | |
339 | //get start rollover day | |
340 | $rolloverMonth = substr($membershipTypeDetails['fixed_period_rollover_day'], 0, | |
341 | strlen($membershipTypeDetails['fixed_period_rollover_day']) - 2 | |
342 | ); | |
343 | $rolloverDay = substr($membershipTypeDetails['fixed_period_rollover_day'], -2); | |
344 | ||
345 | $fixedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year)); | |
346 | ||
347 | //CRM-7825 -membership date rules are : | |
348 | //1. Membership should not be start in future. | |
349 | //2. rollover window should be subset of membership window. | |
350 | ||
351 | //store original fixed start date as per current year. | |
352 | $actualStartDate = $fixedStartDate; | |
353 | ||
354 | //store original fixed rollover date as per current year. | |
355 | $actualRolloverDate = $fixedRolloverDate; | |
356 | ||
6a488035 TO |
357 | //get the fixed end date here. |
358 | $dateParts = explode('-', $actualStartDate); | |
359 | $fixedEndDate = date('Y-m-d', mktime(0, 0, 0, | |
360 | $dateParts[1], | |
361 | $dateParts[2] - 1, | |
362 | $dateParts[0] + ($numRenewTerms * $membershipTypeDetails['duration_interval']) | |
363 | )); | |
364 | ||
365 | //make sure rollover window should be | |
366 | //subset of membership period window. | |
367 | if ($fixedEndDate < $actualRolloverDate) { | |
368 | $actualRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year - 1)); | |
369 | } | |
370 | if ($actualRolloverDate < $actualStartDate) { | |
371 | $actualRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year + 1)); | |
372 | } | |
373 | ||
374 | //do check signup is in rollover window. | |
375 | if ($actualRolloverDate <= $joinDate) { | |
376 | $fixed_period_rollover = TRUE; | |
377 | } | |
378 | ||
379 | if (!$startDate) { | |
380 | $startDate = $actualStartDate; | |
381 | } | |
382 | } | |
383 | elseif ($membershipTypeDetails['duration_unit'] == 'month') { | |
384 | // Check if we are on or after rollover day of the month - CRM-10585 | |
385 | // If so, set fixed_period_rollover TRUE so we increment end_date month below. | |
386 | $dateParts = explode('-', $actualStartDate); | |
387 | if ($dateParts[2] >= $membershipTypeDetails['fixed_period_rollover_day']){ | |
388 | $fixed_period_rollover = True; | |
389 | } | |
03e04002 | 390 | |
6a488035 TO |
391 | // Start date is always first day of actualStartDate month |
392 | if (!$startDate) { | |
393 | $actualStartDate = $startDate = $year . '-' . $month . '-01'; | |
394 | } | |
395 | } | |
396 | } | |
397 | ||
398 | //calculate end date if it is not passed by user | |
399 | if (!$endDate) { | |
400 | //end date calculation | |
401 | $date = explode('-', $actualStartDate); | |
402 | $year = $date[0]; | |
403 | $month = $date[1]; | |
404 | $day = $date[2]; | |
405 | ||
406 | switch ($membershipTypeDetails['duration_unit']) { | |
407 | case 'year': | |
408 | $year = $year + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
409 | //extend membership date by duration interval. | |
410 | if ($fixed_period_rollover) { | |
411 | $year += 1; | |
412 | } | |
413 | break; | |
414 | ||
415 | case 'month': | |
416 | $month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
417 | //duration interval is month | |
418 | if ($fixed_period_rollover) { | |
419 | //CRM-10585 | |
420 | $month += 1; | |
421 | } | |
422 | break; | |
423 | ||
424 | case 'day': | |
425 | $day = $day + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
426 | ||
427 | if ($fixed_period_rollover) { | |
428 | //Fix Me: Currently we don't allow rollover if | |
429 | //duration interval is day | |
430 | } | |
431 | break; | |
432 | } | |
433 | ||
434 | if ($membershipTypeDetails['duration_unit'] == 'lifetime') { | |
435 | $endDate = NULL; | |
436 | } | |
437 | else { | |
438 | $endDate = date('Y-m-d', mktime(0, 0, 0, $month, $day - 1, $year)); | |
439 | } | |
440 | } | |
441 | ||
442 | $membershipDates = array(); | |
443 | ||
444 | $dates = array( | |
445 | 'start_date' => 'startDate', | |
446 | 'end_date' => 'endDate', | |
447 | 'join_date' => 'joinDate', | |
448 | ); | |
449 | foreach ($dates as $varName => $valName) { | |
450 | $membershipDates[$varName] = CRM_Utils_Date::customFormat($$valName, '%Y%m%d'); | |
451 | } | |
452 | ||
453 | return $membershipDates; | |
454 | } | |
455 | ||
456 | /** | |
100fef9d | 457 | * Calculate start date and end date for renewal membership |
6a488035 TO |
458 | * |
459 | * @param int $membershipId | |
460 | * @param $changeToday | |
461 | * @param int $membershipTypeID - if provided, overrides the membership type of the $membershipID membership | |
462 | * @param int $numRenewTerms how many membership terms are being added to end date (default is 1) | |
463 | * | |
464 | * CRM-7297 Membership Upsell - Added $membershipTypeID param to facilitate calculations of dates when membership type changes | |
465 | * | |
466 | * @return Array array fo the start date, end date and join date of the membership | |
467 | * @static | |
468 | */ | |
469 | public static function getRenewalDatesForMembershipType($membershipId, $changeToday = NULL, $membershipTypeID = NULL, $numRenewTerms = 1) { | |
470 | $params = array('id' => $membershipId); | |
471 | $membershipDetails = CRM_Member_BAO_Membership::getValues($params, $values); | |
472 | $statusID = $membershipDetails[$membershipId]->status_id; | |
01f6b47e CW |
473 | $membershipDates = array( |
474 | 'join_date' => CRM_Utils_Date::customFormat($membershipDetails[$membershipId]->join_date, '%Y%m%d'), | |
475 | ); | |
6a488035 TO |
476 | |
477 | $oldPeriodType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
478 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id'), 'period_type'); | |
479 | ||
480 | // CRM-7297 Membership Upsell | |
481 | if (is_null($membershipTypeID)) { | |
482 | $membershipTypeDetails = self::getMembershipTypeDetails($membershipDetails[$membershipId]->membership_type_id); | |
483 | } | |
484 | else { | |
485 | $membershipTypeDetails = self::getMembershipTypeDetails($membershipTypeID); | |
486 | } | |
487 | $statusDetails = CRM_Member_BAO_MembershipStatus::getMembershipStatus($statusID); | |
488 | ||
489 | if ($statusDetails['is_current_member'] == 1) { | |
490 | $startDate = $membershipDetails[$membershipId]->start_date; | |
491 | // CRM=7297 Membership Upsell: we need to handle null end_date in case we are switching | |
492 | // from a lifetime to a different membership type | |
493 | if (is_null($membershipDetails[$membershipId]->end_date)) { | |
494 | $date = date('Y-m-d'); | |
495 | } | |
496 | else { | |
497 | $date = $membershipDetails[$membershipId]->end_date; | |
498 | } | |
499 | $date = explode('-', $date); | |
500 | $logStartDate = date('Y-m-d', mktime(0, 0, 0, | |
501 | (double) $date[1], | |
502 | (double)($date[2] + 1), | |
503 | (double) $date[0] | |
504 | )); | |
505 | ||
506 | $date = explode('-', $logStartDate); | |
507 | $year = $date[0]; | |
508 | $month = $date[1]; | |
509 | $day = $date[2]; | |
510 | ||
511 | switch ($membershipTypeDetails['duration_unit']) { | |
512 | case 'year': | |
513 | //need to check if the upsell is from rolling to fixed and adjust accordingly | |
514 | if ($membershipTypeDetails['period_type'] == 'fixed' && $oldPeriodType == 'rolling' ) { | |
515 | $month = substr($membershipTypeDetails['fixed_period_start_day'], 0, strlen($membershipTypeDetails['fixed_period_start_day']) - 2); | |
516 | $day = substr($membershipTypeDetails['fixed_period_start_day'], -2); | |
517 | $year += 1; | |
518 | } else { | |
519 | $year = $year + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
520 | } | |
521 | break; | |
522 | ||
523 | case 'month': | |
524 | $month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
525 | break; | |
526 | ||
527 | case 'day': | |
528 | $day = $day + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
529 | break; | |
530 | } | |
531 | if ($membershipTypeDetails['duration_unit'] == 'lifetime') { | |
532 | $endDate = NULL; | |
533 | } | |
534 | else { | |
535 | $endDate = date('Y-m-d', mktime(0, 0, 0, | |
536 | $month, | |
537 | $day - 1, | |
538 | $year | |
539 | )); | |
540 | } | |
541 | $today = date('Y-m-d'); | |
6a488035 TO |
542 | $membershipDates['today'] = CRM_Utils_Date::customFormat($today, '%Y%m%d'); |
543 | $membershipDates['start_date'] = CRM_Utils_Date::customFormat($startDate, '%Y%m%d'); | |
544 | $membershipDates['end_date'] = CRM_Utils_Date::customFormat($endDate, '%Y%m%d'); | |
545 | $membershipDates['log_start_date'] = CRM_Utils_Date::customFormat($logStartDate, '%Y%m%d'); | |
546 | } | |
547 | else { | |
548 | $today = date('Y-m-d'); | |
549 | if ($changeToday) { | |
550 | $today = CRM_Utils_Date::processDate($changeToday, NULL, FALSE, 'Y-m-d'); | |
551 | } | |
552 | // Calculate new start/end dates when join date is today | |
553 | $renewalDates = self::getDatesForMembershipType($membershipTypeDetails['id'], | |
554 | $today, NULL, NULL, $numRenewTerms | |
555 | ); | |
6a488035 TO |
556 | $membershipDates['today'] = CRM_Utils_Date::customFormat($today, '%Y%m%d'); |
557 | $membershipDates['start_date'] = $renewalDates['start_date']; | |
558 | $membershipDates['end_date'] = $renewalDates['end_date']; | |
559 | $membershipDates['log_start_date'] = $renewalDates['start_date']; | |
560 | } | |
561 | ||
562 | return $membershipDates; | |
563 | } | |
564 | ||
565 | /** | |
100fef9d | 566 | * Retrieve all Membership Types associated |
6a488035 TO |
567 | * with an Organization |
568 | * | |
569 | * @param int $orgID Id of Organization | |
570 | * | |
571 | * @return Array array of the details of membership types | |
572 | * @static | |
573 | */ | |
574 | static function getMembershipTypesByOrg($orgID) { | |
575 | $membershipTypes = array(); | |
576 | $dao = new CRM_Member_DAO_MembershipType(); | |
577 | $dao->member_of_contact_id = $orgID; | |
578 | $dao->find(); | |
579 | while ($dao->fetch()) { | |
580 | $membershipTypes[$dao->id] = array(); | |
581 | CRM_Core_DAO::storeValues($dao, $membershipTypes[$dao->id]); | |
582 | } | |
583 | return $membershipTypes; | |
584 | } | |
585 | ||
586 | /** | |
100fef9d | 587 | * Retrieve all Membership Types with Member of Contact id |
6a488035 TO |
588 | * |
589 | * @param array membership types | |
590 | * | |
591 | * @return Array array of the details of membership types with Member of Contact id | |
592 | * @static | |
593 | */ | |
594 | static function getMemberOfContactByMemTypes($membershipTypes) { | |
595 | $memTypeOrgs = array(); | |
596 | if (empty($membershipTypes)) { | |
597 | return $memTypeOrgs; | |
598 | } | |
599 | ||
600 | $result = CRM_Core_DAO::executeQuery("SELECT id, member_of_contact_id FROM civicrm_membership_type WHERE id IN (" . implode(',', $membershipTypes) . ")"); | |
601 | while ($result->fetch()) { | |
602 | $memTypeOrgs[$result->id] = $result->member_of_contact_id; | |
603 | } | |
604 | ||
605 | return $memTypeOrgs; | |
606 | } | |
607 | ||
77b97be7 EM |
608 | /** |
609 | * The function returns all the Organization for all membershiptypes . | |
6a488035 | 610 | * |
c490a46a | 611 | * @param int $membershipTypeId |
77b97be7 EM |
612 | * |
613 | * @return array | |
6a488035 TO |
614 | */ |
615 | static function getMembershipTypeOrganization($membershipTypeId = NULL) { | |
616 | $allmembershipTypes = array(); | |
617 | ||
618 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
619 | ||
620 | if (isset($membershipTypeId)) { | |
621 | $membershipType->id = $membershipTypeId; | |
622 | } | |
623 | $membershipType->find(); | |
624 | ||
625 | while ($membershipType->fetch()) { | |
626 | $allmembershipTypes[$membershipType->id] = $membershipType->member_of_contact_id; | |
627 | } | |
628 | return $allmembershipTypes; | |
629 | } | |
630 | ||
631 | /** | |
632 | * Funtion to retrieve organization and associated membership | |
633 | * types | |
634 | * | |
635 | * @return array arrays of organization and membership types | |
636 | * | |
637 | * @static | |
638 | * @access public | |
639 | */ | |
640 | static function getMembershipTypeInfo() { | |
641 | if (!self::$_membershipTypeInfo) { | |
642 | $orgs = $types = array(); | |
643 | ||
644 | $query = 'SELECT memType.id, memType.name, memType.member_of_contact_id, c.sort_name | |
645 | FROM civicrm_membership_type memType INNER JOIN civicrm_contact c ON c.id = memType.member_of_contact_id | |
646 | WHERE memType.is_active = 1 '; | |
647 | $dao = CRM_Core_DAO::executeQuery( $query ); | |
648 | while ($dao->fetch()) { | |
649 | $orgs[$dao->member_of_contact_id] = $dao->sort_name; | |
650 | $types[$dao->member_of_contact_id][$dao->id] = $dao->name; | |
651 | } | |
652 | ||
653 | self::$_membershipTypeInfo = array($orgs, $types); | |
654 | } | |
655 | return self::$_membershipTypeInfo; | |
656 | } | |
657 | ||
658 | ||
bb3a214a | 659 | /** |
c490a46a | 660 | * @param array $params |
100fef9d CW |
661 | * @param int $previousID |
662 | * @param int $membershipTypeId | |
bb3a214a | 663 | */ |
301db4f8 | 664 | public static function createMembershipPriceField($params, $previousID, $membershipTypeId) { |
6a488035 | 665 | |
9da8dc8c | 666 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name'); |
6a488035 | 667 | |
a7488080 | 668 | if (!empty($params['member_of_contact_id'])) { |
6a488035 TO |
669 | $fieldName = $params['member_of_contact_id']; |
670 | } | |
671 | else { | |
672 | $fieldName = $previousID; | |
673 | } | |
674 | $fieldLabel = 'Membership Amount'; | |
675 | $optionsIds = NULL; | |
676 | $fieldParams = array( | |
677 | 'price_set_id ' => $priceSetId, | |
678 | 'name' => $fieldName, | |
679 | ); | |
680 | $results = array(); | |
9da8dc8c | 681 | CRM_Price_BAO_PriceField::retrieve($fieldParams, $results); |
6a488035 TO |
682 | if (empty($results)) { |
683 | $fieldParams = array(); | |
684 | $fieldParams['label'] = $fieldLabel; | |
685 | $fieldParams['name'] = $fieldName; | |
686 | $fieldParams['price_set_id'] = $priceSetId; | |
687 | $fieldParams['html_type'] = 'Radio'; | |
688 | $fieldParams['is_display_amounts'] = $fieldParams['is_required'] = 0; | |
689 | $fieldParams['weight'] = $fieldParams['option_weight'][1] = 1; | |
690 | $fieldParams['option_label'][1] = $params['name']; | |
0e09eaf5 | 691 | $fieldParams['option_description'][1] = CRM_Utils_Array::value('description', $params); |
03e04002 | 692 | |
6a488035 TO |
693 | $fieldParams['membership_type_id'][1] = $membershipTypeId; |
694 | $fieldParams['option_amount'][1] = empty($params['minimum_fee']) ? 0 : $params['minimum_fee']; | |
695 | $fieldParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params); | |
696 | ||
697 | if ($previousID) { | |
698 | CRM_Member_Form_MembershipType::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds); | |
699 | $fieldParams['option_id'] = CRM_Utils_Array::value('option_id', $optionsIds); | |
700 | } | |
806e9b71 | 701 | CRM_Price_BAO_PriceField::create($fieldParams); |
03e04002 | 702 | } |
6a488035 TO |
703 | else { |
704 | $fieldID = $results['id']; | |
705 | $fieldValueParams = array( | |
706 | 'price_field_id' => $fieldID, | |
707 | 'membership_type_id' => $membershipTypeId, | |
708 | ); | |
709 | $results = array(); | |
9da8dc8c | 710 | CRM_Price_BAO_PriceFieldValue::retrieve($fieldValueParams, $results); |
6a488035 TO |
711 | if (!empty($results)) { |
712 | $results['label'] = $results['name'] = $params['name']; | |
713 | $results['amount'] = empty($params['minimum_fee']) ? 0 : $params['minimum_fee']; | |
714 | $optionsIds['id'] = $results['id']; | |
03e04002 | 715 | } |
6a488035 TO |
716 | else { |
717 | $results = array( | |
718 | 'price_field_id' => $fieldID, | |
719 | 'name' => $params['name'], | |
720 | 'label' => $params['name'], | |
721 | 'amount' => empty($params['minimum_fee']) ? 0 : $params['minimum_fee'], | |
722 | 'membership_type_id' => $membershipTypeId, | |
723 | 'is_active' => 1, | |
724 | ); | |
725 | } | |
726 | ||
727 | if ($previousID) { | |
728 | CRM_Member_Form_MembershipType::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds); | |
a7488080 | 729 | if (!empty($optionsIds['option_id'])) { |
6a488035 TO |
730 | $optionsIds['id'] = current(CRM_Utils_Array::value('option_id', $optionsIds)); |
731 | } | |
732 | } | |
733 | $results['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params); | |
0e09eaf5 | 734 | $results['description'] = CRM_Utils_Array::value('description', $params); |
9da8dc8c | 735 | CRM_Price_BAO_PriceFieldValue::add($results, $optionsIds); |
6a488035 TO |
736 | } |
737 | } | |
738 | ||
739 | /** This function updates all price field value for quick config | |
740 | * price set which has membership type | |
741 | * | |
03e04002 | 742 | * @param integer membership type id |
6a488035 | 743 | * |
03e04002 | 744 | * @param integer financial type id |
6a488035 | 745 | */ |
5a9c4d4a | 746 | static function updateAllPriceFieldValue($membershipTypeId, $params) { |
a7488080 | 747 | if (!empty($params['minimum_fee'])){ |
5a9c4d4a PN |
748 | $amount = $params['minimum_fee']; |
749 | } | |
750 | else { | |
751 | $amount = 0; | |
752 | } | |
0e09eaf5 PN |
753 | |
754 | $updateValues = array( | |
755 | 2 => array('financial_type_id', 'financial_type_id', 'Integer'), | |
756 | 3 => array('label', 'name', 'String'), | |
757 | 4 => array('amount', 'minimum_fee', 'Float'), | |
758 | 5 => array('description', 'description', 'String'), | |
759 | ); | |
760 | ||
761 | $queryParams = array(1 => array($membershipTypeId, 'Integer')); | |
03e04002 | 762 | foreach ($updateValues as $key => $value) { |
0e09eaf5 PN |
763 | if (array_key_exists($value[1], $params)) { |
764 | $updateFields[] = "cpfv." . $value[0] . " = %$key"; | |
765 | if ($value[1] == 'minimum_fee') { | |
766 | $fieldValue = $amount; | |
767 | } | |
768 | else { | |
769 | $fieldValue = $params[$value[1]]; | |
770 | } | |
771 | $queryParams[$key] = array($fieldValue, $value[2]); | |
772 | } | |
773 | } | |
774 | ||
6a488035 | 775 | $query = "UPDATE `civicrm_price_field_value` cpfv |
03e04002 | 776 | INNER JOIN civicrm_price_field cpf on cpf.id = cpfv.price_field_id |
6a488035 | 777 | INNER JOIN civicrm_price_set cps on cps.id = cpf.price_set_id |
03e04002 | 778 | SET " . implode(' , ', $updateFields) . " WHERE cpfv.membership_type_id = %1 |
0e09eaf5 PN |
779 | AND cps.is_quick_config = 1 AND cps.name != 'default_membership_type_amount'"; |
780 | CRM_Core_DAO::executeQuery($query, $queryParams); | |
03e04002 | 781 | } |
6a488035 TO |
782 | } |
783 |