Skip to content

Commit

Permalink
IIDA-72, fixed deferred revenue account validation
Browse files Browse the repository at this point in the history
Conflicts:

	CRM/Contribute/BAO/Contribution.php

Conflicts:

	CRM/Contribute/BAO/Contribution.php

IIDA-72, added missing file

CRM-20385, removed code to call undefined function

----------------------------------------
* CRM-20385: oversensitive deferred revenue account validation
  https://issues.civicrm.org/jira/browse/CRM-20385

QA fix

CRM-20385, removed extra check since we need to call JS even though $deferredFinancialType is empty()

----------------------------------------
* CRM-20385: oversensitive deferred revenue account validation
  https://issues.civicrm.org/jira/browse/CRM-20385
  • Loading branch information
pradpnayak committed Apr 7, 2017
1 parent 42e98b9 commit 9fc5d01
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 283 deletions.
28 changes: 0 additions & 28 deletions CRM/Admin/Form/Preferences/Contribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,34 +166,6 @@ public function buildQuickForm() {
}
$this->assign('htmlFields', $htmlFields);
parent::buildQuickForm();
$this->addFormRule(array('CRM_Admin_Form_Preferences_Contribute', 'formRule'), $this);
}

/**
* Global validation rules for the form.
*
* @param array $values
* posted values of the form
* @param $files
* @param $self
*
* @return array
* list of errors to be posted back to the form
*/
public static function formRule($values, $files, $self) {
$errors = array();
if (CRM_Utils_Array::value('deferred_revenue_enabled', $values)) {
$errorMessage = CRM_Financial_BAO_FinancialAccount::validateTogglingDeferredRevenue();
if ($errorMessage) {
// Since the error msg is too long and
// takes the whole space to display inline
// therefore setting blank text to highlight the field
// setting actual error msg to _qf_default to show in pop-up screen
$errors['deferred_revenue_enabled'] = ' ';
$errors['_qf_default'] = $errorMessage;
}
}
return $errors;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ public static function add(&$params, $ids = array()) {
$params['prevContribution'] = self::getOriginalContribution($contributionID);
}

// CRM-16189
CRM_Financial_BAO_FinancialAccount::checkFinancialTypeHasDeferred($params, $contributionID);
if ($contributionID && !empty($params['revenue_recognition_date']) && !empty($params['prevContribution'])
&& !($contributionStatus[$params['prevContribution']->contribution_status_id] == 'Pending')
&& !self::allowUpdateRevenueRecognitionDate($contributionID)
Expand Down
5 changes: 0 additions & 5 deletions CRM/Event/BAO/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ public static function add(&$params) {
CRM_Utils_Hook::pre('create', 'Event', NULL, $params);
}

// CRM-16189
if (!empty($params['financial_type_id'])) {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($params['financial_type_id']);
}

$event = new CRM_Event_DAO_Event();

$event->copyValues($params);
Expand Down
12 changes: 4 additions & 8 deletions CRM/Event/Form/ManageEvent/Fee.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,10 @@ public function buildQuickForm() {
$this->addElement('submit', $this->getButtonName('submit'), ts('Add Discount Set to Fee Table'),
array('class' => 'crm-form-submit cancel')
);

if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
$deferredFinancialType = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType();
$this->assign('deferredFinancialType', array_keys($deferredFinancialType));
}
$this->buildAmountLabel();
parent::buildQuickForm();
}
Expand Down Expand Up @@ -524,13 +527,6 @@ public static function formRule($values) {
}
}
}
// CRM-16189
try {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($values['financial_type_id']);
}
catch (CRM_Core_Exception $e) {
$errors['financial_type_id'] = $e->getMessage();
}
return empty($errors) ? TRUE : $errors;
}

Expand Down
108 changes: 1 addition & 107 deletions CRM/Financial/BAO/FinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,118 +426,12 @@ public static function checkFinancialTypeHasDeferred($params, $contributionID =
}

if ($isError) {
$error = ts('Revenue recognition date can only be specified if the financial type selected has a deferred revenue account configured. 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');
$error = ts('Revenue Recognition Date cannot be processed unless there is a Deferred Revenue account setup for the Financial Type. Please remove Revenue Recognition Date, select a different Financial Type with a Deferred Revenue account setup for it, or setup a Deferred Revenue account for this Financial Type.');
throw new CRM_Core_Exception($error);
}
return $isError;
}

/**
* Check if financial type has Deferred Revenue Account is relationship
* with Financial Account.
*
* @param int $financialTypeId
* Financial Type Id.
*
* @param int $entityID
* Holds id for PriceSet/PriceField/PriceFieldValue.
*
* @param string $entity
* Entity like PriceSet/PriceField/PriceFieldValue.
*
* @return bool
*
*/
public static function validateFinancialType($financialTypeId, $entityID = NULL, $entity = NULL) {
if (!CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
return FALSE;
}
if ($entityID) {
$query = ' SELECT ps.extends FROM civicrm_price_set ps';
$params = array(
1 => array('ps', 'Text'),
2 => array($entityID, 'Integer'),
);
if ($entity == 'PriceField') {
$params[1] = array('pf', 'Text');
$query .= ' INNER JOIN civicrm_price_field pf ON pf.price_set_id = ps.id ';
}
$query .= ' WHERE %1.id = %2';
$extends = CRM_Core_DAO::singleValueQuery($query, $params);
$extends = explode('', $extends);
if (!(in_array(CRM_Core_Component::getComponentID('CiviEvent'), $extends)
|| in_array(CRM_Core_Component::getComponentID('CiviMember'), $extends))
) {
return FALSE;
}
}
$deferredFinancialType = self::getDeferredFinancialType();
if (!array_key_exists($financialTypeId, $deferredFinancialType)) {
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'));
}
return FALSE;
}

/**
* Validate if Deferred Account is set for Financial Type
* when Deferred Revenue is enabled
*
* @return string
*
*/
public static function validateTogglingDeferredRevenue() {
$deferredFinancialType = self::getDeferredFinancialType();
$message = ts('Before Deferred Revenue can be enabled, a Deferred Revenue Account relationship must be defined for all financial types currently used for Memberships and Events, including
<ul>
<li>those specified for each membership type at Admin > CiviMember > Membership Types, edit</li>
<li>on the Fees tab when managing events</li>
<li>the default financial type associated with a membership or event price set</li>
<li>the financial type for a membership price set field option when the membership type is non-blank</li>
<li>as the financial type for a price set field with participant count > 0</li>
<li>as the financial type for a price set field option with participant count > 0</li>
</ul>
In other words, please create deferred revenue accounts at Administer > CiviContribute > Financial Accounts, then configure them for the following financial types at Administer > CiviContribute > Financial Types, accounts:');
$tables = array(
'civicrm_membership_type',
'civicrm_event',
'civicrm_price_set',
'civicrm_price_field_value',
);
$params[2] = array('', 'Text');
if (!empty($deferredFinancialType)) {
$params[2] = array(' AND financial_type_id NOT IN (' . implode(',', array_keys($deferredFinancialType)) . ') ', 'Text');
}
$query_1 = 'SELECT %5.id FROM %4 WHERE %5.is_active = 1';
$query_2 = $query_1 . ' %2';
foreach ($tables as $table) {
$params[4] = array($table, 'Text');
$params[5] = array($table, 'Text');
$dao = CRM_Core_DAO::executeQuery($query_1, $params);
if ($dao->N) {
if (in_array($table, array('civicrm_price_set', 'civicrm_price_field_value'))) {
$query_2 .= " AND civicrm_price_set.name NOT IN ('default_contribution_amount', 'default_membership_type_amount') AND (civicrm_price_set.extends LIKE '%1%' OR civicrm_price_set.extends like '3')";
if ($table == 'civicrm_price_field_value') {
$string = $table . ' INNER JOIN civicrm_price_field ON civicrm_price_field.id = civicrm_price_field_value.price_field_id INNER JOIN civicrm_price_set ON civicrm_price_set.id = civicrm_price_field.price_set_id ';
$params[4] = array($string, 'Text');
$params[2][0] = str_replace('financial_type_id', "{$table}.financial_type_id", $params[2][0]);
}
}
$dao = CRM_Core_DAO::executeQuery($query_2, $params);
if ($dao->N) {
$message .= '<ul>';
$financialTypes = CRM_Contribute_PseudoConstant::financialType();
$financialTypes = array_diff_key($financialTypes, $deferredFinancialType);
foreach ($financialTypes as $financialType) {
$message .= "<li>{$financialType}</li>";
}
$message .= '</ul>';
return $message;
}
}
}
return NULL;
}

/**
* Retrieve all Deferred Financial Accounts.
*
Expand Down
7 changes: 0 additions & 7 deletions CRM/Member/BAO/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,6 @@ public static function add(&$params, $ids = array()) {
}
}

// CRM-16189
if (!empty($params['financial_type_id'])) {
CRM_Financial_BAO_FinancialAccount::validateFinancialType(
$params['financial_type_id']
);
}

// action is taken depending upon the mode
$membershipType = new CRM_Member_DAO_MembershipType();
$membershipType->copyValues($params);
Expand Down
13 changes: 5 additions & 8 deletions CRM/Member/Form/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ public function buildQuickForm() {
$this->addFormRule(array('CRM_Member_Form_MembershipType', 'formRule'));

$this->assign('membershipTypeId', $this->_id);

if (CRM_Contribute_BAO_Contribution::checkContributeSettings('deferred_revenue_enabled')) {
$deferredFinancialType = CRM_Financial_BAO_FinancialAccount::getDeferredFinancialType();
$this->assign('deferredFinancialType', array_keys($deferredFinancialType));
}
}

/**
Expand Down Expand Up @@ -282,14 +287,6 @@ public static function formRule($params) {
}
}

// CRM-16189
try {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($params['financial_type_id']);
}
catch (CRM_Core_Exception $e) {
$errors['financial_type_id'] = $e->getMessage();
}

return empty($errors) ? TRUE : $errors;
}

Expand Down
7 changes: 0 additions & 7 deletions CRM/Price/BAO/PriceFieldValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ public static function add(&$params, $ids = array()) {
if (!$priceFieldID) {
$priceFieldID = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_PriceFieldValue', $id, 'price_field_id');
}
if (!empty($params['financial_type_id'])) {
CRM_Financial_BAO_FinancialAccount::validateFinancialType(
$params['financial_type_id'],
$priceFieldID,
'PriceField'
);
}
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'));
Expand Down
7 changes: 0 additions & 7 deletions CRM/Price/BAO/PriceSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,6 @@ public static function create(&$params) {
else {
$priceSetID = CRM_Utils_Array::value('id', $params);
}
// CRM-16189
if ($validatePriceSet && !empty($params['financial_type_id'])) {
CRM_Financial_BAO_FinancialAccount::validateFinancialType(
$params['financial_type_id'],
$priceSetID
);
}
$priceSetBAO = new CRM_Price_BAO_PriceSet();
$priceSetBAO->copyValues($params);
if (self::eventPriceSetDomainID()) {
Expand Down
16 changes: 0 additions & 16 deletions CRM/Price/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,6 @@ public static function formRule($fields, $files, $form) {
if ($fields['financial_type_id'] == '') {
$errors['financial_type_id'] = ts('Financial Type is a required field');
}
else {
// CRM-16189
try {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id'], $form->_sid);
}
catch (CRM_Core_Exception $e) {
$errors['financial_type_id'] = $e->getMessage();
}
}
}

//avoid the same price field label in Within PriceSet
Expand Down Expand Up @@ -529,13 +520,6 @@ public static function formRule($fields, $files, $form) {
}

$_flagOption = $_emptyRow = 0;
// CRM-16189
try {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['option_financial_type_id'][$index], $form->_fid, 'PriceField');
}
catch(CRM_Core_Exception $e) {
$errors["option_financial_type_id[{$index}]"] = $e->getMessage();
}
}

if (!empty($memTypesIDS)) {
Expand Down
7 changes: 0 additions & 7 deletions CRM/Price/Form/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,13 +288,6 @@ public static function formRule($fields, $files, $form) {
) {
$errors['count'] = ts('Participant count can not be greater than max participants.');
}
// CRM-16189
try {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id'], $form->_fid, 'PriceField');
}
catch (CRM_Core_Exception $e) {
$errors['financial_type_id'] = $e->getMessage();
}
return empty($errors) ? TRUE : $errors;
}

Expand Down
12 changes: 0 additions & 12 deletions CRM/Price/Form/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ public static function formRule($fields, $files, $options) {
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']))
) {
try {
CRM_Financial_BAO_FinancialAccount::validateFinancialType($fields['financial_type_id']);
}
catch (CRM_Core_Exception $e) {
$errors['financial_type_id'] = $e->getMessage();
}
}
return empty($errors) ? TRUE : $errors;
}

Expand Down
3 changes: 2 additions & 1 deletion templates/CRM/Event/Form/ManageEvent/Fee.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
</div>
</div>
{include file="CRM/common/deferredFinancialType.tpl" context='Event'}
{include file="CRM/common/showHide.tpl"}
<script type="text/javascript">
{if $price}
Expand Down Expand Up @@ -362,4 +363,4 @@
});
</script>
{/literal}
{/if}
{/if}
1 change: 1 addition & 0 deletions templates/CRM/Member/Form/MembershipType.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
</div>
</div>

{include file="CRM/common/deferredFinancialType.tpl" context='MembershipType'}
{literal}
<script type="text/javascript">
CRM.$(function($) {
Expand Down
Loading

0 comments on commit 9fc5d01

Please sign in to comment.