Merge pull request #581 from eileenmcnaughton/CRM-12472
[civicrm-core.git] / CRM / Financial / BAO / FinancialAccount.php
CommitLineData
6a488035
TO
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
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
32 * @copyright CiviCRM LLC (c) 2004-2013
33 * $Id$
34 *
35 */
36
37class CRM_Financial_BAO_FinancialAccount extends CRM_Financial_DAO_FinancialAccount {
38
39 /**
40 * static holder for the default LT
41 */
42 static $_defaultContributionType = null;
43
44 /**
45 * class constructor
46 */
47 function __construct( ) {
48 parent::__construct( );
49 }
50
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) {
66 $financialAccount = new CRM_Financial_DAO_FinancialAccount( );
67 $financialAccount->copyValues( $params );
68 if ($financialAccount->find(true)) {
69 CRM_Core_DAO::storeValues($financialAccount, $defaults);
70 return $financialAccount;
71 }
72 return null;
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
93 *
94 * @access public
95 * @static
96 * @return object
97 */
98 static function add(&$params, &$ids) {
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 if (CRM_Utils_Array::value('is_default', $params)) {
105 $query = 'UPDATE civicrm_financial_account SET is_default = 0';
106 CRM_Core_DAO::executeQuery( $query );
107 }
108
109 // action is taken depending upon the mode
110 $financialAccount = new CRM_Financial_DAO_FinancialAccount( );
111 $financialAccount->copyValues($params);
112 $financialAccount->id = CRM_Utils_Array::value( 'contributionType', $ids );
113 $financialAccount->save( );
114 return $financialAccount;
115 }
116
117 /**
118 * Function to delete financial Types
119 *
120 * @param int $financialAccountId
121 * @static
122 */
123 static function financialAccountValidation($fields,&$errors) {
124 $financialAccount = array( );
125 if (CRM_Utils_Array::value('financial_type_id', $fields)) {
126 CRM_Core_PseudoConstant::populate( $financialAccount,
127 'CRM_Financial_DAO_EntityFinancialAccount',
128 $all = True,
129 $retrieve = 'financial_account_id',
130 $filter = null,
131 " account_relationship = 6 AND entity_id = {$fields['financial_type_id']} " );
132 if ( !current( $financialAccount ) ) {
133 $errors['financial_type_id'] = "Financial Account of account relationship of 'Asset Account is' is not configured for this Financial Type";
134 }
135 }
136 }
137
138 /**
139 * Function to delete financial Types
140 *
141 * @param int $financialAccountId
142 * @static
143 */
144 static function del($financialAccountId) {
145 //checking if financial type is present
146 $check = false;
147
148 //check dependencies
149 $dependancy = array(
150 array('Core', 'FinancialTrxn', 'to_financial_account_id'),
151 array('Financial', 'FinancialTypeAccount', 'financial_account_id' ),
152 );
153 foreach ($dependancy as $name) {
154 require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_" . $name[0] . "_BAO_" . $name[1]) . ".php");
155 eval('$bao = new CRM_' . $name[0] . '_BAO_' . $name[1] . '();');
156 $bao->$name[2] = $financialAccountId;
157 if ($bao->find(true)) {
158 $check = true;
159 }
160 }
161
162 if ($check) {
163 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.') );
164 return CRM_Utils_System::redirect( CRM_Utils_System::url( 'civicrm/admin/financial/financialAccount', "reset=1&action=browse" ));
165 }
166
167 //delete from financial Type table
168 $financialAccount = new CRM_Financial_DAO_FinancialAccount( );
169 $financialAccount->id = $financialAccountId;
170 $financialAccount->delete();
171 }
172
173 /**
174 * get accounting code for a financial type with account relation Income Account is
175 *
176 * @financialTypeId int Financial Type Id
177 *
178 * @return accounting code
179 * @static
180 */
181 static function getAccountingCode($financialTypeId) {
182 $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
183 $query = "SELECT cfa.accounting_code
184FROM civicrm_financial_type cft
185LEFT JOIN civicrm_entity_financial_account cefa ON cefa.entity_id = cft.id AND cefa.entity_table = 'civicrm_financial_type'
186LEFT JOIN civicrm_financial_account cfa ON cefa.financial_account_id = cfa.id
187WHERE cft.id = %1
188 AND account_relationship = %2";
189 $params = array(
190 1 => array($financialTypeId, 'Integer'),
191 2 => array($relationTypeId, 'Integer'),
192 );
193 return CRM_Core_DAO::singleValueQuery($query, $params);
194 }
195}
196