Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REF] Move wrangling of Front end form contribution param for autoRenew back to form #15927

Merged
merged 1 commit into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -1380,7 +1380,7 @@ protected function submit($submittedValues, $action, $pledgePaymentID) {
// as a point of fragility rather than a logical 'if' clause.
if ($priceSetId) {
CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
$submittedValues, $lineItem[$priceSetId], NULL, $priceSetId);
$submittedValues, $lineItem[$priceSetId], $priceSetId);
// Unset tax amount for offline 'is_quick_config' contribution.
// @todo WHY - quick config was conceived as a quick way to configure contribution forms.
// this is an example of 'other' functionality being hung off it.
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contribute/Form/Contribution/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -1967,7 +1967,7 @@ public static function submit($params) {
}
$priceFields = $priceFields[$priceSetID]['fields'];
$lineItems = [];
CRM_Price_BAO_PriceSet::processAmount($priceFields, $paramsProcessedForForm, $lineItems, 'civicrm_contribution', $priceSetID);
$form->processAmountAndGetAutoRenew($priceFields, $paramsProcessedForForm, $lineItems, $priceSetID);
$form->_lineItem = [$priceSetID => $lineItems];
$membershipPriceFieldIDs = [];
foreach ((array) $lineItems as $lineItem) {
Expand Down
8 changes: 5 additions & 3 deletions CRM/Contribute/Form/Contribution/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -1124,12 +1124,14 @@ public function submit($params) {
}
}
}
$component = '';

if ($this->_membershipBlock) {
$component = 'membership';
$this->processAmountAndGetAutoRenew($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId);
}
else {
CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $priceSetId);
}

CRM_Price_BAO_PriceSet::processAmount($this->_values['fee'], $params, $lineItem[$priceSetId], $component, $priceSetId);
if ($params['tax_amount']) {
$this->set('tax_amount', $params['tax_amount']);
}
Expand Down
29 changes: 29 additions & 0 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,4 +1403,33 @@ protected function getMainContributionAmount($params) {
return $params['amount'] ?? 0;
}

/**
* Wrapper for processAmount that also sets autorenew.
*
* @param $fields
* This is the output of the function CRM_Price_BAO_PriceSet::getSetDetail($priceSetID, FALSE, FALSE);
* And, it would make sense to introduce caching into that function and call it from here rather than
* require the $fields array which is passed from pillar to post around the form in order to pass it in here.
* @param array $params
* Params reflecting form input e.g with fields 'price_5' => 7, 'price_8' => array(7, 8)
* @param $lineItems
* Line item array to be altered.
* @param int $priceSetID
*/
public function processAmountAndGetAutoRenew($fields, &$params, &$lineItems, $priceSetID = NULL) {
CRM_Price_BAO_PriceSet::processAmount($fields, $params, $lineItems, $priceSetID);
$autoRenew = [];
$autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0;
foreach ($lineItems as $lineItem) {
if (!empty($lineItem['auto_renew']) &&
is_numeric($lineItem['auto_renew'])
) {
$autoRenew[$lineItem['auto_renew']] += $lineItem['line_total'];
}
}
if (count($autoRenew) > 1) {
$params['autoRenew'] = $autoRenew;
}
}

}
3 changes: 3 additions & 0 deletions CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ public static function formRule($fields, $files, $self) {

/**
* Process the form submission.
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
*/
public function postProcess() {
$now = date('YmdHis');
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -1153,7 +1153,7 @@ public function submit() {
// END Fix for dev/core/issues/860

CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
$formValues, $lineItem[$this->_priceSetId], NULL, $this->_priceSetId);
$formValues, $lineItem[$this->_priceSetId], $this->_priceSetId);

if (!empty($formValues['tax_amount'])) {
$params['tax_amount'] = $formValues['tax_amount'];
Expand Down
2 changes: 1 addition & 1 deletion CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ protected function submit() {
$lineItem = [];
$this->_params = $this->setPriceSetParameters($this->_params);
CRM_Price_BAO_PriceSet::processAmount($this->_priceSet['fields'],
$this->_params, $lineItem[$this->_priceSetId], NULL, $this->_priceSetId
$this->_params, $lineItem[$this->_priceSetId], $this->_priceSetId
);
//CRM-11529 for quick config backoffice transactions
//when financial_type_id is passed in form, update the
Expand Down
42 changes: 1 addition & 41 deletions CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,12 +660,9 @@ public static function initSet(&$form, $entityTable = 'civicrm_event', $validOnl
* Params reflecting form input e.g with fields 'price_5' => 7, 'price_8' => array(7, 8)
* @param $lineItem
* Line item array to be altered.
* @param string $component
* This parameter appears to only be relevant to determining whether memberships should be auto-renewed.
* (and is effectively a boolean for 'is_membership' which could be calculated from the line items.)
* @param int $priceSetID
*/
public static function processAmount($fields, &$params, &$lineItem, $component = '', $priceSetID = NULL) {
public static function processAmount($fields, &$params, &$lineItem, $priceSetID = NULL) {
// using price set
$totalPrice = $totalTax = 0;
// CRM-18701 Sometimes the amount in the price set is overridden by the amount on the form.
Expand All @@ -675,10 +672,6 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
// set up (which allows a free form field).
$amount_override = NULL;

if ($component) {
$autoRenew = [];
$autoRenew[0] = $autoRenew[1] = $autoRenew[2] = 0;
}
if ($priceSetID) {
$priceFields = self::filterPriceFieldsFromParams($priceSetID, $params);
if (count($priceFields) == 1) {
Expand Down Expand Up @@ -730,15 +723,6 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
}
}
$totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]);
if (
$component &&
// auto_renew exists and is empty in some workflows, which php treat as a 0
// and hence we explicitly check to see if auto_renew is numeric
isset($lineItem[$optionValueId]['auto_renew']) &&
is_numeric($lineItem[$optionValueId]['auto_renew'])
) {
$autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total'];
}
break;

case 'Select':
Expand All @@ -750,13 +734,6 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
$lineItem = self::setLineItem($field, $lineItem, $optionValueId, $totalTax);
}
$totalPrice += $lineItem[$optionValueId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionValueId]);
if (
$component &&
isset($lineItem[$optionValueId]['auto_renew']) &&
is_numeric($lineItem[$optionValueId]['auto_renew'])
) {
$autoRenew[$lineItem[$optionValueId]['auto_renew']] += $lineItem[$optionValueId]['line_total'];
}
break;

case 'CheckBox':
Expand All @@ -767,13 +744,6 @@ public static function processAmount($fields, &$params, &$lineItem, $component =
$lineItem = self::setLineItem($field, $lineItem, $optionId, $totalTax);
}
$totalPrice += $lineItem[$optionId]['line_total'] + CRM_Utils_Array::value('tax_amount', $lineItem[$optionId]);
if (
$component &&
isset($lineItem[$optionId]['auto_renew']) &&
is_numeric($lineItem[$optionId]['auto_renew'])
) {
$autoRenew[$lineItem[$optionId]['auto_renew']] += $lineItem[$optionId]['line_total'];
}
}
break;
}
Expand Down Expand Up @@ -819,16 +789,6 @@ public static function processAmount($fields, &$params, &$lineItem, $component =

$params['amount'] = $totalPrice;
$params['tax_amount'] = $totalTax;
if ($component) {
foreach ($autoRenew as $dontCare => $eachAmount) {
if (!$eachAmount) {
unset($autoRenew[$dontCare]);
}
}
if (count($autoRenew) > 1) {
$params['autoRenew'] = $autoRenew;
}
}
}

/**
Expand Down