From: eileenmcnaugton <eileen@fuzion.co.nz> Date: Tue, 25 Aug 2015 22:58:15 +0000 (+1200) Subject: CRM-17089 Consolidate billing block address field processing on membership forms X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=09108d7d42cd08f21b92da2ff28bf4ad342bd765;p=civicrm-core.git CRM-17089 Consolidate billing block address field processing on membership forms Currenlty the membership form is not saving correctly & giving enotices. Am modifiying to be using the same code for both forms as part of this fix. Conflicts: CRM/Member/Form/Membership.php CRM-17089 consolidated billing name assignment between the forms CRM-17089 fix & consolidate saving of billing fields --- diff --git a/CRM/Contact/Form/ProfileContact.php b/CRM/Contact/Form/ProfileContact.php index e96af55b07..79bd68b52b 100644 --- a/CRM/Contact/Form/ProfileContact.php +++ b/CRM/Contact/Form/ProfileContact.php @@ -108,9 +108,9 @@ class CRM_Contact_Form_ProfileContact { /** * @param $form + * @param array $params Parameters from the form. */ - public static function postProcess($form) { - $params = $form->_params; + public static function postProcess($form, $params) { if (!empty($form->_honor_block_is_active) && !empty($params['soft_credit_type_id'])) { $honorId = NULL; diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index c705eeb568..8f28e5989b 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -832,13 +832,10 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $contactID = $contributionParams['contact_id']; $isEmailReceipt = !empty($form->_values['is_email_receipt']); - // How do these vary from params? These are currently passed to - // - custom data function.... - $formParams = $form->_params; - $isSeparateMembershipPayment = empty($formParams['separate_membership_payment']) ? FALSE : TRUE; - $pledgeID = empty($formParams['pledge_id']) ? NULL : $formParams['pledge_id']; + $isSeparateMembershipPayment = empty($params['separate_membership_payment']) ? FALSE : TRUE; + $pledgeID = empty($params['pledge_id']) ? NULL : $params['pledge_id']; if (!$isSeparateMembershipPayment && !empty($form->_values['pledge_block_id']) && - (!empty($formParams['is_pledge']) || $pledgeID)) { + (!empty($params['is_pledge']) || $pledgeID)) { $isPledge = TRUE; } else { @@ -927,7 +924,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } //CRM-13981, processing honor contact into soft-credit contribution - CRM_Contact_Form_ProfileContact::postProcess($form); + CRM_Contact_Form_ProfileContact::postProcess($form, $params); // process soft credit / pcp pages CRM_Contribute_Form_Contribution_Confirm::processPcpSoft($params, $contribution); @@ -937,7 +934,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr if ($pledgeID) { //when user doing pledge payments. //update the schedule when payment(s) are made - foreach ($form->_params['pledge_amount'] as $paymentId => $dontCare) { + foreach ($params['pledge_amount'] as $paymentId => $dontCare) { $scheduledAmount = CRM_Core_DAO::getFieldValue( 'CRM_Pledge_DAO_PledgePayment', $paymentId, @@ -1010,7 +1007,7 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr } if ($online && $contribution) { - CRM_Core_BAO_CustomValueTable::postProcess($form->_params, + CRM_Core_BAO_CustomValueTable::postProcess($params, 'civicrm_contribution', $contribution->id, 'Contribution' diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index 627fd5c64c..282b4a3a8b 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -84,6 +84,13 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { */ public $_priceSet; + /** + * Values submitted to the form, processed along the way. + * + * @var array + */ + protected $_params = array(); + public function preProcess() { // Check for edit permission. if (!CRM_Core_Permission::checkActionPermission('CiviMember', $this->_action)) { @@ -429,4 +436,81 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { return $formValues; } + /** + * Add the billing address to the contact who paid. + */ + protected function processBillingAddress() { + $fields = array(); + + // set email for primary location. + $fields['email-Primary'] = 1; + $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail; + // now set the values for the billing location. + foreach (array_keys($this->_fields) as $name) { + $fields[$name] = 1; + } + + // also add location name to the array + $this->_params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); + $this->_params["address_name-{$this->_bltID}"] = trim($this->_params["address_name-{$this->_bltID}"]); + + $fields["address_name-{$this->_bltID}"] = 1; + + //ensure we don't over-write the payer's email with the member's email + if ($this->_contributorContactID == $this->_contactID) { + $fields["email-{$this->_bltID}"] = 1; + } + + list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); + $nameFields = array('first_name', 'middle_name', 'last_name'); + + foreach ($nameFields as $name) { + $fields[$name] = 1; + if (array_key_exists("billing_$name", $this->_params)) { + $this->_params[$name] = $this->_params["billing_{$name}"]; + $this->_params['preserveDBName'] = TRUE; + } + } + + if ($hasBillingField) { + $addressParams = array_merge($this->_params, $addressParams); + //here we are setting up the billing contact - if different from the member they are already created + // but they will get billing details assigned + CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields, + $this->_contributorContactID, NULL, NULL, + CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') + ); + } + } + + /** + * Assign billing name to the template. + */ + protected function assignBillingName() { + $name = ''; + if (!empty($this->_params['billing_first_name'])) { + $name = $this->_params['billing_first_name']; + } + + if (!empty($this->_params['billing_middle_name'])) { + $name .= " {$this->_params['billing_middle_name']}"; + } + + if (!empty($this->_params['billing_last_name'])) { + $name .= " {$this->_params['billing_last_name']}"; + } + $this->assign('billingName', $name); + } + + /** + * Wrapper function for unit tests. + * + * @param array $formValues + */ + public function testSubmit($formValues) { + $this->_memType = $formValues['membership_type_id'][1]; + $this->_params = $formValues; + $this->submit(); + } + } diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 65a439deac..9cc6afff2e 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -951,7 +951,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { // get the submitted form values. $this->_params = $this->controller->exportValues($this->_name); - $this->submit($this->_params); + $this->submit(); $this->setUserContext(); } @@ -1003,21 +1003,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $form->assign('customValues', $customValues); if ($form->_mode) { - $name = ''; - if (!empty($form->_params['billing_first_name'])) { - $name = $form->_params['billing_first_name']; - } - - if (!empty($form->_params['billing_middle_name'])) { - $name .= " {$form->_params['billing_middle_name']}"; - } - - if (!empty($form->_params['billing_last_name'])) { - $name .= " {$form->_params['billing_last_name']}"; - } - - $form->assign('billingName', $name); - // assign the address formatted up for display $addressParts = array( "street_address-{$form->_bltID}", @@ -1130,24 +1115,23 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { * * This is also accessed by unit tests. * - * @param array $formValues - * * @return array */ - public function submit($formValues) { + public function submit() { $isTest = ($this->_mode == 'test') ? 1 : 0; - + $this->storeContactFields($this->_params); + $formValues = $this->_params; $joinDate = $startDate = $endDate = NULL; $membershipTypes = $membership = $calcDate = array(); $membershipType = NULL; $mailSend = FALSE; $formValues = $this->setPriceSetParameters($formValues); - $params = $softParams = $ids = array(); $allMemberStatus = CRM_Member_PseudoConstant::membershipStatus(); $allContributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(); + $this->processBillingAddress(); if ($this->_id) { $ids['membership'] = $params['id'] = $this->_id; @@ -1234,8 +1218,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } } - $this->storeContactFields($formValues); - $params['contact_id'] = $this->_contactID; $fields = array( @@ -1389,47 +1371,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { //get the payment processor id as per mode. $params['payment_processor_id'] = $formValues['payment_processor_id'] = $this->_paymentProcessor['id']; - - $now = date('YmdHis'); - $fields = array(); - - // set email for primary location. - $fields['email-Primary'] = 1; - $formValues['email-5'] = $formValues['email-Primary'] = $this->_memberEmail; - $params['register_date'] = $now; - - // now set the values for the billing location. - foreach ($this->_fields as $name => $dontCare) { - $fields[$name] = 1; - } - - // also add location name to the array - $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); - - $formValues["address_name-{$this->_bltID}"] = trim($formValues["address_name-{$this->_bltID}"]); - - $fields["address_name-{$this->_bltID}"] = 1; - //ensure we don't over-write the payer's email with the member's email - if ($this->_contributorContactID == $this->_contactID) { - $fields["email-{$this->_bltID}"] = 1; - } - - $nameFields = array('first_name', 'middle_name', 'last_name'); - - foreach ($nameFields as $name) { - $fields[$name] = 1; - if (array_key_exists("billing_$name", $formValues)) { - $formValues[$name] = $formValues["billing_{$name}"]; - $formValues['preserveDBName'] = TRUE; - } - } - if ($this->_contributorContactID == $this->_contactID) { - //see CRM-12869 for discussion of why we don't do this for separate payee payments - CRM_Contact_BAO_Contact::createProfileContact($formValues, $fields, - $this->_contributorContactID, NULL, NULL, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') - ); - } + $params['register_date'] = date('YmdHis'); // add all the additional payment params we need $formValues["state_province-{$this->_bltID}"] = $formValues["billing_state_province-{$this->_bltID}"] @@ -1549,6 +1491,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } } } + $now = date('YmdHis'); $params['receive_date'] = $now; $params['invoice_id'] = $formValues['invoiceID']; $params['contribution_source'] = ts('%1 Membership Signup: Credit card or direct debit (by %2)', @@ -1763,6 +1706,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { // although at some point we should switch in the templates. $formValues['receipt_text_signup'] = $formValues['receipt_text']; // send email receipt + $this->assignBillingName(); $mailSend = self::emailReceipt($this, $formValues, $membership); } diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 224a3df3e0..178dd1df61 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -29,8 +29,6 @@ * * @package CRM * @copyright CiviCRM LLC (c) 2004-2015 - * $Id$ - * */ /** @@ -462,9 +460,6 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { /** * Process the renewal form. - * - * - * @return void */ public function postProcess() { // get the submitted form values. @@ -510,7 +505,7 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { $now = CRM_Utils_Date::getToday(NULL, 'YmdHis'); $this->convertDateFieldsToMySQL($this->_params); $this->assign('receive_date', $this->_params['receive_date']); - $this->processBillingAddress($now); + $this->processBillingAddress(); list($userName) = CRM_Contact_BAO_Contact_Location::getEmailDetails(CRM_Core_Session::singleton()->get('userID')); $this->_params['total_amount'] = CRM_Utils_Array::value('total_amount', $this->_params, CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $this->_memType, 'minimum_fee') @@ -748,63 +743,4 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { } } - /** - * Wrapper function for unit tests. - * - * @param array $formValues - */ - public function testSubmit($formValues) { - $this->_memType = $formValues['membership_type_id'][1]; - $this->_params = $formValues; - $this->submit($formValues); - } - - protected function assignBillingName() { - $name = ''; - if (!empty($this->_params['billing_first_name'])) { - $name = $this->_params['billing_first_name']; - } - - if (!empty($this->_params['billing_middle_name'])) { - $name .= " {$this->_params['billing_middle_name']}"; - } - - if (!empty($this->_params['billing_last_name'])) { - $name .= " {$this->_params['billing_last_name']}"; - } - $this->assign('billingName', $name); - } - - /** - * Add the billing address to the contact who paid. - */ - protected function processBillingAddress() { - $fields = array(); - - // set email for primary location. - $fields['email-Primary'] = 1; - $this->_params['email-5'] = $this->_params['email-Primary'] = $this->_contributorEmail; - - // also add location name to the array - $this->_params["address_name-{$this->_bltID}"] = CRM_Utils_Array::value('billing_first_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_middle_name', $this->_params) . ' ' . CRM_Utils_Array::value('billing_last_name', $this->_params); - - $this->_params["address_name-{$this->_bltID}"] = trim($this->_params["address_name-{$this->_bltID}"]); - - $fields["address_name-{$this->_bltID}"] = 1; - $fields["email-{$this->_bltID}"] = 1; - - list($hasBillingField, $addressParams) = CRM_Contribute_BAO_Contribution::getPaymentProcessorReadyAddressParams($this->_params, $this->_bltID); - - $addressParams['preserveDBName'] = TRUE; - if ($hasBillingField) { - $addressParams = array_merge($this->_params, $addressParams); - //here we are setting up the billing contact - if different from the member they are already created - // but they will get billing details assigned - CRM_Contact_BAO_Contact::createProfileContact($addressParams, $fields, - $this->_contributorContactID, NULL, NULL, - CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactID, 'contact_type') - ); - } - } - } diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index 3c88f64515..91862b1d85 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -468,7 +468,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { 'billing_postal_code-5' => '90210', 'billing_country_id-5' => '1228', ); - $form->submit($params); + $form->_contactID = $this->_individualId; + $form->testSubmit($params); $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId)); $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 0); $contribution = $this->callAPISuccess('Contribution', 'get', array( @@ -499,8 +500,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { $this->createLoggedInUser(); $params = $this->getBaseSubmitParams(); $form->_mode = 'test'; - - $form->submit($params); + $form->_contactID = $this->_individualId; + $form->testSubmit($params); $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId)); $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1); @@ -517,6 +518,65 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { ), 1); } + /** + * Test the submit function of the membership form. + */ + public function testSubmitPayLaterWithBilling() { + $form = $this->getForm(NULL); + $this->createLoggedInUser(); + $params = array( + 'cid' => $this->_individualId, + 'join_date' => date('m/d/Y', time()), + 'start_date' => '', + 'end_date' => '', + // This format reflects the 23 being the organisation & the 25 being the type. + 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID), + 'auto_renew' => '0', + 'max_related' => '', + 'num_terms' => '2', + 'source' => '', + 'total_amount' => '50.00', + //Member dues, see data.xml + 'financial_type_id' => '2', + 'soft_credit_type_id' => '', + 'soft_credit_contact_id' => '', + 'payment_instrument_id' => 4, + 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>', + 'receipt_text_signup' => 'Thank you text', + 'payment_processor_id' => $this->_paymentProcessorID, + 'record_contribution' => TRUE, + 'trxn_id' => 777, + 'contribution_status_id' => 2, + 'billing_first_name' => 'Test', + 'billing_middlename' => 'Last', + 'billing_street_address-5' => '10 Test St', + 'billing_city-5' => 'Test', + 'billing_state_province_id-5' => '1003', + 'billing_postal_code-5' => '90210', + 'billing_country_id-5' => '1228', + ); + $form->_contactID = $this->_individualId; + + $form->testSubmit($params); + $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId)); + $contribution = $this->callAPISuccessGetSingle('Contribution', array( + 'contact_id' => $this->_individualId, + 'contribution_status_id' => 2, + )); + $this->assertEquals($contribution['trxn_id'], 777); + + $this->callAPISuccessGetCount('LineItem', array( + 'entity_id' => $membership['id'], + 'entity_table' => 'civicrm_membership', + 'contribution_id' => $contribution['id'], + ), 1); + $this->callAPISuccessGetSingle('address', array( + 'contact_id' => $this->_individualId, + 'street_address' => '10 Test St', + 'postal_code' => 90210, + )); + } + /** * Test the submit function of the membership form. */ @@ -535,8 +595,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase { $this->createLoggedInUser(); $params = $this->getBaseSubmitParams(); $form->_mode = 'test'; - - $form->submit($params); + $form->_contactID = $this->_individualId; + $form->testSubmit($params); $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId)); $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1);