3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Financial_BAO_FinancialTypeAccount
extends CRM_Financial_DAO_EntityFinancialAccount
{
41 function __construct( ) {
42 parent
::__construct( );
50 private static $financialAccount;
53 * Fetch object based on array of properties
55 * @param array $params (reference ) an assoc array of name/value pairs
56 * @param array $defaults (reference ) an assoc array to hold the flattened values
58 * @param array $allValues
60 * @return CRM_Contribute_BAO_ContributionType object
64 static function retrieve(&$params, &$defaults, &$allValues = array()) {
65 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
66 $financialTypeAccount->copyValues($params);
67 $financialTypeAccount->find();
68 while ($financialTypeAccount->fetch()) {
69 CRM_Core_DAO
::storeValues($financialTypeAccount, $defaults);
70 $allValues[] = $defaults;
76 * Add the financial types
78 * @param array $params reference array contains the values submitted by the form
79 * @param array $ids reference array contains the id
85 static function add(&$params, &$ids = NULL) {
86 // action is taken depending upon the mode
87 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
88 if ($params['entity_table'] != 'civicrm_financial_type') {
89 $financialTypeAccount->entity_id
= $params['entity_id'];
90 $financialTypeAccount->entity_table
= $params['entity_table'];
91 $financialTypeAccount->find(TRUE);
94 $financialTypeAccount->id
= CRM_Utils_Array
::value('entityFinancialAccount', $ids);
96 if (!empty($ids['entityFinancialAccount'])) {
97 $financialTypeAccount->id
= $ids['entityFinancialAccount'];
99 $financialTypeAccount->copyValues($params);
100 $financialTypeAccount->save();
101 return $financialTypeAccount;
105 * Delete financial Types
107 * @param int $financialTypeAccountId
108 * @param int $accountId
112 static function del($financialTypeAccountId, $accountId = null) {
113 //checking if financial type is present
115 $relationValues = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
117 $financialTypeId = CRM_Core_DAO
::getFieldValue( 'CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id' );
119 // FIXME more table containing financial_type_id to come
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'),
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)) {
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])));
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');
149 return CRM_Utils_System
::redirect( CRM_Utils_System
::url( 'civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}" ));
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
])));
161 * Get Financial Account Name
163 * @param int $entityId
165 * @param string $entityTable
167 * @param string $columnName Column to fetch
169 * @return null|string
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;
176 FROM civicrm_entity_financial_account
178 WHERE entity_table = %1
182 1 => array($entityTable, 'String'),
183 2 => array($entityId, 'Integer'),
185 return CRM_Core_DAO
::singleValueQuery($query, $params);
189 * Financial Account for payment instrument
191 * @param int $paymentInstrumentValue payment instrument value
193 * @return array|null|string
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' ";
204 if ($paymentInstrumentValue) {
205 $query .= " AND cov.value = '{$paymentInstrumentValue}' ";
206 return CRM_Core_DAO
::singleValueQuery($query);
209 $result = CRM_Core_DAO
::executeQuery($query);
210 while ($result->fetch()) {
211 self
::$financialAccount[$result->value
] = $result->financial_account_id
;
213 return self
::$financialAccount;
217 return $paymentInstrumentValue ? self
::$financialAccount[$paymentInstrumentValue] : self
::$financialAccount;
221 * Create default entity financial accounts
225 * @param $financialType
230 static function createDefaultFinancialAccounts($financialType) {
232 $financialAccountTypeID = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type');
233 $accountRelationship = CRM_Core_PseudoConstant
::accountOptionValues('account_relationship');
234 $relationships = array (
235 array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID),
236 array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID),
237 array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
238 array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
241 $dao = CRM_Core_DAO
::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1',
242 array(1 => array($financialType->name
, 'String'))
245 $existingFinancialAccount = array();
248 'name' => $financialType->name
,
249 'contact_id' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config
::domainID(), 'contact_id'),
250 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
251 'description' => $financialType->description
,
252 'account_type_code' => 'INC',
255 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params, CRM_Core_DAO
::$_nullArray);
258 $existingFinancialAccount[$dao->financial_account_type_id
] = $dao->id
;
261 'entity_table' => 'civicrm_financial_type',
262 'entity_id' => $financialType->id
,
264 foreach ($relationships as $key => $value) {
265 if (!array_key_exists($value, $existingFinancialAccount)) {
266 if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
267 $params['financial_account_id'] = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
268 if (!empty($params['financial_account_id'])) {
269 $titles[] = 'Accounts Receivable';
272 $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
273 LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
274 WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
275 $dao = CRM_Core_DAO
::executeQuery($query);
277 $params['financial_account_id'] = $dao->financial_account_id
;
278 $titles[] = $dao->name
;
281 elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) {
282 $params['financial_account_id'] = $financialAccount->id
;
285 $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
286 $dao = CRM_Core_DAO
::executeQuery($query);
288 $params['financial_account_id'] = $dao->id
;
289 $titles[] = $dao->name
;
293 $params['financial_account_id'] = $existingFinancialAccount[$value];
294 $titles[] = $financialType->name
;
296 $params['account_relationship'] = $key;
299 if (!empty($existingFinancialAccount)) {