Merge pull request #4906 from eileenmcnaughton/minor-tidies
[civicrm-core.git] / CRM / Financial / BAO / FinancialAccount.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
6a488035
TO
35class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount {
36
37 /**
100fef9d 38 * Static holder for the default LT
6a488035 39 */
ac7514c2 40 static $_defaultContributionType = NULL;
6a488035
TO
41
42 /**
100fef9d 43 * Class constructor
6a488035 44 */
00be9182 45 public function __construct() {
ac7514c2 46 parent::__construct();
6a488035 47 }
0e6e8724 48
6a488035 49 /**
c490a46a 50 * Fetch object based on array of properties
6a488035 51 *
ed5dd492
TO
52 * @param array $params
53 * (reference ) an assoc array of name/value pairs.
54 * @param array $defaults
55 * (reference ) an assoc array to hold the flattened values.
6a488035 56 *
72b3a70c 57 * @return CRM_Financial_BAO_FinancialAccount
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 72 *
ed5dd492
TO
73 * @param int $id
74 * Id of the database record.
75 * @param bool $is_active
76 * Value we want to set the is_active field.
6a488035 77 *
72b3a70c
CW
78 * @return CRM_Core_DAO|null
79 * DAO object on success, null otherwise
6a488035
TO
80 * @static
81 */
00be9182 82 public static function setIsActive($id, $is_active) {
6a488035
TO
83 return CRM_Core_DAO::setFieldValue('CRM_Financial_DAO_FinancialAccount', $id, 'is_active', $is_active);
84 }
85
86 /**
100fef9d 87 * Add the financial types
6a488035 88 *
ed5dd492
TO
89 * @param array $params
90 * Reference array contains the values submitted by the form.
91 * @param array $ids
92 * Reference array contains the id.
0e6e8724 93 *
0e6e8724 94 * @static
72b3a70c 95 * @return CRM_Financial_DAO_FinancialAccount
6a488035 96 */
00be9182 97 public static function add(&$params, &$ids = array()) {
22e263ad 98 if (empty($params['id'])) {
bc2bc079 99 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
100 $params['is_deductible'] = CRM_Utils_Array::value('is_deductible', $params, FALSE);
101 $params['is_tax'] = CRM_Utils_Array::value('is_tax', $params, FALSE);
102 $params['is_header_account'] = CRM_Utils_Array::value('is_header_account', $params, FALSE);
103 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
104 }
a7488080 105 if (!empty($params['is_default'])) {
ac7514c2
PN
106 $query = 'UPDATE civicrm_financial_account SET is_default = 0 WHERE financial_account_type_id = %1';
107 $queryParams = array(1 => array($params['financial_account_type_id'], 'Integer'));
108 CRM_Core_DAO::executeQuery($query, $queryParams);
0e6e8724
DL
109 }
110
6a488035 111 // action is taken depending upon the mode
ac7514c2 112 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
6a488035 113 $financialAccount->copyValues($params);
e5439581 114 if (!empty($ids['contributionType'])) {
115 $financialAccount->id = CRM_Utils_Array::value('contributionType', $ids);
116 }
ac7514c2 117 $financialAccount->save();
6a488035
TO
118 return $financialAccount;
119 }
0e6e8724 120
6a488035 121 /**
100fef9d 122 * Delete financial Types
0e6e8724 123 *
6a488035
TO
124 * @param int $financialAccountId
125 * @static
126 */
00be9182 127 public static function del($financialAccountId) {
0e6e8724 128 //checking if financial type is present
ac7514c2 129 $check = FALSE;
0e6e8724 130
6a488035 131 //check dependencies
0e6e8724
DL
132 $dependancy = array(
133 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
134 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
353ffa53 135 );
6a488035 136 foreach ($dependancy as $name) {
045f52a3 137 require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php";
0e6e8724
DL
138 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
139 $bao = new $className();
6a488035 140 $bao->$name[2] = $financialAccountId;
ac7514c2
PN
141 if ($bao->find(TRUE)) {
142 $check = TRUE;
6a488035
TO
143 }
144 }
0e6e8724 145
6a488035 146 if ($check) {
ac7514c2
PN
147 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.'));
148 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
6a488035 149 }
0e6e8724 150
6a488035 151 //delete from financial Type table
ac7514c2 152 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
6a488035
TO
153 $financialAccount->id = $financialAccountId;
154 $financialAccount->delete();
155 }
0e6e8724 156
6a488035 157 /**
100fef9d 158 * Get accounting code for a financial type with account relation Income Account is
6a488035 159 *
100fef9d 160 * @param int $financialTypeId
da6b46f4 161 *
72b3a70c
CW
162 * @return int
163 * accounting code
6a488035
TO
164 * @static
165 */
00be9182 166 public 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
169FROM civicrm_financial_type cft
170LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
171LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
172WHERE 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 181 /**
100fef9d 182 * Get AR account
7d289724 183 *
ed5dd492
TO
184 * @param $financialAccountId
185 * Financial account id.
7d289724 186 *
ed5dd492
TO
187 * @param $financialAccountTypeId
188 * Financial account type id.
7d289724 189 *
72b3a70c
CW
190 * @param string $accountTypeCode
191 * account type code
7d289724 192 *
df8d3074 193 * @return int
a6c01b45 194 * count
7d289724
PN
195 * @static
196 */
00be9182 197 public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
ddaa8ef1
PN
198 if (!$financialAccountTypeId) {
199 $financialAccountType = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
200 $financialAccountTypeId = array_search('Asset', $financialAccountType);
201 }
7d289724 202 $query = "SELECT count(id) FROM civicrm_financial_account WHERE financial_account_type_id = %1 AND LCASE(account_type_code) = %2
ddaa8ef1 203 AND id != %3 AND is_active = 1;";
7d289724
PN
204 $params = array(
205 1 => array($financialAccountTypeId, 'Integer'),
206 2 => array(strtolower($accountTypeCode), 'String'),
207 3 => array($financialAccountId, 'Integer'),
208 );
045f52a3 209 return CRM_Core_DAO::singleValueQuery($query, $params);
7d289724 210 }
6a488035 211}