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