From ccb02c2dfb5386ee3f9361ce2b5145c31344ca30 Mon Sep 17 00:00:00 2001 From: eileenmcnaugton Date: Sat, 15 Aug 2015 19:43:53 +1200 Subject: [PATCH] fix for test regressions Note there has been traditional hodgepodge of use of paramters for the same thing causing ongoing confusion here, trying to reduce that --- CRM/Member/BAO/Membership.php | 50 +--------- CRM/Member/Form.php | 76 ++++++++++++++ CRM/Member/Form/Membership.php | 138 ++++++++++++-------------- CRM/Member/Form/MembershipRenewal.php | 7 +- 4 files changed, 144 insertions(+), 127 deletions(-) diff --git a/CRM/Member/BAO/Membership.php b/CRM/Member/BAO/Membership.php index e88fcb454b..f6d58010c4 100644 --- a/CRM/Member/BAO/Membership.php +++ b/CRM/Member/BAO/Membership.php @@ -2031,11 +2031,11 @@ INNER JOIN civicrm_contact contact ON ( contact.id = membership.contact_id AND * @param int $membershipOrg * @param int $membershipTypeID * @param float $total_amount + * @param int $priceSetId * * @return array */ - public static function getQuickConfigMembershipLineItems($membershipOrg, $membershipTypeID, $total_amount) { - $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name'); + public static function setQuickConfigMembershipParameters($membershipOrg, $membershipTypeID, $total_amount, $priceSetId) { $priceSets = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); // The name of the price field corresponds to the membership_type organization contact. @@ -2421,52 +2421,6 @@ WHERE civicrm_membership.is_test = 0"; return $contribution; } - /** - * Record line items for default membership. - * @deprecated - * - * Use getQuickConfigMembershipLineItems - * - * @param CRM_Core_Form $qf - * @param array $membershipType - * Array with membership type and organization. - * - * @return int $priceSetId - */ - public static function createLineItems(&$qf, $membershipType) { - $qf->_priceSetId = $priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_membership_type_amount', 'id', 'name'); - $qf->_priceSet = $priceSets = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSetId)); - - // The name of the price field corresponds to the membership_type organization contact. - $editedFieldParams = array( - 'price_set_id' => $priceSetId, - 'name' => $membershipType[0], - ); - $editedResults = array(); - CRM_Price_BAO_PriceField::retrieve($editedFieldParams, $editedResults); - - if (!empty($editedResults)) { - unset($qf->_priceSet['fields']); - $qf->_priceSet['fields'][$editedResults['id']] = $priceSets['fields'][$editedResults['id']]; - unset($qf->_priceSet['fields'][$editedResults['id']]['options']); - $fid = $editedResults['id']; - $editedFieldParams = array( - 'price_field_id' => $editedResults['id'], - 'membership_type_id' => $membershipType[1], - ); - $editedResults = array(); - CRM_Price_BAO_PriceFieldValue::retrieve($editedFieldParams, $editedResults); - $qf->_priceSet['fields'][$fid]['options'][$editedResults['id']] = $priceSets['fields'][$fid]['options'][$editedResults['id']]; - if (!empty($qf->_params['total_amount'])) { - $qf->_priceSet['fields'][$fid]['options'][$editedResults['id']]['amount'] = $qf->_params['total_amount']; - } - } - - $fieldID = key($qf->_priceSet['fields']); - $qf->_params['price_' . $fieldID] = CRM_Utils_Array::value('id', $editedResults); - return $priceSetId; - } - /** * @todo document me - I seem a bit out of date.... */ diff --git a/CRM/Member/Form.php b/CRM/Member/Form.php index c92e8d1d47..7a4f9e2ae8 100644 --- a/CRM/Member/Form.php +++ b/CRM/Member/Form.php @@ -346,4 +346,80 @@ class CRM_Member_Form extends CRM_Contribute_Form_AbstractEditPayment { return $returnParams; } + /** + * Ensure price parameters are set. + * + * If they are not set it means a quick config option has been chosen so we + * fill them in here to make the two flows the same. They look like 'price_2' => 2 etc. + * + * @param array $formValues + */ + protected function ensurePriceParamsAreSet(&$formValues) { + foreach ($formValues as $key => $value) { + if ((substr($key, 0, 6) == 'price_') && is_int(substr($key, 7))) { + return; + } + } + $priceFields = CRM_Member_BAO_Membership::setQuickConfigMembershipParameters( + $formValues['membership_type_id'][0], + $formValues['membership_type_id'][1], + $formValues['total_amount'], + $this->_priceSetId + ); + $formValues = array_merge($formValues, $priceFields['price_fields']); + } + + /** + * Get the details for the selected price set. + * + * @param array $params + * Parameters submitted to the form. + * + * @return array + */ + protected static function getPriceSetDetails($params) { + $priceSetID = CRM_Utils_Array::value('price_set_id', $params); + if ($priceSetID) { + return CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); + } + else { + $priceSet = CRM_Price_BAO_PriceSet::getDefaultPriceSet('membership'); + $priceSet = reset($priceSet); + return CRM_Price_BAO_PriceSet::getSetDetail($priceSet['setID']); + } + } + + /** + * Get the selected price set id. + * + * @param array $params + * Parameters submitted to the form. + * + * @return int + */ + protected static function getPriceSetID($params) { + $priceSetID = CRM_Utils_Array::value('price_set_id', $params); + if (!$priceSetID) { + $priceSetDetails = self::getPriceSetDetails($params); + return key($priceSetDetails); + } + return $priceSetID; + } + + /** + * Store parameters relating to price sets. + * + * @param array $formValues + * + * @return array + */ + protected function setPriceSetParameters($formValues) { + $this->_priceSetId = self::getPriceSetID($formValues); + $priceSetDetails = self::getPriceSetDetails($formValues); + $this->_priceSet = $priceSetDetails[$this->_priceSetId]; + // process price set and get total amount and line items. + $this->ensurePriceParamsAreSet($formValues); + return $formValues; + } + } diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index 4b16fd04ed..c66bed67ed 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -122,16 +122,16 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { public static function getSelectedMemberships($priceSet, $params) { $memTypeSelected = array(); $priceFieldIDS = self::getPriceFieldIDs($params, $priceSet); - if (!empty($priceSet) && is_array($priceFieldIDS)) { + if (isset($params['membership_type_id']) && !empty($params['membership_type_id'][1])) { + $memTypeSelected = array($params['membership_type_id'][1] => $params['membership_type_id'][1]); + } + else { foreach ($priceFieldIDS as $priceFieldId) { if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceFieldValue', $priceFieldId, 'membership_type_id')) { $memTypeSelected[$id] = $id; } } } - else { - $memTypeSelected = array($params['membership_type_id'][1] => $params['membership_type_id'][1]); - } return $memTypeSelected; } @@ -743,11 +743,12 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { public static function formRule($params, $files, $self) { $errors = array(); - $priceSetId = CRM_Utils_Array::value('price_set_id', $params); - $priceSetDetails = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId); + $priceSetId = self::getPriceSetID($params); + $priceSetDetails = self::getPriceSetDetails($params); $selectedMemberships = self::getSelectedMemberships($priceSetDetails[$priceSetId], $params); - if ($priceSetId) { + + if (!empty($params['price_set_id'])) { CRM_Price_BAO_PriceField::priceSetValidation($priceSetId, $params, $errors); $priceFieldIDS = self::getPriceFieldIDs($params, $priceSetDetails); @@ -798,7 +799,6 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { return $errors; } - if (!empty($params['record_contribution']) && empty($params['payment_instrument_id'])) { $errors['payment_instrument_id'] = ts('Payment Method is a required field.'); } @@ -1143,7 +1143,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $membershipType = NULL; $mailSend = FALSE; - $priceSetID = CRM_Utils_Array::value('price_set_id', $formValues); + $formValues = $this->setPriceSetParameters($formValues); $params = $softParams = $ids = array(); @@ -1159,13 +1159,13 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { // In form mode these are set in preProcess. //TODO: set memberships, fixme $this->setContextVariables($formValues); - $priceSetDetails = CRM_Price_BAO_PriceSet::getSetDetail($priceSetID); + $this->_memTypeSelected = self::getSelectedMemberships( - $priceSetDetails[$priceSetID], + $this->_priceSet, $formValues ); if (empty($formValues['financial_type_id'])) { - $formValues['financial_type_id'] = $priceSetDetails[$priceSetID]['financial_type_id']; + $formValues['financial_type_id'] = $this->_priceSet['financial_type_id']; } $config = CRM_Core_Config::singleton(); @@ -1179,7 +1179,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { //take the required membership recur values. if ($this->_mode && !empty($formValues['auto_renew'])) { - $params['is_recur'] = $this->_params['is_recur'] = $formValues['is_recur'] = TRUE; + $params['is_recur'] = $formValues['is_recur'] = TRUE; $mapping = array( 'frequency_interval' => 'duration_interval', 'frequency_unit' => 'duration_unit', @@ -1195,7 +1195,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $recurMembershipTypeValues ); if (!$count) { - $this->_params[$mapVal] = $formValues[$mapVal] = CRM_Utils_Array::value($mapParam, + $formValues[$mapVal] = CRM_Utils_Array::value($mapParam, $recurMembershipTypeValues ); } @@ -1204,30 +1204,22 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } } - // process price set and get total amount and line items. - if (!$priceSetID) { - $priceSetID = CRM_Member_BAO_Membership::createLineItems( - $this, - $formValues['membership_type_id'], - NULL - ); - } - $isQuickConfig = FALSE; - if (CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $priceSetID, 'is_quick_config')) { - $isQuickConfig = 1; - } + $isQuickConfig = $this->_priceSet['is_quick_config']; + $termsByType = array(); - $lineItem = array(); + $lineItem = array($this->_priceSetId => array()); + CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], - $this->_params, $lineItem[$priceSetID]); - if (CRM_Utils_Array::value('tax_amount', $this->_params)) { - $params['tax_amount'] = $this->_params['tax_amount']; + $formValues, $lineItem[$this->_priceSetId]); + + if (CRM_Utils_Array::value('tax_amount', $formValues)) { + $params['tax_amount'] = $formValues['tax_amount']; } - $params['total_amount'] = CRM_Utils_Array::value('amount', $this->_params); + $params['total_amount'] = CRM_Utils_Array::value('amount', $formValues); $submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $formValues); - if (!empty($lineItem[$priceSetID])) { - foreach ($lineItem[$priceSetID] as &$li) { + if (!empty($lineItem[$this->_priceSetId])) { + foreach ($lineItem[$this->_priceSetId] as &$li) { if (!empty($li['membership_type_id'])) { if (!empty($li['membership_num_terms'])) { $termsByType[$li['membership_type_id']] = $li['membership_num_terms']; @@ -1321,8 +1313,8 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { //CRM-13981, allow different person as a soft-contributor of chosen type if ($this->_contributorContactID != $this->_contactID) { $params['contribution_contact_id'] = $this->_contributorContactID; - if (!empty($this->_params['soft_credit_type_id'])) { - $softParams['soft_credit_type_id'] = $this->_params['soft_credit_type_id']; + if (!empty($formValues['soft_credit_type_id'])) { + $softParams['soft_credit_type_id'] = $formValues['soft_credit_type_id']; $softParams['contact_id'] = $this->_contactID; } } @@ -1380,19 +1372,11 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } $createdMemberships = array(); if ($this->_mode) { - if (empty($formValues['total_amount']) && !$priceSetID) { - // if total amount not provided minimum for membership type is used - $params['total_amount'] = $formValues['total_amount'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', - $formValues['membership_type_id'][1], 'minimum_fee' - ); - } - else { - $params['total_amount'] = CRM_Utils_Array::value('total_amount', $formValues, 0); - } + $params['total_amount'] = CRM_Utils_Array::value('total_amount', $formValues, 0); - if ($priceSetID && !$isQuickConfig) { + if (!$isQuickConfig) { $params['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', - $priceSetID, + $this->_priceSetId, 'financial_type_id' ); } @@ -1405,7 +1389,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { ); //get the payment processor id as per mode. - $params['payment_processor_id'] = $this->_params['payment_processor_id'] = $formValues['payment_processor_id'] = $this->_paymentProcessor['id']; + $params['payment_processor_id'] = $formValues['payment_processor_id'] = $this->_paymentProcessor['id']; $now = date('YmdHis'); $fields = array(); @@ -1449,38 +1433,37 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } // add all the additional payment params we need - $this->_params["state_province-{$this->_bltID}"] = $this->_params["billing_state_province-{$this->_bltID}"] + $formValues["state_province-{$this->_bltID}"] = $formValues["billing_state_province-{$this->_bltID}"] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($formValues["billing_state_province_id-{$this->_bltID}"]); - $this->_params["country-{$this->_bltID}"] = $this->_params["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($formValues["billing_country_id-{$this->_bltID}"]); + $formValues["country-{$this->_bltID}"] = $formValues["billing_country-{$this->_bltID}"] = CRM_Core_PseudoConstant::countryIsoCode($formValues["billing_country_id-{$this->_bltID}"]); - $this->_params['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($formValues); - $this->_params['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($formValues); - $this->_params['ip_address'] = CRM_Utils_System::ipAddress(); - $this->_params['amount'] = $params['total_amount']; - $this->_params['currencyID'] = $config->defaultCurrency; - $this->_params['description'] = ts("Contribution submitted by a staff person using member's credit card for signup"); - $this->_params['invoiceID'] = md5(uniqid(rand(), TRUE)); - $this->_params['financial_type_id'] = $params['financial_type_id']; + $formValues['year'] = CRM_Core_Payment_Form::getCreditCardExpirationYear($formValues); + $formValues['month'] = CRM_Core_Payment_Form::getCreditCardExpirationMonth($formValues); + $formValues['ip_address'] = CRM_Utils_System::ipAddress(); + $formValues['amount'] = $params['total_amount']; + $formValues['currencyID'] = $config->defaultCurrency; + $formValues['description'] = ts("Contribution submitted by a staff person using member's credit card for signup"); + $formValues['invoiceID'] = md5(uniqid(rand(), TRUE)); + $formValues['financial_type_id'] = $params['financial_type_id']; // at this point we've created a contact and stored its address etc // all the payment processors expect the name and address to be in the // so we copy stuff over to first_name etc. - $paymentParams = $this->_params; + $paymentParams = $formValues; $paymentParams['contactID'] = $this->_contributorContactID; //CRM-10377 if payment is by an alternate contact then we need to set that person // as the contact in the payment params if ($this->_contributorContactID != $this->_contactID) { - if (!empty($this->_params['soft_credit_type_id'])) { + if (!empty($formValues['soft_credit_type_id'])) { $softParams['contact_id'] = $params['contact_id']; - $softParams['soft_credit_type_id'] = $this->_params['soft_credit_type_id']; + $softParams['soft_credit_type_id'] = $formValues['soft_credit_type_id']; } } - if (!empty($this->_params['send_receipt'])) { + if (!empty($formValues['send_receipt'])) { $paymentParams['email'] = $this->_contributorEmail; } - CRM_Core_Payment_Form::mapParams($this->_bltID, $this->_params, $paymentParams, TRUE); - + CRM_Core_Payment_Form::mapParams($this->_bltID, $formValues, $paymentParams, TRUE); // CRM-7137 -for recurring membership, // we do need contribution and recurring records. $result = NULL; @@ -1488,7 +1471,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $financialType = new CRM_Financial_DAO_FinancialType(); $financialType->id = $params['financial_type_id']; $financialType->find(TRUE); - + $this->_params = $formValues; $contribution = CRM_Contribute_Form_Contribution_Confirm::processFormContribution($this, $paymentParams, NULL, @@ -1497,7 +1480,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { 'line_item' => $lineItem, 'is_test' => $isTest, 'campaign_id' => CRM_Utils_Array::value('campaign_id', $paymentParams), - 'contribution_page_id' => CRM_Utils_Array::value('contribution_page_id', $this->_params), + 'contribution_page_id' => CRM_Utils_Array::value('contribution_page_id', $formValues), 'source' => CRM_Utils_Array::value('source', $paymentParams, CRM_Utils_Array::value('description', $paymentParams)), 'thankyou_date' => CRM_Utils_Array::value('thankyou_date', $paymentParams), 'payment_instrument_id' => $this->_paymentProcessor['payment_instrument_id'], @@ -1529,7 +1512,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $payment = $this->_paymentProcessor['object']; try { $result = $payment->doPayment($paymentParams); - $this->_params = array_merge($this->_params, $result); + $formValues = array_merge($formValues, $result); // Assign amount to template if payment was successful. $this->assign('amount', $params['total_amount']); } @@ -1550,11 +1533,11 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } } - if ($this->_params['payment_status_id'] != array_search('Completed', $allContributionStatus)) { + if ($formValues['payment_status_id'] != array_search('Completed', $allContributionStatus)) { $params['status_id'] = array_search('Pending', $allMemberStatus); $params['skipStatusCal'] = TRUE; // unset send-receipt option, since receipt will be sent when ipn is received. - unset($this->_params['send_receipt'], $formValues['send_receipt']); + unset($formValues['send_receipt'], $formValues['send_receipt']); //as membership is pending set dates to null. $memberDates = array( 'join_date' => 'joinDate', @@ -1569,7 +1552,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } } $params['receive_date'] = $now; - $params['invoice_id'] = $this->_params['invoiceID']; + $params['invoice_id'] = $formValues['invoiceID']; $params['contribution_source'] = ts('%1 Membership Signup: Credit card or direct debit (by %2)', array(1 => $membershipType, 2 => $userName) ); @@ -1577,14 +1560,14 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { $params['trxn_id'] = CRM_Utils_Array::value('trxn_id', $result); $params['payment_instrument_id'] = 1; $params['is_test'] = ($this->_mode == 'live') ? 0 : 1; - if (!empty($this->_params['send_receipt'])) { + if (!empty($formValues['send_receipt'])) { $params['receipt_date'] = $now; } else { $params['receipt_date'] = NULL; } - $this->set('params', $this->_params); + $this->set('params', $formValues); $this->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $result)); $this->assign('receive_date', CRM_Utils_Date::mysqlToIso($params['receive_date']) @@ -1614,7 +1597,12 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { // process - // @see http://wiki.civicrm.org/confluence/pages/viewpage.action?pageId=261062657#Payments&AccountsRoadmap-Movetowardsalwaysusinga2-steppaymentprocess $membershipParams['contribution_status_id'] = CRM_Utils_Array::value('payment_status_id', $result); - unset($membershipParams['lineItems']); + if (!empty($paymentParams['is_recur'])) { + // The earlier process created the line items (although we want to get rid of the earlier one in favour + // of a single path! + unset($membershipParams['lineItems']); + } + $membership = CRM_Member_BAO_Membership::create($membershipParams, $ids); $params['contribution'] = CRM_Utils_Array::value('contribution', $membershipParams); unset($params['lineItems']); @@ -1719,12 +1707,12 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } } - if (!empty($lineItem[$priceSetID])) { + if (!empty($lineItem[$this->_priceSetId])) { $invoiceSettings = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::CONTRIBUTE_PREFERENCES_NAME, 'contribution_invoice_settings'); $invoicing = CRM_Utils_Array::value('invoicing', $invoiceSettings); $taxAmount = FALSE; $totalTaxAmount = 0; - foreach ($lineItem[$priceSetID] as & $priceFieldOp) { + foreach ($lineItem[$this->_priceSetId] as & $priceFieldOp) { if (!empty($priceFieldOp['membership_type_id'])) { $priceFieldOp['start_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['start_date'], '%B %E%f, %Y') : '-'; $priceFieldOp['end_date'] = $membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'] ? CRM_Utils_Date::customFormat($membershipTypeValues[$priceFieldOp['membership_type_id']]['end_date'], '%B %E%f, %Y') : '-'; @@ -1739,7 +1727,7 @@ class CRM_Member_Form_Membership extends CRM_Member_Form { } if ($invoicing) { $dataArray = array(); - foreach ($lineItem[$priceSetID] as $key => $value) { + foreach ($lineItem[$this->_priceSetId] as $key => $value) { if (isset($value['tax_amount']) && isset($value['tax_rate'])) { if (isset($dataArray[$value['tax_rate']])) { $dataArray[$value['tax_rate']] = $dataArray[$value['tax_rate']] + CRM_Utils_Array::value('tax_amount', $value); diff --git a/CRM/Member/Form/MembershipRenewal.php b/CRM/Member/Form/MembershipRenewal.php index 8c63c91165..653fc69d22 100644 --- a/CRM/Member/Form/MembershipRenewal.php +++ b/CRM/Member/Form/MembershipRenewal.php @@ -615,16 +615,15 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form { //create line items $lineItem = array(); - - $priceSetId = CRM_Member_BAO_Membership::createLineItems($this, $this->_params['membership_type_id']); + $this->_params = $this->setPriceSetParameters($this->_params); CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'], - $this->_params, $lineItem[$priceSetId] + $this->_params, $lineItem[$this->_priceSetId] ); //CRM-11529 for quick config backoffice transactions //when financial_type_id is passed in form, update the //line items with the financial type selected in form if ($submittedFinancialType = CRM_Utils_Array::value('financial_type_id', $this->_params)) { - foreach ($lineItem[$priceSetId] as &$li) { + foreach ($lineItem[$this->_priceSetId] as &$li) { $li['financial_type_id'] = $submittedFinancialType; } } -- 2.25.1