'entity_table' => "civicrm_contribution",
'financial_trxn_id' => $trxn->id,
'amount' => $params['total_amount'],
- 'currency' => $trxn->currency,
);
if (!empty($trxnEntityTable)) {
* array of category id's the contact belongs to.
*
*/
- public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE, $whereClause = '') {
+ public static function getFinancialTrxnId($entity_id, $orderBy = 'ASC', $newTrxn = FALSE, $whereClause = '', $fromAccountID = NULL) {
$ids = array('entityFinancialTrxnId' => NULL, 'financialTrxnId' => NULL);
+ $params = array(1 => array($entity_id, 'Integer'));
$condition = "";
if (!$newTrxn) {
$condition = " AND ((ceft1.entity_table IS NOT NULL) OR (cft.payment_instrument_id IS NOT NULL AND ceft1.entity_table IS NULL)) ";
}
+ if ($fromAccountID) {
+ $condition .= " AND (cft.from_financial_account_id <> %2 OR cft.from_financial_account_id IS NULL)";
+ $params[2] = array($fromAccountID, 'Integer');
+ }
if ($orderBy) {
$orderBy = CRM_Utils_Type::escape($orderBy, 'String');
}
ORDER BY cft.id {$orderBy}
LIMIT 1;";
- $params = array(1 => array($entity_id, 'Integer'));
$dao = CRM_Core_DAO::executeQuery($query, $params);
if ($dao->fetch()) {
$ids['entityFinancialTrxnId'] = $dao->id;
$params['trxnParams']['fee_amount'] = $params['trxnParams']['net_amount'] = 0;
$params['trxnParams']['status_id'] = $params['contribution_status_id'];
$params['trxnParams']['contribution_id'] = $contributionId;
+ $params['trxnParams']['is_payment'] = FALSE;
$trxn = self::create($params['trxnParams']);
if (empty($params['entity_id'])) {
$financialTrxnID = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($params['trxnParams']['contribution_id'], 'DESC');
if ($entityName == 'participant') {
$contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_ParticipantPayment', $entityId, 'contribution_id', 'participant_id');
- $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');
-
- if ($contributionId && $financialTypeId) {
- $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
- $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name');
-
- $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
- $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId);
- $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
- $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId);
-
- if (empty($lineItemTotal)) {
- $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId);
- if (count($ids) > 1) {
- $total = 0;
- foreach ($ids as $val) {
- $total += CRM_Price_BAO_LineItem::getLineTotal($val, 'civicrm_participant');
- }
- $lineItemTotal = $total;
- }
- else {
- $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($entityId, 'civicrm_participant');
- }
- }
- $sqlFtTotalAmt = "
+ }
+ else {
+ $contributionId = $entityId;
+ }
+ $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id');
+
+ if ($contributionId && $financialTypeId) {
+ $statusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name');
+ $refundStatusId = CRM_Core_OptionGroup::getValue('contribution_status', 'Refunded', 'name');
+
+ $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' "));
+ $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $relationTypeId);
+ $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' "));
+ $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId);
+
+ if (empty($lineItemTotal)) {
+ $lineItemTotal = CRM_Price_BAO_LineItem::getLineTotal($contributionId);
+ }
+ $sqlFtTotalAmt = "
SELECT SUM(ft.total_amount)
FROM civicrm_financial_trxn ft
- LEFT JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution')
- LEFT JOIN civicrm_contribution c ON (eft.entity_id = c.id)
- LEFT JOIN civicrm_participant_payment pp ON (pp.contribution_id = c.id)
-WHERE pp.participant_id = {$entityId} AND ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount}
+ INNER JOIN civicrm_entity_financial_trxn eft ON (ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_contribution' AND eft.entity_id = {$contributionId})
+WHERE ft.to_financial_account_id != {$toFinancialAccount} AND ft.to_financial_account_id != {$feeFinancialAccount}
AND ft.status_id IN ({$statusId}, {$refundStatusId})
";
- $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);
- $value = 0;
- if ($ftTotalAmt) {
- $value = $paymentVal = $lineItemTotal - $ftTotalAmt;
+ $ftTotalAmt = CRM_Core_DAO::singleValueQuery($sqlFtTotalAmt);
+ $value = 0;
+ if (!$ftTotalAmt) {
+ $ftTotalAmt = 0;
+ }
+ $value = $paymentVal = $lineItemTotal - $ftTotalAmt;
+ if ($returnType) {
+ $value = array();
+ if ($paymentVal < 0) {
+ $value['refund_due'] = $paymentVal;
}
- if ($returnType) {
- $value = array();
- if ($paymentVal < 0) {
- $value['refund_due'] = $paymentVal;
- }
- elseif ($paymentVal > 0) {
- $value['amount_owed'] = $paymentVal;
- }
- elseif ($lineItemTotal == $ftTotalAmt) {
- $value['full_paid'] = $ftTotalAmt;
+ elseif ($paymentVal > 0) {
+ $value['amount_owed'] = $paymentVal;
}
+ elseif ($lineItemTotal == $ftTotalAmt) {
+ $value['full_paid'] = $ftTotalAmt;
}
}
}
'status_id' => $contributionDetails->contribution_status_id,
'payment_instrument_id' => $contributionDetails->payment_instrument_id,
'check_number' => $contributionDetails->check_number,
- 'is_payment' => 1,
);
$deferredRevenues = array();
continue;
}
foreach ($lineItem as $key => $item) {
- if ($item['line_total'] <= 0 && !$update) {
+ $lineTotal = !empty($item['deferred_line_total']) ? $item['deferred_line_total'] : $item['line_total'];
+ if ($lineTotal <= 0 && !$update) {
continue;
}
$deferredRevenues[$key] = $item;
array('civicrm_participant', 'civicrm_contribution'))
) {
$deferredRevenues[$key]['revenue'][] = array(
- 'amount' => $item['line_total'],
+ 'amount' => $lineTotal,
'revenue_date' => $revenueRecognitionDate,
);
}
else {
// for membership
+ $item['line_total'] = $lineTotal;
$deferredRevenues[$key]['revenue'] = self::getMembershipRevenueAmount($item);
}
}
}
$accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
- // TODO: Call hook to alter $deferredRevenues
+
+ CRM_Utils_Hook::alterDeferredRevenueItems($deferredRevenues, $contributionDetails, $update, $context);
+
foreach ($deferredRevenues as $key => $deferredRevenue) {
$results = civicrm_api3('EntityFinancialAccount', 'get', array(
'entity_table' => 'civicrm_financial_type',