3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 class CRM_Core_BAO_FinancialTrxn
extends CRM_Financial_DAO_FinancialTrxn
{
39 * @return \CRM_Financial_DAO_FinancialTrxn
43 public function __construct() {
44 parent
::__construct();
48 * Takes an associative array and creates a financial transaction object.
50 * @param array $params
51 * (reference ) an assoc array of name/value pairs.
53 * @param string $trxnEntityTable
56 * @return CRM_Core_BAO_FinancialTrxn
58 public static function create(&$params, $trxnEntityTable = NULL) {
59 $trxn = new CRM_Financial_DAO_FinancialTrxn();
60 $trxn->copyValues($params);
62 if (!CRM_Utils_Rule
::currencyCode($trxn->currency
)) {
63 $trxn->currency
= CRM_Core_Config
::singleton()->defaultCurrency
;
68 // save to entity_financial_trxn table
69 $entityFinancialTrxnParams
71 'entity_table' => "civicrm_contribution",
72 'financial_trxn_id' => $trxn->id
,
73 'amount' => $params['total_amount'],
74 'currency' => $trxn->currency
,
77 if (!empty($trxnEntityTable)) {
78 $entityFinancialTrxnParams['entity_table'] = $trxnEntityTable['entity_table'];
79 $entityFinancialTrxnParams['entity_id'] = $trxnEntityTable['entity_id'];
82 $entityFinancialTrxnParams['entity_id'] = $params['contribution_id'];
85 $entityTrxn = new CRM_Financial_DAO_EntityFinancialTrxn();
86 $entityTrxn->copyValues($entityFinancialTrxnParams);
92 * @param int $contributionId
93 * @param int $contributionFinancialTypeId
97 public static function getBalanceTrxnAmt($contributionId, $contributionFinancialTypeId = NULL) {
98 if (!$contributionFinancialTypeId) {
99 $contributionFinancialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_BAO_Contribution', $contributionId, 'financial_type_id');
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);
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";
105 $p[1] = array($contributionId, 'Integer');
106 $p[2] = array($toFinancialAccount, 'Integer');
108 $balanceAmtDAO = CRM_Core_DAO
::executeQuery($q, $p);
110 if ($balanceAmtDAO->N
) {
111 $ret['total_amount'] = 0;
113 while ($balanceAmtDAO->fetch()) {
114 $ret['trxn_id'] = $balanceAmtDAO->id
;
115 $ret['total_amount'] +
= $balanceAmtDAO->total_amount
;
122 * Fetch object based on array of properties.
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.
129 * @return CRM_Contribute_BAO_ContributionType
131 public static function retrieve(&$params, &$defaults) {
132 $financialItem = new CRM_Financial_DAO_FinancialTrxn();
133 $financialItem->copyValues($params);
134 if ($financialItem->find(TRUE)) {
135 CRM_Core_DAO
::storeValues($financialItem, $defaults);
136 return $financialItem;
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.
146 * Id of the entity usually the contributionID.
147 * @param string $orderBy
148 * To get single trxn id for a entity table i.e last or first.
149 * @param bool $newTrxn
150 * @param string $whereClause
151 * Additional where parameters
154 * array of category id's the contact belongs to.
157 public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE, $whereClause = '') {
158 $ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL);
162 $condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) ";
166 $orderBy = CRM_Utils_Type
::escape($orderBy, 'String');
169 $query = "SELECT ceft.id, ceft.financial_trxn_id, cft.trxn_id FROM `civicrm_financial_trxn` cft
170 LEFT JOIN civicrm_entity_financial_trxn ceft
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)
178 ORDER BY cft.id {$orderBy}
181 $params = array(1 => array($entity_id, 'Integer'));
182 $dao = CRM_Core_DAO
::executeQuery($query, $params);
184 $ids['entityFinancialTrxnId'] = $dao->id
;
185 $ids['financialTrxnId'] = $dao->financial_trxn_id
;
186 $ids['trxn_id'] = $dao->trxn_id
;
192 * Get the transaction id for the (latest) refund associated with a contribution.
194 * @param int $contributionID
197 public static function getRefundTransactionTrxnID($contributionID) {
198 $ids = self
::getRefundTransactionIDs($contributionID);
199 return isset($ids['trxn_id']) ?
$ids['trxn_id'] : NULL;
203 * Get the transaction id for the (latest) refund associated with a contribution.
205 * @param int $contributionID
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");
214 * Given an entity_id and entity_table, check for corresponding entity_financial_trxn and financial_trxn record.
215 * @todo This should be moved to separate BAO for EntityFinancialTrxn when we start adding more code for that object.
217 * @param int $entity_id
218 * Id of the entity usually the contactID.
221 * array of category id's the contact belongs to.
224 public static function getFinancialTrxnTotal($entity_id) {
226 SELECT (ft.amount+SUM(ceft.amount)) AS total FROM civicrm_entity_financial_trxn AS ft
227 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.financial_trxn_id = ceft.entity_id
228 WHERE ft.entity_table = 'civicrm_contribution' AND ft.entity_id = %1
231 $sqlParams = array(1 => array($entity_id, 'Integer'));
232 return CRM_Core_DAO
::singleValueQuery($query, $sqlParams);
237 * Given an financial_trxn_id check for previous entity_financial_trxn.
239 * @param $financial_trxn_id
240 * Id of the latest payment.
244 * array of previous payments
247 public static function getPayments($financial_trxn_id) {
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
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'";
264 $sqlParams = array(1 => array($financial_trxn_id, 'Integer'));
265 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
268 while ($dao->fetch()) {
269 $result[$i]['financial_trxn_id'] = $dao->financial_trxn_id
;
270 $result[$i]['amount'] = $dao->amount
;
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);
280 $result[0]['financial_trxn_id'] = $financial_trxn_id;
281 $result[0]['amount'] = $dao->amount
;
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.
292 * Id of the entity usually the contactID.
293 * @param string $entity_table
294 * Name of the entity table usually 'civicrm_contact'.
297 * array of category id's the contact belongs to.
300 public static function getFinancialTrxnLineTotal($entity_id, $entity_table = 'civicrm_contribution') {
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'
303 LEFT JOIN civicrm_line_item AS lt ON lt.id = fi.entity_id AND lt.entity_table = %2
304 WHERE lt.entity_id = %1 ";
306 $sqlParams = array(1 => array($entity_id, 'Integer'), 2 => array($entity_table, 'String'));
307 $dao = CRM_Core_DAO
::executeQuery($query, $sqlParams);
308 while ($dao->fetch()) {
309 $result[$dao->financial_trxn_id
][$dao->id
] = $dao->amount
;
311 if (!empty($result)) {
320 * Delete financial transaction.
322 * @param int $entity_id
324 * TRUE on success, FALSE otherwise.
326 public static function deleteFinancialTrxn($entity_id) {
327 $query = "DELETE ceft1, cfi, ceft, cft FROM `civicrm_financial_trxn` cft
328 LEFT JOIN civicrm_entity_financial_trxn ceft
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'
332 LEFT JOIN civicrm_financial_item cfi
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')));
340 * Create financial transaction for premium.
342 * @param array $params
344 * - financial_type_id
350 public static function createPremiumTrxn($params) {
351 if ((empty($params['financial_type_id']) ||
empty($params['contributionId'])) && empty($params['oldPremium'])) {
355 if (!empty($params['cost'])) {
356 $contributionStatuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
357 $financialAccountType = CRM_Contribute_PseudoConstant
::financialAccountType($params['financial_type_id']);
358 $accountRelationship = CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name IN ('Premiums Inventory Account is', 'Cost of Sales Account is')");
359 $toFinancialAccount = !empty($params['isDeleted']) ?
'Premiums Inventory Account is' : 'Cost of Sales Account is';
360 $fromFinancialAccount = !empty($params['isDeleted']) ?
'Cost of Sales Account is' : 'Premiums Inventory Account is';
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),
368 'status_id' => array_search('Completed', $contributionStatuses),
370 $trxnEntityTable['entity_table'] = 'civicrm_contribution';
371 $trxnEntityTable['entity_id'] = $params['contributionId'];
372 CRM_Core_BAO_FinancialTrxn
::create($financialtrxn, $trxnEntityTable);
375 if (!empty($params['oldPremium'])) {
376 $premiumParams = array(
377 'id' => $params['oldPremium']['product_id'],
379 $productDetails = array();
380 CRM_Contribute_BAO_ManagePremiums
::retrieve($premiumParams, $productDetails);
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'],
388 CRM_Core_BAO_FinancialTrxn
::createPremiumTrxn($params);
393 * Create financial trxn and items when fee is charged.
395 * @param array $params
396 * To create trxn entries.
400 public static function recordFees($params) {
401 $expenseTypeId = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
402 $domainId = CRM_Core_Config
::domainID();
404 if (!empty($params['prevContribution'])) {
405 $amount = $params['prevContribution']->fee_amount
;
407 $amount = $params['fee_amount'] - $amount;
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');
416 $financialTypeId = $params['financial_type_id'];
418 $financialAccount = CRM_Contribute_PseudoConstant
::financialAccountType($financialTypeId, $expenseTypeId);
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;
423 $params['trxnParams']['fee_amount'] = $params['trxnParams']['net_amount'] = 0;
424 $params['trxnParams']['status_id'] = $params['contribution_status_id'];
425 $params['trxnParams']['contribution_id'] = $contributionId;
426 $trxn = self
::create($params['trxnParams']);
427 if (empty($params['entity_id'])) {
428 $financialTrxnID = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC');
429 $params['entity_id'] = $financialTrxnID['financialTrxnId'];
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'),
438 'description' => 'Fee',
439 'status_id' => CRM_Core_OptionGroup
::getValue('financial_item_status', 'Paid', 'name'),
440 'entity_table' => 'civicrm_financial_trxn',
441 'entity_id' => $params['entity_id'],
442 'currency' => $params['trxnParams']['currency'],
444 $trxnIDS['id'] = $trxn->id
;
445 $financialItem = CRM_Financial_BAO_FinancialItem
::create($fItemParams, NULL, $trxnIDS);
449 * get partial payment amount and type of it.
451 * @param int $entityId
452 * @param string $entityName
453 * @param bool $returnType
454 * @param int $lineItemTotal
456 * @return array|int|NULL|string
457 * [payment type => amount]
458 * payment type: 'amount_owed' or 'refund_due'
460 public static function getPartialPaymentWithType($entityId, $entityName = 'participant', $returnType = TRUE, $lineItemTotal = NULL) {
462 if (empty($entityName)) {
466 if ($entityName == 'participant') {
467 $contributionId = CRM_Core_DAO
::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id');
468 $financialTypeId = CRM_Core_DAO
::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');
470 if ($contributionId && $financialTypeId) {
471 $statusId = CRM_Core_OptionGroup
::getValue('contribution_status', 'Completed', 'name');
472 $refundStatusId = CRM_Core_OptionGroup
::getValue('contribution_status', 'Refunded', 'name');
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);
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);
479 if (empty($lineItemTotal)) {
480 $ids = CRM_Event_BAO_Participant
::getParticipantIds($contributionId);
481 if (count($ids) > 1) {
483 foreach ($ids as $val) {
484 $total +
= CRM_Price_BAO_LineItem
::getLineTotal($val, 'civicrm_participant');
486 $lineItemTotal = $total;
489 $lineItemTotal = CRM_Price_BAO_LineItem
::getLineTotal($entityId, 'civicrm_participant');
493 SELECT SUM(ft.total_amount)
494 FROM civicrm_financial_trxn ft
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)
498 WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount}
499 AND ft.status_id IN ({$statusId}, {$refundStatusId})
501 $ftTotalAmt = CRM_Core_DAO
::singleValueQuery($sqlFtTotalAmt);
504 $value = $paymentVal = $lineItemTotal - $ftTotalAmt;
508 if ($paymentVal < 0) {
509 $value['refund_due'] = $paymentVal;
511 elseif ($paymentVal > 0) {
512 $value['amount_owed'] = $paymentVal;
514 elseif ($lineItemTotal == $ftTotalAmt) {
515 $value['full_paid'] = $ftTotalAmt;
524 * @param int $contributionId
528 public static function getTotalPayments($contributionId) {
529 $statusId = CRM_Core_PseudoConstant
::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
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')
533 WHERE eft.entity_id = %1 AND ft.is_payment = 1 AND ft.status_id = %2";
536 1 => array($contributionId, 'Integer'),
537 2 => array($statusId, 'Integer'),
540 return CRM_Core_DAO
::singleValueQuery($sql, $params);
544 * Function records partial payment, complete's contribution if payment is fully paid
545 * and returns latest payment ie financial trxn
547 * @param array $contribution
548 * @param array $params
550 * @return CRM_Core_BAO_FinancialTrxn
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']));
564 * Get revenue amount for membership.
566 * @param array $lineItem
570 public static function getMembershipRevenueAmount($lineItem) {
571 $revenueAmount = array();
572 $membershipDetail = civicrm_api3('Membership', 'getsingle', array(
573 'id' => $lineItem['entity_id'],
575 if (empty($membershipDetail['end_date'])) {
576 return $revenueAmount;
579 $startDate = strtotime($membershipDetail['start_date']);
580 $endDate = strtotime($membershipDetail['end_date']);
581 $startYear = date('Y', $startDate);
582 $endYear = date('Y', $endDate);
583 $startMonth = date('m', $startDate);
584 $endMonth = date('m', $endDate);
586 $monthOfService = (($endYear - $startYear) * 12) +
($endMonth - $startMonth);
587 $startDateOfRevenue = $membershipDetail['start_date'];
588 $typicalPayment = round(($lineItem['line_total'] / $monthOfService), 2);
589 for ($i = 0; $i <= $monthOfService - 1; $i++
) {
590 $revenueAmount[$i]['amount'] = $typicalPayment;
592 $revenueAmount[$i]['amount'] -= (($typicalPayment * $monthOfService) - $lineItem['line_total']);
594 $revenueAmount[$i]['revenue_date'] = $startDateOfRevenue;
595 $startDateOfRevenue = date('Y-m', strtotime('+1 month', strtotime($startDateOfRevenue))) . '-01';
597 return $revenueAmount;
601 * Create transaction for deferred revenue.
603 * @param array $lineItems
605 * @param array $contributionDetails
607 * @param bool $update
609 * @param string $context
612 public static function createDeferredTrxn($lineItems, $contributionDetails, $update = FALSE, $context = NULL) {
613 if (empty($lineItems)) {
616 $revenueRecognitionDate = $contributionDetails->revenue_recognition_date
;
617 if (!CRM_Utils_System
::isNull($revenueRecognitionDate)) {
618 $statuses = CRM_Contribute_PseudoConstant
::contributionStatus(NULL, 'name');
620 && (CRM_Utils_Array
::value($contributionDetails->contribution_status_id
, $statuses) != 'Completed'
621 ||
(CRM_Utils_Array
::value($contributionDetails->contribution_status_id
, $statuses) != 'Pending'
622 && $contributionDetails->is_pay_later
)
628 'contribution_id' => $contributionDetails->id
,
629 'fee_amount' => '0.00',
630 'currency' => $contributionDetails->currency
,
631 'trxn_id' => $contributionDetails->trxn_id
,
632 'status_id' => $contributionDetails->contribution_status_id
,
633 'payment_instrument_id' => $contributionDetails->payment_instrument_id
,
634 'check_number' => $contributionDetails->check_number
,
638 $deferredRevenues = array();
639 foreach ($lineItems as $priceSetID => $lineItem) {
643 foreach ($lineItem as $key => $item) {
644 if ($item['line_total'] <= 0 && !$update) {
647 $deferredRevenues[$key] = $item;
648 if ($context == 'changeFinancialType') {
649 $deferredRevenues[$key]['financial_type_id'] = CRM_Core_DAO
::getFieldValue('CRM_Price_DAO_LineItem', $item['id'], 'financial_type_id');
651 if (in_array($item['entity_table'],
652 array('civicrm_participant', 'civicrm_contribution'))
654 $deferredRevenues[$key]['revenue'][] = array(
655 'amount' => $item['line_total'],
656 'revenue_date' => $revenueRecognitionDate,
661 $deferredRevenues[$key]['revenue'] = self
::getMembershipRevenueAmount($item);
665 $accountRel = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
666 // TODO: Call hook to alter $deferredRevenues
667 foreach ($deferredRevenues as $key => $deferredRevenue) {
668 $results = civicrm_api3('EntityFinancialAccount', 'get', array(
669 'entity_table' => 'civicrm_financial_type',
670 'entity_id' => $deferredRevenue['financial_type_id'],
671 'account_relationship' => array('IN' => array('Income Account is', 'Deferred Revenue Account is')),
673 if ($results['count'] != 2) {
676 foreach ($results['values'] as $result) {
677 if ($result['account_relationship'] == $accountRel) {
678 $trxnParams['to_financial_account_id'] = $result['financial_account_id'];
681 $trxnParams['from_financial_account_id'] = $result['financial_account_id'];
684 foreach ($deferredRevenue['revenue'] as $revenue) {
685 $trxnParams['total_amount'] = $trxnParams['net_amount'] = $revenue['amount'];
686 $trxnParams['trxn_date'] = CRM_Utils_Date
::isoToMysql($revenue['revenue_date']);
687 $financialTxn = CRM_Core_BAO_FinancialTrxn
::create($trxnParams);
688 $entityParams = array(
689 'entity_id' => $deferredRevenue['financial_item_id'],
690 'entity_table' => 'civicrm_financial_item',
691 'amount' => $revenue['amount'],
692 'financial_trxn_id' => $financialTxn->id
,
694 civicrm_api3('EntityFinancialTrxn', 'create', $entityParams);