Skip to content

Commit

Permalink
CRM-19585, added fix for tax amount
Browse files Browse the repository at this point in the history
----------------------------------------
* CRM-19585: Sales tax issue
  https://issues.civicrm.org/jira/browse/CRM-19585
  • Loading branch information
pradpnayak committed Jan 16, 2017
1 parent 4a4051a commit f47e577
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,8 @@ public static function calculateMissingAmountParams(&$params, $contributionID) {
$params['fee_amount'] = 0;
}
}
if (!isset($params['net_amount'])) {
// recalculate net amount if it is not set or if tax amount is set
if (!isset($params['net_amount']) || isset($params['tax_amount'])) {
if (!$contributionID) {
$params['net_amount'] = $params['total_amount'] - $params['fee_amount'];
}
Expand Down Expand Up @@ -3218,7 +3219,7 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues =
$params['trxnParams'] = $trxnParams;

if (!empty($params['prevContribution'])) {
$updated = FALSE;
$updated = $ignoreChangeAmount = FALSE;
$params['trxnParams']['total_amount'] = $trxnParams['total_amount'] = $params['total_amount'] = $params['prevContribution']->total_amount;
$params['trxnParams']['fee_amount'] = $params['prevContribution']->fee_amount;
$params['trxnParams']['net_amount'] = $params['prevContribution']->net_amount;
Expand Down Expand Up @@ -3263,9 +3264,9 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues =
}
}
self::updateFinancialAccounts($params, 'changeFinancialType');
/* $params['trxnParams']['to_financial_account_id'] = $trxnParams['to_financial_account_id']; */
$params['financial_account_id'] = $newFinancialAccount;
$params['total_amount'] = $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = $trxnParams['total_amount'];
list($changeFTAmount, $ignoreChangeAmount) = self::calcluateFTChangeAmount($params, $trxnParams['total_amount']);
$params['total_amount'] = $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = $changeFTAmount;
self::updateFinancialAccounts($params);
$params['trxnParams']['to_financial_account_id'] = $trxnParams['to_financial_account_id'];
$updated = TRUE;
Expand Down Expand Up @@ -3341,7 +3342,7 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues =
$params['trxnParams']['net_amount'] = CRM_Utils_Array::value('net_amount', $params);
$params['trxnParams']['total_amount'] = $trxnParams['total_amount'] = $params['total_amount'] = $totalAmount;
$params['trxnParams']['trxn_id'] = $params['contribution']->trxn_id;
if (isset($totalAmount) &&
if (!$ignoreChangeAmount && isset($totalAmount) &&
$totalAmount != $params['prevContribution']->total_amount
) {
//Update Financial Records
Expand Down Expand Up @@ -3444,6 +3445,12 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
}
if ($context == 'changedAmount' || $context == 'changeFinancialType') {
$itemAmount = $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = $params['total_amount'] - $params['prevContribution']->total_amount;
if (isset($params['tax_amount'])) {
$itemAmount -= CRM_Utils_Array::value('tax_amount', $params, 0);
}
if (isset($params['tax_amount'])) {
$itemAmount += $params['prevContribution']->tax_amount;
}
}
if ($context == 'changedStatus') {
//get all the statuses
Expand Down Expand Up @@ -3546,6 +3553,7 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
}
}
$trxn = CRM_Core_BAO_FinancialTrxn::create($params['trxnParams']);
$previousLineItem = CRM_Price_BAO_LineItem::getLineItemsByContributionID($params['prevContribution']->id);
$params['entity_id'] = $trxn->id;
if ($context != 'changePaymentInstrument') {
$itemParams['entity_table'] = 'civicrm_line_item';
Expand All @@ -3568,16 +3576,15 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
if ($context == 'changeFinancialType' || self::isContributionStatusNegative($params['contribution']->contribution_status_id)) {
$diff = -1;
}
if (!empty($params['is_quick_config'])) {
$amount = $itemAmount;
if (!$amount) {
$amount = $params['total_amount'];
}
}
else {
$amount = $diff * $fieldValues['line_total'];
}

// calculate financial item amount
$amountParams = array(
'diff' => $diff,
'line_total' => $fieldValues['line_total'],
'previous_line_total' => CRM_Utils_Array::value('line_total', CRM_Utils_Array::value($fieldValues['id'], $previousLineItem)),
'item_amount' => $itemAmount,
);
$amount = self::calcluateFinancialItemAmount($params, $amountParams, $context);
$itemParams = array(
'transaction_date' => $receiveDate,
'contact_id' => $params['prevContribution']->contact_id,
Expand All @@ -3593,10 +3600,22 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
$params['line_item'][$fieldId][$fieldValueId]['deferred_line_total'] = $amount;
$params['line_item'][$fieldId][$fieldValueId]['financial_item_id'] = $financialItem->id;

if ($fieldValues['tax_amount']) {
$taxAmount = CRM_Utils_Array::value('tax_amount', $fieldValues);
$taxFinancialType = $fieldValues['financial_type_id'];
if ($context == 'changeFinancialType') {
$taxAmount = $previousLineItem[$fieldValues['id']]['tax_amount'];
$taxFinancialType = $previousLineItem[$fieldValues['id']]['financial_type_id'];
}

if ($taxAmount) {
$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
$itemParams['amount'] = $diff * $fieldValues['tax_amount'];
if ($context == 'changedAmount') {
$itemParams['amount'] = $diff * ($fieldValues['tax_amount'] - $previousLineItem[$fieldValues['id']]['tax_amount']);
}
else {
$itemParams['amount'] = $diff * $taxAmount;
}
$itemParams['description'] = $taxTerm;
if ($fieldValues['financial_type_id']) {
$itemParams['financial_account_id'] = self::getFinancialAccountId($fieldValues['financial_type_id']);
Expand Down

0 comments on commit f47e577

Please sign in to comment.