From: Pradeep Nayak Date: Wed, 18 Sep 2013 14:36:52 +0000 (+0530) Subject: --CRM-13231, fixed upgrade issue, created default COGS and EXP account if arent prese... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=3b67ab13d6b97f373f6d616afdb10600329c0c69;p=civicrm-core.git --CRM-13231, fixed upgrade issue, created default COGS and EXP account if arent present, added form rule to check if financial type has expense account configured when recording fees and also payment processor ---------------------------------------- * CRM-13231: Incremental upgrade 4.3.4 fails if no COGS and EXP default account set http://issues.civicrm.org/jira/browse/CRM-13231 --- diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 8b2f5e7945..fc21c4bff5 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -3018,4 +3018,23 @@ WHERE contribution_id = %1 "; self::deleteContribution($contribution->id); } } + /** + * Function to validate financial type + * + * CRM-13231 + * + * @param integer $financialTypeId Financial Type id + * + * @access public + * @static + */ + static function validateFinancialType($financialTypeId, $relationName = 'Expense Account is') { + $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE '{$relationName}' ")); + $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); + + if (!$financialAccount) { + return CRM_Contribute_PseudoConstant::financialType($financialTypeId); + } + return FALSE; + } } diff --git a/CRM/Contribute/Form/Contribution.php b/CRM/Contribute/Form/Contribution.php index 2c33cd62be..b1748f9254 100644 --- a/CRM/Contribute/Form/Contribution.php +++ b/CRM/Contribute/Form/Contribution.php @@ -976,17 +976,9 @@ class CRM_Contribute_Form_Contribution extends CRM_Contribute_Form_AbstractEditP } //FIXME FOR NEW DATA FLOW http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+4.3+Data+Flow - if (CRM_Utils_Array::value('fee_amount', $fields)) { - $financialAccount = array(); - CRM_Core_PseudoConstant::populate($financialAccount, - 'CRM_Financial_DAO_EntityFinancialAccount', - $all = TRUE, - $retrieve = 'financial_account_id', - $filter = NULL, - " account_relationship = 5 AND entity_id = {$fields['financial_type_id']} "); - if (!current($financialAccount)) { - $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for this Financial Type"); - } + if (CRM_Utils_Array::value('fee_amount', $fields) + && $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; } return $errors; } diff --git a/CRM/Contribute/Form/ContributionPage/Amount.php b/CRM/Contribute/Form/ContributionPage/Amount.php index de4a9aaed3..94b5ff2833 100644 --- a/CRM/Contribute/Form/ContributionPage/Amount.php +++ b/CRM/Contribute/Form/ContributionPage/Amount.php @@ -363,7 +363,12 @@ SELECT id } } } - + + if (CRM_Utils_Array::value('payment_processor', $fields) + && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($self->_defaultValues['financial_type_id'])) { + $errors['payment_processor'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; + } + if (CRM_Utils_Array::value('is_recur_interval', $fields)) { foreach(array_keys($fields['payment_processor']) as $paymentProcessorID) { $paymentProcessorTypeId = CRM_Core_DAO::getFieldValue( diff --git a/CRM/Contribute/Form/ContributionPage/Settings.php b/CRM/Contribute/Form/ContributionPage/Settings.php index 91bc8c0204..c87e3ee85d 100644 --- a/CRM/Contribute/Form/ContributionPage/Settings.php +++ b/CRM/Contribute/Form/ContributionPage/Settings.php @@ -179,7 +179,7 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $this->addDateTime('start_date', ts('Start Date')); $this->addDateTime('end_date', ts('End Date')); - $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this->_id); + $this->addFormRule(array('CRM_Contribute_Form_ContributionPage_Settings', 'formRule'), $this); parent::buildQuickForm(); } @@ -193,9 +193,9 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ * @static * @access public */ - static function formRule($values, $files, $contributionPageId) { + static function formRule($values, $files, $self) { $errors = array(); - + $contributionPageId = $self->_id; //CRM-4286 if (strstr($values['title'], '/')) { $errors['title'] = ts("Please do not use '/' in Title"); @@ -213,7 +213,12 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ if (($end < $start) && ($end != 0)) { $errors['end_date'] = ts('End date should be after Start date.'); } - + + if (CRM_Utils_Array::value('payment_processor', $self->_values) + && $financialType = CRM_Contribute_BAO_Contribution::validateFinancialType($values['financial_type_id'])) { + $errors['financial_type_id'] = ts("Financial Account of account relationship of 'Expense Account is' is not configured for Financial Type : ") . $financialType; + } + //dont allow on behalf of save when //pre or post profile consists of membership fields if ($contributionPageId && CRM_Utils_Array::value('is_organization', $values)) { @@ -234,7 +239,7 @@ class CRM_Contribute_Form_ContributionPage_Settings extends CRM_Contribute_Form_ $conProfileType = "'Includes Profile (top of page)'"; } } - + if ($contributionProfiles['custom_post_id']) { $postProfileType = CRM_Core_BAO_UFField::getProfileType($contributionProfiles['custom_post_id']); if ($postProfileType == 'Membership') { diff --git a/CRM/Contribute/Form/ManagePremiums.php b/CRM/Contribute/Form/ManagePremiums.php index 8e73708dc5..82d52b84b4 100644 --- a/CRM/Contribute/Form/ManagePremiums.php +++ b/CRM/Contribute/Form/ManagePremiums.php @@ -242,14 +242,17 @@ class CRM_Contribute_Form_ManagePremiums extends CRM_Contribute_Form { if (isset($params['imageOption'])) { if ($params['imageOption'] == 'thumbnail') { if (!$params['imageUrl']) { - $errors['imageUrl'] = 'Image URL is Required '; + $errors['imageUrl'] = ts('Image URL is Required'); } if (!$params['thumbnailUrl']) { - $errors['thumbnailUrl'] = 'Thumbnail URL is Required '; + $errors['thumbnailUrl'] = ts('Thumbnail URL is Required'); } } } - + // CRM-13231 financial type required if product has cost + if (CRM_Utils_Array::value('cost', $params)) { + $errors['financial_type_id'] = ts('Financial Type is required for product having cost.'); + } $fileLocation = $files['uploadFile']['tmp_name']; if ($fileLocation != "") { list($width, $height) = getimagesize($fileLocation); diff --git a/CRM/Financial/BAO/FinancialTypeAccount.php b/CRM/Financial/BAO/FinancialTypeAccount.php index 811670a64e..cc636ef153 100644 --- a/CRM/Financial/BAO/FinancialTypeAccount.php +++ b/CRM/Financial/BAO/FinancialTypeAccount.php @@ -144,7 +144,7 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin } else { $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship'); - CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', array(1 => $relationValues[$accountRelationShipId]))); + CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', array(1 => $relationValues[$accountRelationShipId])), NUll, 'error'); } return CRM_Utils_System::redirect( CRM_Utils_System::url( 'civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}" )); } @@ -152,7 +152,9 @@ class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFin //delete from financial Type table $financialType = new CRM_Financial_DAO_EntityFinancialAccount( ); $financialType->id = $financialTypeAccountId; + $financialType->find(TRUE); $financialType->delete(); + CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete %1 account.', array(1 => $relationValues[$financialType->account_relationship]))); } /** diff --git a/CRM/Upgrade/Incremental/sql/4.3.4.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.3.4.mysql.tpl index 3c0d1680fb..dbca1af7d1 100644 --- a/CRM/Upgrade/Incremental/sql/4.3.4.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.3.4.mysql.tpl @@ -75,6 +75,12 @@ SELECT @option_value_rel_id_cg := value FROM civicrm_option_value WHERE option_g SELECT @opCost := value FROM civicrm_option_value WHERE name = 'Cost of Sales' and option_group_id = @option_group_id_fat; SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opCost; +-- CRM-13231 +INSERT IGNORE INTO civicrm_financial_account (id, name, contact_id, financial_account_type_id, description, account_type_code, accounting_code, is_active, is_default) +VALUES (@financialAccountId, 'Premiums', @domainContactId, @opCost, 'Account to record cost of premiums provided to payors', 'COGS', '5100', 1, 1); + +SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opCost; + INSERT INTO civicrm_entity_financial_account(entity_table, entity_id, account_relationship, financial_account_id) SELECT 'civicrm_financial_type', cft.id, @option_value_rel_id_cg, @financialAccountId FROM civicrm_financial_type cft @@ -85,11 +91,19 @@ WHERE ceft.entity_id IS NULL; -- for Expense Account is SELECT @option_value_rel_id_exp := value FROM civicrm_option_value WHERE option_group_id = @option_group_id_arel AND name = 'Expense Account is'; SELECT @opexp := value FROM civicrm_option_value WHERE name = 'Expenses' and option_group_id = @option_group_id_fat; +SET @financialAccountId := ''; SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opexp; +-- CRM-13231 +INSERT IGNORE INTO civicrm_financial_account (id, name, contact_id, financial_account_type_id, description, account_type_code, accounting_code, is_active, is_default) +VALUES (@financialAccountId, 'Banking Fees', @domainContactId, @opexp, 'Payment processor fees and manually recorded banking fees', 'EXP', '5200', 1, 1); + +SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opexp; + + INSERT INTO civicrm_entity_financial_account(entity_table, entity_id, account_relationship, financial_account_id) SELECT 'civicrm_financial_type', cft.id, @option_value_rel_id_exp, @financialAccountId FROM civicrm_financial_type cft LEFT JOIN civicrm_entity_financial_account ceft -ON ceft.entity_id = cft.id AND ceft.account_relationship = 5 AND ceft.entity_table = 'civicrm_financial_type' +ON ceft.entity_id = cft.id AND ceft.account_relationship = @option_value_rel_id_exp AND ceft.entity_table = 'civicrm_financial_type' WHERE ceft.entity_id IS NULL; diff --git a/CRM/Upgrade/Incremental/sql/4.3.6.mysql.tpl b/CRM/Upgrade/Incremental/sql/4.3.6.mysql.tpl index 021d20fdc8..b5ce8df174 100644 --- a/CRM/Upgrade/Incremental/sql/4.3.6.mysql.tpl +++ b/CRM/Upgrade/Incremental/sql/4.3.6.mysql.tpl @@ -45,3 +45,32 @@ AND cefa.entity_table = 'civicrm_payment_processor' AND cefa.financial_account_i -- CRM-13096 DROP TABLE IF EXISTS civicrm_official_receipt; + +-- CRM-13231 +SELECT @option_group_id_arel := max(id) from civicrm_option_group where name = 'account_relationship'; +SELECT @option_group_id_fat := max(id) from civicrm_option_group where name = 'financial_account_type'; +SELECT @opexp := value FROM civicrm_option_value WHERE name = 'Expenses' and option_group_id = @option_group_id_fat; +SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opexp; + +SELECT @option_value_rel_id_exp := value FROM civicrm_option_value WHERE option_group_id = @option_group_id_arel AND name = 'Expense Account is'; + +INSERT IGNORE INTO civicrm_financial_account (id, name, contact_id, financial_account_type_id, description, account_type_code, accounting_code, is_active, is_default) +VALUES (@financialAccountId, 'Banking Fees', @domainContactId, @opexp, 'Payment processor fees and manually recorded banking fees', 'EXP', '5200', 1, 1); + +SELECT @financialAccountId := id FROM civicrm_financial_account WHERE is_default = 1 and financial_account_type_id = @opexp; + +INSERT INTO civicrm_entity_financial_account(entity_table, entity_id, account_relationship, financial_account_id) +SELECT 'civicrm_financial_type', cft.id, @option_value_rel_id_exp, @financialAccountId +FROM civicrm_financial_type cft +LEFT JOIN civicrm_entity_financial_account ceft +ON ceft.entity_id = cft.id AND ceft.account_relationship = @option_value_rel_id_exp AND ceft.entity_table = 'civicrm_financial_type' +WHERE ceft.entity_id IS NULL; + +UPDATE civicrm_financial_trxn cft +INNER JOIN civicrm_entity_financial_trxn ceft ON ceft.financial_trxn_id = cft .id +INNER JOIN civicrm_entity_financial_trxn ceft1 ON ceft1.financial_trxn_id = cft .id +INNER JOIN civicrm_financial_item cfi ON cfi.id = ceft1.entity_id +INNER JOIN civicrm_contribution cc ON cc.id = ceft.entity_id +INNER JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cc.financial_type_id +SET cft.to_financial_account_id = cefa.financial_account_id +WHERE ceft.entity_table = 'civicrm_contribution' AND ceft1.entity_table = 'civicrm_financial_item' AND cfi.entity_table = 'civicrm_financial_trxn' AND cft.to_financial_account_id IS NULL AND cefa.entity_table = 'civicrm_financial_type' AND cefa.account_relationship = @option_value_rel_id_exp; \ No newline at end of file diff --git a/templates/CRM/Financial/Form/FinancialTypeAccount.tpl b/templates/CRM/Financial/Form/FinancialTypeAccount.tpl index 18279b9941..41280a7e1e 100644 --- a/templates/CRM/Financial/Form/FinancialTypeAccount.tpl +++ b/templates/CRM/Financial/Form/FinancialTypeAccount.tpl @@ -29,7 +29,7 @@ {if $action eq 8}
- {ts}WARNING: You cannot delete a financial type if it is currently used by any Contributions, Contribution Pages or Membership Types. Consider disabling this option instead.{/ts} {ts}Deleting a financial type cannot be undone.{/ts} {ts}Do you want to continue?{/ts} + {ts}WARNING: You cannot delete a financial type if it is currently used by any Contributions, Contribution Pages or Membership Types. Consider disabling this option instead.{/ts} {ts}Deleting a financial type cannot be undone. Unbalanced transactions may be created if you delete this account.{/ts} {ts}Do you want to continue?{/ts}
{else}
{include file="CRM/common/formButtons.tpl" location="top"}