Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
6a488035 | 35 | class CRM_Core_BAO_FinancialTrxn extends CRM_Financial_DAO_FinancialTrxn { |
b5c2afd0 | 36 | /** |
fe482240 | 37 | * Class constructor. |
b5c2afd0 | 38 | * |
b5c2afd0 EM |
39 | * @return \CRM_Financial_DAO_FinancialTrxn |
40 | */ | |
41 | /** | |
b5c2afd0 | 42 | */ |
00be9182 | 43 | public function __construct() { |
6a488035 TO |
44 | parent::__construct(); |
45 | } | |
46 | ||
47 | /** | |
fe482240 | 48 | * Takes an associative array and creates a financial transaction object. |
6a488035 | 49 | * |
6a0b768e TO |
50 | * @param array $params |
51 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 52 | * |
6a0b768e TO |
53 | * @param string $trxnEntityTable |
54 | * Entity_table. | |
6a488035 | 55 | * |
16b10e64 | 56 | * @return CRM_Core_BAO_FinancialTrxn |
6a488035 | 57 | */ |
e60f24eb | 58 | public static function create(&$params, $trxnEntityTable = NULL) { |
6a488035 TO |
59 | $trxn = new CRM_Financial_DAO_FinancialTrxn(); |
60 | $trxn->copyValues($params); | |
61 | $fids = array(); | |
62 | if (!CRM_Utils_Rule::currencyCode($trxn->currency)) { | |
63 | $config = CRM_Core_Config::singleton(); | |
64 | $trxn->currency = $config->defaultCurrency; | |
65 | } | |
66 | ||
67 | $trxn->save(); | |
68 | ||
69 | // save to entity_financial_trxn table | |
79d7553f | 70 | $entityFinancialTrxnParams |
71 | = array( | |
6a488035 TO |
72 | 'entity_table' => "civicrm_contribution", |
73 | 'financial_trxn_id' => $trxn->id, | |
74 | 'amount' => $params['total_amount'], | |
75 | 'currency' => $trxn->currency, | |
76 | ); | |
77 | ||
78 | if (!empty($trxnEntityTable)) { | |
79 | $entityFinancialTrxnParams['entity_table'] = $trxnEntityTable['entity_table']; | |
353ffa53 | 80 | $entityFinancialTrxnParams['entity_id'] = $trxnEntityTable['entity_id']; |
6a488035 TO |
81 | } |
82 | else { | |
353ffa53 | 83 | $entityFinancialTrxnParams['entity_id'] = $params['contribution_id']; |
6a488035 TO |
84 | } |
85 | ||
86 | $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn(); | |
87 | $entityTrxn->copyValues($entityFinancialTrxnParams); | |
88 | $entityTrxn->save(); | |
89 | return $trxn; | |
90 | } | |
91 | ||
b5c2afd0 | 92 | /** |
100fef9d CW |
93 | * @param int $contributionId |
94 | * @param int $contributionFinancialTypeId | |
b5c2afd0 EM |
95 | * |
96 | * @return array | |
97 | */ | |
00be9182 | 98 | public static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId = NULL) { |
29c61b58 PJ |
99 | if (!$contributionFinancialTypeId) { |
100 | $contributionFinancialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'financial_type_id'); | |
101 | } | |
ede1935f PJ |
102 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
103 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($contributionFinancialTypeId, $relationTypeId); | |
520ec403 | 104 | $q = "SELECT ft.id, ft.total_amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') WHERE eft.entity_id = %1 AND ft.to_financial_account_id = %2"; |
1010c4e1 | 105 | |
ede1935f PJ |
106 | $p[1] = array($contributionId, 'Integer'); |
107 | $p[2] = array($toFinancialAccount, 'Integer'); | |
1010c4e1 | 108 | |
ede1935f PJ |
109 | $balanceAmtDAO = CRM_Core_DAO::executeQuery($q, $p); |
110 | $ret = array(); | |
18fdfcc3 PN |
111 | if ($balanceAmtDAO->N) { |
112 | $ret['total_amount'] = 0; | |
113 | } | |
22e263ad | 114 | while ($balanceAmtDAO->fetch()) { |
ede1935f | 115 | $ret['trxn_id'] = $balanceAmtDAO->id; |
18fdfcc3 | 116 | $ret['total_amount'] += $balanceAmtDAO->total_amount; |
ede1935f PJ |
117 | } |
118 | ||
119 | return $ret; | |
120 | } | |
121 | ||
6a488035 | 122 | /** |
fe482240 | 123 | * Fetch object based on array of properties. |
6a488035 | 124 | * |
6a0b768e TO |
125 | * @param array $params |
126 | * (reference ) an assoc array of name/value pairs. | |
127 | * @param array $defaults | |
128 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 129 | * |
16b10e64 | 130 | * @return CRM_Contribute_BAO_ContributionType |
6a488035 | 131 | */ |
481a74f4 TO |
132 | public static function retrieve(&$params, &$defaults) { |
133 | $financialItem = new CRM_Financial_DAO_FinancialTrxn(); | |
6a488035 | 134 | $financialItem->copyValues($params); |
4eeb9a5b | 135 | if ($financialItem->find(TRUE)) { |
481a74f4 | 136 | CRM_Core_DAO::storeValues($financialItem, $defaults); |
6a488035 TO |
137 | return $financialItem; |
138 | } | |
e60f24eb | 139 | return NULL; |
6a488035 TO |
140 | } |
141 | ||
142 | /** | |
6a488035 TO |
143 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. |
144 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
145 | * | |
6a0b768e TO |
146 | * @param $entity_id |
147 | * Id of the entity usually the contactID. | |
148 | * @param string $orderBy | |
149 | * To get single trxn id for a entity table i.e last or first. | |
dd244018 EM |
150 | * @param bool $newTrxn |
151 | * | |
a6c01b45 CW |
152 | * @return array |
153 | * array of category id's the contact belongs to. | |
6a488035 | 154 | * |
6a488035 | 155 | */ |
00be9182 | 156 | public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE) { |
6a488035 TO |
157 | $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL); |
158 | ||
159 | $condition = ""; | |
160 | if (!$newTrxn) { | |
161 | $condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) "; | |
162 | } | |
21d32567 DL |
163 | |
164 | if ($orderBy) { | |
165 | $orderBy = CRM_Utils_Type::escape($orderBy, 'String'); | |
166 | } | |
167 | ||
6a488035 | 168 | $query = "SELECT ceft.id, ceft.financial_trxn_id FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 169 | LEFT JOIN civicrm_entity_financial_trxn ceft |
6a488035 TO |
170 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
171 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
172 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
173 | LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id | |
174 | WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or cfi.entity_table is NULL) | |
175 | {$condition} | |
176 | ORDER BY cft.id {$orderBy} | |
cbb7c7e0 | 177 | LIMIT 1;"; |
178 | ||
6a488035 TO |
179 | $params = array(1 => array($entity_id, 'Integer')); |
180 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
181 | if ($dao->fetch()) { | |
182 | $ids['entityFinancialTrxnId'] = $dao->id; | |
183 | $ids['financialTrxnId'] = $dao->financial_trxn_id; | |
184 | } | |
185 | return $ids; | |
186 | } | |
187 | ||
188 | /** | |
189 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
c490a46a | 190 | * @todo This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. |
da6b46f4 | 191 | * |
6a0b768e TO |
192 | * @param int $entity_id |
193 | * Id of the entity usually the contactID. | |
6a488035 | 194 | * |
a6c01b45 | 195 | * @return array |
b44e3f84 | 196 | * array of category id's the contact belongs to. |
6a488035 | 197 | * |
6a488035 | 198 | */ |
00be9182 | 199 | public static function getFinancialTrxnTotal($entity_id) { |
6a488035 TO |
200 | $query = " |
201 | SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft | |
cbb7c7e0 | 202 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id |
6a488035 TO |
203 | WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1 |
204 | "; | |
205 | ||
206 | $sqlParams = array(1 => array($entity_id, 'Integer')); | |
d3e86119 | 207 | return CRM_Core_DAO::singleValueQuery($query, $sqlParams); |
6a488035 TO |
208 | |
209 | } | |
77b97be7 | 210 | |
6a488035 TO |
211 | /** |
212 | * Given an financial_trxn_id check for previous entity_financial_trxn. | |
213 | * | |
6a0b768e TO |
214 | * @param $financial_trxn_id |
215 | * Id of the latest payment. | |
77b97be7 | 216 | * |
6a488035 | 217 | * |
a6c01b45 CW |
218 | * @return array |
219 | * array of previous payments | |
6a488035 | 220 | * |
6a488035 | 221 | */ |
00be9182 | 222 | public static function getPayments($financial_trxn_id) { |
6a488035 TO |
223 | $query = " |
224 | SELECT ef1.financial_trxn_id, sum(ef1.amount) amount | |
225 | FROM civicrm_entity_financial_trxn ef1 | |
226 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.financial_trxn_id = ef2.entity_id | |
227 | WHERE ef2.financial_trxn_id =%1 | |
228 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
229 | AND ef1.entity_table = 'civicrm_financial_item' | |
230 | GROUP BY ef1.financial_trxn_id | |
231 | UNION | |
232 | SELECT ef1.financial_trxn_id, ef1.amount | |
233 | FROM civicrm_entity_financial_trxn ef1 | |
234 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.entity_id = ef2.entity_id | |
235 | WHERE ef2.financial_trxn_id =%1 | |
236 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
237 | AND ef1.entity_table = 'civicrm_financial_trxn'"; | |
238 | ||
239 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
240 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
241 | $i = 0; | |
242 | $result = array(); | |
243 | while ($dao->fetch()) { | |
244 | $result[$i]['financial_trxn_id'] = $dao->financial_trxn_id; | |
245 | $result[$i]['amount'] = $dao->amount; | |
246 | $i++; | |
247 | } | |
248 | ||
249 | if (empty($result)) { | |
250 | $query = "SELECT sum( amount ) amount FROM civicrm_entity_financial_trxn WHERE financial_trxn_id =%1 AND entity_table = 'civicrm_financial_item'"; | |
251 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
252 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
253 | ||
254 | if ($dao->fetch()) { | |
255 | $result[0]['financial_trxn_id'] = $financial_trxn_id; | |
256 | $result[0]['amount'] = $dao->amount; | |
257 | } | |
258 | } | |
259 | return $result; | |
260 | } | |
261 | ||
262 | /** | |
263 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
264 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
265 | * | |
6a0b768e TO |
266 | * @param $entity_id |
267 | * Id of the entity usually the contactID. | |
268 | * @param string $entity_table | |
269 | * Name of the entity table usually 'civicrm_contact'. | |
6a488035 | 270 | * |
a6c01b45 | 271 | * @return array |
b44e3f84 | 272 | * array of category id's the contact belongs to. |
6a488035 | 273 | * |
6a488035 | 274 | */ |
00be9182 | 275 | public static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') { |
6a488035 TO |
276 | $query = "SELECT lt.price_field_value_id AS id, ft.financial_trxn_id,ft.amount AS amount FROM civicrm_entity_financial_trxn AS ft |
277 | LEFT JOIN civicrm_financial_item AS fi ON fi.id = ft.entity_id AND fi.entity_table = 'civicrm_line_item' AND ft.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 278 | LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2 |
6a488035 TO |
279 | WHERE lt.entity_id = %1 "; |
280 | ||
281 | $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String')); | |
353ffa53 | 282 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); |
9b873358 | 283 | while ($dao->fetch()) { |
6a488035 TO |
284 | $result[$dao->financial_trxn_id][$dao->id] = $dao->amount; |
285 | } | |
286 | if (!empty($result)) { | |
287 | return $result; | |
288 | } | |
289 | else { | |
e60f24eb | 290 | return NULL; |
6a488035 TO |
291 | } |
292 | } | |
293 | ||
294 | /** | |
fe482240 | 295 | * Delete financial transaction. |
6a488035 | 296 | * |
100fef9d | 297 | * @param int $entity_id |
4eeb9a5b | 298 | * @return bool |
ab8a593e | 299 | * TRUE on success, FALSE otherwise. |
6a488035 | 300 | */ |
00be9182 | 301 | public static function deleteFinancialTrxn($entity_id) { |
de1c25e1 | 302 | $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 303 | LEFT JOIN civicrm_entity_financial_trxn ceft |
de1c25e1 PN |
304 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
305 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
306 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 307 | LEFT JOIN civicrm_financial_item cfi |
de1c25e1 PN |
308 | ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id |
309 | WHERE ceft.entity_id = %1"; | |
310 | CRM_Core_DAO::executeQuery($query, array(1 => array($entity_id, 'Integer'))); | |
311 | return TRUE; | |
6a488035 TO |
312 | } |
313 | ||
314 | /** | |
fe482240 | 315 | * Create financial transaction for premium. |
6a488035 | 316 | * |
6a488035 | 317 | */ |
00be9182 | 318 | public static function createPremiumTrxn($params) { |
8cc574cf | 319 | if ((empty($params['financial_type_id']) || empty($params['contributionId'])) && empty($params['oldPremium'])) { |
6a488035 TO |
320 | return; |
321 | } | |
cbb7c7e0 | 322 | |
a7488080 | 323 | if (!empty($params['cost'])) { |
6a488035 TO |
324 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
325 | $financialAccountType = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id']); | |
f743a6eb | 326 | $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND label IN ('Premiums Inventory Account is', 'Cost of Sales Account is')"); |
0d8afee2 | 327 | $toFinancialAccount = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is'; |
d3e86119 | 328 | $fromFinancialAccount = !empty($params['isDeleted']) ? 'Cost of Sales Account is' : 'Premiums Inventory Account is'; |
6a488035 TO |
329 | $accountRelationship = array_flip($accountRelationship); |
330 | $financialtrxn = array( | |
331 | 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]], | |
332 | 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]], | |
333 | 'trxn_date' => date('YmdHis'), | |
334 | 'total_amount' => CRM_Utils_Array::value('cost', $params) ? $params['cost'] : 0, | |
335 | 'currency' => CRM_Utils_Array::value('currency', $params), | |
21dfd5f5 | 336 | 'status_id' => array_search('Completed', $contributionStatuses), |
6a488035 TO |
337 | ); |
338 | $trxnEntityTable['entity_table'] = 'civicrm_contribution'; | |
339 | $trxnEntityTable['entity_id'] = $params['contributionId']; | |
340 | CRM_Core_BAO_FinancialTrxn::create($financialtrxn, $trxnEntityTable); | |
341 | } | |
342 | ||
a7488080 | 343 | if (!empty($params['oldPremium'])) { |
6a488035 | 344 | $premiumParams = array( |
21dfd5f5 | 345 | 'id' => $params['oldPremium']['product_id'], |
6a488035 TO |
346 | ); |
347 | $productDetails = array(); | |
348 | CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails); | |
349 | $params = array( | |
350 | 'cost' => CRM_Utils_Array::value('cost', $productDetails), | |
351 | 'currency' => CRM_Utils_Array::value('currency', $productDetails), | |
352 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails), | |
353 | 'contributionId' => $params['oldPremium']['contribution_id'], | |
21dfd5f5 | 354 | 'isDeleted' => TRUE, |
6a488035 TO |
355 | ); |
356 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params); | |
357 | } | |
358 | } | |
c490a46a | 359 | |
6a488035 | 360 | /** |
fe482240 | 361 | * Create financial trxn and items when fee is charged. |
6a488035 | 362 | * |
6a0b768e TO |
363 | * @param array $params |
364 | * To create trxn entries. | |
6a488035 | 365 | * |
7a9ab499 | 366 | * @return bool |
6a488035 | 367 | */ |
00be9182 | 368 | public static function recordFees($params) { |
f743a6eb | 369 | $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
6a488035 TO |
370 | $domainId = CRM_Core_Config::domainID(); |
371 | $amount = 0; | |
a7488080 | 372 | if (!empty($params['prevContribution'])) { |
6a488035 TO |
373 | $amount = $params['prevContribution']->fee_amount; |
374 | } | |
375 | $amount = $params['fee_amount'] - $amount; | |
78b79549 PN |
376 | if (!$amount) { |
377 | return FALSE; | |
378 | } | |
39383f5f PJ |
379 | $contributionId = isset($params['contribution']->id) ? $params['contribution']->id : $params['contribution_id']; |
380 | if (empty($params['financial_type_id'])) { | |
381 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id', 'id'); | |
382 | } | |
383 | else { | |
384 | $financialTypeId = $params['financial_type_id']; | |
385 | } | |
386 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); | |
387 | ||
6a488035 TO |
388 | $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id']; |
389 | $params['trxnParams']['to_financial_account_id'] = $financialAccount; | |
390 | $params['trxnParams']['total_amount'] = $amount; | |
79d7553f | 391 | $params['trxnParams']['fee_amount'] = $params['trxnParams']['net_amount'] = 0; |
353ffa53 | 392 | $params['trxnParams']['status_id'] = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); |
39383f5f | 393 | $params['trxnParams']['contribution_id'] = $contributionId; |
6a488035 | 394 | $trxn = self::create($params['trxnParams']); |
a7488080 | 395 | if (empty($params['entity_id'])) { |
6a488035 TO |
396 | $financialTrxnID = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC'); |
397 | $params['entity_id'] = $financialTrxnID['financialTrxnId']; | |
398 | } | |
79d7553f | 399 | $fItemParams |
400 | = array( | |
6a488035 TO |
401 | 'financial_account_id' => $financialAccount, |
402 | 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id'), | |
403 | 'created_date' => date('YmdHis'), | |
404 | 'transaction_date' => date('YmdHis'), | |
405 | 'amount' => $amount, | |
406 | 'description' => 'Fee', | |
353ffa53 | 407 | 'status_id' => CRM_Core_OptionGroup::getValue('financial_item_status', 'Paid', 'name'), |
6a488035 TO |
408 | 'entity_table' => 'civicrm_financial_trxn', |
409 | 'entity_id' => $params['entity_id'], | |
410 | 'currency' => $params['trxnParams']['currency'], | |
411 | ); | |
412 | $trxnIDS['id'] = $trxn->id; | |
413 | $financialItem = CRM_Financial_BAO_FinancialItem::create($fItemParams, NULL, $trxnIDS); | |
414 | } | |
6a488035 | 415 | |
b5c2afd0 | 416 | /** |
fe482240 | 417 | * get partial payment amount and type of it. |
d424ffde | 418 | * |
100fef9d | 419 | * @param int $entityId |
b5c2afd0 EM |
420 | * @param string $entityName |
421 | * @param bool $returnType | |
79d7553f | 422 | * @param int $lineItemTotal |
b5c2afd0 | 423 | * |
e60f24eb | 424 | * @return array|int|NULL|string |
d424ffde CW |
425 | * [payment type => amount] |
426 | * payment type: 'amount_owed' or 'refund_due' | |
b5c2afd0 | 427 | */ |
00be9182 | 428 | public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) { |
0f602e3f PJ |
429 | $value = NULL; |
430 | if (empty($entityName)) { | |
431 | return $value; | |
432 | } | |
433 | ||
434 | if ($entityName == 'participant') { | |
bc2eeabb | 435 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id'); |
e8cf3013 | 436 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); |
0f602e3f PJ |
437 | |
438 | if ($contributionId && $financialTypeId) { | |
439 | $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); | |
1010c4e1 PJ |
440 | $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name'); |
441 | ||
0f602e3f PJ |
442 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
443 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId); | |
8cf01b22 DG |
444 | $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
445 | $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); | |
0f602e3f | 446 | |
bc2eeabb | 447 | if (empty($lineItemTotal)) { |
28e4e707 PJ |
448 | $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); |
449 | if (count($ids) > 1) { | |
450 | $total = 0; | |
451 | foreach ($ids as $val) { | |
452 | $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant'); | |
453 | } | |
454 | $lineItemTotal = $total; | |
455 | } | |
456 | else { | |
457 | $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant'); | |
458 | } | |
bc2eeabb PJ |
459 | } |
460 | $sqlFtTotalAmt = " | |
461 | SELECT SUM(ft.total_amount) | |
0f602e3f | 462 | FROM civicrm_financial_trxn ft |
bc2eeabb PJ |
463 | LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') |
464 | LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id) | |
465 | LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id) | |
8cf01b22 | 466 | WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount} |
1010c4e1 | 467 | AND ft.status_id IN ({$statusId}, {$refundStatusId}) |
0f602e3f PJ |
468 | "; |
469 | $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt); | |
bc2eeabb PJ |
470 | $value = 0; |
471 | if ($ftTotalAmt) { | |
472 | $value = $paymentVal = $lineItemTotal - $ftTotalAmt; | |
473 | } | |
29c61b58 PJ |
474 | if ($returnType) { |
475 | $value = array(); | |
476 | if ($paymentVal < 0) { | |
353ffa53 | 477 | $value['refund_due'] = $paymentVal; |
29c61b58 PJ |
478 | } |
479 | elseif ($paymentVal > 0) { | |
480 | $value['amount_owed'] = $paymentVal; | |
481 | } | |
bc2eeabb PJ |
482 | elseif ($lineItemTotal == $ftTotalAmt) { |
483 | $value['full_paid'] = $ftTotalAmt; | |
484 | } | |
0f602e3f PJ |
485 | } |
486 | } | |
487 | } | |
488 | return $value; | |
489 | } | |
96025800 | 490 | |
0d8afee2 | 491 | } |