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