Merge pull request #4780 from williamtheaker/patch-1
[civicrm-core.git] / CRM / Financial / BAO / FinancialAccount.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
39de6fd5 5 | CiviCRM version 4.6 |
6a488035 6 +--------------------------------------------------------------------+
06b69b18 7 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27*/
28
29/**
30 *
31 * @package CRM
06b69b18 32 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
33 * $Id$
34 *
35 */
36
37class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount {
38
39 /**
100fef9d 40 * Static holder for the default LT
6a488035 41 */
ac7514c2 42 static $_defaultContributionType = NULL;
6a488035
TO
43
44 /**
100fef9d 45 * Class constructor
6a488035 46 */
ac7514c2
PN
47 function __construct() {
48 parent::__construct();
6a488035 49 }
0e6e8724 50
6a488035 51 /**
c490a46a 52 * Fetch object based on array of properties
6a488035
TO
53 *
54 * @param array $params (reference ) an assoc array of name/value pairs
55 * @param array $defaults (reference ) an assoc array to hold the flattened values
56 *
c490a46a 57 * @return CRM_Contribute_BAO_FinancialAccount object
6a488035
TO
58 * @access public
59 * @static
60 */
61 static function retrieve(&$params, &$defaults) {
ac7514c2
PN
62 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
63 $financialAccount->copyValues($params);
64 if ($financialAccount->find(TRUE)) {
6a488035
TO
65 CRM_Core_DAO::storeValues($financialAccount, $defaults);
66 return $financialAccount;
67 }
ac7514c2 68 return NULL;
6a488035
TO
69 }
70
71 /**
100fef9d 72 * Update the is_active flag in the db
6a488035
TO
73 *
74 * @param int $id id of the database record
75 * @param boolean $is_active value we want to set the is_active field
76 *
77 * @return Object DAO object on sucess, null otherwise
78 * @static
79 */
80 static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active);
82 }
83
84 /**
100fef9d 85 * Add the financial types
6a488035
TO
86 *
87 * @param array $params reference array contains the values submitted by the form
88 * @param array $ids reference array contains the id
0e6e8724 89 *
6a488035 90 * @access public
0e6e8724 91 * @static
6a488035
TO
92 * @return object
93 */
db47ea7b 94 static function add(&$params, &$ids = array()) {
bc2bc079 95 if(empty($params['id'])) {
96 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
97 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
98 $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE);
99 $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE);
100 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
101 }
a7488080 102 if (!empty($params['is_default'])) {
ac7514c2
PN
103 $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1';
104 $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer'));
105 CRM_Core_DAO::executeQuery($query, $queryParams);
0e6e8724
DL
106 }
107
6a488035 108 // action is taken depending upon the mode
ac7514c2 109 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
6a488035 110 $financialAccount->copyValues($params);
e5439581 111 if (!empty($ids['contributionType'])) {
112 $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids);
113 }
ac7514c2 114 $financialAccount->save();
6a488035
TO
115 return $financialAccount;
116 }
0e6e8724 117
6a488035 118 /**
100fef9d 119 * Delete financial Types
0e6e8724 120 *
6a488035
TO
121 * @param int $financialAccountId
122 * @static
123 */
124 static function del($financialAccountId) {
0e6e8724 125 //checking if financial type is present
ac7514c2 126 $check = FALSE;
0e6e8724 127
6a488035 128 //check dependencies
0e6e8724
DL
129 $dependancy = array(
130 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
131 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
6a488035
TO
132 );
133 foreach ($dependancy as $name) {
134 require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php");
0e6e8724
DL
135 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
136 $bao = new $className();
6a488035 137 $bao->$name[2] = $financialAccountId;
ac7514c2
PN
138 if ($bao->find(TRUE)) {
139 $check = TRUE;
6a488035
TO
140 }
141 }
0e6e8724 142
6a488035 143 if ($check) {
ac7514c2
PN
144 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.'));
145 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
6a488035 146 }
0e6e8724 147
6a488035 148 //delete from financial Type table
ac7514c2 149 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
6a488035
TO
150 $financialAccount->id = $financialAccountId;
151 $financialAccount->delete();
152 }
0e6e8724 153
6a488035 154 /**
100fef9d 155 * Get accounting code for a financial type with account relation Income Account is
6a488035
TO
156 *
157 * @financialTypeId int Financial Type Id
158 *
100fef9d 159 * @param int $financialTypeId
da6b46f4 160 *
6a488035
TO
161 * @return accounting code
162 * @static
163 */
164 static function getAccountingCode($financialTypeId) {
f743a6eb 165 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
6a488035
TO
166 $query = "SELECT cfa.accounting_code
167FROM civicrm_financial_type cft
168LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
169LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
170WHERE cft.id = %1
171 AND account_relationship = %2";
172 $params = array(
173 1 => array($financialTypeId, 'Integer'),
174 2 => array($relationTypeId, 'Integer'),
175 );
176 return CRM_Core_DAO::singleValueQuery($query, $params);
177 }
0e6e8724 178
7d289724 179 /**
100fef9d 180 * Get AR account
7d289724
PN
181 *
182 * @param $financialAccountId financial account id
183 *
184 * @param $financialAccountTypeId financial account type id
185 *
da6b46f4 186 * @param \account|string $accountTypeCode account type code
7d289724
PN
187 *
188 * @return integer count
189 * @static
190 */
ddaa8ef1
PN
191 static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
192 if (!$financialAccountTypeId) {
193 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
194 $financialAccountTypeId = array_search('Asset', $financialAccountType);
195 }
7d289724 196 $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2
ddaa8ef1 197 AND id != %3 AND is_active = 1;";
7d289724
PN
198 $params = array(
199 1 => array($financialAccountTypeId, 'Integer'),
200 2 => array(strtolower($accountTypeCode), 'String'),
201 3 => array($financialAccountId, 'Integer'),
202 );
203 return CRM_Core_DAO::singleValueQuery($query, $params);
204 }
6a488035
TO
205}
206