CRM-15851 search builder fix for custom date fields
[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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
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 58 */
00be9182 59 public static function retrieve(&$params, &$defaults) {
ac7514c2
PN
60 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
61 $financialAccount->copyValues($params);
62 if ($financialAccount->find(TRUE)) {
6a488035
TO
63 CRM_Core_DAO::storeValues($financialAccount, $defaults);
64 return $financialAccount;
65 }
ac7514c2 66 return NULL;
6a488035
TO
67 }
68
69 /**
100fef9d 70 * Update the is_active flag in the db
6a488035 71 *
ed5dd492
TO
72 * @param int $id
73 * Id of the database record.
74 * @param bool $is_active
75 * Value we want to set the is_active field.
6a488035 76 *
72b3a70c
CW
77 * @return CRM_Core_DAO|null
78 * DAO object on success, null otherwise
6a488035 79 */
00be9182 80 public static function setIsActive($id, $is_active) {
6a488035
TO
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 86 *
ed5dd492
TO
87 * @param array $params
88 * Reference array contains the values submitted by the form.
89 * @param array $ids
90 * Reference array contains the id.
0e6e8724 91 *
72b3a70c 92 * @return CRM_Financial_DAO_FinancialAccount
6a488035 93 */
00be9182 94 public static function add(&$params, &$ids = array()) {
22e263ad 95 if (empty($params['id'])) {
bc2bc079 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 121 * @param int $financialAccountId
6a488035 122 */
00be9182 123 public static function del($financialAccountId) {
0e6e8724 124 //checking if financial type is present
ac7514c2 125 $check = FALSE;
0e6e8724 126
6a488035 127 //check dependencies
0e6e8724
DL
128 $dependancy = array(
129 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
130 array('Financial', 'FinancialTypeAccount', 'financial_account_id'),
353ffa53 131 );
6a488035 132 foreach ($dependancy as $name) {
045f52a3 133 require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php";
0e6e8724
DL
134 $className = "CRM_{$name[0]}_BAO_{$name[1]}";
135 $bao = new $className();
6a488035 136 $bao->$name[2] = $financialAccountId;
ac7514c2
PN
137 if ($bao->find(TRUE)) {
138 $check = TRUE;
6a488035
TO
139 }
140 }
0e6e8724 141
6a488035 142 if ($check) {
ac7514c2
PN
143 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.'));
144 return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialAccount', "reset=1&action=browse"));
6a488035 145 }
0e6e8724 146
6a488035 147 //delete from financial Type table
ac7514c2 148 $financialAccount = new CRM_Financial_DAO_FinancialAccount();
6a488035
TO
149 $financialAccount->id = $financialAccountId;
150 $financialAccount->delete();
151 }
0e6e8724 152
6a488035 153 /**
100fef9d 154 * Get accounting code for a financial type with account relation Income Account is
6a488035 155 *
100fef9d 156 * @param int $financialTypeId
da6b46f4 157 *
72b3a70c
CW
158 * @return int
159 * accounting code
6a488035 160 */
00be9182 161 public static function getAccountingCode($financialTypeId) {
f743a6eb 162 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
6a488035
TO
163 $query = "SELECT cfa.accounting_code
164FROM civicrm_financial_type cft
165LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
166LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
167WHERE cft.id = %1
168 AND account_relationship = %2";
169 $params = array(
170 1 => array($financialTypeId, 'Integer'),
171 2 => array($relationTypeId, 'Integer'),
172 );
173 return CRM_Core_DAO::singleValueQuery($query, $params);
174 }
0e6e8724 175
7d289724 176 /**
100fef9d 177 * Get AR account
7d289724 178 *
ed5dd492
TO
179 * @param $financialAccountId
180 * Financial account id.
7d289724 181 *
ed5dd492
TO
182 * @param $financialAccountTypeId
183 * Financial account type id.
7d289724 184 *
72b3a70c
CW
185 * @param string $accountTypeCode
186 * account type code
7d289724 187 *
df8d3074 188 * @return int
a6c01b45 189 * count
7d289724 190 */
00be9182 191 public static function getARAccounts($financialAccountId, $financialAccountTypeId = NULL, $accountTypeCode = 'ar') {
ddaa8ef1
PN
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 );
045f52a3 203 return CRM_Core_DAO::singleValueQuery($query, $params);
7d289724 204 }
96025800 205
6a488035 206}