3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for Membership Renewal
40 class CRM_Member_Form_MembershipRenewal
extends CRM_Member_Form
{
43 * Display name of the member.
47 protected $_memberDisplayName = NULL;
50 * email of the person paying for the membership (used for receipts)
52 protected $_memberEmail = NULL;
55 * 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)
72 protected $_contributorEmail = NULL;
75 * email of the person paying for the membership (used for receipts)
79 protected $_contributorContactID = NULL;
82 * ID of the person the receipt is to go to
86 protected $_receiptContactId = NULL;
89 * context would be set to standalone if the contact is use is being selected from
90 * the form rather than in the URL
95 * An array to hold a list of datefields on the form
96 * so that they can be converted to ISO in a consistent manner
100 protected $_dateFields = array(
101 'receive_date' => array('default' => 'now'),
104 public function preProcess() {
105 //custom data related code
106 $this->_cdType
= CRM_Utils_Array
::value('type', $_GET);
107 $this->assign('cdType', FALSE);
108 if ($this->_cdType
) {
109 $this->assign('cdType', TRUE);
110 return CRM_Custom_Form_CustomData
::preProcess($this);
113 parent
::preProcess();
114 // check for edit permission
115 if (!CRM_Core_Permission
::check('edit memberships')) {
116 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
119 $this->assign('endDate', CRM_Utils_Date
::customFormat(CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
120 $this->_id
, 'end_date'
123 $this->assign('membershipStatus',
124 CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipStatus',
125 CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership',
126 $this->_id
, 'status_id'
133 $membershipFee = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType
, 'minimum_fee');
134 if (!$membershipFee) {
135 $statusMsg = ts('Membership Renewal using a credit card requires a Membership fee. Since there is no fee associated with the selected membership type, you can use the normal renewal mode.');
136 CRM_Core_Session
::setStatus($statusMsg, '', 'info');
137 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/membership',
138 "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership"
143 // when custom data is included in this page
144 if (!empty($_POST['hidden_custom'])) {
145 CRM_Custom_Form_CustomData
::preProcess($this);
146 CRM_Custom_Form_CustomData
::buildQuickForm($this);
147 CRM_Custom_Form_CustomData
::setDefaultValues($this);
150 CRM_Utils_System
::setTitle(ts('Renew Membership'));
152 parent
::preProcess();
156 * Set default values for the form.
157 * the default values are retrieved from the database
162 public function setDefaultValues() {
163 if ($this->_cdType
) {
164 return CRM_Custom_Form_CustomData
::setDefaultValues($this);
167 $defaults = parent
::setDefaultValues();
168 $this->_memType
= $defaults['membership_type_id'];
170 // set renewal_date and receive_date to today in correct input format (setDateDefaults uses today if no value passed)
171 list($now, $currentTime) = CRM_Utils_Date
::setDateDefaults();
172 $defaults['renewal_date'] = $now;
173 $defaults['receive_date'] = $now;
174 $defaults['receive_date_time'] = $currentTime;
176 if ($defaults['id']) {
177 $defaults['record_contribution'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipPayment',
184 if (is_numeric($this->_memType
)) {
185 $defaults['membership_type_id'] = array();
186 $defaults['membership_type_id'][0] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
188 'member_of_contact_id',
191 $defaults['membership_type_id'][1] = $this->_memType
;
194 $defaults['membership_type_id'] = $this->_memType
;
197 $defaults['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType
, 'financial_type_id');
200 if (empty($defaults['payment_instrument_id'])) {
201 $defaults['payment_instrument_id'] = key(CRM_Core_OptionGroup
::values('payment_instrument', FALSE, FALSE, FALSE, 'AND is_default = 1'));
204 $defaults['total_amount'] = CRM_Utils_Money
::format(CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
209 $defaults['record_contribution'] = 0;
210 $defaults['num_terms'] = 1;
211 $defaults['send_receipt'] = 0;
213 //set Soft Credit Type to Gift by default
214 $scTypes = CRM_Core_OptionGroup
::values("soft_credit_type");
215 $defaults['soft_credit_type_id'] = CRM_Utils_Array
::value(ts('Gift'), array_flip($scTypes));
217 $renewalDate = CRM_Utils_Date
::processDate(CRM_Utils_Array
::value('renewal_date', $defaults),
220 $this->assign('renewalDate', $renewalDate);
221 $this->assign('member_is_test', CRM_Utils_Array
::value('member_is_test', $defaults));
224 // set default country from config if no country set
225 $config = CRM_Core_Config
::singleton();
226 if (empty($defaults["billing_country_id-{$this->_bltID}"])) {
227 $defaults["billing_country_id-{$this->_bltID}"] = $config->defaultContactCountry
;
230 if (empty($defaults["billing_state_province_id-{$this->_bltID}"])) {
231 $defaults["billing_state_province_id-{$this->_bltID}"] = $config->defaultContactStateProvince
;
234 $billingDefaults = $this->getProfileDefaults('Billing', $this->_contactID
);
235 $defaults = array_merge($defaults, $billingDefaults);
242 * Build the form object.
246 public function buildQuickForm() {
247 if ($this->_cdType
) {
248 return CRM_Custom_Form_CustomData
::buildQuickForm($this);
251 parent
::buildQuickForm();
253 $defaults = parent
::setDefaultValues();
254 $this->_memType
= $defaults['membership_type_id'];
255 $this->assign('customDataType', 'Membership');
256 $this->assign('customDataSubType', $this->_memType
);
257 $this->assign('entityID', $this->_id
);
258 $selOrgMemType[0][0] = $selMemTypeOrg[0] = ts('- select -');
260 $allMemberships = CRM_Member_BAO_Membership
::buildMembershipTypeValues($this);
262 $allMembershipInfo = $membershipType = array();
264 // auto renew options if enabled for the membership
265 $options = CRM_Core_SelectValues
::memberAutoRenew();
267 foreach ($allMemberships as $key => $values) {
268 if (!empty($values['is_active'])) {
269 $membershipType[$key] = CRM_Utils_Array
::value('name', $values);
270 if ($this->_mode
&& empty($values['minimum_fee'])) {
274 $memberOfContactId = CRM_Utils_Array
::value('member_of_contact_id', $values);
275 if (empty($selMemTypeOrg[$memberOfContactId])) {
276 $selMemTypeOrg[$memberOfContactId] = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact',
282 $selOrgMemType[$memberOfContactId][0] = ts('- select -');
284 if (empty($selOrgMemType[$memberOfContactId][$key])) {
285 $selOrgMemType[$memberOfContactId][$key] = CRM_Utils_Array
::value('name', $values);
289 // build membership info array, which is used to set the payment information block when
290 // membership type is selected.
291 $allMembershipInfo[$key] = array(
292 'financial_type_id' => CRM_Utils_Array
::value('financial_type_id', $values),
293 'total_amount' => CRM_Utils_Money
::format($values['minimum_fee'], NULL, '%a'),
294 'total_amount_numeric' => CRM_Utils_Array
::value('minimum_fee', $values),
297 if (!empty($values['auto_renew'])) {
298 $allMembershipInfo[$key]['auto_renew'] = $options[$values['auto_renew']];
303 $this->assign('allMembershipInfo', json_encode($allMembershipInfo));
305 if ($this->_memType
) {
306 $this->assign('orgName', $selMemTypeOrg[$allMemberships[$this->_memType
]['member_of_contact_id']]);
307 $this->assign('memType', $allMemberships[$this->_memType
]['name']);
310 // force select of organization by default, if only one organization in
312 if (count($selMemTypeOrg) == 2) {
313 unset($selMemTypeOrg[0], $selOrgMemType[0][0]);
315 //sort membership organization and type, CRM-6099
316 natcasesort($selMemTypeOrg);
317 foreach ($selOrgMemType as $index => $orgMembershipType) {
318 natcasesort($orgMembershipType);
319 $selOrgMemType[$index] = $orgMembershipType;
322 $js = array('onChange' => "setPaymentBlock( ); CRM.buildCustomData( 'Membership', this.value );");
324 //build the form for auto renew.
325 $recurProcessor = array();
326 if ($this->_mode ||
($this->_action
& CRM_Core_Action
::UPDATE
)) {
327 //get the valid recurring processors.
328 $recurring = CRM_Core_PseudoConstant
::paymentProcessor(FALSE, FALSE, 'is_recur = 1');
329 $recurProcessor = array_intersect_assoc($this->_processors
, $recurring);
330 if (!empty($recurProcessor)) {
331 $autoRenew = array();
332 if (!empty($membershipType)) {
338 FROM civicrm_membership_type
339 WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )';
340 $recurMembershipTypes = CRM_Core_DAO
::executeQuery($sql);
341 while ($recurMembershipTypes->fetch()) {
342 $autoRenew[$recurMembershipTypes->id
] = $recurMembershipTypes->auto_renew
;
349 $this->_recurMembershipTypes
[$recurMembershipTypes->id
][$fld] = $recurMembershipTypes->$fld;
353 $js = array('onChange' => "setPaymentBlock(); CRM.buildCustomData( 'Membership', this.value );");
354 $this->assign('autoRenew', json_encode($autoRenew));
356 $autoRenewElement = $this->addElement('checkbox', 'auto_renew', ts('Membership renewed automatically'),
357 NULL, array('onclick' => "showHideByValue('auto_renew','','send-receipt','table-row','radio',true); showHideNotice( );")
359 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
360 $autoRenewElement->freeze();
363 $this->assign('recurProcessor', json_encode($recurProcessor));
365 $sel = &$this->addElement('hierselect',
366 'membership_type_id',
367 ts('Renewal Membership Organization and Type'), $js
370 $sel->setOptions(array($selMemTypeOrg, $selOrgMemType));
376 $this->applyFilter('__ALL__', 'trim');
378 $this->addDate('renewal_date', ts('Date Renewal Entered'), FALSE, array('formatType' => 'activityDate'));
380 $this->add('select', 'financial_type_id', ts('Financial Type'),
381 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::financialType()
383 if (CRM_Core_Permission
::access('CiviContribute') && !$this->_mode
) {
384 $this->addElement('checkbox', 'record_contribution', ts('Record Renewal Payment?'), NULL, array('onclick' => "checkPayment();"));
386 $this->add('text', 'total_amount', ts('Amount'));
387 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
389 $this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));
391 $this->add('text', 'num_terms', ts('Extend Membership by'), array('onchange' => "setPaymentBlock();"), TRUE);
392 $this->addRule('num_terms', ts('Please enter a whole number for how many periods to renew.'), 'integer');
394 $this->add('select', 'payment_instrument_id', ts('Paid By'),
395 array('' => ts('- select -')) + CRM_Contribute_PseudoConstant
::paymentInstrument(),
396 FALSE, array('onChange' => "return showHideByValue('payment_instrument_id','4','checkNumber','table-row','select',false);")
399 $this->add('text', 'trxn_id', ts('Transaction ID'));
400 $this->addRule('trxn_id', ts('Transaction ID already exists in Database.'),
401 'objectExists', array('CRM_Contribute_DAO_Contribution', $this->_id
, 'trxn_id')
404 $this->add('select', 'contribution_status_id', ts('Payment Status'),
405 CRM_Contribute_PseudoConstant
::contributionStatus()
408 $this->add('text', 'check_number', ts('Check Number'),
409 CRM_Core_DAO
::getAttribute('CRM_Contribute_DAO_Contribution', 'check_number')
413 $this->add('text', 'total_amount', ts('Amount'));
414 $this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');
416 $this->addElement('checkbox', 'send_receipt', ts('Send Confirmation and Receipt?'), NULL,
417 array('onclick' => "showHideByValue( 'send_receipt', '', 'notice', 'table-row', 'radio', false ); showHideByValue( 'send_receipt', '', 'fromEmail', 'table-row', 'radio',false);")
420 $this->add('select', 'from_email_address', ts('Receipt From'), $this->_fromEmails
);
422 $this->add('textarea', 'receipt_text_renewal', ts('Renewal Message'));
424 // Retrieve the name and email of the contact - this will be the TO for receipt email
425 list($this->_contributorDisplayName
,
426 $this->_contributorEmail
427 ) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($this->_contactID
);
428 $this->assign('email', $this->_contributorEmail
);
430 $mailingInfo = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::MAILING_PREFERENCES_NAME
,
433 $this->assign('outBound_option', $mailingInfo['outBound_option']);
435 if (CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_id
, 'contribution_recur_id')) {
436 if (CRM_Member_BAO_Membership
::isCancelSubscriptionSupported($this->_id
)) {
437 $this->assign('cancelAutoRenew',
438 CRM_Utils_System
::url('civicrm/contribute/unsubscribe', "reset=1&mid={$this->_id}")
442 $this->addFormRule(array('CRM_Member_Form_MembershipRenewal', 'formRule'));
443 if ($this->_context
!= 'standalone') {
444 //CRM-10223 - allow contribution to be recorded against different contact
445 // causes a conflict in standalone mode so skip in standalone for now
446 $this->addElement('checkbox', 'contribution_contact', ts('Record Payment from a Different Contact?'));
447 $this->addSelect('soft_credit_type_id', array('entity' => 'contribution_soft'));
448 $this->addEntityRef('soft_credit_contact_id', ts('Payment From'), array('create' => TRUE));
455 * @param array $params
456 * (ref.) an assoc array of name/value pairs.
459 * mixed true or array of errors
461 public static function formRule($params) {
463 if ($params['membership_type_id'][0] == 0) {
464 $errors['membership_type_id'] = ts('Oops. It looks like you are trying to change the membership type while renewing the membership. Please click the "change membership type" link, and select a Membership Organization.');
466 if ($params['membership_type_id'][1] == 0) {
467 $errors['membership_type_id'] = ts('Oops. It looks like you are trying to change the membership type while renewing the membership. Please click the "change membership type" link and select a Membership Type from the list.');
470 //total amount condition arise when membership type having no
472 if (isset($params['record_contribution'])) {
473 if (!$params['financial_type_id']) {
474 $errors['financial_type_id'] = ts('Please select a Financial Type.');
476 if (!$params['total_amount']) {
477 $errors['total_amount'] = ts('Please enter a Contribution Amount.');
479 if (empty($params['payment_instrument_id'])) {
480 $errors['payment_instrument_id'] = ts('Paid By is a required field.');
483 return empty($errors) ?
TRUE : $errors;
487 * Process the renewal form.
492 public function postProcess() {
495 $config = CRM_Core_Config
::singleton();
497 // get the submitted form values.
498 $this->_params
= $formValues = $this->controller
->exportValues($this->_name
);
500 $this->storeContactFields($formValues);
501 // use values from screen
503 if ($formValues['membership_type_id'][1] <> 0) {
504 $defaults['receipt_text_renewal'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
505 $formValues['membership_type_id'][1],
506 'receipt_text_renewal'
510 $now = CRM_Utils_Date
::getToday(NULL, 'YmdHis');
511 $this->convertDateFieldsToMySQL($formValues);
512 $this->assign('receive_date', $formValues['receive_date']);
514 if (!empty($this->_params
['send_receipt'])) {
515 $formValues['receipt_date'] = $now;
516 $this->assign('receipt_date', CRM_Utils_Date
::mysqlToIso($formValues['receipt_date']));
519 $formValues['receipt_date'] = NULL;
523 $formValues['total_amount'] = CRM_Utils_Array
::value('total_amount', $this->_params
, CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
524 $this->_memType
, 'minimum_fee'
526 if (empty($formValues['financial_type_id'])) {
527 $formValues['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType
, 'financial_type_id');
530 $this->_paymentProcessor
= CRM_Financial_BAO_PaymentProcessor
::getPayment($formValues['payment_processor_id'],
536 // set email for primary location.
537 $fields['email-Primary'] = 1;
538 $formValues['email-5'] = $formValues['email-Primary'] = $this->_contributorEmail
;
539 $formValues['register_date'] = $now;
541 // now set the values for the billing location.
542 foreach ($this->_fields
as $name => $dontCare) {
546 // also add location name to the array
547 $formValues["address_name-{$this->_bltID}"] = CRM_Utils_Array
::value('billing_first_name', $formValues) . ' ' . CRM_Utils_Array
::value('billing_middle_name', $formValues) . ' ' . CRM_Utils_Array
::value('billing_last_name', $formValues);
549 $formValues["address_name-{$this->_bltID}"] = trim($formValues["address_name-{$this->_bltID}"]);
551 $fields["address_name-{$this->_bltID}"] = 1;
553 $fields["email-{$this->_bltID}"] = 1;
555 $ctype = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID
, 'contact_type');
557 $nameFields = array('first_name', 'middle_name', 'last_name');
559 foreach ($nameFields as $name) {
561 if (array_key_exists("billing_$name", $formValues)) {
562 $formValues[$name] = $formValues["billing_{$name}"];
563 $formValues['preserveDBName'] = TRUE;
567 //here we are setting up the billing contact - if different from the member they are already created
568 // but they will get billing details assigned
569 CRM_Contact_BAO_Contact
::createProfileContact($formValues, $fields,
570 $this->_contributorContactID
, NULL, NULL, $ctype
573 // add all the additional payment params we need
574 $this->_params
["state_province-{$this->_bltID}"] = $this->_params
["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant
::stateProvinceAbbreviation($this->_params
["billing_state_province_id-{$this->_bltID}"]);
575 $this->_params
["country-{$this->_bltID}"] = $this->_params
["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant
::countryIsoCode($this->_params
["billing_country_id-{$this->_bltID}"]);
577 $this->_params
['year'] = CRM_Core_Payment_Form
::getCreditCardExpirationYear($this->_params
);
578 $this->_params
['month'] = CRM_Core_Payment_Form
::getCreditCardExpirationMonth($this->_params
);
579 $this->_params
['description'] = ts('Office Credit Card Membership Renewal Contribution');
580 $this->_params
['ip_address'] = CRM_Utils_System
::ipAddress();
581 $this->_params
['amount'] = $formValues['total_amount'];
582 $this->_params
['currencyID'] = $config->defaultCurrency
;
583 $this->_params
['payment_action'] = 'Sale';
584 $this->_params
['invoiceID'] = md5(uniqid(rand(), TRUE));
586 // at this point we've created a contact and stored its address etc
587 // all the payment processors expect the name and address to be in the passed params
588 // so we copy stuff over to first_name etc.
589 $paymentParams = $this->_params
;
590 if (!empty($this->_params
['send_receipt'])) {
591 $paymentParams['email'] = $this->_contributorEmail
;
594 $paymentParams['contactID'] = $this->_contributorContactID
;
596 CRM_Core_Payment_Form
::mapParams($this->_bltID
, $this->_params
, $paymentParams, TRUE);
598 $payment = CRM_Core_Payment
::singleton($this->_mode
, $this->_paymentProcessor
, $this);
600 $result = &$payment->doDirectPayment($paymentParams);
602 if (is_a($result, 'CRM_Core_Error')) {
603 CRM_Core_Error
::displaySessionError($result);
604 CRM_Utils_System
::redirect(CRM_Utils_System
::url('civicrm/contact/view/membership',
605 "reset=1&action=renew&cid={$this->_contactID}&id={$this->_id}&context=membership&mode={$this->_mode}"
610 $this->_params
= array_merge($this->_params
, $result);
612 $formValues['contribution_status_id'] = 1;
613 $formValues['invoice_id'] = $this->_params
['invoiceID'];
614 $formValues['trxn_id'] = $result['trxn_id'];
615 $formValues['payment_instrument_id'] = 1;
616 $formValues['is_test'] = ($this->_mode
== 'live') ?
0 : 1;
617 $this->set('params', $this->_params
);
618 $this->assign('trxn_id', $result['trxn_id']);
623 if ($formValues['renewal_date']) {
624 $this->set('renewalDate', CRM_Utils_Date
::processDate($formValues['renewal_date']));
626 $this->_membershipId
= $this->_id
;
628 // membership type custom data
629 $customFields = CRM_Core_BAO_CustomField
::getFields('Membership', FALSE, FALSE,
630 $formValues['membership_type_id'][1]
633 $customFields = CRM_Utils_Array
::crmArrayMerge($customFields,
634 CRM_Core_BAO_CustomField
::getFields('Membership',
640 $customFieldsFormatted = CRM_Core_BAO_CustomField
::postProcess($formValues,
646 // check for test membership.
647 $isTestMembership = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_Membership', $this->_membershipId
, 'is_test');
649 // chk for renewal for multiple terms CRM-8750
651 if (is_numeric(CRM_Utils_Array
::value('num_terms', $formValues))) {
652 $numRenewTerms = $formValues['num_terms'];
655 //if contribution status is pending then set pay later
656 if ($formValues['contribution_status_id'] == array_search('Pending', CRM_Contribute_PseudoConstant
::contributionStatus())) {
657 $this->_params
['is_pay_later'] = 1;
659 $renewMembership = CRM_Member_BAO_Membership
::renewMembershipFormWrapper($this->_contactID
,
660 $formValues['membership_type_id'][1],
661 $isTestMembership, $this, NULL, NULL,
662 $customFieldsFormatted, $numRenewTerms,
666 $endDate = CRM_Utils_Date
::processDate($renewMembership->end_date
);
668 // Retrieve the name and email of the current user - this will be the FROM for the receipt email
669 $session = CRM_Core_Session
::singleton();
670 $userID = $session->get('userID');
672 list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location
::getEmailDetails($userID);
674 $memType = CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType', $renewMembership->membership_type_id
, 'name');
676 if (!empty($formValues['record_contribution']) ||
$this->_mode
) {
678 $formValues['contribution_source'] = "{$memType} Membership: Offline membership renewal (by {$userName})";
683 CRM_Member_BAO_Membership
::createLineItems($this, $formValues['membership_type_id'], $priceSetId);
684 CRM_Price_BAO_PriceSet
::processAmount($this->_priceSet
['fields'],
685 $this->_params
, $lineItem[$priceSetId]
687 //CRM-11529 for quick config backoffice transactions
688 //when financial_type_id is passed in form, update the
689 //line items with the financial type selected in form
690 if ($submittedFinancialType = CRM_Utils_Array
::value('financial_type_id', $formValues)) {
691 foreach ($lineItem[$priceSetId] as &$li) {
692 $li['financial_type_id'] = $submittedFinancialType;
695 $formValues['total_amount'] = CRM_Utils_Array
::value('amount', $this->_params
);
696 if (!empty($lineItem)) {
697 $formValues['lineItems'] = $lineItem;
698 $formValues['processPriceSet'] = TRUE;
701 //assign contribution contact id to the field expected by recordMembershipContribution
702 if ($this->_contributorContactID
!= $this->_contactID
) {
703 $formValues['contribution_contact_id'] = $this->_contributorContactID
;
704 if (!empty($this->_params
['soft_credit_type_id'])) {
705 $formValues['soft_credit'] = array(
706 'soft_credit_type_id' => $this->_params
['soft_credit_type_id'],
707 'contact_id' => $this->_contactID
,
711 $formValues['contact_id'] = $this->_contactID
;
712 //recordMembershipContribution receives params as a reference & adds one variable. This is
713 // not a great pattern & ideally it would not receive as a reference. We assign our params as a
714 // temporary variable to avoid e-notice & to make it clear to future refactorer that
715 // this function is NOT reliant on that var being set
716 $temporaryParams = array_merge($formValues, array('membership_id' => $renewMembership->id
));
717 CRM_Member_BAO_Membership
::recordMembershipContribution($temporaryParams);
720 $receiptSend = FALSE;
721 if (!empty($formValues['send_receipt'])) {
724 $receiptFrom = $formValues['from_email_address'];
726 if (!empty($formValues['payment_instrument_id'])) {
727 $paymentInstrument = CRM_Contribute_PseudoConstant
::paymentInstrument();
728 $formValues['paidBy'] = $paymentInstrument[$formValues['payment_instrument_id']];
731 $this->_groupTree
= CRM_Core_BAO_CustomGroup
::getTree('Membership', $this, $this->_id
, FALSE, $this->_memType
);
733 // retrieve custom data
734 $customFields = $customValues = $fo = array();
735 foreach ($this->_groupTree
as $groupID => $group) {
736 if ($groupID == 'info') {
739 foreach ($group['fields'] as $k => $field) {
740 $field['title'] = $field['label'];
741 $customFields["custom_{$k}"] = $field;
744 $members = array(array('member_id', '=', $this->_membershipId
, 0, 0));
745 // check whether its a test drive
746 if ($this->_mode
== 'test') {
747 $members[] = array('member_test', '=', 1, 0, 0);
749 CRM_Core_BAO_UFGroup
::getValues($this->_contactID
, $customFields, $customValues, FALSE, $members);
751 $this->assign_by_ref('formValues', $formValues);
752 if (!empty($formValues['contribution_id'])) {
753 $this->assign('contributionID', $formValues['contribution_id']);
755 $this->assign('membershipID', $this->_id
);
756 $this->assign('contactID', $this->_contactID
);
757 $this->assign('module', 'Membership');
758 $this->assign('receiptType', 'membership renewal');
759 $this->assign('mem_start_date', CRM_Utils_Date
::customFormat($renewMembership->start_date
));
760 $this->assign('mem_end_date', CRM_Utils_Date
::customFormat($renewMembership->end_date
));
761 $this->assign('membership_name', CRM_Core_DAO
::getFieldValue('CRM_Member_DAO_MembershipType',
762 $renewMembership->membership_type_id
764 $this->assign('customValues', $customValues);
766 if (!empty($this->_params
['billing_first_name'])) {
767 $name = $this->_params
['billing_first_name'];
770 if (!empty($this->_params
['billing_middle_name'])) {
771 $name .= " {$this->_params['billing_middle_name']}";
774 if (!empty($this->_params
['billing_last_name'])) {
775 $name .= " {$this->_params['billing_last_name']}";
777 $this->assign('billingName', $name);
779 // assign the address formatted up for display
780 $addressParts = array(
781 "street_address-{$this->_bltID}",
782 "city-{$this->_bltID}",
783 "postal_code-{$this->_bltID}",
784 "state_province-{$this->_bltID}",
785 "country-{$this->_bltID}",
787 $addressFields = array();
788 foreach ($addressParts as $part) {
789 list($n, $id) = explode('-', $part);
790 if (isset($this->_params
['billing_' . $part])) {
791 $addressFields[$n] = $this->_params
['billing_' . $part];
794 $this->assign('address', CRM_Utils_Address
::format($addressFields));
795 $date = CRM_Utils_Date
::format($this->_params
['credit_card_exp_date']);
796 $date = CRM_Utils_Date
::mysqlToIso($date);
797 $this->assign('credit_card_exp_date', $date);
798 $this->assign('credit_card_number',
799 CRM_Utils_System
::mungeCreditCard($this->_params
['credit_card_number'])
801 $this->assign('credit_card_type', $this->_params
['credit_card_type']);
802 $this->assign('contributeMode', 'direct');
803 $this->assign('isAmountzero', 0);
804 $this->assign('is_pay_later', 0);
805 $this->assign('isPrimary', 1);
806 if ($this->_mode
== 'test') {
807 $this->assign('action', '1024');
811 list($mailSend, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate
::sendTemplate(
813 'groupName' => 'msg_tpl_workflow_membership',
814 'valueName' => 'membership_offline_receipt',
815 'contactId' => $this->_receiptContactId
,
816 'from' => $receiptFrom,
817 'toName' => $this->_contributorDisplayName
,
818 'toEmail' => $this->_contributorEmail
,
819 'isTest' => $this->_mode
== 'test',
824 $statusMsg = ts('%1 membership for %2 has been renewed.', array(1 => $memType, 2 => $this->_memberDisplayName
));
827 $statusMsg .= ' ' . ts('The new membership End Date is %1.', array(1 => CRM_Utils_Date
::customFormat(substr($endDate, 0, 8))));
830 if ($receiptSend && $mailSend) {
831 $statusMsg .= ' ' . ts('A renewal confirmation and receipt has been sent to %1.', array(1 => $this->_contributorEmail
));
834 CRM_Core_Session
::setStatus($statusMsg, ts('Complete'), 'success');