From 07dd658b402838cd0779d0ac7acae1a00d04dfca Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 3 May 2015 23:53:39 +1200 Subject: [PATCH] CRM-16367 (Backoffice Contribution Form) extract calculation of non-deductible amount --- CRM/Contribute/Form/Contribution.php | 91 +++++++++++++++++----------- 1 file changed, 54 insertions(+), 37 deletions(-) diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index addad1c60a..6f07b2c87d 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -1715,44 +1715,8 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP if ($isQuickConfig) { $params['is_quick_config'] = 1; } + $params['non_deductible_amount'] = $this->calculateNonDeductibleAmount($params, $formValues); - // CRM-11956 - // if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] - - // if non_deductible_amount does NOT exist - then calculate it depending on: - // $ContributionType->is_deductible and whether there is a product (premium). - if (empty($params['non_deductible_amount'])) { - $contributionType = new CRM_Financial_DAO_FinancialType(); - $contributionType->id = $params['financial_type_id']; - - if ($contributionType->is_deductible) { - - if (isset($formValues['product_name'][0])) { - $selectProduct = $formValues['product_name'][0]; - } - // if there is a product - compare the value to the contribution amount - if (isset($selectProduct)) { - $productDAO = new CRM_Contribute_DAO_Product(); - $productDAO->id = $selectProduct; - $productDAO->find(TRUE); - // product value exceeds contribution amount - if ($params['total_amount'] < $productDAO->price) { - $params['non_deductible_amount'] = $params['total_amount']; - } - // product value does NOT exceed contribution amount - else { - $params['non_deductible_amount'] = $productDAO->price; - } - } - // contribution is deductible - but there is no product - else { - $params['non_deductible_amount'] = '0.00'; - } - } - // contribution is NOT deductible - else { - $params['non_deductible_amount'] = $params['total_amount']; - } - } $contribution = CRM_Contribute_BAO_Contribution::create($params, $ids); @@ -1872,4 +1836,57 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } } + /** + * Calculate non deductible amount. + * + * CRM-11956 + * if non_deductible_amount exists i.e. Additional Details field set was opened [and staff typed something] - + * if non_deductible_amount does NOT exist - then calculate it depending on: + * $financialType->is_deductible and whether there is a product (premium). + * + * @param $params + * @param $formValues + * + * @return array + */ + protected function calculateNonDeductibleAmount($params, $formValues) { + if (!empty($params['non_deductible_amount'])) { + return $params['non_deductible_amount']; + } + if (empty($params['non_deductible_amount'])) { + $contributionType = new CRM_Financial_DAO_FinancialType(); + $contributionType->id = $params['financial_type_id']; + + if ($contributionType->is_deductible) { + + if (isset($formValues['product_name'][0])) { + $selectProduct = $formValues['product_name'][0]; + } + // if there is a product - compare the value to the contribution amount + if (isset($selectProduct)) { + $productDAO = new CRM_Contribute_DAO_Product(); + $productDAO->id = $selectProduct; + $productDAO->find(TRUE); + // product value exceeds contribution amount + if ($params['total_amount'] < $productDAO->price) { + return $params['total_amount']; + } + // product value does NOT exceed contribution amount + else { + return $productDAO->price; + } + } + // contribution is deductible - but there is no product + else { + return '0.00'; + } + } + // contribution is NOT deductible + else { + return $params['total_amount']; + } + } + return 0; + } + } -- 2.25.1