Commit | Line | Data |
---|---|---|
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 | ||
37 | class 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 | */ |
00be9182 | 47 | public function __construct() { |
ac7514c2 | 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 | * @static |
59 | */ | |
00be9182 | 60 | public static function retrieve(&$params, &$defaults) { |
ac7514c2 PN |
61 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
62 | $financialAccount->copyValues($params); | |
63 | if ($financialAccount->find(TRUE)) { | |
6a488035 TO |
64 | CRM_Core_DAO::storeValues($financialAccount, $defaults); |
65 | return $financialAccount; | |
66 | } | |
ac7514c2 | 67 | return NULL; |
6a488035 TO |
68 | } |
69 | ||
70 | /** | |
100fef9d | 71 | * Update the is_active flag in the db |
6a488035 TO |
72 | * |
73 | * @param int $id id of the database record | |
74 | * @param boolean $is_active value we want to set the is_active field | |
75 | * | |
76 | * @return Object DAO object on sucess, null otherwise | |
77 | * @static | |
78 | */ | |
00be9182 | 79 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
80 | return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active); |
81 | } | |
82 | ||
83 | /** | |
100fef9d | 84 | * Add the financial types |
6a488035 TO |
85 | * |
86 | * @param array $params reference array contains the values submitted by the form | |
87 | * @param array $ids reference array contains the id | |
0e6e8724 | 88 | * |
0e6e8724 | 89 | * @static |
6a488035 TO |
90 | * @return object |
91 | */ | |
00be9182 | 92 | public static function add(&$params, &$ids = array()) { |
bc2bc079 | 93 | if(empty($params['id'])) { |
94 | $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE); | |
95 | $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE); | |
96 | $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE); | |
97 | $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE); | |
98 | $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE); | |
99 | } | |
a7488080 | 100 | if (!empty($params['is_default'])) { |
ac7514c2 PN |
101 | $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1'; |
102 | $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer')); | |
103 | CRM_Core_DAO::executeQuery($query, $queryParams); | |
0e6e8724 DL |
104 | } |
105 | ||
6a488035 | 106 | // action is taken depending upon the mode |
ac7514c2 | 107 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
6a488035 | 108 | $financialAccount->copyValues($params); |
e5439581 | 109 | if (!empty($ids['contributionType'])) { |
110 | $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids); | |
111 | } | |
ac7514c2 | 112 | $financialAccount->save(); |
6a488035 TO |
113 | return $financialAccount; |
114 | } | |
0e6e8724 | 115 | |
6a488035 | 116 | /** |
100fef9d | 117 | * Delete financial Types |
0e6e8724 | 118 | * |
6a488035 TO |
119 | * @param int $financialAccountId |
120 | * @static | |
121 | */ | |
00be9182 | 122 | public static function del($financialAccountId) { |
0e6e8724 | 123 | //checking if financial type is present |
ac7514c2 | 124 | $check = FALSE; |
0e6e8724 | 125 | |
6a488035 | 126 | //check dependencies |
0e6e8724 DL |
127 | $dependancy = array( |
128 | array('Core', 'FinancialTrxn', 'to_financial_account_id'), | |
129 | array('Financial', 'FinancialTypeAccount', 'financial_account_id'), | |
6a488035 TO |
130 | ); |
131 | foreach ($dependancy as $name) { | |
132 | require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php"); | |
0e6e8724 DL |
133 | $className = "CRM_{$name[0]}_BAO_{$name[1]}"; |
134 | $bao = new $className(); | |
6a488035 | 135 | $bao->$name[2] = $financialAccountId; |
ac7514c2 PN |
136 | if ($bao->find(TRUE)) { |
137 | $check = TRUE; | |
6a488035 TO |
138 | } |
139 | } | |
0e6e8724 | 140 | |
6a488035 | 141 | if ($check) { |
ac7514c2 PN |
142 | 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.')); |
143 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse")); | |
6a488035 | 144 | } |
0e6e8724 | 145 | |
6a488035 | 146 | //delete from financial Type table |
ac7514c2 | 147 | $financialAccount = new CRM_Financial_DAO_FinancialAccount(); |
6a488035 TO |
148 | $financialAccount->id = $financialAccountId; |
149 | $financialAccount->delete(); | |
150 | } | |
0e6e8724 | 151 | |
6a488035 | 152 | /** |
100fef9d | 153 | * Get accounting code for a financial type with account relation Income Account is |
6a488035 TO |
154 | * |
155 | * @financialTypeId int Financial Type Id | |
156 | * | |
100fef9d | 157 | * @param int $financialTypeId |
da6b46f4 | 158 | * |
6a488035 TO |
159 | * @return accounting code |
160 | * @static | |
161 | */ | |
00be9182 | 162 | public static function getAccountingCode($financialTypeId) { |
f743a6eb | 163 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); |
6a488035 TO |
164 | $query = "SELECT cfa.accounting_code |
165 | FROM civicrm_financial_type cft | |
166 | LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type' | |
167 | LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id | |
168 | WHERE cft.id = %1 | |
169 | AND account_relationship = %2"; | |
170 | $params = array( | |
171 | 1 => array($financialTypeId, 'Integer'), | |
172 | 2 => array($relationTypeId, 'Integer'), | |
173 | ); | |
174 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
175 | } | |
0e6e8724 | 176 | |
7d289724 | 177 | /** |
100fef9d | 178 | * Get AR account |
7d289724 PN |
179 | * |
180 | * @param $financialAccountId financial account id | |
181 | * | |
182 | * @param $financialAccountTypeId financial account type id | |
183 | * | |
da6b46f4 | 184 | * @param \account|string $accountTypeCode account type code |
7d289724 PN |
185 | * |
186 | * @return integer count | |
187 | * @static | |
188 | */ | |
00be9182 | 189 | public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') { |
ddaa8ef1 PN |
190 | if (!$financialAccountTypeId) { |
191 | $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); | |
192 | $financialAccountTypeId = array_search('Asset', $financialAccountType); | |
193 | } | |
7d289724 | 194 | $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2 |
ddaa8ef1 | 195 | AND id != %3 AND is_active = 1;"; |
7d289724 PN |
196 | $params = array( |
197 | 1 => array($financialAccountTypeId, 'Integer'), | |
198 | 2 => array(strtolower($accountTypeCode), 'String'), | |
199 | 3 => array($financialAccountId, 'Integer'), | |
200 | ); | |
201 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
202 | } | |
6a488035 | 203 | } |