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