Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 | 32 | */ |
6a488035 TO |
33 | class CRM_Financial_BAO_FinancialTypeAccount extends CRM_Financial_DAO_EntityFinancialAccount { |
34 | ||
35 | /** | |
fe482240 | 36 | * Class constructor. |
6a488035 | 37 | */ |
045f52a3 | 38 | public function __construct() { |
481a74f4 | 39 | parent::__construct(); |
6a488035 TO |
40 | } |
41 | ||
6a488035 | 42 | /** |
fe482240 | 43 | * Fetch object based on array of properties. |
6a488035 | 44 | * |
ed5dd492 TO |
45 | * @param array $params |
46 | * (reference ) an assoc array of name/value pairs. | |
47 | * @param array $defaults | |
48 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 49 | * |
6c8f6e67 EM |
50 | * @param array $allValues |
51 | * | |
16b10e64 | 52 | * @return CRM_Contribute_BAO_ContributionType |
6a488035 | 53 | */ |
00be9182 | 54 | public static function retrieve(&$params, &$defaults, &$allValues = array()) { |
ecc1e0ef PN |
55 | $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount(); |
56 | $financialTypeAccount->copyValues($params); | |
57 | $financialTypeAccount->find(); | |
58 | while ($financialTypeAccount->fetch()) { | |
59 | CRM_Core_DAO::storeValues($financialTypeAccount, $defaults); | |
60 | $allValues[] = $defaults; | |
6a488035 | 61 | } |
ecc1e0ef | 62 | return $defaults; |
6a488035 | 63 | } |
03e04002 | 64 | |
6a488035 | 65 | /** |
fe482240 | 66 | * Add the financial types. |
6a488035 | 67 | * |
ed5dd492 TO |
68 | * @param array $params |
69 | * Reference array contains the values submitted by the form. | |
70 | * @param array $ids | |
71 | * Reference array contains the id. | |
03e04002 | 72 | * |
6a488035 TO |
73 | * @return object |
74 | */ | |
00be9182 | 75 | public static function add(&$params, &$ids = NULL) { |
6a488035 TO |
76 | // action is taken depending upon the mode |
77 | $financialTypeAccount = new CRM_Financial_DAO_EntityFinancialAccount(); | |
78 | if ($params['entity_table'] != 'civicrm_financial_type') { | |
79 | $financialTypeAccount->entity_id = $params['entity_id']; | |
80 | $financialTypeAccount->entity_table = $params['entity_table']; | |
81 | $financialTypeAccount->find(TRUE); | |
82 | } | |
a7488080 | 83 | if (!empty($ids['entityFinancialAccount'])) { |
6a488035 | 84 | $financialTypeAccount->id = $ids['entityFinancialAccount']; |
b70cd45c | 85 | $financialTypeAccount->find(TRUE); |
6a488035 TO |
86 | } |
87 | $financialTypeAccount->copyValues($params); | |
b70cd45c | 88 | self::validateRelationship($financialTypeAccount); |
6a488035 TO |
89 | $financialTypeAccount->save(); |
90 | return $financialTypeAccount; | |
91 | } | |
03e04002 | 92 | |
6a488035 | 93 | /** |
fe482240 | 94 | * Delete financial Types. |
03e04002 | 95 | * |
c490a46a CW |
96 | * @param int $financialTypeAccountId |
97 | * @param int $accountId | |
fd31fa4c | 98 | * |
6a488035 | 99 | */ |
045f52a3 | 100 | public static function del($financialTypeAccountId, $accountId = NULL) { |
cded2ebf | 101 | // check if financial type is present |
045f52a3 | 102 | $check = FALSE; |
7611ae71 | 103 | $relationValues = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); |
03e04002 | 104 | |
481a74f4 | 105 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'entity_id'); |
cded2ebf | 106 | // check dependencies |
6a488035 | 107 | // FIXME more table containing financial_type_id to come |
b44e3f84 | 108 | $dependency = array( |
6a488035 TO |
109 | array('Contribute', 'Contribution'), |
110 | array('Contribute', 'ContributionPage'), | |
111 | array('Member', 'MembershipType'), | |
9f61848d | 112 | array('Price', 'PriceFieldValue'), |
6a488035 TO |
113 | array('Grant', 'Grant'), |
114 | array('Contribute', 'PremiumsProduct'), | |
115 | array('Contribute', 'Product'), | |
116 | array('Price', 'LineItem'), | |
117 | ); | |
118 | ||
b44e3f84 | 119 | foreach ($dependency as $name) { |
4d5c2eb5 | 120 | $daoString = 'CRM_' . $name[0] . '_DAO_' . $name[1]; |
121 | $dao = new $daoString(); | |
6a488035 | 122 | $dao->financial_type_id = $financialTypeId; |
045f52a3 TO |
123 | if ($dao->find(TRUE)) { |
124 | $check = TRUE; | |
6a488035 TO |
125 | break; |
126 | } | |
127 | } | |
2efcf0c2 | 128 | |
6a488035 TO |
129 | if ($check) { |
130 | if ($name[1] == 'PremiumsProduct' || $name[1] == 'Product') { | |
10a5be27 | 131 | 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 |
132 | } |
133 | else { | |
b2e6c21f | 134 | $accountRelationShipId = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_EntityFinancialAccount', $financialTypeAccountId, 'account_relationship'); |
045f52a3 | 135 | 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 | 136 | } |
481a74f4 | 137 | return CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/financial/financialType/accounts', "reset=1&action=browse&aid={$accountId}")); |
6a488035 | 138 | } |
03e04002 | 139 | |
cded2ebf | 140 | // delete from financial Type table |
481a74f4 | 141 | $financialType = new CRM_Financial_DAO_EntityFinancialAccount(); |
6a488035 | 142 | $financialType->id = $financialTypeAccountId; |
3b67ab13 | 143 | $financialType->find(TRUE); |
6a488035 | 144 | $financialType->delete(); |
0161a899 | 145 | 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 | 146 | } |
03e04002 | 147 | |
6a488035 | 148 | /** |
fe482240 | 149 | * Financial Account for payment instrument. |
03e04002 | 150 | * |
ed5dd492 TO |
151 | * @param int $paymentInstrumentValue |
152 | * Payment instrument value. | |
03e04002 | 153 | * |
d8fbd797 | 154 | * @return null|int |
6a488035 | 155 | */ |
d8fbd797 | 156 | public static function getInstrumentFinancialAccount($paymentInstrumentValue) { |
fe74d2b5 | 157 | if (!isset(\Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue])) { |
158 | $paymentInstrumentID = civicrm_api3('OptionValue', 'getvalue', array( | |
159 | 'return' => 'id', | |
160 | 'value' => $paymentInstrumentValue, | |
161 | 'option_group_id' => "payment_instrument", | |
162 | )); | |
163 | $accounts = civicrm_api3('EntityFinancialAccount', 'get', [ | |
164 | 'return' => 'financial_account_id', | |
165 | 'entity_table' => 'civicrm_option_value', | |
166 | 'entity_id' => $paymentInstrumentID, | |
167 | 'options' => ['limit' => 1], | |
168 | 'sequential' => 1, | |
169 | ])['values']; | |
170 | if (empty($accounts)) { | |
171 | \Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue] = NULL; | |
172 | } | |
173 | else { | |
174 | \Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue] = $accounts[0]['financial_account_id']; | |
175 | } | |
176 | } | |
177 | return \Civi::$statics[__CLASS__]['instrument_financial_accounts'][$paymentInstrumentValue]; | |
6a488035 | 178 | } |
ddaa8ef1 PN |
179 | |
180 | /** | |
100fef9d | 181 | * Create default entity financial accounts |
ddaa8ef1 | 182 | * for financial type |
8ef12e64 | 183 | * CRM-12470 |
ddaa8ef1 | 184 | * |
77b97be7 EM |
185 | * @param $financialType |
186 | * | |
187 | * @return array | |
ddaa8ef1 | 188 | */ |
00be9182 | 189 | public static function createDefaultFinancialAccounts($financialType) { |
ddaa8ef1 | 190 | $titles = array(); |
01615c46 DG |
191 | $financialAccountTypeID = CRM_Core_OptionGroup::values('financial_account_type', FALSE, FALSE, FALSE, NULL, 'name'); |
192 | $accountRelationship = CRM_Core_OptionGroup::values('account_relationship', FALSE, FALSE, FALSE, NULL, 'name'); | |
056e736c | 193 | |
045f52a3 | 194 | $relationships = array( |
ddaa8ef1 PN |
195 | array_search('Accounts Receivable Account is', $accountRelationship) => array_search('Asset', $financialAccountTypeID), |
196 | array_search('Expense Account is', $accountRelationship) => array_search('Expenses', $financialAccountTypeID), | |
197 | array_search('Cost of Sales Account is', $accountRelationship) => array_search('Cost of Sales', $financialAccountTypeID), | |
198 | array_search('Income Account is', $accountRelationship) => array_search('Revenue', $financialAccountTypeID), | |
199 | ); | |
d75f2f47 | 200 | |
045f52a3 | 201 | $dao = CRM_Core_DAO::executeQuery('SELECT id, financial_account_type_id FROM civicrm_financial_account WHERE name LIKE %1', |
77292b55 | 202 | array(1 => array($financialType->name, 'String')) |
ddaa8ef1 | 203 | ); |
77292b55 PN |
204 | $dao->fetch(); |
205 | $existingFinancialAccount = array(); | |
206 | if (!$dao->N) { | |
207 | $params = array( | |
208 | 'name' => $financialType->name, | |
209 | 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', CRM_Core_Config::domainID(), 'contact_id'), | |
210 | 'financial_account_type_id' => array_search('Revenue', $financialAccountTypeID), | |
211 | 'description' => $financialType->description, | |
212 | 'account_type_code' => 'INC', | |
213 | 'is_active' => 1, | |
214 | ); | |
b70cd45c | 215 | $financialAccount = CRM_Financial_BAO_FinancialAccount::add($params); |
77292b55 PN |
216 | } |
217 | else { | |
218 | $existingFinancialAccount[$dao->financial_account_type_id] = $dao->id; | |
219 | } | |
045f52a3 | 220 | $params = array( |
ddaa8ef1 PN |
221 | 'entity_table' => 'civicrm_financial_type', |
222 | 'entity_id' => $financialType->id, | |
223 | ); | |
224 | foreach ($relationships as $key => $value) { | |
77292b55 PN |
225 | if (!array_key_exists($value, $existingFinancialAccount)) { |
226 | if ($accountRelationship[$key] == 'Accounts Receivable Account is') { | |
227 | $params['financial_account_id'] = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', 'Accounts Receivable', 'id', 'name'); | |
228 | if (!empty($params['financial_account_id'])) { | |
229 | $titles[] = 'Accounts Receivable'; | |
230 | } | |
231 | else { | |
232 | $query = "SELECT financial_account_id, name FROM civicrm_entity_financial_account | |
ddaa8ef1 PN |
233 | LEFT JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id |
234 | WHERE account_relationship = {$key} AND entity_table = 'civicrm_financial_type' LIMIT 1"; | |
77292b55 PN |
235 | $dao = CRM_Core_DAO::executeQuery($query); |
236 | $dao->fetch(); | |
237 | $params['financial_account_id'] = $dao->financial_account_id; | |
238 | $titles[] = $dao->name; | |
239 | } | |
240 | } | |
241 | elseif ($accountRelationship[$key] == 'Income Account is' && empty($existingFinancialAccount)) { | |
242 | $params['financial_account_id'] = $financialAccount->id; | |
243 | } | |
244 | else { | |
245 | $query = "SELECT id, name FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = {$value}"; | |
ddaa8ef1 PN |
246 | $dao = CRM_Core_DAO::executeQuery($query); |
247 | $dao->fetch(); | |
77292b55 | 248 | $params['financial_account_id'] = $dao->id; |
ddaa8ef1 PN |
249 | $titles[] = $dao->name; |
250 | } | |
251 | } | |
ddaa8ef1 | 252 | else { |
045f52a3 TO |
253 | $params['financial_account_id'] = $existingFinancialAccount[$value]; |
254 | $titles[] = $financialType->name; | |
ddaa8ef1 PN |
255 | } |
256 | $params['account_relationship'] = $key; | |
8ef12e64 | 257 | self::add($params); |
ddaa8ef1 | 258 | } |
77292b55 PN |
259 | if (!empty($existingFinancialAccount)) { |
260 | $titles = array(); | |
261 | } | |
ddaa8ef1 PN |
262 | return $titles; |
263 | } | |
96025800 | 264 | |
ccaca683 PN |
265 | /** |
266 | * Validate account relationship with financial account type | |
267 | * | |
5b9d2fd7 | 268 | * @param obj $financialTypeAccount of CRM_Financial_DAO_EntityFinancialAccount |
ccaca683 | 269 | * |
6bef1cdd | 270 | * @throws CRM_Core_Exception |
ccaca683 PN |
271 | */ |
272 | public static function validateRelationship($financialTypeAccount) { | |
273 | $financialAccountLinks = CRM_Financial_BAO_FinancialAccount::getfinancialAccountRelations(); | |
274 | $financialAccountType = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialAccount', $financialTypeAccount->financial_account_id, 'financial_account_type_id'); | |
275 | if (CRM_Utils_Array::value($financialTypeAccount->account_relationship, $financialAccountLinks) != $financialAccountType) { | |
276 | $accountRelationships = CRM_Core_PseudoConstant::get('CRM_Financial_DAO_EntityFinancialAccount', 'account_relationship'); | |
277 | $params = array( | |
278 | 1 => $accountRelationships[$financialTypeAccount->account_relationship], | |
279 | ); | |
a48a4a2b | 280 | throw new CRM_Core_Exception(ts("This financial account cannot have '%1' relationship.", $params)); |
ccaca683 PN |
281 | } |
282 | } | |
283 | ||
6a488035 | 284 | } |