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 | */ |
8cf6bd83 | 157 | public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE, $whereClause = '', $fromAccountID = NULL) { |
6a488035 TO |
158 | $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL); |
159 | ||
8cf6bd83 | 160 | $params = array(1 => array($entity_id, 'Integer')); |
6a488035 TO |
161 | $condition = ""; |
162 | if (!$newTrxn) { | |
163 | $condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) "; | |
164 | } | |
21d32567 | 165 | |
8cf6bd83 PN |
166 | if ($fromAccountID) { |
167 | $condition .= " AND (cft.from_financial_account_id <> %2 OR cft.from_financial_account_id IS NULL)"; | |
168 | $params[2] = array($fromAccountID, 'Integer'); | |
169 | } | |
21d32567 DL |
170 | if ($orderBy) { |
171 | $orderBy = CRM_Utils_Type::escape($orderBy, 'String'); | |
172 | } | |
173 | ||
99cdd94d | 174 | $query = "SELECT ceft.id, ceft.financial_trxn_id, cft.trxn_id FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 175 | LEFT JOIN civicrm_entity_financial_trxn ceft |
6a488035 TO |
176 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
177 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
178 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
179 | LEFT JOIN civicrm_financial_item cfi ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id | |
180 | WHERE ceft.entity_id = %1 AND (cfi.entity_table <> 'civicrm_financial_trxn' or cfi.entity_table is NULL) | |
181 | {$condition} | |
99cdd94d | 182 | {$whereClause} |
6a488035 | 183 | ORDER BY cft.id {$orderBy} |
cbb7c7e0 | 184 | LIMIT 1;"; |
185 | ||
6a488035 TO |
186 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
187 | if ($dao->fetch()) { | |
188 | $ids['entityFinancialTrxnId'] = $dao->id; | |
189 | $ids['financialTrxnId'] = $dao->financial_trxn_id; | |
99cdd94d | 190 | $ids['trxn_id'] = $dao->trxn_id; |
6a488035 TO |
191 | } |
192 | return $ids; | |
193 | } | |
194 | ||
99cdd94d | 195 | /** |
196 | * Get the transaction id for the (latest) refund associated with a contribution. | |
197 | * | |
198 | * @param int $contributionID | |
199 | * @return string | |
200 | */ | |
201 | public static function getRefundTransactionTrxnID($contributionID) { | |
202 | $ids = self::getRefundTransactionIDs($contributionID); | |
203 | return isset($ids['trxn_id']) ? $ids['trxn_id'] : NULL; | |
204 | } | |
205 | ||
206 | /** | |
207 | * Get the transaction id for the (latest) refund associated with a contribution. | |
208 | * | |
209 | * @param int $contributionID | |
210 | * @return string | |
211 | */ | |
212 | public static function getRefundTransactionIDs($contributionID) { | |
213 | $refundStatusID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Refunded'); | |
214 | return self::getFinancialTrxnId($contributionID, 'DESC', FALSE, " AND cft.status_id = $refundStatusID"); | |
215 | } | |
216 | ||
6a488035 TO |
217 | /** |
218 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
c490a46a | 219 | * @todo This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. |
da6b46f4 | 220 | * |
6a0b768e TO |
221 | * @param int $entity_id |
222 | * Id of the entity usually the contactID. | |
6a488035 | 223 | * |
a6c01b45 | 224 | * @return array |
b44e3f84 | 225 | * array of category id's the contact belongs to. |
6a488035 | 226 | * |
6a488035 | 227 | */ |
00be9182 | 228 | public static function getFinancialTrxnTotal($entity_id) { |
6a488035 TO |
229 | $query = " |
230 | SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft | |
cbb7c7e0 | 231 | LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id |
6a488035 TO |
232 | WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1 |
233 | "; | |
234 | ||
235 | $sqlParams = array(1 => array($entity_id, 'Integer')); | |
d3e86119 | 236 | return CRM_Core_DAO::singleValueQuery($query, $sqlParams); |
6a488035 TO |
237 | |
238 | } | |
77b97be7 | 239 | |
6a488035 TO |
240 | /** |
241 | * Given an financial_trxn_id check for previous entity_financial_trxn. | |
242 | * | |
6a0b768e TO |
243 | * @param $financial_trxn_id |
244 | * Id of the latest payment. | |
77b97be7 | 245 | * |
6a488035 | 246 | * |
a6c01b45 CW |
247 | * @return array |
248 | * array of previous payments | |
6a488035 | 249 | * |
6a488035 | 250 | */ |
00be9182 | 251 | public static function getPayments($financial_trxn_id) { |
6a488035 TO |
252 | $query = " |
253 | SELECT ef1.financial_trxn_id, sum(ef1.amount) amount | |
254 | FROM civicrm_entity_financial_trxn ef1 | |
255 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.financial_trxn_id = ef2.entity_id | |
256 | WHERE ef2.financial_trxn_id =%1 | |
257 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
258 | AND ef1.entity_table = 'civicrm_financial_item' | |
259 | GROUP BY ef1.financial_trxn_id | |
260 | UNION | |
261 | SELECT ef1.financial_trxn_id, ef1.amount | |
262 | FROM civicrm_entity_financial_trxn ef1 | |
263 | LEFT JOIN civicrm_entity_financial_trxn ef2 ON ef1.entity_id = ef2.entity_id | |
264 | WHERE ef2.financial_trxn_id =%1 | |
265 | AND ef2.entity_table = 'civicrm_financial_trxn' | |
266 | AND ef1.entity_table = 'civicrm_financial_trxn'"; | |
267 | ||
268 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
269 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
270 | $i = 0; | |
271 | $result = array(); | |
272 | while ($dao->fetch()) { | |
273 | $result[$i]['financial_trxn_id'] = $dao->financial_trxn_id; | |
274 | $result[$i]['amount'] = $dao->amount; | |
275 | $i++; | |
276 | } | |
277 | ||
278 | if (empty($result)) { | |
279 | $query = "SELECT sum( amount ) amount FROM civicrm_entity_financial_trxn WHERE financial_trxn_id =%1 AND entity_table = 'civicrm_financial_item'"; | |
280 | $sqlParams = array(1 => array($financial_trxn_id, 'Integer')); | |
281 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); | |
282 | ||
283 | if ($dao->fetch()) { | |
284 | $result[0]['financial_trxn_id'] = $financial_trxn_id; | |
285 | $result[0]['amount'] = $dao->amount; | |
286 | } | |
287 | } | |
288 | return $result; | |
289 | } | |
290 | ||
291 | /** | |
292 | * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record. | |
293 | * NOTE: This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object. | |
294 | * | |
6a0b768e TO |
295 | * @param $entity_id |
296 | * Id of the entity usually the contactID. | |
297 | * @param string $entity_table | |
298 | * Name of the entity table usually 'civicrm_contact'. | |
6a488035 | 299 | * |
a6c01b45 | 300 | * @return array |
b44e3f84 | 301 | * array of category id's the contact belongs to. |
6a488035 | 302 | * |
6a488035 | 303 | */ |
00be9182 | 304 | public static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') { |
6a488035 TO |
305 | $query = "SELECT lt.price_field_value_id AS id, ft.financial_trxn_id,ft.amount AS amount FROM civicrm_entity_financial_trxn AS ft |
306 | 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 | 307 | LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2 |
6a488035 TO |
308 | WHERE lt.entity_id = %1 "; |
309 | ||
310 | $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String')); | |
353ffa53 | 311 | $dao = CRM_Core_DAO::executeQuery($query, $sqlParams); |
9b873358 | 312 | while ($dao->fetch()) { |
6a488035 TO |
313 | $result[$dao->financial_trxn_id][$dao->id] = $dao->amount; |
314 | } | |
315 | if (!empty($result)) { | |
316 | return $result; | |
317 | } | |
318 | else { | |
e60f24eb | 319 | return NULL; |
6a488035 TO |
320 | } |
321 | } | |
322 | ||
323 | /** | |
fe482240 | 324 | * Delete financial transaction. |
6a488035 | 325 | * |
100fef9d | 326 | * @param int $entity_id |
4eeb9a5b | 327 | * @return bool |
ab8a593e | 328 | * TRUE on success, FALSE otherwise. |
6a488035 | 329 | */ |
00be9182 | 330 | public static function deleteFinancialTrxn($entity_id) { |
de1c25e1 | 331 | $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft |
cbb7c7e0 | 332 | LEFT JOIN civicrm_entity_financial_trxn ceft |
de1c25e1 PN |
333 | ON ceft.financial_trxn_id = cft.id AND ceft.entity_table = 'civicrm_contribution' |
334 | LEFT JOIN civicrm_entity_financial_trxn ceft1 | |
335 | ON ceft1.financial_trxn_id = cft.id AND ceft1.entity_table = 'civicrm_financial_item' | |
cbb7c7e0 | 336 | LEFT JOIN civicrm_financial_item cfi |
de1c25e1 PN |
337 | ON ceft1.entity_table = 'civicrm_financial_item' and cfi.id = ceft1.entity_id |
338 | WHERE ceft.entity_id = %1"; | |
339 | CRM_Core_DAO::executeQuery($query, array(1 => array($entity_id, 'Integer'))); | |
340 | return TRUE; | |
6a488035 TO |
341 | } |
342 | ||
343 | /** | |
fe482240 | 344 | * Create financial transaction for premium. |
6a488035 | 345 | * |
dd9db60b EM |
346 | * @param array $params |
347 | * - oldPremium | |
348 | * - financial_type_id | |
349 | * - contributionId | |
350 | * - isDeleted | |
351 | * - cost | |
352 | * - currency | |
6a488035 | 353 | */ |
00be9182 | 354 | public static function createPremiumTrxn($params) { |
8cc574cf | 355 | if ((empty($params['financial_type_id']) || empty($params['contributionId'])) && empty($params['oldPremium'])) { |
6a488035 TO |
356 | return; |
357 | } | |
cbb7c7e0 | 358 | |
a7488080 | 359 | if (!empty($params['cost'])) { |
6a488035 TO |
360 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); |
361 | $financialAccountType = CRM_Contribute_PseudoConstant::financialAccountType($params['financial_type_id']); | |
7d45c236 | 362 | $accountRelationship = CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name IN ('Premiums Inventory Account is', 'Cost of Sales Account is')"); |
0d8afee2 | 363 | $toFinancialAccount = !empty($params['isDeleted']) ? 'Premiums Inventory Account is' : 'Cost of Sales Account is'; |
d3e86119 | 364 | $fromFinancialAccount = !empty($params['isDeleted']) ? 'Cost of Sales Account is' : 'Premiums Inventory Account is'; |
6a488035 TO |
365 | $accountRelationship = array_flip($accountRelationship); |
366 | $financialtrxn = array( | |
367 | 'to_financial_account_id' => $financialAccountType[$accountRelationship[$toFinancialAccount]], | |
368 | 'from_financial_account_id' => $financialAccountType[$accountRelationship[$fromFinancialAccount]], | |
369 | 'trxn_date' => date('YmdHis'), | |
370 | 'total_amount' => CRM_Utils_Array::value('cost', $params) ? $params['cost'] : 0, | |
371 | 'currency' => CRM_Utils_Array::value('currency', $params), | |
21dfd5f5 | 372 | 'status_id' => array_search('Completed', $contributionStatuses), |
6a488035 TO |
373 | ); |
374 | $trxnEntityTable['entity_table'] = 'civicrm_contribution'; | |
375 | $trxnEntityTable['entity_id'] = $params['contributionId']; | |
376 | CRM_Core_BAO_FinancialTrxn::create($financialtrxn, $trxnEntityTable); | |
377 | } | |
378 | ||
a7488080 | 379 | if (!empty($params['oldPremium'])) { |
6a488035 | 380 | $premiumParams = array( |
21dfd5f5 | 381 | 'id' => $params['oldPremium']['product_id'], |
6a488035 TO |
382 | ); |
383 | $productDetails = array(); | |
384 | CRM_Contribute_BAO_ManagePremiums::retrieve($premiumParams, $productDetails); | |
385 | $params = array( | |
386 | 'cost' => CRM_Utils_Array::value('cost', $productDetails), | |
387 | 'currency' => CRM_Utils_Array::value('currency', $productDetails), | |
388 | 'financial_type_id' => CRM_Utils_Array::value('financial_type_id', $productDetails), | |
389 | 'contributionId' => $params['oldPremium']['contribution_id'], | |
21dfd5f5 | 390 | 'isDeleted' => TRUE, |
6a488035 TO |
391 | ); |
392 | CRM_Core_BAO_FinancialTrxn::createPremiumTrxn($params); | |
393 | } | |
394 | } | |
c490a46a | 395 | |
6a488035 | 396 | /** |
fe482240 | 397 | * Create financial trxn and items when fee is charged. |
6a488035 | 398 | * |
6a0b768e TO |
399 | * @param array $params |
400 | * To create trxn entries. | |
6a488035 | 401 | * |
7a9ab499 | 402 | * @return bool |
6a488035 | 403 | */ |
00be9182 | 404 | public static function recordFees($params) { |
f743a6eb | 405 | $expenseTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
6a488035 TO |
406 | $domainId = CRM_Core_Config::domainID(); |
407 | $amount = 0; | |
a7488080 | 408 | if (!empty($params['prevContribution'])) { |
6a488035 TO |
409 | $amount = $params['prevContribution']->fee_amount; |
410 | } | |
411 | $amount = $params['fee_amount'] - $amount; | |
78b79549 PN |
412 | if (!$amount) { |
413 | return FALSE; | |
414 | } | |
39383f5f PJ |
415 | $contributionId = isset($params['contribution']->id) ? $params['contribution']->id : $params['contribution_id']; |
416 | if (empty($params['financial_type_id'])) { | |
417 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id', 'id'); | |
418 | } | |
419 | else { | |
420 | $financialTypeId = $params['financial_type_id']; | |
421 | } | |
422 | $financialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $expenseTypeId); | |
423 | ||
6a488035 TO |
424 | $params['trxnParams']['from_financial_account_id'] = $params['to_financial_account_id']; |
425 | $params['trxnParams']['to_financial_account_id'] = $financialAccount; | |
426 | $params['trxnParams']['total_amount'] = $amount; | |
79d7553f | 427 | $params['trxnParams']['fee_amount'] = $params['trxnParams']['net_amount'] = 0; |
76c28c8d | 428 | $params['trxnParams']['status_id'] = $params['contribution_status_id']; |
39383f5f | 429 | $params['trxnParams']['contribution_id'] = $contributionId; |
6a488035 | 430 | $trxn = self::create($params['trxnParams']); |
a7488080 | 431 | if (empty($params['entity_id'])) { |
6a488035 TO |
432 | $financialTrxnID = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC'); |
433 | $params['entity_id'] = $financialTrxnID['financialTrxnId']; | |
434 | } | |
79d7553f | 435 | $fItemParams |
436 | = array( | |
6a488035 TO |
437 | 'financial_account_id' => $financialAccount, |
438 | 'contact_id' => CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Domain', $domainId, 'contact_id'), | |
439 | 'created_date' => date('YmdHis'), | |
440 | 'transaction_date' => date('YmdHis'), | |
441 | 'amount' => $amount, | |
442 | 'description' => 'Fee', | |
353ffa53 | 443 | 'status_id' => CRM_Core_OptionGroup::getValue('financial_item_status', 'Paid', 'name'), |
6a488035 TO |
444 | 'entity_table' => 'civicrm_financial_trxn', |
445 | 'entity_id' => $params['entity_id'], | |
446 | 'currency' => $params['trxnParams']['currency'], | |
447 | ); | |
448 | $trxnIDS['id'] = $trxn->id; | |
449 | $financialItem = CRM_Financial_BAO_FinancialItem::create($fItemParams, NULL, $trxnIDS); | |
450 | } | |
6a488035 | 451 | |
b5c2afd0 | 452 | /** |
fe482240 | 453 | * get partial payment amount and type of it. |
d424ffde | 454 | * |
100fef9d | 455 | * @param int $entityId |
b5c2afd0 EM |
456 | * @param string $entityName |
457 | * @param bool $returnType | |
79d7553f | 458 | * @param int $lineItemTotal |
b5c2afd0 | 459 | * |
e60f24eb | 460 | * @return array|int|NULL|string |
d424ffde CW |
461 | * [payment type => amount] |
462 | * payment type: 'amount_owed' or 'refund_due' | |
b5c2afd0 | 463 | */ |
00be9182 | 464 | public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) { |
0f602e3f PJ |
465 | $value = NULL; |
466 | if (empty($entityName)) { | |
467 | return $value; | |
468 | } | |
469 | ||
470 | if ($entityName == 'participant') { | |
bc2eeabb | 471 | $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id'); |
e8cf3013 | 472 | $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); |
0f602e3f PJ |
473 | |
474 | if ($contributionId && $financialTypeId) { | |
475 | $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'); | |
1010c4e1 PJ |
476 | $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name'); |
477 | ||
0f602e3f PJ |
478 | $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); |
479 | $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId); | |
8cf01b22 DG |
480 | $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); |
481 | $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); | |
0f602e3f | 482 | |
bc2eeabb | 483 | if (empty($lineItemTotal)) { |
28e4e707 PJ |
484 | $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); |
485 | if (count($ids) > 1) { | |
486 | $total = 0; | |
487 | foreach ($ids as $val) { | |
488 | $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant'); | |
489 | } | |
490 | $lineItemTotal = $total; | |
491 | } | |
492 | else { | |
493 | $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant'); | |
494 | } | |
bc2eeabb PJ |
495 | } |
496 | $sqlFtTotalAmt = " | |
497 | SELECT SUM(ft.total_amount) | |
0f602e3f | 498 | FROM civicrm_financial_trxn ft |
bc2eeabb PJ |
499 | LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution') |
500 | LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id) | |
501 | LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id) | |
8cf01b22 | 502 | WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount} |
1010c4e1 | 503 | AND ft.status_id IN ({$statusId}, {$refundStatusId}) |
0f602e3f PJ |
504 | "; |
505 | $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt); | |
bc2eeabb PJ |
506 | $value = 0; |
507 | if ($ftTotalAmt) { | |
508 | $value = $paymentVal = $lineItemTotal - $ftTotalAmt; | |
509 | } | |
29c61b58 PJ |
510 | if ($returnType) { |
511 | $value = array(); | |
512 | if ($paymentVal < 0) { | |
353ffa53 | 513 | $value['refund_due'] = $paymentVal; |
29c61b58 PJ |
514 | } |
515 | elseif ($paymentVal > 0) { | |
516 | $value['amount_owed'] = $paymentVal; | |
517 | } | |
bc2eeabb PJ |
518 | elseif ($lineItemTotal == $ftTotalAmt) { |
519 | $value['full_paid'] = $ftTotalAmt; | |
520 | } | |
0f602e3f PJ |
521 | } |
522 | } | |
523 | } | |
524 | return $value; | |
525 | } | |
96025800 | 526 | |
3efd9c58 DG |
527 | /** |
528 | * @param int $contributionId | |
529 | * | |
530 | * @return array | |
531 | */ | |
532 | public static function getTotalPayments($contributionId) { | |
76c28c8d DG |
533 | $statusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
534 | ||
535 | $sql = "SELECT SUM(ft.total_amount) FROM civicrm_financial_trxn ft | |
536 | INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') | |
3efd9c58 DG |
537 | WHERE eft.entity_id = %1 AND ft.is_payment = 1 AND ft.status_id = %2"; |
538 | ||
539 | $params = array( | |
540 | 1 => array($contributionId, 'Integer'), | |
541 | 2 => array($statusId, 'Integer'), | |
542 | ); | |
76c28c8d | 543 | |
3efd9c58 DG |
544 | return CRM_Core_DAO::singleValueQuery($sql, $params); |
545 | } | |
546 | ||
78c99516 | 547 | /** |
a40f5d6b PN |
548 | * Function records partial payment, complete's contribution if payment is fully paid |
549 | * and returns latest payment ie financial trxn | |
550 | * | |
78c99516 PN |
551 | * @param array $contribution |
552 | * @param array $params | |
553 | * | |
554 | * @return CRM_Core_BAO_FinancialTrxn | |
555 | */ | |
556 | public static function getPartialPaymentTrxn($contribution, $params) { | |
557 | $trxn = CRM_Contribute_BAO_Contribution::recordPartialPayment($contribution, $params); | |
558 | $paid = CRM_Core_BAO_FinancialTrxn::getTotalPayments($params['contribution_id']); | |
559 | $total = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution_id'], 'total_amount'); | |
560 | $cmp = bccomp($total, $paid, 5); | |
561 | if ($cmp == 0 || $cmp == -1) {// If paid amount is greater or equal to total amount | |
562 | civicrm_api3('Contribution', 'completetransaction', array('id' => $contribution['id'])); | |
563 | } | |
564 | return $trxn; | |
565 | } | |
566 | ||
f1eab68f PN |
567 | /** |
568 | * Get revenue amount for membership. | |
569 | * | |
570 | * @param array $lineItem | |
571 | * | |
572 | * @return array | |
573 | */ | |
574 | public static function getMembershipRevenueAmount($lineItem) { | |
575 | $revenueAmount = array(); | |
576 | $membershipDetail = civicrm_api3('Membership', 'getsingle', array( | |
577 | 'id' => $lineItem['entity_id'], | |
578 | )); | |
579 | if (empty($membershipDetail['end_date'])) { | |
580 | return $revenueAmount; | |
581 | } | |
582 | ||
583 | $startDate = strtotime($membershipDetail['start_date']); | |
584 | $endDate = strtotime($membershipDetail['end_date']); | |
585 | $startYear = date('Y', $startDate); | |
586 | $endYear = date('Y', $endDate); | |
587 | $startMonth = date('m', $startDate); | |
588 | $endMonth = date('m', $endDate); | |
589 | ||
590 | $monthOfService = (($endYear - $startYear) * 12) + ($endMonth - $startMonth); | |
591 | $startDateOfRevenue = $membershipDetail['start_date']; | |
592 | $typicalPayment = round(($lineItem['line_total'] / $monthOfService), 2); | |
593 | for ($i = 0; $i <= $monthOfService - 1; $i++) { | |
594 | $revenueAmount[$i]['amount'] = $typicalPayment; | |
595 | if ($i == 0) { | |
596 | $revenueAmount[$i]['amount'] -= (($typicalPayment * $monthOfService) - $lineItem['line_total']); | |
597 | } | |
598 | $revenueAmount[$i]['revenue_date'] = $startDateOfRevenue; | |
599 | $startDateOfRevenue = date('Y-m', strtotime('+1 month', strtotime($startDateOfRevenue))) . '-01'; | |
600 | } | |
601 | return $revenueAmount; | |
602 | } | |
603 | ||
6419695f PN |
604 | /** |
605 | * Create transaction for deferred revenue. | |
606 | * | |
607 | * @param array $lineItems | |
608 | * | |
609 | * @param array $contributionDetails | |
610 | * | |
611 | * @param bool $update | |
612 | * | |
613 | * @param string $context | |
614 | * | |
615 | */ | |
616 | public static function createDeferredTrxn($lineItems, $contributionDetails, $update = FALSE, $context = NULL) { | |
617 | if (empty($lineItems)) { | |
618 | return FALSE; | |
619 | } | |
620 | $revenueRecognitionDate = $contributionDetails->revenue_recognition_date; | |
621 | if (!CRM_Utils_System::isNull($revenueRecognitionDate)) { | |
622 | $statuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
623 | if (!$update | |
624 | && (CRM_Utils_Array::value($contributionDetails->contribution_status_id, $statuses) != 'Completed' | |
625 | || (CRM_Utils_Array::value($contributionDetails->contribution_status_id, $statuses) != 'Pending' | |
626 | && $contributionDetails->is_pay_later) | |
627 | ) | |
628 | ) { | |
629 | return; | |
630 | } | |
631 | $trxnParams = array( | |
632 | 'contribution_id' => $contributionDetails->id, | |
633 | 'fee_amount' => '0.00', | |
634 | 'currency' => $contributionDetails->currency, | |
635 | 'trxn_id' => $contributionDetails->trxn_id, | |
636 | 'status_id' => $contributionDetails->contribution_status_id, | |
637 | 'payment_instrument_id' => $contributionDetails->payment_instrument_id, | |
638 | 'check_number' => $contributionDetails->check_number, | |
639 | 'is_payment' => 1, | |
640 | ); | |
641 | ||
642 | $deferredRevenues = array(); | |
643 | foreach ($lineItems as $priceSetID => $lineItem) { | |
644 | if (!$priceSetID) { | |
645 | continue; | |
646 | } | |
647 | foreach ($lineItem as $key => $item) { | |
8cf6bd83 PN |
648 | $lineTotal = !empty($item['deferred_line_total']) ? $item['deferred_line_total'] : $item['line_total']; |
649 | if ($lineTotal <= 0 && !$update) { | |
6419695f PN |
650 | continue; |
651 | } | |
652 | $deferredRevenues[$key] = $item; | |
653 | if ($context == 'changeFinancialType') { | |
654 | $deferredRevenues[$key]['financial_type_id'] = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_LineItem', $item['id'], 'financial_type_id'); | |
655 | } | |
656 | if (in_array($item['entity_table'], | |
657 | array('civicrm_participant', 'civicrm_contribution')) | |
658 | ) { | |
659 | $deferredRevenues[$key]['revenue'][] = array( | |
8cf6bd83 | 660 | 'amount' => $lineTotal, |
6419695f PN |
661 | 'revenue_date' => $revenueRecognitionDate, |
662 | ); | |
663 | } | |
664 | else { | |
665 | // for membership | |
8cf6bd83 | 666 | $item['line_total'] = $lineTotal; |
6419695f PN |
667 | $deferredRevenues[$key]['revenue'] = self::getMembershipRevenueAmount($item); |
668 | } | |
669 | } | |
670 | } | |
671 | $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); | |
7340e501 | 672 | |
7f35de6b | 673 | CRM_Utils_Hook::alterDeferredRevenueItems($deferredRevenues, $contributionDetails, $update, $context); |
7340e501 | 674 | |
6419695f PN |
675 | foreach ($deferredRevenues as $key => $deferredRevenue) { |
676 | $results = civicrm_api3('EntityFinancialAccount', 'get', array( | |
677 | 'entity_table' => 'civicrm_financial_type', | |
678 | 'entity_id' => $deferredRevenue['financial_type_id'], | |
679 | 'account_relationship' => array('IN' => array('Income Account is', 'Deferred Revenue Account is')), | |
680 | )); | |
681 | if ($results['count'] != 2) { | |
682 | continue; | |
683 | } | |
684 | foreach ($results['values'] as $result) { | |
685 | if ($result['account_relationship'] == $accountRel) { | |
686 | $trxnParams['to_financial_account_id'] = $result['financial_account_id']; | |
687 | } | |
688 | else { | |
689 | $trxnParams['from_financial_account_id'] = $result['financial_account_id']; | |
690 | } | |
691 | } | |
692 | foreach ($deferredRevenue['revenue'] as $revenue) { | |
693 | $trxnParams['total_amount'] = $trxnParams['net_amount'] = $revenue['amount']; | |
694 | $trxnParams['trxn_date'] = CRM_Utils_Date::isoToMysql($revenue['revenue_date']); | |
695 | $financialTxn = CRM_Core_BAO_FinancialTrxn::create($trxnParams); | |
696 | $entityParams = array( | |
697 | 'entity_id' => $deferredRevenue['financial_item_id'], | |
698 | 'entity_table' => 'civicrm_financial_item', | |
699 | 'amount' => $revenue['amount'], | |
700 | 'financial_trxn_id' => $financialTxn->id, | |
701 | ); | |
702 | civicrm_api3('EntityFinancialTrxn', 'create', $entityParams); | |
703 | } | |
704 | } | |
705 | } | |
706 | } | |
707 | ||
0d8afee2 | 708 | } |