3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 * 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
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
62 * @param array $allValues
64 * @return object CRM_Contribute_BAO_ContributionType object
68 static function retrieve(&$params, &$defaults, &$allValues = array()) {
69 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
70 $financialTypeAccount->copyValues($params);
71 $financialTypeAccount->find();
72 while ($financialTypeAccount->fetch()) {
73 CRM_Core_DAO
::storeValues($financialTypeAccount, $defaults);
74 $allValues[] = $defaults;
80 * function to add the financial types
82 * @param array $params reference array contains the values submitted by the form
83 * @param array $ids reference array contains the id
89 static function add(&$params, &$ids = NULL) {
90 // action is taken depending upon the mode
91 $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount();
92 if ($params['entity_table'] != 'civicrm_financial_type') {
93 $financialTypeAccount->entity_id
= $params['entity_id'];
94 $financialTypeAccount->entity_table
= $params['entity_table'];
95 $financialTypeAccount->find(TRUE);
98 $financialTypeAccount->id
= CRM_Utils_Array
::value('entityFinancialAccount', $ids);
100 if (!empty($ids['entityFinancialAccount'])) {
101 $financialTypeAccount->id
= $ids['entityFinancialAccount'];
103 $financialTypeAccount->copyValues($params);
104 $financialTypeAccount->save();
105 return $financialTypeAccount;
109 * Function to delete financial Types
111 * @param int $contributionTypeId
114 static function del($financialTypeAccountId, $accountId = null) {
115 //checking if financial type is present
117 $relationValues = CRM_Core_PseudoConstant
::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship');
119 $financialTypeId = CRM_Core_DAO
::getFieldValue( 'CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id' );
121 // FIXME more table containing financial_type_id to come
123 array('Contribute', 'Contribution'),
124 array('Contribute', 'ContributionPage'),
125 array('Member', 'MembershipType'),
126 array('Price', 'PriceFieldValue'),
127 array('Grant', 'Grant'),
128 array('Contribute', 'PremiumsProduct'),
129 array('Contribute', 'Product'),
130 array('Price', 'LineItem'),
133 foreach ($dependancy as $name) {
134 $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1];
135 $dao = new $daoString();
136 $dao->financial_type_id
= $financialTypeId;
137 if ($dao->find(true)) {
144 if ($name[1] == 'PremiumsProduct' ||
$name[1] == 'Product') {
145 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])));
148 $accountRelationShipId = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship');
149 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');
151 return CRM_Utils_System
::redirect( CRM_Utils_System
::url( 'civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}" ));
154 //delete from financial Type table
155 $financialType = new CRM_Financial_DAO_EntityFinancialAccount( );
156 $financialType->id
= $financialTypeAccountId;
157 $financialType->find(TRUE);
158 $financialType->delete();
159 CRM_Core_Session
::setStatus(ts('Unbalanced transactions may be created if you delete the account of type: %1.', array(1 => $relationValues[$financialType->account_relationship
])));
163 * Function to get Financial Account Name
165 * @param int $entityId
167 * @param string $entityTable
169 * @param string $columnName Column to fetch
171 * @return null|string
174 static function getFinancialAccount($entityId, $entityTable, $columnName = 'name') {
175 $join = $columnName == 'name' ?
'LEFT JOIN civicrm_financial_account ON civicrm_entity_financial_account.financial_account_id = civicrm_financial_account.id' : NULL;
178 FROM civicrm_entity_financial_account
180 WHERE entity_table = %1
184 1 => array($entityTable, 'String'),
185 2 => array($entityId, 'Integer'),
187 return CRM_Core_DAO
::singleValueQuery($query, $params);
191 * Function to financial Account for payment instrument
193 * @param int $paymentInstrumentValue payment instrument value
195 * @return array|null|string
198 static function getInstrumentFinancialAccount($paymentInstrumentValue = NULL) {
199 if (!self
::$financialAccount) {
200 $query = "SELECT ceft.financial_account_id, cov.value
201 FROM civicrm_entity_financial_account ceft
202 INNER JOIN civicrm_option_value cov ON cov.id = ceft.entity_id AND ceft.entity_table = 'civicrm_option_value'
203 INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id
204 WHERE cog.name = 'payment_instrument' ";
206 if ($paymentInstrumentValue) {
207 $query .= " AND cov.value = '{$paymentInstrumentValue}' ";
208 return CRM_Core_DAO
::singleValueQuery($query);
211 $result = CRM_Core_DAO
::executeQuery($query);
212 while ($result->fetch()) {
213 self
::$financialAccount[$result->value
] = $result->financial_account_id
;
215 return self
::$financialAccount;
219 return $paymentInstrumentValue ? self
::$financialAccount[$paymentInstrumentValue] : self
::$financialAccount;
223 * Function to create default entity financial accounts
227 * @param $financialType
230 * @internal param int $financialTypeId financial type id
234 static function createDefaultFinancialAccounts($financialType) {
236 $financialAccountTypeID = CRM_Core_PseudoConstant
::accountOptionValues('financial_account_type');
237 $accountRelationship = CRM_Core_PseudoConstant
::accountOptionValues('account_relationship');
238 $relationships = array (
239 array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID),
240 array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID),
241 array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID),
242 array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID),
245 'name' => $financialType->name
,
246 'contact_id' => CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config
::domainID(), 'contact_id'),
247 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID),
248 'description' => $financialType->description
,
249 'account_type_code' => 'INC',
252 $financialAccount = CRM_Financial_BAO_FinancialAccount
::add($params, CRM_Core_DAO
::$_nullArray);
254 'entity_table' => 'civicrm_financial_type',
255 'entity_id' => $financialType->id
,
257 foreach ($relationships as $key => $value) {
258 if ($accountRelationship[$key] == 'Accounts Receivable Account is') {
259 $params['financial_account_id'] = CRM_Core_DAO
::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name');
260 if (!empty($params['financial_account_id'])) {
261 $titles[] = 'Accounts Receivable';
264 $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account
265 LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
266 WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1";
267 $dao = CRM_Core_DAO
::executeQuery($query);
269 $params['financial_account_id'] = $dao->financial_account_id
;
270 $titles[] = $dao->name
;
273 elseif ($accountRelationship[$key] == 'Income Account is') {
274 $params['financial_account_id'] = $financialAccount->id
;
277 $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}";
278 $dao = CRM_Core_DAO
::executeQuery($query);
280 $params['financial_account_id'] = $dao->id
;
281 $titles[] = $dao->name
;
283 $params['account_relationship'] = $key;