From: Pradeep Nayak Date: Wed, 6 Jul 2016 03:18:01 +0000 (+0530) Subject: [ready-for-core-team-review]CRM-16189, Added checks to validate financial type to... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=e68e1c9a30a2a2e8a20a8ebf4a289def632c00f7;p=civicrm-core.git [ready-for-core-team-review]CRM-16189, Added checks to validate financial type to have deferred r… (#8585) * CRM-16189, Added checks to validate financial type to have deferred relationship ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, fixed notice error and code stylings ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 * CRM-16189, updated code to include error msg ---------------------------------------- * CRM-16189: Improve support for Accrual Method bookkeeping https://issues.civicrm.org/jira/browse/CRM-16189 --- diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index 6a42e24883..d5419f8458 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -95,6 +95,14 @@ class CRM_Event_BAO_Event extends CRM_Event_DAO_Event { CRM_Utils_Hook::pre('create', 'Event', NULL, $params); } + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType( + CRM_Utils_Array::value('financial_type_id', $params) + ); + if ($isError) { + throw new CRM_Core_Exception(ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts')); + } + $event = new CRM_Event_DAO_Event(); $event->copyValues($params); diff --git a/CRM/Event/Form/ManageEvent/Fee.php b/CRM/Event/Form/ManageEvent/Fee.php index adef3c6ca8..c86240e146 100644 --- a/CRM/Event/Form/ManageEvent/Fee.php +++ b/CRM/Event/Form/ManageEvent/Fee.php @@ -521,6 +521,11 @@ class CRM_Event_Form_ManageEvent_Fee extends CRM_Event_Form_ManageEvent { } } } + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType($values['financial_type_id']); + if ($isError) { + $errors['financial_type_id'] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'); + } return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Member/BAO/MembershipType.php b/CRM/Member/BAO/MembershipType.php index 5b26ab77f4..1d29b8e7aa 100644 --- a/CRM/Member/BAO/MembershipType.php +++ b/CRM/Member/BAO/MembershipType.php @@ -106,6 +106,14 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType { } } + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType( + CRM_Utils_Array::value('financial_type_id', $params) + ); + if ($isError) { + throw new CRM_Core_Exception(ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts')); + } + // action is taken depending upon the mode $membershipType = new CRM_Member_DAO_MembershipType(); $membershipType->copyValues($params); diff --git a/CRM/Member/Form/MembershipType.php b/CRM/Member/Form/MembershipType.php index b695c8b6db..3cd0264a46 100644 --- a/CRM/Member/Form/MembershipType.php +++ b/CRM/Member/Form/MembershipType.php @@ -282,6 +282,12 @@ class CRM_Member_Form_MembershipType extends CRM_Member_Form_MembershipConfig { } } + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType($params['financial_type_id']); + if ($isError) { + $errors['financial_type_id'] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'); + } + return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Price/BAO/PriceFieldValue.php b/CRM/Price/BAO/PriceFieldValue.php index a89b25c0ed..9a6fdf979c 100644 --- a/CRM/Price/BAO/PriceFieldValue.php +++ b/CRM/Price/BAO/PriceFieldValue.php @@ -61,6 +61,19 @@ class CRM_Price_BAO_PriceFieldValue extends CRM_Price_DAO_PriceFieldValue { self::updateAmountAndFeeLevel($id, $prevLabel, $params['label']); } } + // CRM-16189 + $priceFieldID = CRM_Utils_Array::value('price_field_id', $params); + if (!$priceFieldID) { + $priceFieldID = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $id, 'price_field_id'); + } + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType( + CRM_Utils_Array::value('financial_type_id', $params), + $priceFieldID, + 'PriceField' + ); + if ($isError) { + throw new CRM_Core_Exception(ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts')); + } if (!empty($params['is_default'])) { $query = 'UPDATE civicrm_price_field_value SET is_default = 0 WHERE price_field_id = %1'; $p = array(1 => array($params['price_field_id'], 'Integer')); diff --git a/CRM/Price/BAO/PriceSet.php b/CRM/Price/BAO/PriceSet.php index 1b52e187fe..74e3e5da46 100644 --- a/CRM/Price/BAO/PriceSet.php +++ b/CRM/Price/BAO/PriceSet.php @@ -65,9 +65,29 @@ class CRM_Price_BAO_PriceSet extends CRM_Price_DAO_PriceSet { if (empty($params['id']) && empty($params['name'])) { $params['name'] = CRM_Utils_String::munge($params['title'], '_', 242); } + $priceSetID = NULL; + $validatePriceSet = TRUE; if (!empty($params['extends']) && is_array($params['extends'])) { + if (!array_key_exists(CRM_Core_Component::getComponentID('CiviEvent'), $params['extends']) + || !array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $params['extends']) + ) { + $validatePriceSet = FALSE; + } $params['extends'] = CRM_Utils_Array::implodePadded($params['extends']); } + else { + $priceSetID = CRM_Utils_Array::value('id', $params); + } + // CRM-16189 + if ($validatePriceSet) { + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType( + CRM_Utils_Array::value('financial_type_id', $params), + $priceSetID + ); + if ($isError) { + throw new CRM_Core_Exception(ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts')); + } + } $priceSetBAO = new CRM_Price_BAO_PriceSet(); $priceSetBAO->copyValues($params); if (self::eventPriceSetDomainID()) { diff --git a/CRM/Price/Form/Field.php b/CRM/Price/Form/Field.php index 5c50b9ff72..57c4f09d47 100644 --- a/CRM/Price/Form/Field.php +++ b/CRM/Price/Form/Field.php @@ -399,15 +399,21 @@ class CRM_Price_Form_Field extends CRM_Core_Form { * Incomplete row checking is also required. */ if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) && - $fields['html_type'] == 'Text' && $fields['price'] == NULL + $fields['html_type'] == 'Text' ) { - $errors['price'] = ts('Price is a required field'); - } - - if (($form->_action & CRM_Core_Action::ADD || $form->_action & CRM_Core_Action::UPDATE) && - $fields['html_type'] == 'Text' && $fields['financial_type_id'] == '' - ) { - $errors['financial_type_id'] = ts('Financial Type is a required field'); + if ($fields['price'] == NULL) { + $errors['price'] = ts('Price is a required field'); + } + if ($fields['financial_type_id'] == '') { + $errors['financial_type_id'] = ts('Financial Type is a required field'); + } + else { + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id'], $form->_sid); + if ($isError) { + $errors['financial_type_id'] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'); + } + } } //avoid the same price field label in Within PriceSet @@ -524,6 +530,11 @@ class CRM_Price_Form_Field extends CRM_Core_Form { } $_flagOption = $_emptyRow = 0; + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['option_financial_type_id'][$index], $form->_fid, 'PriceField'); + if ($isError) { + $errors["option_financial_type_id[{$index}]"] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'); + } } if (!empty($memTypesIDS)) { diff --git a/CRM/Price/Form/Option.php b/CRM/Price/Form/Option.php index a29cf32631..e61012232f 100644 --- a/CRM/Price/Form/Option.php +++ b/CRM/Price/Form/Option.php @@ -285,7 +285,11 @@ class CRM_Price_Form_Option extends CRM_Core_Form { ) { $errors['count'] = ts('Participant count can not be greater than max participants.'); } - + // CRM-16189 + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id'], $form->_fid, 'PriceField'); + if ($isError) { + $errors['financial_type_id'] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'); + } return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Price/Form/Set.php b/CRM/Price/Form/Set.php index c1a87bc643..b78bc7de04 100644 --- a/CRM/Price/Form/Set.php +++ b/CRM/Price/Form/Set.php @@ -102,6 +102,16 @@ class CRM_Price_Form_Set extends CRM_Core_Form { if ($asciiValue >= 48 && $asciiValue <= 57) { $errors['title'] = ts("Name cannot not start with a digit"); } + // CRM-16189 + if (!empty($fields['extends']) + && (array_key_exists(CRM_Core_Component::getComponentID('CiviEvent'), $fields['extends']) + || array_key_exists(CRM_Core_Component::getComponentID('CiviMember'), $fields['extends'])) + ) { + $isError = CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id']); + if ($isError) { + $errors['financial_type_id'] = ts('Deferred revenue account is not configured for selected financial type. Please have an administrator set up the deferred revenue account at Administer > CiviContribute > Financial Accounts, then configure it for financial types at Administer > CiviContribution > Financial Types, Accounts'); + } + } return empty($errors) ? TRUE : $errors; }