From 4d47ad174273446cd3e7fea3ce168319eb3e26a9 Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Sun, 15 Mar 2015 02:20:58 +0530 Subject: [PATCH] --CRM-16015, Added form rule to restrict financial type change for having different financial type in line item --- CRM/Contribute/BAO/Contribution.php | 31 ++++++++++++++++++++++++++++ CRM/Contribute/Form/Contribution.php | 5 ++++- api/v3/Contribution.php | 8 ++++++- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 76accef413..5d80490419 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3675,4 +3675,35 @@ WHERE con.id = {$contributionId} return $params; } + /** + * Check financial type validation on update of a contribution. + * + * @param Integer $financialTypeId + * Value of latest Financial Type. + * + * @param Integer + * Contribution Id. + * + * @param array $errors + * List of errors. + * + * @return bool + */ + public static function checkFinancialTypeChange($financialTypeId, $contributionId, &$errors) { + if (!empty($financialTypeId)) { + $oldFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); + if ($oldFinancialTypeId == $financialTypeId) { + return FALSE; + } + } + $sql = 'SELECT financial_type_id FROM civicrm_line_item WHERE contribution_id = %1 GROUP BY financial_type_id;'; + $params = array( + '1' => array($contributionId, 'Integer') + ); + $result = CRM_Core_DAO::executeQuery($sql, $params); + if ($result->N > 1) { + $errors['financial_type_id'] = ts('One or more line items have a different financial type than the contribution. Editing the financial type is not yet supported in this situation.'); + } + } + } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 1abeefd2f9..00de7c927a 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -948,7 +948,10 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP if ($self->_id && $self->_values['contribution_status_id'] != $fields['contribution_status_id']) { CRM_Contribute_BAO_Contribution::checkStatusValidation($self->_values, $fields, $errors); } - + // CRM-16015, add form-rule to restrict change of financial type if using price field of different financial type + if ($self->_id && $self->_values['financial_type_id'] != $fields['financial_type_id']) { + CRM_Contribute_BAO_Contribution::checkFinancialTypeChange(NULL, $self->_id, $errors); + } //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow if (!empty($fields['fee_amount']) && !empty($fields['financial_type_id']) && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($fields['financial_type_id'])) { $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 058f5bcc83..aa385a0fda 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -56,7 +56,13 @@ function civicrm_api3_contribution_create(&$params) { throw new API_Exception($error['contribution_status_id']); } } - + if (!empty($params['id']) && !empty($params['financial_type_id'])) { + $error = array(); + CRM_Contribute_BAO_Contribution::checkFinancialTypeChange($params['financial_type_id'], $params['id'], $error); + if (array_key_exists('financial_type_id', $error)) { + throw new API_Exception($error['financial_type_id']); + } + } _civicrm_api3_contribution_create_legacy_support_45($params); // Make sure tax calculation is handled via api. -- 2.25.1