3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\ContributionRecur
;
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
21 * This class generates form components for offline membership form.
23 class CRM_Member_Form_Membership
extends CRM_Member_Form
{
26 * IDs of relevant entities.
32 protected $_memType = NULL;
36 public $_contributeMode = 'direct';
38 protected $_recurMembershipTypes;
40 protected $_memTypeSelected;
43 * Display name of the member.
47 protected $_memberDisplayName = NULL;
50 * email of the person paying for the membership (used for receipts)
53 protected $_memberEmail = NULL;
56 * Contact ID of the member.
60 public $_contactID = NULL;
63 * Display name of the person paying for the membership (used for receipts)
67 protected $_contributorDisplayName = NULL;
70 * Email of the person paying for the membership (used for receipts).
74 protected $_contributorEmail;
77 * email of the person paying for the membership (used for receipts)
81 protected $_contributorContactID = NULL;
84 * ID of the person the receipt is to go to.
88 protected $_receiptContactId = NULL;
91 * Keep a class variable for ALL membership IDs so
92 * postProcess hook function can do something with it
96 protected $_membershipIDs = [];
99 * Membership created or edited on this form.
101 * If a price set creates multiple this will be the last one created.
103 * This 'last' bias reflects historical code - but it's mostly used in the receipt
104 * and there is all sorts of weird and wonderful handling that potentially compensates.
108 protected $membership = [];
111 * Set entity fields to be assigned to the form.
113 protected function setEntityFields() {
114 $this->entityFields
= [
116 'name' => 'join_date',
117 'description' => ts('Member Since'),
120 'name' => 'start_date',
121 'description' => ts('Start Date'),
124 'name' => 'end_date',
125 'description' => ts('End Date'),
131 * Set the delete message.
133 * We do this from the constructor in order to do a translation.
135 public function setDeleteMessage() {
136 $this->deleteMessage
= '<span class="font-red bold">'
137 . ts('WARNING: Deleting this membership will also delete any related payment (contribution) records.')
139 . ts('This action cannot be undone.')
141 . ts('Consider modifying the membership status instead if you want to maintain an audit trail and avoid losing payment data. You can set the status to Cancelled by editing the membership and clicking the Status Override checkbox.')
143 . ts("Click 'Delete' if you want to continue.") . '</p>';
147 * Overriding this entity trait function as not yet tested.
149 * We continue to rely on legacy handling.
151 public function addCustomDataToForm() {}
154 * Overriding this entity trait function as not yet tested.
156 * We continue to rely on legacy handling.
158 public function addFormButtons() {}
161 * Get selected membership type from the form values.
163 * @param array $priceSet
164 * @param array $params
167 * @throws \CRM_Core_Exception
169 public static function getSelectedMemberships($priceSet, $params) {
170 $memTypeSelected = [];
171 $priceFieldIDS = self
::getPriceFieldIDs($params, $priceSet);
172 if (isset($params['membership_type_id']) && !empty($params['membership_type_id'][1])) {
173 $memTypeSelected = [$params['membership_type_id'][1] => $params['membership_type_id'][1]];
176 foreach ($priceFieldIDS as $priceFieldId) {
177 if ($id = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) {
178 $memTypeSelected[$id] = $id;
182 return $memTypeSelected;
186 * Extract price set fields and values from $params.
188 * @param array $params
189 * @param array $priceSet
193 public static function getPriceFieldIDs($params, $priceSet) {
195 if (isset($priceSet['fields']) && is_array($priceSet['fields'])) {
196 foreach ($priceSet['fields'] as $fieldId => $field) {
197 if (!empty($params['price_' . $fieldId])) {
198 if (is_array($params['price_' . $fieldId])) {
199 foreach ($params['price_' . $fieldId] as $priceFldVal => $isSet) {
201 $priceFieldIDS[] = $priceFldVal;
205 elseif (!$field['is_enter_qty']) {
206 $priceFieldIDS[] = $params['price_' . $fieldId];
211 return $priceFieldIDS;
215 * Form preProcess function.
217 * @throws \CRM_Core_Exception
218 * @throws \CiviCRM_API3_Exception
220 public function preProcess() {
221 // This string makes up part of the class names, differentiating them (not sure why) from the membership fields.
222 $this->assign('formClass', 'membership');
223 parent
::preProcess();
226 $this->_priceSetId
= $_GET['priceSetId'] ??
NULL;
227 $this->set('priceSetId', $this->_priceSetId
);
228 $this->assign('priceSetId', $this->_priceSetId
);
230 if ($this->_action
& CRM_Core_Action
::DELETE
) {
231 $contributionID = CRM_Member_BAO_Membership
::getMembershipContributionId($this->_id
);
232 // check delete permission for contribution
233 if ($this->_id
&& $contributionID && !CRM_Core_Permission
::checkActionPermission('CiviContribute', $this->_action
)) {
234 CRM_Core_Error
::statusBounce(ts("This Membership is linked to a contribution. You must have 'delete in CiviContribute' permission in order to delete this record."));
238 if ($this->_action
& CRM_Core_Action
::ADD
) {
239 if ($this->_contactID
) {
240 //check whether contact has a current membership so we can alert user that they may want to do a renewal instead
241 $contactMemberships = [];
242 $memParams = ['contact_id' => $this->_contactID
];
243 CRM_Member_BAO_Membership
::getValues($memParams, $contactMemberships, TRUE);
245 foreach ($contactMemberships as $mem) {
246 $cMemTypes[] = $mem['membership_type_id'];
248 if (count($cMemTypes) > 0) {
249 foreach ($cMemTypes as $memTypeID) {
250 $memberorgs[$memTypeID] = CRM_Member_BAO_MembershipType
::getMembershipType($memTypeID)['member_of_contact_id'];
253 foreach ($contactMemberships as $mem) {
254 $mem['member_of_contact_id'] = $memberorgs[$mem['membership_type_id']] ??
NULL;
255 if (!empty($mem['membership_end_date'])) {
256 $mem['membership_end_date'] = CRM_Utils_Date
::customFormat($mem['membership_end_date']);
258 $mem['membership_type'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
259 $mem['membership_type_id'],
262 $mem['membership_status'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus',
266 $mem['renewUrl'] = CRM_Utils_System
::url('civicrm/contact/view/membership',
267 "reset=1&action=renew&cid={$this->_contactID}&id={$mem['id']}&context=membership&selectedChild=member"
268 . ($this->_mode ?
'&mode=live' : '')
270 $mem['membershipTab'] = CRM_Utils_System
::url('civicrm/contact/view',
271 "reset=1&force=1&cid={$this->_contactID}&selectedChild=member"
273 $mems_by_org[$mem['member_of_contact_id']] = $mem;
275 $this->assign('existingContactMemberships', $mems_by_org);
279 // In standalone mode we don't have a contact id yet so lookup will be done client-side with this script:
280 $resources = CRM_Core_Resources
::singleton();
281 $resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/MembershipStandalone.js');
283 'typeorgs' => CRM_Member_BAO_MembershipType
::getMembershipTypeOrganization(),
284 'memtypes' => CRM_Core_PseudoConstant
::get('CRM_Member_BAO_Membership', 'membership_type_id'),
285 'statuses' => CRM_Core_PseudoConstant
::get('CRM_Member_BAO_Membership', 'status_id'),
287 $resources->addSetting(['existingMems' => $passthru]);
291 if (!$this->_memType
) {
292 $params = CRM_Utils_Request
::exportValues();
293 if (!empty($params['membership_type_id'][1])) {
294 $this->_memType
= $params['membership_type_id'][1];
298 // Add custom data to form
299 CRM_Custom_Form_CustomData
::addToForm($this, $this->_memType
);
300 $this->setPageTitle(ts('Membership'));
304 * Set default values for the form.
306 public function setDefaultValues() {
308 if ($this->_priceSetId
) {
309 return CRM_Price_BAO_PriceSet
::setDefaultPriceSet($this, $defaults);
312 $defaults = parent
::setDefaultValues();
314 //setting default join date and receive date
315 if ($this->_action
== CRM_Core_Action
::ADD
) {
316 $defaults['receive_date'] = CRM_Utils_Time
::date('Y-m-d H:i:s');
319 $defaults['num_terms'] = 1;
321 if (!empty($defaults['id'])) {
322 $contributionId = CRM_Core_DAO
::singleValueQuery("
323 SELECT contribution_id
324 FROM civicrm_membership_payment
325 WHERE membership_id = $this->_id
326 ORDER BY contribution_id
329 if ($contributionId) {
330 $defaults['record_contribution'] = $contributionId;
334 if ($this->_contactID
) {
335 $defaults['contact_id'] = $this->_contactID
;
339 //set Soft Credit Type to Gift by default
340 $scTypes = CRM_Core_OptionGroup
::values('soft_credit_type');
341 $defaults['soft_credit_type_id'] = CRM_Utils_Array
::value(ts('Gift'), array_flip($scTypes));
344 if (empty($defaults['payment_instrument_id'])) {
345 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
348 // User must explicitly choose to send a receipt in both add and update mode.
349 $defaults['send_receipt'] = 0;
351 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
352 // in this mode by default uncheck this checkbox
353 unset($defaults['record_contribution']);
356 $subscriptionCancelled = FALSE;
357 if (!empty($defaults['id'])) {
358 $subscriptionCancelled = CRM_Member_BAO_Membership
::isSubscriptionCancelled($this->_id
);
361 $alreadyAutoRenew = FALSE;
362 if (!empty($defaults['contribution_recur_id']) && !$subscriptionCancelled) {
363 $defaults['auto_renew'] = 1;
364 $alreadyAutoRenew = TRUE;
366 $this->assign('alreadyAutoRenew', $alreadyAutoRenew);
368 $this->assign('member_is_test', $defaults['member_is_test'] ??
NULL);
369 $this->assign('membership_status_id', $defaults['status_id'] ??
NULL);
370 $this->assign('is_pay_later', !empty($defaults['is_pay_later']));
373 $defaults = $this->getBillingDefaults($defaults);
376 //setting default join date if there is no join date
377 if (empty($defaults['join_date'])) {
378 $defaults['join_date'] = CRM_Utils_Time
::date('Y-m-d');
381 if (!empty($defaults['membership_end_date'])) {
382 $this->assign('endDate', $defaults['membership_end_date']);
389 * Build the form object.
391 * @throws \CRM_Core_Exception
393 public function buildQuickForm() {
395 $this->buildQuickEntityForm();
396 $this->assign('currency_symbol', CRM_Core_BAO_Country
::defaultCurrencySymbol());
397 $isUpdateToExistingRecurringMembership = $this->isUpdateToExistingRecurringMembership();
398 // build price set form.
399 $buildPriceSet = FALSE;
400 if ($this->_priceSetId ||
!empty($_POST['price_set_id'])) {
401 if (!empty($_POST['price_set_id'])) {
402 $buildPriceSet = TRUE;
404 $getOnlyPriceSetElements = TRUE;
405 if (!$this->_priceSetId
) {
406 $this->_priceSetId
= $_POST['price_set_id'];
407 $getOnlyPriceSetElements = FALSE;
410 $this->set('priceSetId', $this->_priceSetId
);
411 CRM_Price_BAO_PriceSet
::buildPriceSet($this);
413 $optionsMembershipTypes = [];
414 foreach ($this->_priceSet
['fields'] as $pField) {
415 if (empty($pField['options'])) {
418 foreach ($pField['options'] as $opId => $opValues) {
419 $optionsMembershipTypes[$opId] = CRM_Utils_Array
::value('membership_type_id', $opValues, 0);
423 $this->assign('autoRenewOption', CRM_Price_BAO_PriceSet
::checkAutoRenewForPriceSet($this->_priceSetId
));
425 $this->assign('optionsMembershipTypes', $optionsMembershipTypes);
426 $this->assign('contributionType', CRM_Utils_Array
::value('financial_type_id', $this->_priceSet
));
428 // get only price set form elements.
429 if ($getOnlyPriceSetElements) {
434 // use to build form during form rule.
435 $this->assign('buildPriceSet', $buildPriceSet);
437 if ($this->_action
& CRM_Core_Action
::ADD
) {
438 $buildPriceSet = FALSE;
439 $priceSets = CRM_Price_BAO_PriceSet
::getAssoc(FALSE, 'CiviMember');
440 if (!empty($priceSets)) {
441 $buildPriceSet = TRUE;
444 if ($buildPriceSet) {
445 $this->add('select', 'price_set_id', ts('Choose price set'),
447 '' => ts('Choose price set'),
449 NULL, ['onchange' => "buildAmount( this.value );"]
452 $this->assign('hasPriceSets', $buildPriceSet);
455 if ($this->_action
& CRM_Core_Action
::DELETE
) {
459 'name' => ts('Delete'),
460 'spacing' => ' ',
465 'name' => ts('Cancel'),
471 $contactField = $this->addEntityRef('contact_id', ts('Member'), ['create' => TRUE, 'api' => ['extra' => ['email']]], TRUE);
472 if ($this->_context
!== 'standalone') {
473 $contactField->freeze();
476 $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
478 // Throw status bounce when no Membership type or priceset is present
479 if (empty($this->allMembershipTypeDetails
) && empty($priceSets)
481 CRM_Core_Error
::statusBounce(ts('You do not have all the permissions needed for this page.'));
483 // retrieve all memberships
484 $allMembershipInfo = [];
485 foreach ($this->allMembershipTypeDetails
as $key => $values) {
486 if ($this->_mode
&& empty($values['minimum_fee'])) {
490 $memberOfContactId = $values['member_of_contact_id'] ??
NULL;
491 if (empty($selMemTypeOrg[$memberOfContactId])) {
492 $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
498 $selOrgMemType[$memberOfContactId][0] = ts('- select -');
500 if (empty($selOrgMemType[$memberOfContactId][$key])) {
501 $selOrgMemType[$memberOfContactId][$key] = $values['name'] ??
NULL;
504 $totalAmount = $values['minimum_fee'] ??
NULL;
505 // build membership info array, which is used when membership type is selected to:
506 // - set the payment information block
507 // - set the max related block
508 $allMembershipInfo[$key] = [
509 'financial_type_id' => $values['financial_type_id'] ??
NULL,
510 'total_amount' => CRM_Utils_Money
::formatLocaleNumericRoundedForDefaultCurrency($totalAmount),
511 'total_amount_numeric' => $totalAmount,
512 'auto_renew' => $values['auto_renew'] ??
NULL,
513 'tax_rate' => $values['tax_rate'],
514 'has_related' => isset($values['relationship_type_id']),
515 'max_related' => $values['max_related'] ??
NULL,
519 $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
521 // show organization by default, if only one organization in
523 if (count($selMemTypeOrg) == 2) {
524 unset($selMemTypeOrg[0], $selOrgMemType[0][0]);
526 //sort membership organization and type, CRM-6099
527 natcasesort($selMemTypeOrg);
528 foreach ($selOrgMemType as $index => $orgMembershipType) {
529 natcasesort($orgMembershipType);
530 $selOrgMemType[$index] = $orgMembershipType;
534 'onChange' => "buildMaxRelated(this.value,true); CRM.buildCustomData('Membership', this.value);",
537 if (!empty($this->_recurPaymentProcessors
)) {
538 $memTypeJs['onChange'] = "" . $memTypeJs['onChange'] . " buildAutoRenew(this.value, null, '{$this->_mode}');";
541 $this->add('text', 'max_related', ts('Max related'),
542 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_Membership', 'max_related')
545 $sel = &$this->addElement('hierselect',
546 'membership_type_id',
547 ts('Membership Organization and Type'),
551 $sel->setOptions([$selMemTypeOrg, $selOrgMemType]);
553 if ($this->_action
& CRM_Core_Action
::ADD
) {
554 $this->add('number', 'num_terms', ts('Number of Terms'), ['size' => 6]);
557 $this->add('text', 'source', ts('Source'),
558 CRM_Core_DAO
::getAttribute('CRM_Member_DAO_Membership', 'source')
561 //CRM-7362 --add campaigns.
564 $campaignId = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'campaign_id');
566 CRM_Campaign_BAO_Campaign
::addCampaign($this, $campaignId);
569 $this->add('select', 'status_id', ts('Membership Status'),
570 ['' => ts('- select -')] + CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'label')
573 $statusOverride = $this->addElement('select', 'is_override', ts('Status Override?'),
574 CRM_Member_StatusOverrideTypes
::getSelectOptions()
577 $this->add('datepicker', 'status_override_end_date', ts('Status Override End Date'), '', FALSE, ['minDate' => CRM_Utils_Time
::date('Y-m-d'), 'time' => FALSE]);
579 $this->addElement('checkbox', 'record_contribution', ts('Record Membership Payment?'));
581 $this->add('text', 'total_amount', ts('Amount'));
582 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
584 $this->add('datepicker', 'receive_date', ts('Received'), [], FALSE, ['time' => TRUE]);
586 $this->add('select', 'payment_instrument_id',
587 ts('Payment Method'),
588 ['' => ts('- select -')] + CRM_Contribute_PseudoConstant
::paymentInstrument(),
589 FALSE, ['onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);"]
591 $this->add('text', 'trxn_id', ts('Transaction ID'));
592 $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
594 'CRM_Contribute_DAO_Contribution',
600 $this->add('select', 'contribution_status_id',
601 ts('Payment Status'), CRM_Contribute_BAO_Contribution_Utils
::getPendingAndCompleteStatuses()
603 $this->add('text', 'check_number', ts('Check Number'),
604 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
608 //add field for amount to allow an amount to be entered that differs from minimum
609 $this->add('text', 'total_amount', ts('Amount'));
611 $this->add('select', 'financial_type_id',
612 ts('Financial Type'),
613 ['' => ts('- select -')] + CRM_Financial_BAO_FinancialType
::getAvailableFinancialTypes($financialTypes, $this->_action
)
616 $this->addElement('checkbox', 'is_different_contribution_contact', ts('Record Payment from a Different Contact?'));
618 $this->addSelect('soft_credit_type_id', ['entity' => 'contribution_soft']);
619 $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), ['create' => TRUE]);
621 $this->addElement('checkbox',
623 ts('Send Confirmation and Receipt?'), NULL,
624 ['onclick' => "showEmailOptions()"]
627 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
629 $this->add('textarea', 'receipt_text', ts('Receipt Message'));
631 // Retrieve the name and email of the contact - this will be the TO for receipt email
632 if ($this->_contactID
) {
633 [$this->_memberDisplayName
, $this->_memberEmail
] = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
635 $this->assign('emailExists', $this->_memberEmail
);
636 $this->assign('displayName', $this->_memberDisplayName
);
638 if ($isUpdateToExistingRecurringMembership && CRM_Member_BAO_Membership
::isCancelSubscriptionSupported($this->_id
)) {
639 $this->assign('cancelAutoRenew',
640 CRM_Utils_System
::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}")
644 $this->assign('isRecur', $isUpdateToExistingRecurringMembership);
646 $this->addFormRule(['CRM_Member_Form_Membership', 'formRule'], $this);
647 $mailingInfo = Civi
::settings()->get('mailing_backend');
648 $this->assign('isEmailEnabledForSite', ($mailingInfo['outBound_option'] != 2));
650 parent
::buildQuickForm();
656 * @param array $params
657 * (ref.) an assoc array of name/value pairs.
659 * @param array $files
660 * @param CRM_Member_Form_Membership $self
663 * mixed true or array of errors
665 * @throws \CRM_Core_Exception
666 * @throws CiviCRM_API3_Exception
668 public static function formRule($params, $files, $self) {
671 $priceSetId = $self->getPriceSetID($params);
672 $priceSetDetails = $self->getPriceSetDetails($params);
674 $selectedMemberships = self
::getSelectedMemberships($priceSetDetails[$priceSetId], $params);
676 if (!empty($params['price_set_id'])) {
677 CRM_Price_BAO_PriceField
::priceSetValidation($priceSetId, $params, $errors);
679 $priceFieldIDS = self
::getPriceFieldIDs($params, $priceSetDetails[$priceSetId]);
681 if (!empty($priceFieldIDS)) {
682 $ids = implode(',', $priceFieldIDS);
684 $count = CRM_Price_BAO_PriceSet
::getMembershipCount($ids);
685 foreach ($count as $occurrence) {
686 if ($occurrence > 1) {
687 $errors['_qf_default'] = ts('Select at most one option associated with the same membership type.');
691 // Return error if empty $self->_memTypeSelected
692 if (empty($errors) && empty($selectedMemberships)) {
693 $errors['_qf_default'] = ts('Select at least one membership option.');
695 if (!$self->_mode
&& empty($params['record_contribution'])) {
696 $errors['record_contribution'] = ts('Record Membership Payment is required when you use a price set.');
700 if (empty($params['membership_type_id'][1])) {
701 $errors['membership_type_id'] = ts('Please select a membership type.');
703 $numterms = $params['num_terms'] ??
NULL;
704 if ($numterms && intval($numterms) != $numterms) {
705 $errors['num_terms'] = ts('Please enter an integer for the number of terms.');
708 if (($self->_mode ||
isset($params['record_contribution'])) && empty($params['financial_type_id'])) {
709 $errors['financial_type_id'] = ts('Please enter the financial Type.');
713 if (!empty($errors) && (count($selectedMemberships) > 1)) {
714 $memberOfContacts = CRM_Member_BAO_MembershipType
::getMemberOfContactByMemTypes($selectedMemberships);
715 $duplicateMemberOfContacts = array_count_values($memberOfContacts);
716 foreach ($duplicateMemberOfContacts as $countDuplicate) {
717 if ($countDuplicate > 1) {
718 $errors['_qf_default'] = ts('Please do not select more than one membership associated with the same organization.');
723 if (!empty($errors)) {
727 if (!empty($params['record_contribution']) && empty($params['payment_instrument_id'])) {
728 $errors['payment_instrument_id'] = ts('Payment Method is a required field.');
731 if (!empty($params['is_different_contribution_contact'])) {
732 if (empty($params['soft_credit_type_id'])) {
733 $errors['soft_credit_type_id'] = ts('Please Select a Soft Credit Type');
735 if (empty($params['soft_credit_contact_id'])) {
736 $errors['soft_credit_contact_id'] = ts('Please select a contact');
740 if (!empty($params['payment_processor_id'])) {
741 // validate payment instrument (e.g. credit card number)
742 CRM_Core_Payment_Form
::validatePaymentInstrument($params['payment_processor_id'], $params, $errors, NULL);
746 if (!empty($params['join_date'])) {
748 $joinDate = CRM_Utils_Date
::processDate($params['join_date']);
750 foreach ($selectedMemberships as $memType) {
752 if (!empty($params['start_date'])) {
753 $startDate = CRM_Utils_Date
::processDate($params['start_date']);
756 // if end date is set, ensure that start date is also set
757 // and that end date is later than start date
759 if (!empty($params['end_date'])) {
760 $endDate = CRM_Utils_Date
::processDate($params['end_date']);
763 $membershipDetails = CRM_Member_BAO_MembershipType
::getMembershipType($memType);
764 if ($startDate && CRM_Utils_Array
::value('period_type', $membershipDetails) === 'rolling') {
765 if ($startDate < $joinDate) {
766 $errors['start_date'] = ts('Start date must be the same or later than Member since.');
771 if ($membershipDetails['duration_unit'] === 'lifetime') {
772 // Check if status is NOT cancelled or similar. For lifetime memberships, there is no automated
773 // process to update status based on end-date. The user must change the status now.
774 $result = civicrm_api3('MembershipStatus', 'get', [
776 'is_current_member' => 0,
778 $tmp_statuses = $result['values'];
780 foreach ($tmp_statuses as $cur_stat) {
781 $status_ids[] = $cur_stat['id'];
784 if (empty($params['status_id']) ||
in_array($params['status_id'], $status_ids) == FALSE) {
785 $errors['status_id'] = ts('Please enter a status that does NOT represent a current membership status.');
788 if (!empty($params['is_override']) && !CRM_Member_StatusOverrideTypes
::isPermanent($params['is_override'])) {
789 $errors['is_override'] = ts('Because you set an End Date for a lifetime membership, This must be set to "Override Permanently"');
794 $errors['start_date'] = ts('Start date must be set if end date is set.');
796 if ($endDate < $startDate) {
797 $errors['end_date'] = ts('End date must be the same or later than start date.');
802 // Default values for start and end dates if not supplied on the form.
803 $defaultDates = CRM_Member_BAO_MembershipType
::getDatesForMembershipType($memType,
810 $startDate = CRM_Utils_Array
::value('start_date',
815 $endDate = CRM_Utils_Array
::value('end_date',
820 //CRM-3724, check for availability of valid membership status.
821 if ((empty($params['is_override']) || CRM_Member_StatusOverrideTypes
::isNo($params['is_override'])) && !isset($errors['_qf_default'])) {
822 $calcStatus = CRM_Member_BAO_MembershipStatus
::getMembershipStatusByDate($startDate,
830 if (empty($calcStatus)) {
831 $url = CRM_Utils_System
::url('civicrm/admin/member/membershipStatus', 'reset=1&action=browse');
832 $errors['_qf_default'] = ts('There is no valid Membership Status available for selected membership dates.');
833 $status = ts('Oops, it looks like there is no valid membership status available for the given membership dates. You can <a href="%1">Configure Membership Status Rules</a>.', [1 => $url]);
835 $status .= ' ' . ts('OR You can sign up by setting Status Override? to something other than "NO".');
837 CRM_Core_Session
::setStatus($status, ts('Membership Status Error'), 'error');
843 $errors['join_date'] = ts('Please enter the Member Since.');
846 if (!empty($params['is_override']) && CRM_Member_StatusOverrideTypes
::isOverridden($params['is_override']) && empty($params['status_id'])) {
847 $errors['status_id'] = ts('Please enter the Membership status.');
850 if (!empty($params['is_override']) && CRM_Member_StatusOverrideTypes
::isUntilDate($params['is_override'])) {
851 if (empty($params['status_override_end_date'])) {
852 $errors['status_override_end_date'] = ts('Please enter the Membership override end date.');
856 //total amount condition arise when membership type having no
858 if (isset($params['record_contribution'])) {
859 if (CRM_Utils_System
::isNull($params['total_amount'])) {
860 $errors['total_amount'] = ts('Please enter the contribution.');
864 return empty($errors) ?
TRUE : $errors;
868 * Process the form submission.
870 * @throws \CRM_Core_Exception
871 * @throws \CiviCRM_API3_Exception
873 public function postProcess() {
874 if ($this->_action
& CRM_Core_Action
::DELETE
) {
875 CRM_Member_BAO_Membership
::del($this->_id
);
878 // get the submitted form values.
879 $this->_params
= $this->controller
->exportValues($this->_name
);
880 $this->prepareStatusOverrideValues();
884 $this->setUserContext();
888 * Prepares the values related to status override.
890 private function prepareStatusOverrideValues() {
891 $this->setOverrideDateValue();
892 $this->convertIsOverrideValue();
896 * Sets status override end date to empty value if
897 * the selected override option is not 'until date'.
899 private function setOverrideDateValue() {
900 if (!CRM_Member_StatusOverrideTypes
::isUntilDate(CRM_Utils_Array
::value('is_override', $this->_params
))) {
901 $this->_params
['status_override_end_date'] = '';
906 * Convert the value of selected (status override?)
907 * option to TRUE if it indicate an overridden status
908 * or FALSE otherwise.
910 private function convertIsOverrideValue() {
911 $this->_params
['is_override'] = CRM_Member_StatusOverrideTypes
::isOverridden($this->_params
['is_override'] ?? CRM_Member_StatusOverrideTypes
::NO
);
915 * Send email receipt.
917 * @param CRM_Core_Form $form
919 * @param array $formValues
922 * true if mail was sent successfully
923 * @throws \CRM_Core_Exception
924 * @throws \CiviCRM_API3_Exception
927 * This function was shared with Batch_Entry which had limited overlap
928 * & needs rationalising.
931 protected function emailReceipt($form, &$formValues) {
932 $membership = $this->getMembership();
933 // retrieve 'from email id' for acknowledgement
934 $receiptFrom = $formValues['from_email_address'] ??
NULL;
936 // @todo figure out how much of the stuff below is genuinely shared with the batch form & a logical shared place.
937 if (!empty($formValues['payment_instrument_id'])) {
938 $paymentInstrument = CRM_Contribute_PseudoConstant
::paymentInstrument();
939 $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']];
942 $form->assign('module', 'Membership');
944 if (!empty($formValues['contribution_id'])) {
945 $form->assign('currency', CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $formValues['contribution_id'], 'currency'));
948 $form->assign('currency', CRM_Core_Config
::singleton()->defaultCurrency
);
951 if (!empty($formValues['contribution_status_id'])) {
952 $form->assign('contributionStatusID', $formValues['contribution_status_id']);
953 $form->assign('contributionStatus', CRM_Contribute_PseudoConstant
::contributionStatus($formValues['contribution_status_id'], 'name'));
956 if (!empty($formValues['is_renew'])) {
957 $form->assign('receiptType', 'membership renewal');
960 $form->assign('receiptType', 'membership signup');
962 $form->assign('receive_date', CRM_Utils_Array
::value('receive_date', $formValues));
963 $form->assign('formValues', $formValues);
965 $form->assign('mem_start_date', CRM_Utils_Date
::formatDateOnlyLong($membership['start_date']));
966 if (!CRM_Utils_System
::isNull($membership['end_date'])) {
967 $form->assign('mem_end_date', CRM_Utils_Date
::formatDateOnlyLong($membership['end_date']));
969 $form->assign('membership_name', CRM_Member_PseudoConstant
::membershipType($membership['membership_type_id']));
971 if ((empty($form->_contributorDisplayName
) ||
empty($form->_contributorEmail
))) {
972 // in this case the form is being called statically from the batch editing screen
973 // having one class in the form layer call another statically is not greate
974 // & we should aim to move this function to the BAO layer in future.
975 // however, we can assume that the contact_id passed in by the batch
976 // function will be the recipient
977 [$form->_contributorDisplayName
, $form->_contributorEmail
]
978 = CRM_Contact_BAO_Contact_Location
::getEmailDetails($formValues['contact_id']);
979 if (empty($form->_receiptContactId
)) {
980 $form->_receiptContactId
= $formValues['contact_id'];
984 CRM_Core_BAO_MessageTemplate
::sendTemplate(
986 'workflow' => 'membership_offline_receipt',
987 'from' => $receiptFrom,
988 'toName' => $form->_contributorDisplayName
,
989 'toEmail' => $form->_contributorEmail
,
990 'PDFFilename' => ts('receipt') . '.pdf',
991 'isEmailPdf' => Civi
::settings()->get('invoice_is_email_pdf'),
992 'isTest' => (bool) ($form->_action
& CRM_Core_Action
::PREVIEW
),
994 'receiptText' => $this->getSubmittedValue('receipt_text'),
995 'contributionId' => $formValues['contribution_id'],
996 'contactId' => $form->_receiptContactId
,
997 'membershipId' => $this->getMembershipID(),
1008 * This is also accessed by unit tests.
1010 * @throws \CRM_Core_Exception
1011 * @throws \CiviCRM_API3_Exception
1012 * @throws \API_Exception
1014 public function submit(): void
{
1015 $this->storeContactFields($this->_params
);
1016 $this->beginPostProcess();
1018 $params = $softParams = $ids = [];
1021 $this->processBillingAddress();
1022 $formValues = $this->_params
;
1023 $formValues = $this->setPriceSetParameters($formValues);
1026 $ids['membership'] = $params['id'] = $this->_id
;
1029 // Set variables that we normally get from context.
1030 // In form mode these are set in preProcess.
1031 //TODO: set memberships, fixme
1032 $this->setContextVariables($formValues);
1034 $this->_memTypeSelected
= self
::getSelectedMemberships(
1038 $formValues['financial_type_id'] = $this->getFinancialTypeID();
1040 $isQuickConfig = $this->_priceSet
['is_quick_config'];
1042 $lineItem = [$this->order
->getPriceSetID() => $this->order
->getLineItems()];
1044 $params['tax_amount'] = $this->order
->getTotalTaxAmount();
1045 $params['total_amount'] = $this->order
->getTotalAmount();
1046 $params['contact_id'] = $this->_contactID
;
1048 $params = array_merge($params, $this->getFormMembershipParams());
1049 $membershipTypeValues = $this->getMembershipParameters();
1051 //CRM-13981, allow different person as a soft-contributor of chosen type
1052 if ($this->_contributorContactID
!= $this->_contactID
) {
1053 $params['contribution_contact_id'] = $this->_contributorContactID
;
1054 if (!empty($formValues['soft_credit_type_id'])) {
1055 $softParams['soft_credit_type_id'] = $formValues['soft_credit_type_id'];
1056 $softParams['contact_id'] = $this->_contactID
;
1060 $pendingMembershipStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Member_BAO_Membership', 'status_id', 'Pending');
1062 if (!empty($formValues['record_contribution'])) {
1063 $recordContribution = [
1065 'payment_instrument_id',
1067 'contribution_status_id',
1074 foreach ($recordContribution as $f) {
1075 $params[$f] = $formValues[$f] ??
NULL;
1077 $params['financial_type_id'] = $this->getFinancialTypeID();
1078 $params['campaign_id'] = $this->getSubmittedValue('campaign_id');
1080 $params['contribution_source'] = $this->getContributionSource();
1082 $completedContributionStatusId = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
1083 if (empty($params['is_override']) &&
1084 CRM_Utils_Array
::value('contribution_status_id', $params) != $completedContributionStatusId
1086 $params['status_id'] = $pendingMembershipStatusId;
1087 $params['skipStatusCal'] = TRUE;
1088 $params['is_pay_later'] = 1;
1089 $this->assign('is_pay_later', 1);
1092 if ($this->getSubmittedValue('send_receipt')) {
1093 $params['receipt_date'] = $formValues['receive_date'] ??
NULL;
1098 // process line items, until no previous line items.
1099 if (!empty($lineItem)) {
1100 $params['lineItems'] = $lineItem;
1101 $params['processPriceSet'] = TRUE;
1105 $params['total_amount'] = $this->order
->getTotalAmount();
1106 $params['financial_type_id'] = $this->getFinancialTypeID();
1108 //get the payment processor id as per mode. Try removing in favour of beginPostProcess.
1109 $params['payment_processor_id'] = $formValues['payment_processor_id'] = $this->getPaymentProcessorID();
1110 $params['register_date'] = CRM_Utils_Time
::date('YmdHis');
1112 // add all the additional payment params we need
1113 $formValues['amount'] = $this->order
->getTotalAmount();
1114 $formValues['currencyID'] = $this->getCurrency();
1115 $formValues['description'] = ts("Contribution submitted by a staff person using member's credit card for signup");
1116 $formValues['invoiceID'] = $this->getInvoiceID();
1117 $formValues['financial_type_id'] = $this->getFinancialTypeID();
1119 // at this point we've created a contact and stored its address etc
1120 // all the payment processors expect the name and address to be in the
1121 // so we copy stuff over to first_name etc.
1122 $paymentParams = $formValues;
1123 $paymentParams['frequency_unit'] = $this->getFrequencyUnit();
1124 $paymentParams['frequency_interval'] = $this->getFrequencyInterval();
1126 $paymentParams['contactID'] = $this->_contributorContactID
;
1127 //CRM-10377 if payment is by an alternate contact then we need to set that person
1128 // as the contact in the payment params
1129 if ($this->_contributorContactID
!= $this->_contactID
) {
1130 if (!empty($formValues['soft_credit_type_id'])) {
1131 $softParams['contact_id'] = $params['contact_id'];
1132 $softParams['soft_credit_type_id'] = $formValues['soft_credit_type_id'];
1135 if ($this->getSubmittedValue('send_receipt')) {
1136 $paymentParams['email'] = $this->_contributorEmail
;
1139 // This is a candidate for shared beginPostProcess function.
1140 // @todo Do we need this now we have $this->formatParamsForPaymentProcessor() ?
1141 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $formValues, $paymentParams, TRUE);
1142 // CRM-7137 -for recurring membership,
1143 // we do need contribution and recurring records.
1146 $this->_params
= $formValues;
1147 $contribution = civicrm_api3('Order', 'create',
1149 'contact_id' => $this->_contributorContactID
,
1150 'line_items' => $this->getLineItemForOrderApi(),
1151 'is_test' => $this->isTest(),
1152 'campaign_id' => $this->getSubmittedValue('campaign_id'),
1153 'source' => CRM_Utils_Array
::value('source', $paymentParams, CRM_Utils_Array
::value('description', $paymentParams)),
1154 'payment_instrument_id' => $this->getPaymentInstrumentID(),
1155 'financial_type_id' => $this->getFinancialTypeID(),
1156 'receive_date' => $this->getReceiveDate(),
1157 'tax_amount' => $this->order
->getTotalTaxAmount(),
1158 'total_amount' => $this->order
->getTotalAmount(),
1159 'invoice_id' => $this->getInvoiceID(),
1160 'currency' => $this->getCurrency(),
1161 'receipt_date' => $this->getSubmittedValue('send_receipt') ?
date('YmdHis') : NULL,
1162 'contribution_recur_id' => $this->getContributionRecurID(),
1163 'skipCleanMoney' => TRUE,
1166 $this->ids
['Contribution'] = $contribution['id'];
1167 $this->setMembershipIDsFromOrder($contribution);
1169 //create new soft-credit record, CRM-13981
1171 $softParams['contribution_id'] = $contribution['id'];
1172 $softParams['currency'] = $this->getCurrency();
1173 $softParams['amount'] = $this->order
->getTotalAmount();
1174 CRM_Contribute_BAO_ContributionSoft
::add($softParams);
1177 $paymentParams['contactID'] = $this->_contactID
;
1178 $paymentParams['contributionID'] = $contribution['id'];
1180 $paymentParams['contributionRecurID'] = $this->getContributionRecurID();
1181 $paymentParams['is_recur'] = $this->isCreateRecurringContribution();
1182 $params['contribution_id'] = $paymentParams['contributionID'];
1183 $params['contribution_recur_id'] = $this->getContributionRecurID();
1185 $paymentStatus = NULL;
1187 if ($this->order
->getTotalAmount() > 0.0) {
1188 $payment = $this->_paymentProcessor
['object'];
1190 $result = $payment->doPayment($paymentParams);
1191 $formValues = array_merge($formValues, $result);
1192 $paymentStatus = CRM_Core_PseudoConstant
::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $formValues['payment_status_id']);
1193 if (!empty($params['contribution_id']) && $paymentStatus === 'Completed') {
1194 civicrm_api3('Payment', 'create', [
1195 'fee_amount' => $result['fee_amount'] ??
0,
1196 'total_amount' => $this->order
->getTotalAmount(),
1197 'payment_instrument_id' => $this->getPaymentInstrumentID(),
1198 'trxn_id' => $result['trxn_id'],
1199 'contribution_id' => $params['contribution_id'],
1200 'is_send_contribution_notification' => FALSE,
1201 'card_type_id' => $this->getCardTypeID(),
1202 'pan_truncation' => $this->getPanTruncation(),
1206 catch (\Civi\Payment\Exception\PaymentProcessorException
$e) {
1207 if (!empty($paymentParams['contributionID'])) {
1208 CRM_Contribute_BAO_Contribution
::failPayment($paymentParams['contributionID'], $this->_contactID
,
1211 if ($this->getContributionRecurID()) {
1212 CRM_Contribute_BAO_ContributionRecur
::deleteRecurContribution($this->getContributionRecurID());
1215 CRM_Core_Session
::singleton()->setStatus($e->getMessage());
1216 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/membership',
1217 "reset=1&action=add&cid={$this->_contactID}&context=membership&mode={$this->_mode}"
1223 if ($paymentStatus !== 'Completed') {
1224 $params['status_id'] = $pendingMembershipStatusId;
1225 $params['skipStatusCal'] = TRUE;
1226 //as membership is pending set dates to null.
1227 foreach ($this->_memTypeSelected
as $memType) {
1228 $membershipTypeValues[$memType]['joinDate'] = NULL;
1229 $membershipTypeValues[$memType]['startDate'] = NULL;
1230 $membershipTypeValues[$memType]['endDate'] = NULL;
1234 $now = CRM_Utils_Time
::date('YmdHis');
1235 $params['receive_date'] = CRM_Utils_Time
::date('Y-m-d H:i:s');
1236 $params['invoice_id'] = $this->getInvoiceID();
1237 $params['contribution_source'] = $this->getContributionSource();
1238 $params['source'] = $formValues['source'] ?
: $params['contribution_source'];
1239 $params['trxn_id'] = $result['trxn_id'] ??
NULL;
1240 $params['is_test'] = $this->isTest();
1241 $params['receipt_date'] = NULL;
1242 if ($this->getSubmittedValue('send_receipt') && $paymentStatus === 'Completed') {
1243 // @todo this should be updated by the send function once sent rather than
1245 $params['receipt_date'] = $now;
1248 $this->set('params', $formValues);
1249 $this->assign('trxn_id', CRM_Utils_Array
::value('trxn_id', $result));
1250 $this->assign('receive_date',
1251 CRM_Utils_Date
::mysqlToIso($params['receive_date'])
1254 // required for creating membership for related contacts
1255 $params['action'] = $this->_action
;
1257 //create membership record.
1259 foreach ($this->_memTypeSelected
as $memType) {
1260 $membershipParams = array_merge($membershipTypeValues[$memType], $params);
1261 if (isset($result['fee_amount'])) {
1262 $membershipParams['fee_amount'] = $result['fee_amount'];
1264 // This is required to trigger the recording of the membership contribution in the
1265 // CRM_Member_BAO_Membership::Create function.
1266 // @todo stop setting this & 'teach' the create function to respond to something
1267 // appropriate as part of our 2-step always create the pending contribution & then finally add the payment
1269 // @see http://wiki.civicrm.org/confluence/pages/viewpage.action?pageId=261062657#Payments&AccountsRoadmap-Movetowardsalwaysusinga2-steppaymentprocess
1270 $membershipParams['contribution_status_id'] = $result['payment_status_id'] ??
NULL;
1271 // The earlier process created the line items (although we want to get rid of the earlier one in favour
1272 // of a single path!
1273 unset($membershipParams['lineItems']);
1274 $membershipParams['payment_instrument_id'] = $this->getPaymentInstrumentID();
1275 // @todo stop passing $ids (membership and userId only are set above)
1276 $params['contribution'] = $membershipParams['contribution'] ??
NULL;
1277 unset($params['lineItems']);
1282 $params['action'] = $this->_action
;
1283 foreach ($lineItem[$this->_priceSetId
] as $id => $lineItemValues) {
1284 if (empty($lineItemValues['membership_type_id'])) {
1288 // @todo figure out why recieve_date isn't being set right here.
1289 if (empty($params['receive_date'])) {
1290 $params['receive_date'] = CRM_Utils_Time
::date('Y-m-d H:i:s');
1292 $membershipParams = array_merge($params, $membershipTypeValues[$lineItemValues['membership_type_id']]);
1294 if (!empty($softParams)) {
1295 $params['soft_credit'] = $softParams;
1297 unset($membershipParams['contribution_status_id']);
1298 $membershipParams['skipLineItem'] = TRUE;
1299 unset($membershipParams['lineItems']);
1300 // @todo stop passing $ids (membership and userId only are set above)
1301 $this->setMembership((array) CRM_Member_BAO_Membership
::create($membershipParams, $ids));
1302 $lineItem[$this->_priceSetId
][$id]['entity_id'] = $this->membership
['id'];
1303 $lineItem[$this->_priceSetId
][$id]['entity_table'] = 'civicrm_membership';
1306 $params['lineItems'] = $lineItem;
1307 if (!empty($formValues['record_contribution'])) {
1308 CRM_Member_BAO_Membership
::recordMembershipContribution($params);
1312 $this->updateContributionOnMembershipTypeChange($params);
1314 if (($this->_action
& CRM_Core_Action
::UPDATE
)) {
1315 $this->addStatusMessage($this->getStatusMessageForUpdate());
1317 elseif (($this->_action
& CRM_Core_Action
::ADD
)) {
1318 $this->addStatusMessage($this->getStatusMessageForCreate());
1321 // This would always be true as we always add price set id into both
1322 // quick config & non quick config price sets.
1323 if (!empty($lineItem[$this->_priceSetId
])) {
1324 foreach ($lineItem[$this->_priceSetId
] as & $priceFieldOp) {
1325 if (!empty($priceFieldOp['membership_type_id'])) {
1326 $priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date
::formatDateOnlyLong($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date']) : '-';
1327 $priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date
::formatDateOnlyLong($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date']) : '-';
1330 $priceFieldOp['start_date'] = $priceFieldOp['end_date'] = 'N/A';
1333 if (Civi
::settings()->get('invoicing')) {
1335 foreach ($lineItem[$this->_priceSetId
] as $key => $value) {
1336 if (isset($value['tax_amount']) && isset($value['tax_rate'])) {
1337 if (isset($dataArray[$value['tax_rate']])) {
1338 $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + CRM_Utils_Array
::value('tax_amount', $value);
1341 $dataArray[$value['tax_rate']] = $value['tax_amount'] ??
NULL;
1346 $this->assign('dataArray', $dataArray);
1349 $this->assign('lineItem', !empty($lineItem) && !$isQuickConfig ?
$lineItem : FALSE);
1351 $receiptSend = FALSE;
1352 $contributionId = $this->ids
['Contribution'] ?? CRM_Member_BAO_Membership
::getMembershipContributionId($this->getMembershipID());
1353 $membershipIds = $this->_membershipIDs
;
1354 if ($contributionId && !empty($membershipIds)) {
1355 $contributionDetails = CRM_Contribute_BAO_Contribution
::getContributionDetails(
1356 CRM_Export_Form_Select
::MEMBER_EXPORT
, $this->_membershipIDs
);
1357 if ($contributionDetails[$this->getMembershipID()]['contribution_status'] === 'Completed') {
1358 $receiptSend = TRUE;
1362 $receiptSent = FALSE;
1363 if ($this->getSubmittedValue('send_receipt') && $receiptSend) {
1364 $formValues['contact_id'] = $this->_contactID
;
1365 $formValues['contribution_id'] = $contributionId;
1366 // receipt_text_signup is no longer used in receipts from 5.47
1367 // but may linger in some sites that have not updated their
1369 $formValues['receipt_text_signup'] = $formValues['receipt_text'];
1370 // send email receipt
1371 $this->assignBillingName();
1372 $mailSend = $this->emailMembershipReceipt($formValues);
1373 $receiptSent = TRUE;
1376 if ($receiptSent && $mailSend) {
1377 $this->addStatusMessage(ts('A membership confirmation and receipt has been sent to %1.', [1 => $this->_contributorEmail
]));
1380 CRM_Core_Session
::setStatus($this->getStatusMessage(), ts('Complete'), 'success');
1381 $this->setStatusMessage();
1383 // finally set membership id if already not set
1385 $this->_id
= $this->getMembershipID();
1390 * Update related contribution of a membership if update_contribution_on_membership_type_change
1391 * contribution setting is enabled and type is changed on edit
1393 * @param array $inputParams
1394 * submitted form values
1396 * @throws \CRM_Core_Exception
1397 * @throws \CiviCRM_API3_Exception
1399 protected function updateContributionOnMembershipTypeChange($inputParams) {
1400 if (Civi
::settings()->get('update_contribution_on_membership_type_change') &&
1402 ($this->_action
& CRM_Core_Action
::UPDATE
) &&
1405 // if selected membership doesn't match with earlier membership
1406 !in_array($this->_memType
, $this->_memTypeSelected
)
1408 if ($this->isCreateRecurringContribution()) {
1409 CRM_Core_Session
::setStatus(ts('Associated recurring contribution cannot be updated on membership type change.', ts('Error'), 'error'));
1413 // fetch lineitems by updated membership ID
1414 $lineItems = CRM_Price_BAO_LineItem
::getLineItems($this->getMembershipID(), 'membership');
1415 // retrieve the related contribution ID
1416 $contributionID = CRM_Core_DAO
::getFieldValue(
1417 'CRM_Member_DAO_MembershipPayment',
1418 $this->getMembershipID(),
1422 // get price fields of chosen price-set
1423 $priceSetDetails = CRM_Utils_Array
::value(
1425 CRM_Price_BAO_PriceSet
::getSetDetail(
1432 // add price field information in $inputParams
1433 self
::addPriceFieldByMembershipType($inputParams, $priceSetDetails['fields'], $this->getMembership()['membership_type_id']);
1435 // update related contribution and financial records
1436 CRM_Price_BAO_LineItem
::changeFeeSelections(
1438 $this->getMembershipID(),
1441 $priceSetDetails['fields'],
1444 CRM_Core_Session
::setStatus(ts('Associated contribution is updated on membership type change.'), ts('Success'), 'success');
1449 * Add selected price field information in $formValues
1451 * @param array $formValues
1452 * submitted form values
1453 * @param array $priceFields
1454 * Price fields of selected Priceset ID
1455 * @param int $membershipTypeID
1456 * Selected membership type ID
1459 public static function addPriceFieldByMembershipType(&$formValues, $priceFields, $membershipTypeID) {
1460 foreach ($priceFields as $priceFieldID => $priceField) {
1461 if (isset($priceField['options']) && count($priceField['options'])) {
1462 foreach ($priceField['options'] as $option) {
1463 if ($option['membership_type_id'] == $membershipTypeID) {
1464 $formValues["price_{$priceFieldID}"] = $option['id'];
1473 * Set context in session.
1475 protected function setUserContext() {
1476 $buttonName = $this->controller
->getButtonName();
1477 $session = CRM_Core_Session
::singleton();
1479 if ($buttonName == $this->getButtonName('upload', 'new')) {
1480 if ($this->_context
=== 'standalone') {
1481 $url = CRM_Utils_System
::url('civicrm/member/add',
1482 'reset=1&action=add&context=standalone'
1486 $url = CRM_Utils_System
::url('civicrm/contact/view/membership',
1487 "reset=1&action=add&context=membership&cid={$this->_contactID}"
1492 $url = CRM_Utils_System
::url('civicrm/contact/view',
1493 "reset=1&cid={$this->_contactID}&selectedChild=member"
1496 $session->replaceUserContext($url);
1500 * Get status message for updating membership.
1503 * @throws \CiviCRM_API3_Exception
1505 protected function getStatusMessageForUpdate(): string {
1506 foreach ($this->getCreatedMemberships() as $membership) {
1507 $endDate = $membership['end_date'] ??
NULL;
1509 $statusMsg = ts('Membership for %1 has been updated.', [1 => $this->_memberDisplayName
]);
1511 $endDate = CRM_Utils_Date
::customFormat($endDate);
1512 $statusMsg .= ' ' . ts('The membership End Date is %1.', [1 => $endDate]);
1518 * Get status message for create action.
1521 * @throws \CiviCRM_API3_Exception
1523 protected function getStatusMessageForCreate(): string {
1524 foreach ($this->getCreatedMemberships() as $membership) {
1525 $statusMsg[$membership['membership_type_id']] = ts('%1 membership for %2 has been added.', [
1526 1 => $this->allMembershipTypeDetails
[$membership['membership_type_id']]['name'],
1527 2 => $this->_memberDisplayName
,
1530 $memEndDate = $membership['end_date'] ??
NULL;
1533 $memEndDate = CRM_Utils_Date
::formatDateOnlyLong($memEndDate);
1534 $statusMsg[$membership['membership_type_id']] .= ' ' . ts('The new membership End Date is %1.', [1 => $memEndDate]);
1537 $statusMsg = implode('<br/>', $statusMsg);
1538 return $statusMsg ??
'';
1543 protected function setStatusMessage() {
1545 // display message when membership type is changed
1546 if (($this->_action
& CRM_Core_Action
::UPDATE
) && $this->getMembershipID() && !in_array($this->_memType
, $this->_memTypeSelected
)) {
1547 $lineItem = CRM_Price_BAO_LineItem
::getLineItems($this->getMembershipID(), 'membership');
1548 $maxID = max(array_keys($lineItem));
1549 $lineItem = $lineItem[$maxID];
1550 $membershipTypeDetails = $this->allMembershipTypeDetails
[$this->getMembership()['membership_type_id']];
1551 if ($membershipTypeDetails['financial_type_id'] != $lineItem['financial_type_id']) {
1552 CRM_Core_Session
::setStatus(
1553 ts('The financial types associated with the old and new membership types are different. You may want to edit the contribution associated with this membership to adjust its financial type.'),
1557 if ($membershipTypeDetails['minimum_fee'] != $lineItem['line_total']) {
1558 CRM_Core_Session
::setStatus(
1559 ts('The cost of the old and new membership types are different. You may want to edit the contribution associated with this membership to adjust its amount.'),
1568 * @throws \CRM_Core_Exception
1570 protected function isUpdateToExistingRecurringMembership() {
1572 if ($this->_action
& CRM_Core_Action
::UPDATE
1573 && CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->getEntityId(),
1574 'contribution_recur_id')
1575 && !CRM_Member_BAO_Membership
::isSubscriptionCancelled($this->getEntityId())) {
1583 * Send a receipt for the membership.
1585 * @param array $formValues
1588 * @throws \CRM_Core_Exception
1589 * @throws \CiviCRM_API3_Exception
1591 protected function emailMembershipReceipt($formValues) {
1592 $customValues = $this->getCustomValuesForReceipt($formValues);
1593 $this->assign('customValues', $customValues);
1594 $this->assign('total_amount', $this->order
->getTotalAmount());
1595 $this->assign('totalTaxAmount', $this->order
->getTotalTaxAmount());
1596 $this->assign('taxTerm', $this->getSalesTaxTerm());
1599 // @todo move this outside shared code as Batch entry just doesn't
1600 $this->assign('address', CRM_Utils_Address
::getFormattedBillingAddressFieldsFromParameters(
1605 $valuesForForm = CRM_Contribute_Form_AbstractEditPayment
::formatCreditCardDetails($this->_params
);
1606 $this->assignVariables($valuesForForm, ['credit_card_exp_date', 'credit_card_type', 'credit_card_number']);
1607 $this->assign('is_pay_later', 0);
1608 $this->assign('isPrimary', 1);
1610 //insert financial type name in receipt.
1611 $formValues['contributionType_name'] = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialType',
1612 $this->getFinancialTypeID()
1614 return $this->emailReceipt($this, $formValues);
1618 * Filter the custom values from the input parameters (for display in the email).
1620 * @todo figure out why the scary code this calls does & document.
1622 * @param array $formValues
1625 protected function getCustomValuesForReceipt($formValues): array {
1626 $customFields = $customValues = [];
1627 if (property_exists($this, '_groupTree')
1628 && !empty($this->_groupTree
)
1630 foreach ($this->_groupTree
as $groupID => $group) {
1631 if ($groupID === 'info') {
1634 foreach ($group['fields'] as $k => $field) {
1635 $field['title'] = $field['label'];
1636 $customFields["custom_{$k}"] = $field;
1641 $members = [['member_id', '=', $this->getMembershipID(), 0, 0]];
1642 // check whether its a test drive
1643 if ($this->_mode
=== 'test') {
1644 $members[] = ['member_test', '=', 1, 0, 0];
1647 CRM_Core_BAO_UFGroup
::getValues($formValues['contact_id'], $customFields, $customValues, FALSE, $members);
1648 return $customValues;
1652 * Get the selected memberships as a string of labels.
1656 protected function getSelectedMembershipLabels(): string {
1658 foreach ($this->_memTypeSelected
as $membershipTypeID) {
1659 $return[] = $this->allMembershipTypeDetails
[$membershipTypeID]['name'];
1661 return implode(', ', $return);
1665 * Get the recurring contribution id, if one is applicable.
1667 * If the recurring contribution is applicable and not yet
1668 * created it will be created at this stage.
1672 * @throws \API_Exception
1673 * @throws \CiviCRM_API3_Exception
1675 protected function getContributionRecurID():?
int {
1676 if (!array_key_exists('ContributionRecur', $this->ids
)) {
1677 $this->createRecurringContribution();
1679 return $this->ids
['ContributionRecur'];
1683 * Create the recurring contribution record if the form submission requires it.
1685 * This function was copied from another form & needs cleanup.
1687 * @throws \API_Exception
1688 * @throws \CiviCRM_API3_Exception
1690 protected function createRecurringContribution(): void
{
1691 if (!$this->isCreateRecurringContribution()) {
1692 $this->ids
['ContributionRecur'] = NULL;
1695 $recurParams = ['contact_id' => $this->getContributionContactID()];
1696 $recurParams['amount'] = $this->order
->getTotalAmount();
1697 // for the legacyProcessRecurringContribution function to be reached auto_renew must be true
1698 $recurParams['auto_renew'] = TRUE;
1699 $recurParams['frequency_unit'] = $this->getFrequencyUnit();
1700 $recurParams['frequency_interval'] = $this->getFrequencyInterval();
1701 $recurParams['financial_type_id'] = $this->getFinancialTypeID();
1702 $recurParams['currency'] = $this->getCurrency();
1703 $recurParams['payment_instrument_id'] = $this->getPaymentInstrumentID();
1704 $recurParams['is_test'] = $this->isTest();
1705 $recurParams['create_date'] = $recurParams['modified_date'] = CRM_Utils_Time
::date('YmdHis');
1706 $recurParams['start_date'] = $this->getReceiveDate();
1707 $recurParams['invoice_id'] = $this->getInvoiceID();
1708 $recurParams['contribution_status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending');
1709 $recurParams['payment_processor_id'] = $this->getPaymentProcessorID();
1710 $recurParams['is_email_receipt'] = (bool) $this->getSubmittedValue('send_receipt');
1711 // we need to add a unique trxn_id to avoid a unique key error
1712 // in paypal IPN we reset this when paypal sends us the real trxn id, CRM-2991
1713 $recurParams['trxn_id'] = $this->getInvoiceID();
1714 $recurParams['campaign_id'] = $this->getSubmittedValue('campaign_id');
1715 $this->ids
['ContributionRecur'] = ContributionRecur
::create(FALSE)->setValues($recurParams)->execute()->first()['id'];
1719 * Is the form being submitted in test mode.
1723 protected function isTest(): bool {
1724 return ($this->_mode
=== 'test') ?
TRUE : FALSE;
1728 * Get the financial type id relevant to the contribution.
1730 * Financial type id is optional when price sets are in use.
1731 * Otherwise they are required for the form to submit.
1735 protected function getFinancialTypeID(): int {
1736 return (int) $this->getSubmittedValue('financial_type_id') ?
: $this->order
->getFinancialTypeID();
1740 * Get the membership type, if any, to be recurred.
1743 * @throws \CiviCRM_API3_Exception
1745 protected function getRecurMembershipType(): array {
1746 foreach ($this->order
->getRenewableMembershipTypes() as $type) {
1753 * Get the frequency interval.
1756 * @throws \CiviCRM_API3_Exception
1758 protected function getFrequencyInterval(): ?
int {
1759 $membershipType = $this->getRecurMembershipType();
1760 return empty($membershipType) ?
NULL : (int) $membershipType['duration_interval'];
1764 * Get the frequency interval.
1766 * @return string|null
1767 * @throws \CiviCRM_API3_Exception
1769 protected function getFrequencyUnit(): ?
string {
1770 $membershipType = $this->getRecurMembershipType();
1771 return empty($membershipType) ?
NULL : (string) $membershipType['duration_unit'];
1775 * Get values that should be passed to all membership create actions.
1777 * These parameters are generic to all memberships created from the form,
1778 * whether a single membership or multiple by price set (although
1779 * the form will not expose all in the latter case.
1781 * By referencing the submitted values directly we can call this
1782 * from anywhere in postProcess and get the same result (protects
1783 * against breakage if code is moved around).
1786 * @throws \API_Exception
1787 * @throws \CiviCRM_API3_Exception
1789 protected function getFormMembershipParams(): array {
1790 $submittedValues = $this->controller
->exportValues($this->_name
);
1792 'status_id' => $this->getSubmittedValue('status_id'),
1793 'source' => $this->getSubmittedValue('source') ??
$this->getContributionSource(),
1794 'contact_id' => $this->getMembershipContactID(),
1795 'is_override' => $this->getSubmittedValue('is_override'),
1796 'status_override_end_date' => $this->getSubmittedValue('status_override_end_date'),
1797 'campaign_id' => $this->getSubmittedValue('campaign_id'),
1798 'custom' => CRM_Core_BAO_CustomField
::postProcess($submittedValues,
1803 // when is_override false ignore is_admin statuses during membership
1804 // status calculation. similarly we did fix for import in CRM-3570.
1805 'exclude_is_admin' => !$this->getSubmittedValue('is_override'),
1806 'contribution_recur_id' => $this->getContributionRecurID(),
1811 * Is it necessary to create a recurring contribution.
1815 protected function isCreateRecurringContribution(): bool {
1816 return $this->_mode
&& $this->getSubmittedValue('auto_renew');
1820 * Get the payment processor ID.
1824 public function getPaymentProcessorID(): int {
1825 return (int) ($this->getSubmittedValue('payment_processor_id') ?
: $this->_paymentProcessor
['id']);
1829 * Get memberships submitted through the form submission.
1832 * @throws \CiviCRM_API3_Exception
1834 protected function getCreatedMemberships(): array {
1835 return civicrm_api3('Membership', 'get', ['id' => ['IN' => $this->_membershipIDs
]])['values'];
1839 * Get parameters for membership create for all memberships to be created.
1842 * @throws \CiviCRM_API3_Exception
1844 protected function getMembershipParameters(): array {
1845 $membershipTypeValues = [];
1846 foreach ($this->_memTypeSelected
as $memType) {
1847 $membershipTypeValues[$memType]['membership_type_id'] = $memType;
1848 if (is_numeric($this->getSubmittedValue('max_related'))) {
1849 // The BAO will set from the membership type is not passed in but we should
1850 // not set this if we don't need to to let the BAO do it's thing.
1851 $membershipTypeValues[$memType]['max_related'] = $this->getSubmittedValue('max_related');
1855 foreach ($this->order
->getMembershipLineItems() as $membershipLineItem) {
1856 $memTypeNumTerms = $this->getSubmittedValue('num_terms') ?
: $membershipLineItem['membership_num_terms'];
1857 $calcDates = CRM_Member_BAO_MembershipType
::getDatesForMembershipType(
1858 $membershipLineItem['membership_type_id'],
1859 $this->getSubmittedValue('join_date'),
1860 $this->getSubmittedValue('start_date'),
1861 $this->getSubmittedValue('end_date'),
1864 $membershipTypeValues[$membershipLineItem['membership_type_id']]['join_date'] = $calcDates['join_date'];
1865 $membershipTypeValues[$membershipLineItem['membership_type_id']]['start_date'] = $calcDates['start_date'];
1866 $membershipTypeValues[$membershipLineItem['membership_type_id']]['end_date'] = $calcDates['end_date'];
1869 return $membershipTypeValues;
1873 * Get the value for the contribution source.
1877 protected function getContributionSource(): string {
1878 [$userName] = CRM_Contact_BAO_Contact_Location
::getEmailDetails(CRM_Core_Session
::getLoggedInContactID());
1880 return ts('%1 Membership Signup: Credit card or direct debit (by %2)',
1881 [1 => $this->getSelectedMembershipLabels(), 2 => $userName]
1884 if ($this->getSubmittedValue('source')) {
1885 return $this->getSubmittedValue('source');
1887 return ts('%1 Membership: Offline signup (by %2)', [
1888 1 => $this->getSelectedMembershipLabels(),
1894 * Get the receive date for the contribution.
1896 * @return string $receive_date
1898 protected function getReceiveDate(): string {
1899 return $this->getSubmittedValue('receive_date') ?
: date('YmdHis');
1903 * Set membership IDs.
1907 protected function setMembershipIDs(array $ids): void
{
1908 $this->_membershipIDs
= $ids;
1912 * Get the created or edited membership ID.
1914 * @return false|mixed
1916 protected function getMembershipID() {
1917 return reset($this->_membershipIDs
);
1921 * Get the membership (or last membership) created or edited on this form.
1924 * @throws \CiviCRM_API3_Exception
1926 protected function getMembership(): array {
1927 if (empty($this->membership
)) {
1928 $this->membership
= civicrm_api3('Membership', 'get', ['id' => $this->getMembershipID()])['values'][$this->getMembershipID()];
1930 return $this->membership
;
1934 * Setter for membership.
1936 * @param array $membership
1938 protected function setMembership(array $membership): void
{
1939 if (!in_array($membership['id'], $this->_membershipIDs
, TRUE)) {
1940 $this->_membershipIDs
[] = $membership['id'];
1942 $this->membership
= $membership;
1946 * Get line items formatted for the Order api.
1950 * @throws \CiviCRM_API3_Exception
1952 protected function getLineItemForOrderApi(): array {
1954 foreach ($this->order
->getLineItems() as $line) {
1956 if (!empty($line['membership_type_id'])) {
1957 $params = $this->getMembershipParamsForType((int) $line['membership_type_id']);
1960 'line_item' => [$line['price_field_value_id'] => $line],
1961 'params' => $params,
1968 * Get the parameters for the given membership type.
1970 * @param int $membershipTypeID
1973 * @throws \CiviCRM_API3_Exception
1975 protected function getMembershipParamsForType(int $membershipTypeID) {
1976 return array_merge($this->getFormMembershipParams(), $this->getMembershipParameters()[$membershipTypeID]);
1980 * @param array $contribution
1982 protected function setMembershipIDsFromOrder(array $contribution): void
{
1984 foreach ($contribution['values'][$contribution['id']]['line_item'] as $line) {
1985 if ($line['entity_table'] ??
'' === 'civicrm_membership') {
1986 $ids[] = $line['entity_id'];
1989 $this->setMembershipIDs($ids);