Skip to content

Commit

Permalink
Merge pull request #9342 from JMAConsulting/CRM-19587
Browse files Browse the repository at this point in the history
CRM-19587, donot delete financial account if its present in civicrm_f…
  • Loading branch information
Yashodha Chaku authored Nov 3, 2016
2 parents d575305 + b4ea799 commit da1c059
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CRM/Financial/BAO/FinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static function del($financialAccountId) {
$dependency = array(
array('Core', 'FinancialTrxn', 'to_financial_account_id'),
array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
array('Financial', 'FinancialItem', 'financial_account_id'),
);
foreach ($dependency as $name) {
require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php";
Expand All @@ -157,13 +158,14 @@ public static function del($financialAccountId) {

if ($check) {
CRM_Core_Session::setStatus(ts('This financial account cannot be deleted since it is being used as a header account. Please remove it from being a header account before trying to delete it again.'));
return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
return FALSE;
}

// delete from financial Type table
$financialAccount = new CRM_Financial_DAO_FinancialAccount();
$financialAccount->id = $financialAccountId;
$financialAccount->delete();
return TRUE;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions CRM/Financial/Form/FinancialAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,12 @@ public function setDefaultValues() {
*/
public function postProcess() {
if ($this->_action & CRM_Core_Action::DELETE) {
CRM_Financial_BAO_FinancialAccount::del($this->_id);
CRM_Core_Session::setStatus(ts('Selected Financial Account has been deleted.'));
if (CRM_Financial_BAO_FinancialAccount::del($this->_id)) {
CRM_Core_Session::setStatus(ts('Selected Financial Account has been deleted.'));
}
else {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
}
}
else {
// store the submitted values in an array
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/CRM/Financial/BAO/FinancialAccountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,39 @@ public function testdel() {
$this->assertEquals(empty($result), TRUE, 'Verify financial account record deletion.');
}

/**
* Check method del()
*/
public function testdelIfHasContribution() {
$params = array(
'name' => 'Donation Test',
'is_active' => 1,
'is_deductible' => 1,
'is_reserved' => 1,
);
$financialType = CRM_Financial_BAO_FinancialType::add($params);
$defaults = array();
$params = array(
'name' => 'Donation Test',
'is_active' => 1,
);
$result = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);

$contactId = $this->individualCreate();
$contributionParams = array(
'total_amount' => 300,
'currency' => 'USD',
'contact_id' => $contactId,
'financial_type_id' => $financialType->id,
'contribution_status_id' => 1,
);
$contributions = CRM_Contribute_BAO_Contribution::create($contributionParams);
CRM_Financial_BAO_FinancialAccount::del($result->id);
$params = array('id' => $result->id);
$result = CRM_Financial_BAO_FinancialAccount::retrieve($params, $defaults);
$this->assertEquals(empty($result), FALSE, 'Verify financial account record deletion.');
}

/**
* Check method getAccountingCode()
*/
Expand Down

0 comments on commit da1c059

Please sign in to comment.