/** * Record adjusted amount. * * @param int $updatedAmount * @param int $paidAmount * @param int $contributionId * * @param int $taxAmount * @param bool $updateAmountLevel * * @return bool|\CRM_Core_BAO_FinancialTrxn */ public static function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId, $taxAmount = NULL, $updateAmountLevel = NULL) { $pendingAmount = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); $pendingAmount = CRM_Utils_Array::value('total_amount', $pendingAmount, 0); $balanceAmt = $updatedAmount - $paidAmount; if ($paidAmount != $pendingAmount) { $balanceAmt -= $pendingAmount; } $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); $pendingRefundStatusId = array_search('Pending refund', $contributionStatuses); $completedStatusId = array_search('Completed', $contributionStatuses); $updatedContributionDAO = new CRM_Contribute_BAO_Contribution(); $adjustedTrxn = $skip = FALSE; if ($balanceAmt) { if ($balanceAmt > 0 && $paidAmount != 0) { $contributionStatusVal = $partiallyPaidStatusId; } elseif ($balanceAmt < 0 && $paidAmount != 0) { $contributionStatusVal = $pendingRefundStatusId; } elseif ($paidAmount == 0) { //skip updating the contribution status if no payment is made $skip = TRUE; $updatedContributionDAO->cancel_date = 'null'; $updatedContributionDAO->cancel_reason = NULL; } // update contribution status and total amount without trigger financial code // as this is handled in current BAO function used for change selection $updatedContributionDAO->id = $contributionId; if (!$skip) { $updatedContributionDAO->contribution_status_id = $contributionStatusVal; } $updatedContributionDAO->total_amount = $updatedContributionDAO->net_amount = $updatedAmount; $updatedContributionDAO->fee_amount = 0; $updatedContributionDAO->tax_amount = $taxAmount; if (!empty($updateAmountLevel)) { $updatedContributionDAO->amount_level = $updateAmountLevel; } $updatedContributionDAO->save(); // adjusted amount financial_trxn creation $updatedContribution = CRM_Contribute_BAO_Contribution::getValues(array('id' => $contributionId), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId); $adjustedTrxnValues = array('from_financial_account_id' => NULL, 'to_financial_account_id' => $toFinancialAccount, 'total_amount' => $balanceAmt, 'status_id' => $completedStatusId, 'payment_instrument_id' => $updatedContribution->payment_instrument_id, 'contribution_id' => $updatedContribution->id, 'trxn_date' => date('YmdHis'), 'currency' => $updatedContribution->currency); $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues); } return $adjustedTrxn; }
/** * @param $updatedAmount * @param $paidAmount * @param $contributionId */ static function recordAdjustedAmt($updatedAmount, $paidAmount, $contributionId) { $balanceAmt = $updatedAmount - $paidAmount; $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); $partiallyPaidStatusId = array_search('Partially paid', $contributionStatuses); $pendngRefundStatusId = array_search('Pending refund', $contributionStatuses); $completedStatusId = array_search('Completed', $contributionStatuses); $updatedContributionDAO = new CRM_Contribute_BAO_Contribution(); if ($balanceAmt) { if ($balanceAmt > 0 && $paidAmount != 0) { $contributionStatusVal = $partiallyPaidStatusId; } elseif ($balanceAmt < 0 && $paidAmount != 0) { $contributionStatusVal = $pendngRefundStatusId; } elseif ($paidAmount == 0) { $contributionStatusVal = $completedStatusId; $updatedContributionDAO->cancel_date = 'null'; $updatedContributionDAO->cancel_reason = NULL; } // update contribution status and total amount without trigger financial code // as this is handled in current BAO function used for change selection $updatedContributionDAO->id = $contributionId; $updatedContributionDAO->contribution_status_id = $contributionStatusVal; $updatedContributionDAO->total_amount = $updatedAmount; $updatedContributionDAO->save(); $ftDetail = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); // adjusted amount financial_trxn creation if (empty($ftDetail['trxn_id'])) { $updatedContribution = CRM_Contribute_BAO_Contribution::getValues(array('id' => $contributionId), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray); $relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Accounts Receivable Account is' ")); $toFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($updatedContribution->financial_type_id, $relationTypeId); $adjustedTrxnValues = array('from_financial_account_id' => NULL, 'to_financial_account_id' => $toFinancialAccount, 'total_amount' => $balanceAmt, 'status_id' => CRM_Core_OptionGroup::getValue('contribution_status', 'Completed', 'name'), 'payment_instrument_id' => $updatedContribution->payment_instrument_id, 'contribution_id' => $updatedContribution->id, 'trxn_date' => date('YmdHis'), 'currency' => $updatedContribution->currency); $adjustedTrxn = CRM_Core_BAO_FinancialTrxn::create($adjustedTrxnValues); } else { // update the financial trxn amount as well, as the fee selections has been updated if ($balanceAmt != $ftDetail['total_amount']) { CRM_Core_DAO::setFieldValue('CRM_Core_BAO_FinancialTrxn', $ftDetail['trxn_id'], 'total_amount', $balanceAmt); } } } }
/** * Get list of payments displayed by Contribute_Page_PaymentInfo. * * @param int $id * @param $component * @param bool $getTrxnInfo * @param bool $usingLineTotal * * @return mixed */ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $usingLineTotal = FALSE) { if ($component == 'event') { $entity = 'participant'; $entityTable = 'civicrm_participant'; $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'contribution_id', 'participant_id'); if (!$contributionId) { if ($primaryParticipantId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_Participant', $id, 'registered_by_id')) { $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $primaryParticipantId, 'contribution_id', 'participant_id'); $id = $primaryParticipantId; } if (!$contributionId) { return; } } } $total = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); $baseTrxnId = !empty($total['trxn_id']) ? $total['trxn_id'] : NULL; $isBalance = NULL; if ($baseTrxnId) { $isBalance = TRUE; } else { $baseTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contributionId); $baseTrxnId = $baseTrxnId['financialTrxnId']; $isBalance = FALSE; } if (!CRM_Utils_Array::value('total_amount', $total) || $usingLineTotal) { // for additional participants if ($entityTable == 'civicrm_participant') { $ids = CRM_Event_BAO_Participant::getParticipantIds($contributionId); $total = 0; foreach ($ids as $val) { $total += CRM_Price_BAO_LineItem::getLineTotal($val, $entityTable); } } else { $total = CRM_Price_BAO_LineItem::getLineTotal($id, $entityTable); } } else { $baseTrxnId = $total['trxn_id']; $total = $total['total_amount']; } $paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE, $total); $contributionIsPayLater = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'is_pay_later'); $feeRelationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Expense Account is' ")); $financialTypeId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'financial_type_id'); $feeFinancialAccount = CRM_Contribute_PseudoConstant::financialAccountType($financialTypeId, $feeRelationTypeId); if ($paymentBalance == 0 && $contributionIsPayLater) { $paymentBalance = $total; } $info['total'] = $total; $info['paid'] = $total - $paymentBalance; $info['balance'] = $paymentBalance; $info['id'] = $id; $info['component'] = $component; $info['payLater'] = $contributionIsPayLater; $rows = array(); if ($getTrxnInfo && $baseTrxnId) { // Need to exclude fee trxn rows so filter out rows where TO FINANCIAL ACCOUNT is expense account $sql = "\nSELECT ft.total_amount, con.financial_type_id, ft.payment_instrument_id, ft.trxn_date, ft.trxn_id, ft.status_id, ft.check_number\nFROM civicrm_contribution con\n LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.entity_id = con.id AND eft.entity_table = 'civicrm_contribution')\n INNER JOIN civicrm_financial_trxn ft ON ft.id = eft.financial_trxn_id AND ft.to_financial_account_id != {$feeFinancialAccount}\nWHERE con.id = {$contributionId}\n"; // conditioned WHERE clause if ($isBalance) { // if balance trxn exists don't include details of it in transaction info $sql .= " AND ft.id != {$baseTrxnId} "; } $resultDAO = CRM_Core_DAO::executeQuery($sql); $statuses = CRM_Contribute_PseudoConstant::contributionStatus(); $financialTypes = CRM_Contribute_PseudoConstant::financialType(); while ($resultDAO->fetch()) { $paidByLabel = CRM_Core_PseudoConstant::getLabel('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $resultDAO->payment_instrument_id); $paidByName = CRM_Core_PseudoConstant::getName('CRM_Core_BAO_FinancialTrxn', 'payment_instrument_id', $resultDAO->payment_instrument_id); $val = array('total_amount' => $resultDAO->total_amount, 'financial_type' => $financialTypes[$resultDAO->financial_type_id], 'payment_instrument' => $paidByLabel, 'receive_date' => $resultDAO->trxn_date, 'trxn_id' => $resultDAO->trxn_id, 'status' => $statuses[$resultDAO->status_id]); if ($paidByName == 'Check') { $val['check_number'] = $resultDAO->check_number; } $rows[] = $val; } $info['transaction'] = $rows; } return $info; }
function getPaymentInfo($id, $component, $getTrxnInfo = FALSE) { if ($component == 'event') { $entity = 'participant'; $entityTable = 'civicrm_participant'; $contributionId = CRM_Core_DAO::getFieldValue('CRM_Event_BAO_ParticipantPayment', $id, 'contribution_id', 'participant_id'); } $total = CRM_Core_BAO_FinancialTrxn::getBalanceTrxnAmt($contributionId); $baseTrxnId = NULL; if (empty($total)) { $total = CRM_Price_BAO_LineItem::getLineTotal($id, $entityTable); } else { $baseTrxnId = $total['trxn_id']; $total = $total['total_amount']; } $paymentBalance = CRM_Core_BAO_FinancialTrxn::getPartialPaymentWithType($id, $entity, FALSE, $total); $info['total'] = $total; $info['paid'] = $total - $paymentBalance; $info['balance'] = $paymentBalance; $info['id'] = $id; $info['component'] = $component; $rows = array(); if ($getTrxnInfo && $baseTrxnId) { $sql = "\nSELECT ft.total_amount, con.financial_type_id, ft.payment_instrument_id, ft.trxn_date, ft.trxn_id, ft.status_id\nFROM civicrm_contribution con\n LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.entity_id = con.id AND eft.entity_table = 'civicrm_contribution')\n LEFT JOIN civicrm_financial_trxn ft ON ft.id = eft.financial_trxn_id\nWHERE ft.id != {$baseTrxnId} AND con.id = {$contributionId}\n"; $resultDAO = CRM_Core_DAO::executeQuery($sql); while ($resultDAO->fetch()) { $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); $statuses = CRM_Contribute_PseudoConstant::contributionStatus(); $financialTypes = CRM_Contribute_PseudoConstant::financialType(); $rows[] = array('total_amount' => $resultDAO->total_amount, 'financial_type' => $financialTypes[$resultDAO->financial_type_id], 'payment_instrument' => $paymentInstrument[$resultDAO->payment_instrument_id], 'receive_date' => $resultDAO->trxn_date, 'trxn_id' => $resultDAO->trxn_id, 'status' => $statuses[$resultDAO->status_id]); } $info['transaction'] = $rows; } return $info; }