Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
06b69b18 | 5 | | CiviCRM version 4.5 | |
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 | ||
37 | class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount { | |
38 | ||
39 | /** | |
40 | * static holder for the default LT | |
41 | */ | |
ac7514c2 | 42 | static $_defaultContributionType = NULL; |
6a488035 TO |
43 | |
44 | /** | |
45 | * class constructor | |
46 | */ | |
ac7514c2 PN |
47 | function __construct() { |
48 | parent::__construct(); | |
6a488035 | 49 | } |
0e6e8724 | 50 | |
6a488035 TO |
51 | /** |
52 | * Takes a bunch of params that are needed to match certain criteria and | |
53 | * retrieves the relevant objects. Typically the valid params are only | |
54 | * contact_id. We'll tweak this function to be more full featured over a period | |
55 | * of time. This is the inverse function of create. It also stores all the retrieved | |
56 | * values in the default array | |
57 | * | |
58 | * @param array $params (reference ) an assoc array of name/value pairs | |
59 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
60 | * | |
61 | * @return object CRM_Contribute_BAO_FinancialAccount object | |
62 | * @access public | |
63 | * @static | |
64 | */ | |
65 | static function retrieve(&$params, &$defaults) { | |
ac7514c2 PN |
66 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
67 | $financialAccount->copyValues($params); | |
68 | if ($financialAccount->find(TRUE)) { | |
6a488035 TO |
69 | CRM_Core_DAO::storeValues($financialAccount, $defaults); |
70 | return $financialAccount; | |
71 | } | |
ac7514c2 | 72 | return NULL; |
6a488035 TO |
73 | } |
74 | ||
75 | /** | |
76 | * update the is_active flag in the db | |
77 | * | |
78 | * @param int $id id of the database record | |
79 | * @param boolean $is_active value we want to set the is_active field | |
80 | * | |
81 | * @return Object DAO object on sucess, null otherwise | |
82 | * @static | |
83 | */ | |
84 | static function setIsActive($id, $is_active) { | |
85 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active); | |
86 | } | |
87 | ||
88 | /** | |
89 | * function to add the financial types | |
90 | * | |
91 | * @param array $params reference array contains the values submitted by the form | |
92 | * @param array $ids reference array contains the id | |
0e6e8724 | 93 | * |
6a488035 | 94 | * @access public |
0e6e8724 | 95 | * @static |
6a488035 TO |
96 | * @return object |
97 | */ | |
db47ea7b | 98 | static function add(&$params, &$ids = array()) { |
bc2bc079 | 99 | if(empty($params['id'])) { |
100 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
101 | $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE); | |
102 | $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE); | |
103 | $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE); | |
104 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
105 | } | |
a7488080 | 106 | if (!empty($params['is_default'])) { |
ac7514c2 PN |
107 | $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1'; |
108 | $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer')); | |
109 | CRM_Core_DAO::executeQuery($query, $queryParams); | |
0e6e8724 DL |
110 | } |
111 | ||
6a488035 | 112 | // action is taken depending upon the mode |
ac7514c2 | 113 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
6a488035 | 114 | $financialAccount->copyValues($params); |
e5439581 | 115 | if (!empty($ids['contributionType'])) { |
116 | $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids); | |
117 | } | |
ac7514c2 | 118 | $financialAccount->save(); |
6a488035 TO |
119 | return $financialAccount; |
120 | } | |
0e6e8724 | 121 | |
6a488035 | 122 | /** |
0e6e8724 DL |
123 | * Function to delete financial Types |
124 | * | |
6a488035 TO |
125 | * @param int $financialAccountId |
126 | * @static | |
127 | */ | |
128 | static function del($financialAccountId) { | |
0e6e8724 | 129 | //checking if financial type is present |
ac7514c2 | 130 | $check = FALSE; |
0e6e8724 | 131 | |
6a488035 | 132 | //check dependencies |
0e6e8724 DL |
133 | $dependancy = array( |
134 | array('Core', 'FinancialTrxn', 'to_financial_account_id'), | |
135 | array('Financial', 'FinancialTypeAccount', 'financial_account_id'), | |
6a488035 TO |
136 | ); |
137 | foreach ($dependancy as $name) { | |
138 | require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php"); | |
0e6e8724 DL |
139 | $className = "CRM_{$name[0]}_BAO_{$name[1]}"; |
140 | $bao = new $className(); | |
6a488035 | 141 | $bao->$name[2] = $financialAccountId; |
ac7514c2 PN |
142 | if ($bao->find(TRUE)) { |
143 | $check = TRUE; | |
6a488035 TO |
144 | } |
145 | } | |
0e6e8724 | 146 | |
6a488035 | 147 | if ($check) { |
ac7514c2 PN |
148 | 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.')); |
149 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse")); | |
6a488035 | 150 | } |
0e6e8724 | 151 | |
6a488035 | 152 | //delete from financial Type table |
ac7514c2 | 153 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
6a488035 TO |
154 | $financialAccount->id = $financialAccountId; |
155 | $financialAccount->delete(); | |
156 | } | |
0e6e8724 | 157 | |
6a488035 TO |
158 | /** |
159 | * get accounting code for a financial type with account relation Income Account is | |
160 | * | |
161 | * @financialTypeId int Financial Type Id | |
162 | * | |
163 | * @return accounting code | |
164 | * @static | |
165 | */ | |
166 | static function getAccountingCode($financialTypeId) { | |
f743a6eb | 167 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); |
6a488035 TO |
168 | $query = "SELECT cfa.accounting_code |
169 | FROM civicrm_financial_type cft | |
170 | LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type' | |
171 | LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id | |
172 | WHERE cft.id = %1 | |
173 | AND account_relationship = %2"; | |
174 | $params = array( | |
175 | 1 => array($financialTypeId, 'Integer'), | |
176 | 2 => array($relationTypeId, 'Integer'), | |
177 | ); | |
178 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
179 | } | |
0e6e8724 | 180 | |
7d289724 PN |
181 | /** |
182 | * get AR account | |
183 | * | |
184 | * @param $financialAccountId financial account id | |
185 | * | |
186 | * @param $financialAccountTypeId financial account type id | |
187 | * | |
188 | * @param $accountTypeCode account type code | |
189 | * | |
190 | * @return integer count | |
191 | * @static | |
192 | */ | |
ddaa8ef1 PN |
193 | static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') { |
194 | if (!$financialAccountTypeId) { | |
195 | $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); | |
196 | $financialAccountTypeId = array_search('Asset', $financialAccountType); | |
197 | } | |
7d289724 | 198 | $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2 |
ddaa8ef1 | 199 | AND id != %3 AND is_active = 1;"; |
7d289724 PN |
200 | $params = array( |
201 | 1 => array($financialAccountTypeId, 'Integer'), | |
202 | 2 => array(strtolower($accountTypeCode), 'String'), | |
203 | 3 => array($financialAccountId, 'Integer'), | |
204 | ); | |
205 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
206 | } | |
6a488035 TO |
207 | } |
208 |