Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id$ |
17 | * | |
18 | */ | |
19 | class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { | |
20 | ||
21 | /** | |
4c429c82 | 22 | * Static holder for the default Membership Type. |
971e129b | 23 | * @var int |
6a488035 | 24 | */ |
971e129b | 25 | public static $_defaultMembershipType = NULL; |
6a488035 | 26 | |
971e129b | 27 | public static $_membershipTypeInfo = []; |
6a488035 TO |
28 | |
29 | /** | |
4c429c82 | 30 | * Class constructor. |
6a488035 | 31 | */ |
00be9182 | 32 | public function __construct() { |
6a488035 TO |
33 | parent::__construct(); |
34 | } | |
35 | ||
36 | /** | |
4c429c82 | 37 | * Fetch object based on array of properties. |
6a488035 | 38 | * |
b2363ea8 TO |
39 | * @param array $params |
40 | * (reference ) an assoc array of name/value pairs. | |
41 | * @param array $defaults | |
42 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 43 | * |
16b10e64 | 44 | * @return CRM_Member_BAO_MembershipType |
6a488035 | 45 | */ |
00be9182 | 46 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
47 | $membershipType = new CRM_Member_DAO_MembershipType(); |
48 | $membershipType->copyValues($params); | |
49 | if ($membershipType->find(TRUE)) { | |
50 | CRM_Core_DAO::storeValues($membershipType, $defaults); | |
51 | return $membershipType; | |
52 | } | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | /** | |
4c429c82 | 57 | * Update the is_active flag in the db. |
6a488035 | 58 | * |
b2363ea8 TO |
59 | * @param int $id |
60 | * Id of the database record. | |
61 | * @param bool $is_active | |
62 | * Value we want to set the is_active field. | |
6a488035 | 63 | * |
8a4fede3 | 64 | * @return bool |
65 | * true if we found and updated the object, else false | |
6a488035 | 66 | */ |
00be9182 | 67 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
68 | return CRM_Core_DAO::setFieldValue('CRM_Member_DAO_MembershipType', $id, 'is_active', $is_active); |
69 | } | |
70 | ||
71 | /** | |
4c429c82 | 72 | * Add the membership types. |
6a488035 | 73 | * |
b2363ea8 TO |
74 | * @param array $params |
75 | * Reference array contains the values submitted by the form. | |
76 | * @param array $ids | |
77 | * Array contains the id (deprecated). | |
6a488035 | 78 | * |
000e4e8d MW |
79 | * @return \CRM_Member_DAO_MembershipType |
80 | * @throws \CiviCRM_API3_Exception | |
6a488035 | 81 | */ |
be2fb01f | 82 | public static function add(&$params, $ids = []) { |
000e4e8d MW |
83 | // DEPRECATED Check if membershipType ID was passed in via $ids |
84 | if (empty($params['id'])) { | |
85 | if (isset($ids['membershipType'])) { | |
86 | Civi::log()->warning('Deprecated: Passing membershipType by $ids array in CRM_Member_BAO_MembershipType::add'); | |
87 | } | |
88 | $params['id'] = CRM_Utils_Array::value('membershipType', $ids); | |
89 | } | |
90 | ||
91 | $hook = empty($params['id']) ? 'create' : 'edit'; | |
92 | CRM_Utils_Hook::pre($hook, 'MembershipType', CRM_Utils_Array::value('id', $params), $params); | |
93 | ||
94 | $membershipTypeId = CRM_Utils_Array::value('id', $params); | |
95 | ||
96 | if (!$membershipTypeId) { | |
36e3b794 EM |
97 | if (!isset($params['is_active'])) { |
98 | // do we need this? | |
99 | $params['is_active'] = FALSE; | |
100 | } | |
101 | if (!isset($params['domain_id'])) { | |
102 | $params['domain_id'] = CRM_Core_Config::domainID(); | |
103 | } | |
104 | } | |
6a488035 | 105 | |
301db4f8 | 106 | // $previousID is the old organization id for membership type i.e 'member_of_contact_id'. This is used when an organization is changed. |
6a488035 | 107 | $previousID = NULL; |
000e4e8d MW |
108 | if ($membershipTypeId) { |
109 | $previousID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipTypeId, 'member_of_contact_id'); | |
6a488035 TO |
110 | } |
111 | ||
000e4e8d MW |
112 | // action is taken depending upon the mode |
113 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
114 | $membershipType->copyValues($params); | |
6a488035 | 115 | $membershipType->save(); |
000e4e8d MW |
116 | |
117 | if ($membershipTypeId) { | |
806e9b71 EM |
118 | // on update we may need to retrieve some details for the price field function - otherwise we get e-notices on attempts to retrieve |
119 | // name etc - the presence of previous id tells us this is an update | |
be2fb01f | 120 | $params = array_merge(civicrm_api3('membership_type', 'getsingle', ['id' => $membershipType->id]), $params); |
806e9b71 | 121 | } |
301db4f8 | 122 | self::createMembershipPriceField($params, $previousID, $membershipType->id); |
6a488035 | 123 | // update all price field value for quick config when membership type is set CRM-11718 |
000e4e8d MW |
124 | if ($membershipTypeId) { |
125 | self::updateAllPriceFieldValue($membershipTypeId, $params); | |
6a488035 | 126 | } |
000e4e8d MW |
127 | |
128 | CRM_Utils_Hook::post($hook, 'MembershipType', $membershipType->id, $membershipType); | |
129 | ||
452b9e04 | 130 | self::flush(); |
6a488035 TO |
131 | return $membershipType; |
132 | } | |
133 | ||
452b9e04 | 134 | /** |
4c429c82 EM |
135 | * Flush anywhere that membership types might be cached. |
136 | * | |
452b9e04 EM |
137 | * @throws \CiviCRM_API3_Exception |
138 | */ | |
c8881afb | 139 | public static function flush() { |
452b9e04 | 140 | CRM_Member_PseudoConstant::membershipType(NULL, TRUE); |
be2fb01f CW |
141 | civicrm_api3('membership', 'getfields', ['cache_clear' => 1, 'fieldname' => 'membership_type_id']); |
142 | civicrm_api3('profile', 'getfields', ['action' => 'submit', 'cache_clear' => 1]); | |
eb151aab | 143 | Civi::cache('metadata')->clear(); |
452b9e04 EM |
144 | } |
145 | ||
6a488035 | 146 | /** |
4c429c82 | 147 | * Delete membership Types. |
6a488035 TO |
148 | * |
149 | * @param int $membershipTypeId | |
77b97be7 EM |
150 | * |
151 | * @throws CRM_Core_Exception | |
152 | * @return bool|mixed | |
6a488035 | 153 | */ |
00be9182 | 154 | public static function del($membershipTypeId) { |
4c429c82 | 155 | // Check dependencies. |
353ffa53 | 156 | $check = FALSE; |
be2fb01f CW |
157 | $status = []; |
158 | $dependency = [ | |
6a488035 TO |
159 | 'Membership' => 'membership_type_id', |
160 | 'MembershipBlock' => 'membership_type_default', | |
be2fb01f | 161 | ]; |
6a488035 | 162 | |
edc9b94e | 163 | foreach ($dependency as $name => $field) { |
4d5c2eb5 | 164 | $baoString = 'CRM_Member_BAO_' . $name; |
165 | $dao = new $baoString(); | |
6a488035 | 166 | $dao->$field = $membershipTypeId; |
b204fd50 | 167 | /** @noinspection PhpUndefinedMethodInspection */ |
6a488035 TO |
168 | if ($dao->find(TRUE)) { |
169 | $check = TRUE; | |
170 | $status[] = $name; | |
171 | } | |
172 | } | |
173 | if ($check) { | |
6a488035 TO |
174 | $cnt = 1; |
175 | $message = ts('This membership type cannot be deleted due to following reason(s):'); | |
176 | if (in_array('Membership', $status)) { | |
d6d6bc96 DG |
177 | $findMembersURL = CRM_Utils_System::url('civicrm/member/search', 'reset=1'); |
178 | $deleteURL = CRM_Utils_System::url('civicrm/contact/search/advanced', 'reset=1'); | |
be2fb01f | 179 | $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".', [ |
c5c263ca AH |
180 | 1 => $findMembersURL, |
181 | 2 => $deleteURL, | |
182 | 3 => $cnt, | |
be2fb01f | 183 | ]); |
6a488035 TO |
184 | $cnt++; |
185 | } | |
186 | ||
187 | if (in_array('MembershipBlock', $status)) { | |
188 | $deleteURL = CRM_Utils_System::url('civicrm/admin/contribute', 'reset=1'); | |
be2fb01f | 189 | $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.', [ |
c5c263ca AH |
190 | 1 => $deleteURL, |
191 | 2 => $cnt, | |
be2fb01f | 192 | ]); |
dcc4f6a7 | 193 | throw new CRM_Core_Exception($message); |
6a488035 TO |
194 | } |
195 | } | |
dcc4f6a7 | 196 | CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipType', $membershipTypeId); |
6a488035 TO |
197 | //delete from membership Type table |
198 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
199 | $membershipType->id = $membershipTypeId; | |
200 | ||
201 | //fix for membership type delete api | |
202 | $result = FALSE; | |
203 | if ($membershipType->find(TRUE)) { | |
8c33a68c | 204 | return $membershipType->delete(); |
6a488035 TO |
205 | } |
206 | ||
207 | return $result; | |
208 | } | |
209 | ||
210 | /** | |
4c429c82 | 211 | * Convert membership type's 'start day' & 'rollover day' to human readable formats. |
6a488035 | 212 | * |
b2363ea8 TO |
213 | * @param array $membershipType |
214 | * An array of membershipType-details. | |
6a488035 | 215 | */ |
00be9182 | 216 | public static function convertDayFormat(&$membershipType) { |
be2fb01f | 217 | $periodDays = [ |
6a488035 TO |
218 | 'fixed_period_start_day', |
219 | 'fixed_period_rollover_day', | |
be2fb01f | 220 | ]; |
6a488035 TO |
221 | foreach ($membershipType as $id => $details) { |
222 | foreach ($periodDays as $pDay) { | |
a7488080 | 223 | if (!empty($details[$pDay])) { |
6a488035 | 224 | if ($details[$pDay] > 31) { |
353ffa53 TO |
225 | $month = substr($details[$pDay], 0, strlen($details[$pDay]) - 2); |
226 | $day = substr($details[$pDay], -2); | |
be2fb01f | 227 | $monthMap = [ |
6a488035 TO |
228 | '1' => 'Jan', |
229 | '2' => 'Feb', | |
230 | '3' => 'Mar', | |
231 | '4' => 'Apr', | |
232 | '5' => 'May', | |
233 | '6' => 'Jun', | |
234 | '7' => 'Jul', | |
235 | '8' => 'Aug', | |
236 | '9' => 'Sep', | |
237 | '10' => 'Oct', | |
238 | '11' => 'Nov', | |
239 | '12' => 'Dec', | |
be2fb01f | 240 | ]; |
6a488035 TO |
241 | $membershipType[$id][$pDay] = $monthMap[$month] . ' ' . $day; |
242 | } | |
243 | else { | |
244 | $membershipType[$id][$pDay] = $details[$pDay]; | |
245 | } | |
246 | } | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | /** | |
4c429c82 | 252 | * Get membership Types. |
6a488035 | 253 | * |
eb151aab | 254 | * @deprecated use getAllMembershipTypes. |
255 | * | |
77b97be7 EM |
256 | * @param bool $public |
257 | * | |
258 | * @return array | |
6a488035 | 259 | */ |
00be9182 | 260 | public static function getMembershipTypes($public = TRUE) { |
be2fb01f | 261 | $membershipTypes = []; |
6a488035 TO |
262 | $membershipType = new CRM_Member_DAO_MembershipType(); |
263 | $membershipType->is_active = 1; | |
264 | if ($public) { | |
265 | $membershipType->visibility = 'Public'; | |
266 | } | |
267 | $membershipType->orderBy(' weight'); | |
268 | $membershipType->find(); | |
269 | while ($membershipType->fetch()) { | |
270 | $membershipTypes[$membershipType->id] = $membershipType->name; | |
271 | } | |
6a488035 TO |
272 | return $membershipTypes; |
273 | } | |
274 | ||
275 | /** | |
4c429c82 | 276 | * Get membership Type Details. |
6a488035 | 277 | * |
eb151aab | 278 | * @deprecated use getMembershipType. |
279 | * | |
6a488035 | 280 | * @param int $membershipTypeId |
77b97be7 EM |
281 | * |
282 | * @return array|null | |
6a488035 | 283 | */ |
00be9182 | 284 | public static function getMembershipTypeDetails($membershipTypeId) { |
be2fb01f | 285 | $membershipTypeDetails = []; |
6a488035 TO |
286 | |
287 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
288 | $membershipType->is_active = 1; | |
289 | $membershipType->id = $membershipTypeId; | |
290 | if ($membershipType->find(TRUE)) { | |
291 | CRM_Core_DAO::storeValues($membershipType, $membershipTypeDetails); | |
6a488035 TO |
292 | return $membershipTypeDetails; |
293 | } | |
294 | else { | |
295 | return NULL; | |
296 | } | |
297 | } | |
298 | ||
299 | /** | |
4c429c82 | 300 | * Calculate start date and end date for new membership. |
6a488035 | 301 | * |
b2363ea8 TO |
302 | * @param int $membershipTypeId |
303 | * Membership type id. | |
87ad884a | 304 | * @param string $joinDate |
b2363ea8 | 305 | * Member since ( in mysql date format ). |
87ad884a | 306 | * @param string $startDate |
b2363ea8 | 307 | * Start date ( in mysql date format ). |
da6b46f4 | 308 | * @param null $endDate |
b2363ea8 TO |
309 | * @param int $numRenewTerms |
310 | * How many membership terms are being added to end date (default is 1). | |
6a488035 | 311 | * |
a6c01b45 CW |
312 | * @return array |
313 | * associated array with start date, end date and join date for the membership | |
6a488035 TO |
314 | */ |
315 | public static function getDatesForMembershipType($membershipTypeId, $joinDate = NULL, $startDate = NULL, $endDate = NULL, $numRenewTerms = 1) { | |
316 | $membershipTypeDetails = self::getMembershipTypeDetails($membershipTypeId); | |
317 | ||
4c429c82 | 318 | // Convert all dates to 'Y-m-d' format. |
be2fb01f | 319 | foreach ([ |
c5c263ca AH |
320 | 'joinDate', |
321 | 'startDate', | |
322 | 'endDate', | |
be2fb01f | 323 | ] as $dateParam) { |
6a488035 TO |
324 | if (!empty($$dateParam)) { |
325 | $$dateParam = CRM_Utils_Date::processDate($$dateParam, NULL, FALSE, 'Y-m-d'); | |
326 | } | |
327 | } | |
328 | if (!$joinDate) { | |
329 | $joinDate = date('Y-m-d'); | |
330 | } | |
331 | $actualStartDate = $joinDate; | |
332 | if ($startDate) { | |
333 | $actualStartDate = $startDate; | |
334 | } | |
335 | ||
336 | $fixed_period_rollover = FALSE; | |
337 | if (CRM_Utils_Array::value('period_type', $membershipTypeDetails) == 'rolling') { | |
338 | if (!$startDate) { | |
339 | $startDate = $joinDate; | |
340 | } | |
341 | $actualStartDate = $startDate; | |
342 | } | |
343 | elseif (CRM_Utils_Array::value('period_type', $membershipTypeDetails) == 'fixed') { | |
5ae96933 DG |
344 | // calculate start date |
345 | // if !$startDate then use $joinDate | |
346 | $toDay = explode('-', (empty($startDate) ? $joinDate : $startDate)); | |
6a488035 TO |
347 | $year = $toDay[0]; |
348 | $month = $toDay[1]; | |
f7660a39 | 349 | $day = $toDay[2]; |
6a488035 TO |
350 | |
351 | if ($membershipTypeDetails['duration_unit'] == 'year') { | |
352 | ||
353 | //get start fixed day | |
354 | $startMonth = substr($membershipTypeDetails['fixed_period_start_day'], 0, | |
355 | strlen($membershipTypeDetails['fixed_period_start_day']) - 2 | |
356 | ); | |
357 | $startDay = substr($membershipTypeDetails['fixed_period_start_day'], -2); | |
358 | ||
f7660a39 | 359 | if (date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year)) <= date('Y-m-d', mktime(0, 0, 0, $month, $day, $year))) { |
4c429c82 | 360 | $actualStartDate = date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year)); |
f7660a39 DG |
361 | } |
362 | else { | |
4c429c82 | 363 | $actualStartDate = date('Y-m-d', mktime(0, 0, 0, $startMonth, $startDay, $year - 1)); |
f7660a39 | 364 | } |
82d11b3f | 365 | |
4c429c82 | 366 | $fixed_period_rollover = self::isDuringFixedAnnualRolloverPeriod($joinDate, $membershipTypeDetails, $year, $actualStartDate); |
6a488035 TO |
367 | |
368 | if (!$startDate) { | |
369 | $startDate = $actualStartDate; | |
370 | } | |
371 | } | |
372 | elseif ($membershipTypeDetails['duration_unit'] == 'month') { | |
373 | // Check if we are on or after rollover day of the month - CRM-10585 | |
374 | // If so, set fixed_period_rollover TRUE so we increment end_date month below. | |
375 | $dateParts = explode('-', $actualStartDate); | |
9b873358 | 376 | if ($dateParts[2] >= $membershipTypeDetails['fixed_period_rollover_day']) { |
b09fe5ed | 377 | $fixed_period_rollover = TRUE; |
6a488035 | 378 | } |
03e04002 | 379 | |
6a488035 TO |
380 | // Start date is always first day of actualStartDate month |
381 | if (!$startDate) { | |
382 | $actualStartDate = $startDate = $year . '-' . $month . '-01'; | |
383 | } | |
384 | } | |
385 | } | |
386 | ||
4c429c82 | 387 | // Calculate end date if it is not passed by user. |
6a488035 TO |
388 | if (!$endDate) { |
389 | //end date calculation | |
353ffa53 TO |
390 | $date = explode('-', $actualStartDate); |
391 | $year = $date[0]; | |
6a488035 | 392 | $month = $date[1]; |
353ffa53 | 393 | $day = $date[2]; |
6a488035 TO |
394 | |
395 | switch ($membershipTypeDetails['duration_unit']) { | |
396 | case 'year': | |
397 | $year = $year + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
398 | //extend membership date by duration interval. | |
399 | if ($fixed_period_rollover) { | |
400 | $year += 1; | |
401 | } | |
402 | break; | |
403 | ||
404 | case 'month': | |
405 | $month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
406 | //duration interval is month | |
407 | if ($fixed_period_rollover) { | |
408 | //CRM-10585 | |
409 | $month += 1; | |
410 | } | |
411 | break; | |
412 | ||
413 | case 'day': | |
414 | $day = $day + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
415 | ||
416 | if ($fixed_period_rollover) { | |
417 | //Fix Me: Currently we don't allow rollover if | |
418 | //duration interval is day | |
419 | } | |
420 | break; | |
421 | } | |
422 | ||
423 | if ($membershipTypeDetails['duration_unit'] == 'lifetime') { | |
424 | $endDate = NULL; | |
425 | } | |
426 | else { | |
427 | $endDate = date('Y-m-d', mktime(0, 0, 0, $month, $day - 1, $year)); | |
428 | } | |
429 | } | |
430 | ||
be2fb01f | 431 | $membershipDates = [ |
c8881afb | 432 | 'start_date' => CRM_Utils_Date::customFormat($startDate, '%Y%m%d'), |
edc9b94e EM |
433 | 'end_date' => CRM_Utils_Date::customFormat($endDate, '%Y%m%d'), |
434 | 'join_date' => CRM_Utils_Date::customFormat($joinDate, '%Y%m%d'), | |
be2fb01f | 435 | ]; |
6a488035 TO |
436 | |
437 | return $membershipDates; | |
438 | } | |
439 | ||
87ad884a | 440 | /** |
fe482240 | 441 | * Does this membership start between the rollover date and the start of the next period. |
87ad884a EM |
442 | * (in which case they will get an extra membership period) |
443 | * ie if annual memberships run June - May & the rollover is in May memberships between | |
444 | * May and June will return TRUE and between June and May will return FALSE | |
445 | * | |
446 | * @param string $startDate start date of current membership period | |
87ad884a | 447 | * @param array $membershipTypeDetails |
81d97e72 | 448 | * @param int $year |
e9ac29eb | 449 | * @param string $actualStartDate |
87ad884a EM |
450 | * @return bool is this in the window where the membership gets an extra part-period added |
451 | */ | |
70a87708 | 452 | public static function isDuringFixedAnnualRolloverPeriod($startDate, $membershipTypeDetails, $year, $actualStartDate) { |
87ad884a EM |
453 | |
454 | $rolloverMonth = substr($membershipTypeDetails['fixed_period_rollover_day'], 0, | |
455 | strlen($membershipTypeDetails['fixed_period_rollover_day']) - 2 | |
456 | ); | |
457 | $rolloverDay = substr($membershipTypeDetails['fixed_period_rollover_day'], -2); | |
458 | ||
8454b618 | 459 | $calculatedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year)); |
87ad884a EM |
460 | |
461 | //CRM-7825 -membership date rules are : | |
462 | //1. Membership should not be start in future. | |
463 | //2. rollover window should be subset of membership window. | |
464 | ||
87ad884a EM |
465 | //get the fixed end date here. |
466 | $dateParts = explode('-', $actualStartDate); | |
8454b618 | 467 | $endDateOfFirstYearMembershipPeriod = date('Y-m-d', mktime(0, 0, 0, |
87ad884a EM |
468 | $dateParts[1], |
469 | $dateParts[2] - 1, | |
6f1c82f5 | 470 | $dateParts[0] + 1 |
87ad884a EM |
471 | )); |
472 | ||
8454b618 EM |
473 | //we know the month and day of the rollover date but not the year (we're just |
474 | //using the start date year at the moment. So if it's after the end | |
475 | // of the first year of membership then it's the next period & we'll adjust back a year. If it's | |
476 | // before the start_date then it's too early & we'll adjust forward. | |
477 | if ($endDateOfFirstYearMembershipPeriod < $calculatedRolloverDate) { | |
478 | $calculatedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year - 1)); | |
87ad884a | 479 | } |
8454b618 EM |
480 | if ($calculatedRolloverDate < $actualStartDate) { |
481 | $calculatedRolloverDate = date('Y-m-d', mktime(0, 0, 0, $rolloverMonth, $rolloverDay, $year + 1)); | |
87ad884a EM |
482 | } |
483 | ||
8454b618 | 484 | if ($calculatedRolloverDate <= $startDate) { |
87ad884a EM |
485 | return TRUE; |
486 | } | |
487 | return FALSE; | |
488 | } | |
489 | ||
6a488035 | 490 | /** |
055e2441 | 491 | * Calculate start date and end date for membership updates. |
492 | * | |
493 | * Note that this function is called by the api for any membership update although it was | |
494 | * originally written for renewal (which feels a bit fragile but hey....). | |
6a488035 TO |
495 | * |
496 | * @param int $membershipId | |
497 | * @param $changeToday | |
b4d1a1bf | 498 | * If provided, specify an alternative date to use as "today" for renewal |
b2363ea8 TO |
499 | * @param int $membershipTypeID |
500 | * If provided, overrides the membership type of the $membershipID membership. | |
501 | * @param int $numRenewTerms | |
502 | * How many membership terms are being added to end date (default is 1). | |
6a488035 TO |
503 | * |
504 | * CRM-7297 Membership Upsell - Added $membershipTypeID param to facilitate calculations of dates when membership type changes | |
505 | * | |
5396af74 | 506 | * @return array |
a6c01b45 | 507 | * array fo the start date, end date and join date of the membership |
6a488035 TO |
508 | */ |
509 | public static function getRenewalDatesForMembershipType($membershipId, $changeToday = NULL, $membershipTypeID = NULL, $numRenewTerms = 1) { | |
be2fb01f | 510 | $params = ['id' => $membershipId]; |
6a488035 | 511 | $membershipDetails = CRM_Member_BAO_Membership::getValues($params, $values); |
055e2441 | 512 | $membershipDetails = $membershipDetails[$membershipId]; |
513 | $statusID = $membershipDetails->status_id; | |
be2fb01f | 514 | $membershipDates = []; |
055e2441 | 515 | if (!empty($membershipDetails->join_date)) { |
516 | $membershipDates['join_date'] = CRM_Utils_Date::customFormat($membershipDetails->join_date, '%Y%m%d'); | |
517 | } | |
6a488035 TO |
518 | |
519 | $oldPeriodType = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', | |
353ffa53 | 520 | CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id'), 'period_type'); |
6a488035 TO |
521 | |
522 | // CRM-7297 Membership Upsell | |
523 | if (is_null($membershipTypeID)) { | |
055e2441 | 524 | $membershipTypeDetails = self::getMembershipTypeDetails($membershipDetails->membership_type_id); |
6a488035 TO |
525 | } |
526 | else { | |
527 | $membershipTypeDetails = self::getMembershipTypeDetails($membershipTypeID); | |
528 | } | |
529 | $statusDetails = CRM_Member_BAO_MembershipStatus::getMembershipStatus($statusID); | |
530 | ||
531 | if ($statusDetails['is_current_member'] == 1) { | |
055e2441 | 532 | $startDate = $membershipDetails->start_date; |
6a488035 TO |
533 | // CRM=7297 Membership Upsell: we need to handle null end_date in case we are switching |
534 | // from a lifetime to a different membership type | |
055e2441 | 535 | if (is_null($membershipDetails->end_date)) { |
6a488035 TO |
536 | $date = date('Y-m-d'); |
537 | } | |
538 | else { | |
055e2441 | 539 | $date = $membershipDetails->end_date; |
6a488035 | 540 | } |
353ffa53 | 541 | $date = explode('-', $date); |
3bd63a4b MW |
542 | // We have to add 1 day first in case it's the end of the month, then subtract afterwards |
543 | // eg. 2018-02-28 should renew to 2018-03-31, if we just added 1 month we'd get 2018-03-28 | |
6a488035 | 544 | $logStartDate = date('Y-m-d', mktime(0, 0, 0, |
353ffa53 TO |
545 | (double) $date[1], |
546 | (double) ($date[2] + 1), | |
547 | (double) $date[0] | |
548 | )); | |
6a488035 | 549 | |
353ffa53 TO |
550 | $date = explode('-', $logStartDate); |
551 | $year = $date[0]; | |
6a488035 | 552 | $month = $date[1]; |
353ffa53 | 553 | $day = $date[2]; |
6a488035 TO |
554 | |
555 | switch ($membershipTypeDetails['duration_unit']) { | |
556 | case 'year': | |
557 | //need to check if the upsell is from rolling to fixed and adjust accordingly | |
481a74f4 | 558 | if ($membershipTypeDetails['period_type'] == 'fixed' && $oldPeriodType == 'rolling') { |
6a488035 TO |
559 | $month = substr($membershipTypeDetails['fixed_period_start_day'], 0, strlen($membershipTypeDetails['fixed_period_start_day']) - 2); |
560 | $day = substr($membershipTypeDetails['fixed_period_start_day'], -2); | |
561 | $year += 1; | |
0db6c3e1 TO |
562 | } |
563 | else { | |
b09fe5ed | 564 | $year = $year + ($numRenewTerms * $membershipTypeDetails['duration_interval']); |
6a488035 TO |
565 | } |
566 | break; | |
567 | ||
568 | case 'month': | |
569 | $month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
570 | break; | |
571 | ||
572 | case 'day': | |
573 | $day = $day + ($numRenewTerms * $membershipTypeDetails['duration_interval']); | |
574 | break; | |
575 | } | |
576 | if ($membershipTypeDetails['duration_unit'] == 'lifetime') { | |
577 | $endDate = NULL; | |
578 | } | |
579 | else { | |
580 | $endDate = date('Y-m-d', mktime(0, 0, 0, | |
353ffa53 TO |
581 | $month, |
582 | $day - 1, | |
583 | $year | |
584 | )); | |
6a488035 TO |
585 | } |
586 | $today = date('Y-m-d'); | |
6a488035 TO |
587 | $membershipDates['today'] = CRM_Utils_Date::customFormat($today, '%Y%m%d'); |
588 | $membershipDates['start_date'] = CRM_Utils_Date::customFormat($startDate, '%Y%m%d'); | |
589 | $membershipDates['end_date'] = CRM_Utils_Date::customFormat($endDate, '%Y%m%d'); | |
590 | $membershipDates['log_start_date'] = CRM_Utils_Date::customFormat($logStartDate, '%Y%m%d'); | |
591 | } | |
592 | else { | |
593 | $today = date('Y-m-d'); | |
594 | if ($changeToday) { | |
595 | $today = CRM_Utils_Date::processDate($changeToday, NULL, FALSE, 'Y-m-d'); | |
596 | } | |
597 | // Calculate new start/end dates when join date is today | |
598 | $renewalDates = self::getDatesForMembershipType($membershipTypeDetails['id'], | |
599 | $today, NULL, NULL, $numRenewTerms | |
600 | ); | |
6a488035 TO |
601 | $membershipDates['today'] = CRM_Utils_Date::customFormat($today, '%Y%m%d'); |
602 | $membershipDates['start_date'] = $renewalDates['start_date']; | |
603 | $membershipDates['end_date'] = $renewalDates['end_date']; | |
604 | $membershipDates['log_start_date'] = $renewalDates['start_date']; | |
41dcb974 | 605 | // CRM-18503 - set join_date as today in case the membership type is fixed. |
606 | if ($membershipTypeDetails['period_type'] == 'fixed' && !isset($membershipDates['join_date'])) { | |
607 | $membershipDates['join_date'] = $renewalDates['join_date']; | |
608 | } | |
6a488035 | 609 | } |
055e2441 | 610 | if (!isset($membershipDates['join_date'])) { |
611 | $membershipDates['join_date'] = $membershipDates['start_date']; | |
612 | } | |
6a488035 TO |
613 | |
614 | return $membershipDates; | |
615 | } | |
616 | ||
6a488035 | 617 | /** |
fe482240 | 618 | * Retrieve all Membership Types with Member of Contact id. |
6a488035 | 619 | * |
5396af74 | 620 | * @param array $membershipTypes |
621 | * array of membership type ids | |
622 | * @return array | |
a6c01b45 | 623 | * array of the details of membership types with Member of Contact id |
6a488035 | 624 | */ |
00be9182 | 625 | public static function getMemberOfContactByMemTypes($membershipTypes) { |
be2fb01f | 626 | $memTypeOrganizations = []; |
6a488035 | 627 | if (empty($membershipTypes)) { |
b204fd50 | 628 | return $memTypeOrganizations; |
6a488035 TO |
629 | } |
630 | ||
631 | $result = CRM_Core_DAO::executeQuery("SELECT id, member_of_contact_id FROM civicrm_membership_type WHERE id IN (" . implode(',', $membershipTypes) . ")"); | |
632 | while ($result->fetch()) { | |
b204fd50 | 633 | $memTypeOrganizations[$result->id] = $result->member_of_contact_id; |
6a488035 TO |
634 | } |
635 | ||
b204fd50 | 636 | return $memTypeOrganizations; |
6a488035 TO |
637 | } |
638 | ||
77b97be7 | 639 | /** |
87ad884a | 640 | * The function returns all the Organization for all membershipTypes . |
6a488035 | 641 | * |
c490a46a | 642 | * @param int $membershipTypeId |
77b97be7 EM |
643 | * |
644 | * @return array | |
6a488035 | 645 | */ |
00be9182 | 646 | public static function getMembershipTypeOrganization($membershipTypeId = NULL) { |
be2fb01f | 647 | $allMembershipTypes = []; |
6a488035 TO |
648 | |
649 | $membershipType = new CRM_Member_DAO_MembershipType(); | |
650 | ||
651 | if (isset($membershipTypeId)) { | |
652 | $membershipType->id = $membershipTypeId; | |
653 | } | |
654 | $membershipType->find(); | |
655 | ||
656 | while ($membershipType->fetch()) { | |
b204fd50 | 657 | $allMembershipTypes[$membershipType->id] = $membershipType->member_of_contact_id; |
6a488035 | 658 | } |
b204fd50 | 659 | return $allMembershipTypes; |
6a488035 TO |
660 | } |
661 | ||
662 | /** | |
87ad884a | 663 | * Function to retrieve organization and associated membership |
6a488035 TO |
664 | * types |
665 | * | |
a6c01b45 CW |
666 | * @return array |
667 | * arrays of organization and membership types | |
6a488035 | 668 | * |
6a488035 | 669 | */ |
00be9182 | 670 | public static function getMembershipTypeInfo() { |
6a488035 | 671 | if (!self::$_membershipTypeInfo) { |
be2fb01f | 672 | $orgs = $types = []; |
6a488035 TO |
673 | |
674 | $query = 'SELECT memType.id, memType.name, memType.member_of_contact_id, c.sort_name | |
675 | FROM civicrm_membership_type memType INNER JOIN civicrm_contact c ON c.id = memType.member_of_contact_id | |
676 | WHERE memType.is_active = 1 '; | |
481a74f4 | 677 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
678 | while ($dao->fetch()) { |
679 | $orgs[$dao->member_of_contact_id] = $dao->sort_name; | |
680 | $types[$dao->member_of_contact_id][$dao->id] = $dao->name; | |
681 | } | |
682 | ||
be2fb01f | 683 | self::$_membershipTypeInfo = [$orgs, $types]; |
6a488035 TO |
684 | } |
685 | return self::$_membershipTypeInfo; | |
686 | } | |
687 | ||
bb3a214a | 688 | /** |
c490a46a | 689 | * @param array $params |
100fef9d CW |
690 | * @param int $previousID |
691 | * @param int $membershipTypeId | |
bb3a214a | 692 | */ |
301db4f8 | 693 | public static function createMembershipPriceField($params, $previousID, $membershipTypeId) { |
6a488035 | 694 | |
9da8dc8c | 695 | $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name'); |
6a488035 | 696 | |
a7488080 | 697 | if (!empty($params['member_of_contact_id'])) { |
6a488035 TO |
698 | $fieldName = $params['member_of_contact_id']; |
699 | } | |
700 | else { | |
701 | $fieldName = $previousID; | |
702 | } | |
353ffa53 TO |
703 | $fieldLabel = 'Membership Amount'; |
704 | $optionsIds = NULL; | |
be2fb01f | 705 | $fieldParams = [ |
6a488035 TO |
706 | 'price_set_id ' => $priceSetId, |
707 | 'name' => $fieldName, | |
be2fb01f CW |
708 | ]; |
709 | $results = []; | |
9da8dc8c | 710 | CRM_Price_BAO_PriceField::retrieve($fieldParams, $results); |
6a488035 | 711 | if (empty($results)) { |
be2fb01f | 712 | $fieldParams = []; |
6a488035 TO |
713 | $fieldParams['label'] = $fieldLabel; |
714 | $fieldParams['name'] = $fieldName; | |
715 | $fieldParams['price_set_id'] = $priceSetId; | |
716 | $fieldParams['html_type'] = 'Radio'; | |
717 | $fieldParams['is_display_amounts'] = $fieldParams['is_required'] = 0; | |
718 | $fieldParams['weight'] = $fieldParams['option_weight'][1] = 1; | |
719 | $fieldParams['option_label'][1] = $params['name']; | |
0e09eaf5 | 720 | $fieldParams['option_description'][1] = CRM_Utils_Array::value('description', $params); |
03e04002 | 721 | |
6a488035 TO |
722 | $fieldParams['membership_type_id'][1] = $membershipTypeId; |
723 | $fieldParams['option_amount'][1] = empty($params['minimum_fee']) ? 0 : $params['minimum_fee']; | |
724 | $fieldParams['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params); | |
725 | ||
726 | if ($previousID) { | |
727 | CRM_Member_Form_MembershipType::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds); | |
728 | $fieldParams['option_id'] = CRM_Utils_Array::value('option_id', $optionsIds); | |
729 | } | |
806e9b71 | 730 | CRM_Price_BAO_PriceField::create($fieldParams); |
03e04002 | 731 | } |
6a488035 TO |
732 | else { |
733 | $fieldID = $results['id']; | |
be2fb01f | 734 | $fieldValueParams = [ |
6a488035 TO |
735 | 'price_field_id' => $fieldID, |
736 | 'membership_type_id' => $membershipTypeId, | |
be2fb01f CW |
737 | ]; |
738 | $results = []; | |
9da8dc8c | 739 | CRM_Price_BAO_PriceFieldValue::retrieve($fieldValueParams, $results); |
6a488035 | 740 | if (!empty($results)) { |
353ffa53 | 741 | $results['label'] = $results['name'] = $params['name']; |
6a488035 | 742 | $results['amount'] = empty($params['minimum_fee']) ? 0 : $params['minimum_fee']; |
353ffa53 | 743 | $optionsIds['id'] = $results['id']; |
03e04002 | 744 | } |
6a488035 | 745 | else { |
be2fb01f | 746 | $results = [ |
6a488035 TO |
747 | 'price_field_id' => $fieldID, |
748 | 'name' => $params['name'], | |
749 | 'label' => $params['name'], | |
750 | 'amount' => empty($params['minimum_fee']) ? 0 : $params['minimum_fee'], | |
751 | 'membership_type_id' => $membershipTypeId, | |
752 | 'is_active' => 1, | |
be2fb01f | 753 | ]; |
6a488035 TO |
754 | } |
755 | ||
756 | if ($previousID) { | |
757 | CRM_Member_Form_MembershipType::checkPreviousPriceField($previousID, $priceSetId, $membershipTypeId, $optionsIds); | |
a7488080 | 758 | if (!empty($optionsIds['option_id'])) { |
6a488035 TO |
759 | $optionsIds['id'] = current(CRM_Utils_Array::value('option_id', $optionsIds)); |
760 | } | |
761 | } | |
762 | $results['financial_type_id'] = CRM_Utils_Array::value('financial_type_id', $params); | |
0e09eaf5 | 763 | $results['description'] = CRM_Utils_Array::value('description', $params); |
9da8dc8c | 764 | CRM_Price_BAO_PriceFieldValue::add($results, $optionsIds); |
6a488035 TO |
765 | } |
766 | } | |
767 | ||
be2e0c6a TO |
768 | /** |
769 | * This function updates all price field value for quick config | |
6a488035 TO |
770 | * price set which has membership type |
771 | * | |
87ad884a | 772 | * @param int $membershipTypeId membership type id |
6a488035 | 773 | * |
87ad884a | 774 | * @param array $params |
6a488035 | 775 | */ |
00be9182 | 776 | public static function updateAllPriceFieldValue($membershipTypeId, $params) { |
be2fb01f CW |
777 | $defaults = []; |
778 | $fieldsToUpdate = [ | |
5afcf706 JP |
779 | 'financial_type_id' => 'financial_type_id', |
780 | 'name' => 'label', | |
781 | 'minimum_fee' => 'amount', | |
782 | 'description' => 'description', | |
783 | 'visibility' => 'visibility_id', | |
be2fb01f | 784 | ]; |
5afcf706 JP |
785 | $priceFieldValueBAO = new CRM_Price_BAO_PriceFieldValue(); |
786 | $priceFieldValueBAO->membership_type_id = $membershipTypeId; | |
787 | $priceFieldValueBAO->find(); | |
788 | while ($priceFieldValueBAO->fetch()) { | |
be2fb01f | 789 | $updateParams = [ |
5afcf706 JP |
790 | 'id' => $priceFieldValueBAO->id, |
791 | 'price_field_id' => $priceFieldValueBAO->price_field_id, | |
be2fb01f | 792 | ]; |
5afcf706 | 793 | //Get priceset details. |
be2fb01f | 794 | $fieldParams = ['fid' => $priceFieldValueBAO->price_field_id]; |
5afcf706 | 795 | $setID = CRM_Price_BAO_PriceSet::getSetId($fieldParams); |
be2fb01f | 796 | $setParams = ['id' => $setID]; |
5afcf706 JP |
797 | $setValues = CRM_Price_BAO_PriceSet::retrieve($setParams, $defaults); |
798 | if (!empty($setValues->is_quick_config) && $setValues->name != 'default_membership_type_amount') { | |
799 | foreach ($fieldsToUpdate as $key => $value) { | |
800 | if ($value == 'visibility_id' && !empty($params['visibility'])) { | |
801 | $updateParams['visibility_id'] = CRM_Price_BAO_PriceField::getVisibilityOptionID(strtolower($params['visibility'])); | |
802 | } | |
803 | else { | |
804 | $updateParams[$value] = CRM_Utils_Array::value($key, $params); | |
805 | } | |
0e09eaf5 | 806 | } |
5afcf706 | 807 | CRM_Price_BAO_PriceFieldValue::add($updateParams); |
0e09eaf5 PN |
808 | } |
809 | } | |
03e04002 | 810 | } |
96025800 | 811 | |
eb151aab | 812 | /** |
813 | * Cached wrapper for membership types. | |
814 | * | |
815 | * Since this is used from the batched script caching helps. | |
816 | * | |
817 | * @throws \CiviCRM_API3_Exception | |
818 | */ | |
819 | public static function getAllMembershipTypes() { | |
820 | if (!Civi::cache('metadata')->has(__CLASS__ . __FUNCTION__)) { | |
322b59f0 | 821 | $types = civicrm_api3('MembershipType', 'get', ['options' => ['limit' => 0, 'sort' => 'weight']])['values']; |
49bbb50e | 822 | $taxRates = CRM_Core_PseudoConstant::getTaxRates(); |
322b59f0 | 823 | $keys = ['description', 'relationship_type_id', 'relationship_direction', 'max_related']; |
824 | // In order to avoid down-stream e-notices we undo api v3 filtering of NULL values. This is covered | |
825 | // in Unit tests & ideally we might switch to apiv4 but I would argue we should build caching | |
826 | // of metadata entities like this directly into apiv4. | |
827 | foreach ($types as $id => $type) { | |
828 | foreach ($keys as $key) { | |
829 | if (!isset($type[$key])) { | |
830 | $types[$id][$key] = NULL; | |
831 | } | |
832 | } | |
833 | if (isset($type['contribution_type_id'])) { | |
834 | unset($types[$id]['contribution_type_id']); | |
835 | } | |
49bbb50e | 836 | $types[$id]['tax_rate'] = (float) ($taxRates[$type['financial_type_id']] ?? 0.0); |
837 | $multiplier = 1; | |
838 | if ($types[$id]['tax_rate'] !== 0.0) { | |
839 | $multiplier += ($types[$id]['tax_rate'] / 100); | |
840 | } | |
841 | $types[$id]['minimum_fee_with_tax'] = (float) $types[$id]['minimum_fee'] * $multiplier; | |
322b59f0 | 842 | } |
843 | Civi::cache('metadata')->set(__CLASS__ . __FUNCTION__, $types); | |
eb151aab | 844 | } |
845 | return Civi::cache('metadata')->get(__CLASS__ . __FUNCTION__); | |
846 | } | |
847 | ||
848 | /** | |
849 | * Get a specific membership type (leveraging the cache). | |
850 | * | |
851 | * @param int $id | |
852 | * | |
853 | * @return mixed | |
854 | * @throws \CiviCRM_API3_Exception | |
855 | */ | |
856 | public static function getMembershipType($id) { | |
857 | return self::getAllMembershipTypes()[$id]; | |
858 | } | |
859 | ||
322b59f0 | 860 | /** |
861 | * Get an array of all membership types the contact is permitted to access. | |
862 | * | |
863 | * @throws \CiviCRM_API3_Exception | |
864 | */ | |
865 | public static function getPermissionedMembershipTypes() { | |
866 | $types = self::getAllMembershipTypes(); | |
867 | $financialTypes = NULL; | |
868 | $financialTypes = CRM_Financial_BAO_FinancialType::getAvailableFinancialTypes($financialTypes, CRM_Core_Action::ADD); | |
869 | foreach ($types as $id => $type) { | |
870 | if (!isset($financialTypes[$type['financial_type_id']])) { | |
871 | unset($types[$id]); | |
872 | } | |
873 | } | |
874 | return $types; | |
875 | } | |
876 | ||
6a488035 | 877 | } |