Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | */ | |
ecc1e0ef PN |
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; | |
6a488035 | 73 | } |
ecc1e0ef | 74 | return $defaults; |
6a488035 | 75 | } |
03e04002 | 76 | |
6a488035 TO |
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 | |
03e04002 | 82 | * |
6a488035 | 83 | * @access public |
03e04002 | 84 | * @static |
6a488035 TO |
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 | } | |
a7488080 | 98 | if (!empty($ids['entityFinancialAccount'])) { |
6a488035 TO |
99 | $financialTypeAccount->id = $ids['entityFinancialAccount']; |
100 | } | |
101 | $financialTypeAccount->copyValues($params); | |
102 | $financialTypeAccount->save(); | |
103 | return $financialTypeAccount; | |
104 | } | |
03e04002 | 105 | |
6a488035 | 106 | /** |
03e04002 | 107 | * Function to delete financial Types |
108 | * | |
6a488035 TO |
109 | * @param int $contributionTypeId |
110 | * @static | |
111 | */ | |
112 | static function del($financialTypeAccountId, $accountId = null) { | |
03e04002 | 113 | //checking if financial type is present |
6a488035 | 114 | $check = false; |
7611ae71 | 115 | $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); |
03e04002 | 116 | |
6a488035 TO |
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'), | |
9f61848d | 124 | array('Price', 'PriceFieldValue'), |
6a488035 TO |
125 | array('Grant', 'Grant'), |
126 | array('Contribute', 'PremiumsProduct'), | |
127 | array('Contribute', 'Product'), | |
128 | array('Price', 'LineItem'), | |
129 | ); | |
130 | ||
131 | foreach ($dependancy as $name) { | |
4d5c2eb5 | 132 | $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1]; |
133 | $dao = new $daoString(); | |
6a488035 TO |
134 | $dao->financial_type_id = $financialTypeId; |
135 | if ($dao->find(true)) { | |
136 | $check = true; | |
137 | break; | |
138 | } | |
139 | } | |
2efcf0c2 | 140 | |
6a488035 TO |
141 | if ($check) { |
142 | if ($name[1] == 'PremiumsProduct' || $name[1] == 'Product') { | |
10a5be27 | 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]))); |
6a488035 TO |
144 | } |
145 | else { | |
b2e6c21f | 146 | $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship'); |
3b67ab13 | 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'); |
6a488035 TO |
148 | } |
149 | return CRM_Utils_System::redirect( CRM_Utils_System::url( 'civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}" )); | |
150 | } | |
03e04002 | 151 | |
6a488035 TO |
152 | //delete from financial Type table |
153 | $financialType = new CRM_Financial_DAO_EntityFinancialAccount( ); | |
154 | $financialType->id = $financialTypeAccountId; | |
3b67ab13 | 155 | $financialType->find(TRUE); |
6a488035 | 156 | $financialType->delete(); |
0161a899 | 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]))); |
6a488035 | 158 | } |
03e04002 | 159 | |
6a488035 | 160 | /** |
03e04002 | 161 | * Function to get Financial Account Name |
162 | * | |
6a488035 | 163 | * @param int $entityId |
03e04002 | 164 | * |
165 | * @param string $entityTable | |
166 | * | |
6a488035 TO |
167 | * @param string $columnName Column to fetch |
168 | * @static | |
169 | */ | |
170 | static function getFinancialAccount($entityId, $entityTable, $columnName = 'name') { | |
171 | $join = $columnName == 'name' ? 'LEFT JOIN civicrm_financial_account ON civicrm_entity_financial_account.financial_account_id = civicrm_financial_account.id' : NULL; | |
172 | $query = " | |
173 | SELECT {$columnName} | |
174 | FROM civicrm_entity_financial_account | |
175 | {$join} | |
176 | WHERE entity_table = %1 | |
177 | AND entity_id = %2"; | |
178 | ||
179 | $params = array( | |
180 | 1 => array($entityTable, 'String'), | |
181 | 2 => array($entityId, 'Integer'), | |
182 | ); | |
183 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
184 | } | |
185 | ||
186 | /** | |
03e04002 | 187 | * Function to financial Account for payment instrument |
188 | * | |
6a488035 | 189 | * @param int $paymentInstrumentValue payment instrument value |
03e04002 | 190 | * |
6a488035 TO |
191 | * @static |
192 | */ | |
193 | static function getInstrumentFinancialAccount($paymentInstrumentValue = NULL) { | |
194 | if (!self::$financialAccount) { | |
195 | $query = "SELECT ceft.financial_account_id, cov.value | |
196 | FROM civicrm_entity_financial_account ceft | |
03e04002 | 197 | INNER JOIN civicrm_option_value cov ON cov.id = ceft.entity_id AND ceft.entity_table = 'civicrm_option_value' |
198 | INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id | |
6a488035 TO |
199 | WHERE cog.name = 'payment_instrument' "; |
200 | ||
201 | if ($paymentInstrumentValue) { | |
202 | $query .= " AND cov.value = '{$paymentInstrumentValue}' "; | |
203 | return CRM_Core_DAO::singleValueQuery($query); | |
204 | } | |
205 | else { | |
206 | $result = CRM_Core_DAO::executeQuery($query); | |
207 | while ($result->fetch()) { | |
208 | self::$financialAccount[$result->value] = $result->financial_account_id; | |
209 | } | |
210 | return self::$financialAccount; | |
211 | } | |
212 | } | |
213 | ||
214 | return $paymentInstrumentValue ? self::$financialAccount[$paymentInstrumentValue] : self::$financialAccount; | |
215 | } | |
ddaa8ef1 PN |
216 | |
217 | /** | |
218 | * Function to create default entity financial accounts | |
219 | * for financial type | |
8ef12e64 | 220 | * CRM-12470 |
ddaa8ef1 PN |
221 | * |
222 | * @param int $financialTypeId financial type id | |
8ef12e64 | 223 | * |
ddaa8ef1 PN |
224 | * @static |
225 | */ | |
226 | static function createDefaultFinancialAccounts($financialType) { | |
227 | $titles = array(); | |
228 | $financialAccountTypeID = CRM_Core_PseudoConstant::accountOptionValues('financial_account_type'); | |
229 | $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship'); | |
230 | $relationships = array ( | |
231 | array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID), | |
232 | array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID), | |
233 | array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID), | |
234 | array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID), | |
235 | ); | |
236 | $params = array( | |
237 | 'name' => $financialType->name, | |
238 | 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'), | |
239 | 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID), | |
240 | 'description' => $financialType->description, | |
241 | 'account_type_code' => 'INC', | |
242 | 'is_active' => 1, | |
243 | ); | |
244 | $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params, CRM_Core_DAO::$_nullArray); | |
245 | $params = array ( | |
246 | 'entity_table' => 'civicrm_financial_type', | |
247 | 'entity_id' => $financialType->id, | |
248 | ); | |
249 | foreach ($relationships as $key => $value) { | |
250 | if ($accountRelationship[$key] == 'Accounts Receivable Account is') { | |
251 | $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name'); | |
252 | if (!empty($params['financial_account_id'])) { | |
253 | $titles[] = 'Accounts Receivable'; | |
254 | } | |
255 | else { | |
8ef12e64 | 256 | $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account |
ddaa8ef1 PN |
257 | LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id |
258 | WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1"; | |
259 | $dao = CRM_Core_DAO::executeQuery($query); | |
260 | $dao->fetch(); | |
261 | $params['financial_account_id'] = $dao->financial_account_id; | |
262 | $titles[] = $dao->name; | |
263 | } | |
264 | } | |
265 | elseif ($accountRelationship[$key] == 'Income Account is') { | |
266 | $params['financial_account_id'] = $financialAccount->id; | |
267 | } | |
268 | else { | |
269 | $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}"; | |
270 | $dao = CRM_Core_DAO::executeQuery($query); | |
271 | $dao->fetch(); | |
272 | $params['financial_account_id'] = $dao->id; | |
273 | $titles[] = $dao->name; | |
274 | } | |
275 | $params['account_relationship'] = $key; | |
8ef12e64 | 276 | self::add($params); |
ddaa8ef1 PN |
277 | } |
278 | return $titles; | |
279 | } | |
6a488035 TO |
280 | } |
281 |