mass update of comment blocks
[civicrm-core.git] / CRM / Financial / BAO / FinancialTypeAccount.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFinancialAccount {
37
38 /**
39 * class constructor
40 */
41 function __construct( ) {
42 parent::__construct( );
43 }
44
45 /**
46 * financial account
47 * @var array
48 * @static
49 */
50 private static $financialAccount;
51
52 /**
53 * Takes a bunch of params that are needed to match certain criteria and
54 * retrieves the relevant objects. Typically the valid params are only
55 * contact_id. We'll tweak this function to be more full featured over a period
56 * of time. This is the inverse function of create. It also stores all the retrieved
57 * values in the default array
58 *
59 * @param array $params (reference ) an assoc array of name/value pairs
60 * @param array $defaults (reference ) an assoc array to hold the flattened values
61 *
62 * @return object CRM_Contribute_BAO_ContributionType object
63 * @access public
64 * @static
65 */
66 static function retrieve(&$params, &$defaults, &$allValues = array()) {
67 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
68 $financialTypeAccount->copyValues($params);
69 $financialTypeAccount->find();
70 while ($financialTypeAccount->fetch()) {
71 CRM_Core_DAO::storeValues($financialTypeAccount, $defaults);
72 $allValues[] = $defaults;
73 }
74 return $defaults;
75 }
76
77 /**
78 * function to add the financial types
79 *
80 * @param array $params reference array contains the values submitted by the form
81 * @param array $ids reference array contains the id
82 *
83 * @access public
84 * @static
85 * @return object
86 */
87 static function add(&$params, &$ids = NULL) {
88 // action is taken depending upon the mode
89 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
90 if ($params['entity_table'] != 'civicrm_financial_type') {
91 $financialTypeAccount->entity_id = $params['entity_id'];
92 $financialTypeAccount->entity_table = $params['entity_table'];
93 $financialTypeAccount->find(TRUE);
94 }
95 else {
96 $financialTypeAccount->id = CRM_Utils_Array::value('entityFinancialAccount', $ids);
97 }
98 if (!empty($ids['entityFinancialAccount'])) {
99 $financialTypeAccount->id = $ids['entityFinancialAccount'];
100 }
101 $financialTypeAccount->copyValues($params);
102 $financialTypeAccount->save();
103 return $financialTypeAccount;
104 }
105
106 /**
107 * Function to delete financial Types
108 *
109 * @param int $contributionTypeId
110 * @static
111 */
112 static function del($financialTypeAccountId, $accountId = null) {
113 //checking if financial type is present
114 $check = false;
115 $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
116
117 $financialTypeId = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id' );
118 //check dependencies
119 // FIXME more table containing financial_type_id to come
120 $dependancy = array(
121 array('Contribute', 'Contribution'),
122 array('Contribute', 'ContributionPage'),
123 array('Member', 'MembershipType'),
124 array('Price', 'PriceFieldValue'),
125 array('Grant', 'Grant'),
126 array('Contribute', 'PremiumsProduct'),
127 array('Contribute', 'Product'),
128 array('Price', 'LineItem'),
129 );
130
131 foreach ($dependancy as $name) {
132 $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
133 $dao = new $daoString();
134 $dao->financial_type_id = $financialTypeId;
135 if ($dao->find(true)) {
136 $check = true;
137 break;
138 }
139 }
140
141 if ($check) {
142 if ($name[1] == 'PremiumsProduct' || $name[1] == 'Product') {
143 CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship while the Financial Type is used for a Premium.', array(1 => $relationValues[$financialTypeAccountId])));
144 }
145 else {
146 $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship');
147 CRM_Core_Session::setStatus(ts('You cannot remove an account with a %1 relationship because it is being referenced by one or more of the following types of records: Contributions, Contribution Pages, or Membership Types. Consider disabling this type instead if you no longer want it used.', array(1 => $relationValues[$accountRelationShipId])), NUll, 'error');
148 }
149 return CRM_Utils_System::redirect( CRM_Utils_System::url( 'civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}" ));
150 }
151
152 //delete from financial Type table
153 $financialType = new CRM_Financial_DAO_EntityFinancialAccount( );
154 $financialType->id = $financialTypeAccountId;
155 $financialType->find(TRUE);
156 $financialType->delete();
157 CRM_Core_Session::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', array(1 => $relationValues[$financialType->account_relationship])));
158 }
159
160 /**
161 * Function to get Financial Account Name
162 *
163 * @param int $entityId
164 *
165 * @param string $entityTable
166 *
167 * @param string $columnName Column to fetch
168 *
169 * @return null|string
170 * @static
171 */
172 static function getFinancialAccount($entityId, $entityTable, $columnName = 'name') {
173 $join = $columnName == 'name' ? 'LEFT JOIN civicrm_financial_account ON civicrm_entity_financial_account.financial_account_id = civicrm_financial_account.id' : NULL;
174 $query = "
175 SELECT {$columnName}
176 FROM civicrm_entity_financial_account
177 {$join}
178 WHERE entity_table = %1
179 AND entity_id = %2";
180
181 $params = array(
182 1 => array($entityTable, 'String'),
183 2 => array($entityId, 'Integer'),
184 );
185 return CRM_Core_DAO::singleValueQuery($query, $params);
186 }
187
188 /**
189 * Function to financial Account for payment instrument
190 *
191 * @param int $paymentInstrumentValue payment instrument value
192 *
193 * @return array|null|string
194 * @static
195 */
196 static function getInstrumentFinancialAccount($paymentInstrumentValue = NULL) {
197 if (!self::$financialAccount) {
198 $query = "SELECT ceft.financial_account_id, cov.value
199 FROM civicrm_entity_financial_account ceft
200 INNER JOIN civicrm_option_value cov ON cov.id = ceft.entity_id AND ceft.entity_table = 'civicrm_option_value'
201 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
202 WHERE cog.name = 'payment_instrument' ";
203
204 if ($paymentInstrumentValue) {
205 $query .= " AND cov.value = '{$paymentInstrumentValue}' ";
206 return CRM_Core_DAO::singleValueQuery($query);
207 }
208 else {
209 $result = CRM_Core_DAO::executeQuery($query);
210 while ($result->fetch()) {
211 self::$financialAccount[$result->value] = $result->financial_account_id;
212 }
213 return self::$financialAccount;
214 }
215 }
216
217 return $paymentInstrumentValue ? self::$financialAccount[$paymentInstrumentValue] : self::$financialAccount;
218 }
219
220 /**
221 * Function to create default entity financial accounts
222 * for financial type
223 * CRM-12470
224 *
225 * @param $financialType
226 *
227 * @return array
228 * @internal param int $financialTypeId financial type id
229 *
230 * @static
231 */
232 static function createDefaultFinancialAccounts($financialType) {
233 $titles = array();
234 $financialAccountTypeID = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type');
235 $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship');
236 $relationships = array (
237 array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID),
238 array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID),
239 array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
240 array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
241 );
242 $params = array(
243 'name' => $financialType->name,
244 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'),
245 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
246 'description' => $financialType->description,
247 'account_type_code' => 'INC',
248 'is_active' => 1,
249 );
250 $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray);
251 $params = array (
252 'entity_table' => 'civicrm_financial_type',
253 'entity_id' => $financialType->id,
254 );
255 foreach ($relationships as $key => $value) {
256 if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
257 $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
258 if (!empty($params['financial_account_id'])) {
259 $titles[] = 'Accounts Receivable';
260 }
261 else {
262 $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
263 LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
264 WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
265 $dao = CRM_Core_DAO::executeQuery($query);
266 $dao->fetch();
267 $params['financial_account_id'] = $dao->financial_account_id;
268 $titles[] = $dao->name;
269 }
270 }
271 elseif ($accountRelationship[$key] == 'Income Account is') {
272 $params['financial_account_id'] = $financialAccount->id;
273 }
274 else {
275 $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
276 $dao = CRM_Core_DAO::executeQuery($query);
277 $dao->fetch();
278 $params['financial_account_id'] = $dao->id;
279 $titles[] = $dao->name;
280 }
281 $params['account_relationship'] = $key;
282 self::add($params);
283 }
284 return $titles;
285 }
286 }
287