-
-
Notifications
You must be signed in to change notification settings - Fork 850
dev/financial#236 Fix financial txn rows on contribution convert #33753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3079,15 +3079,67 @@ public static function recordFinancialAccounts(&$params, CRM_Contribute_DAO_Cont | |
| $updated = CRM_Core_BAO_FinancialTrxn::updateFinancialAccountsOnPaymentInstrumentChange($params); | ||
| } | ||
|
|
||
| //if Change contribution amount | ||
| $params['trxnParams']['fee_amount'] = $params['fee_amount'] ?? NULL; | ||
| $params['trxnParams']['net_amount'] = $params['net_amount'] ?? NULL; | ||
| $params['trxnParams']['total_amount'] = $trxnParams['total_amount'] = $params['total_amount'] = $totalAmount; | ||
| $params['trxnParams']['trxn_id'] = $params['contribution']->trxn_id; | ||
| if (isset($totalAmount) && | ||
| bccomp($totalAmount, $params['prevContribution']->total_amount, 5) !== 0 | ||
| ) { | ||
| //Update Financial Records | ||
|
|
||
| // Change contribution currency | ||
| if (isset($params['currency']) && $params['currency'] !== $params['prevContribution']->currency) { | ||
| // Create a negative transaction for the first amount in the first currency | ||
| $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = - $params['prevContribution']->net_amount; | ||
| $params['trxnParams']['currency'] = $params['prevContribution']->currency; | ||
| CRM_Contribute_BAO_FinancialProcessor::updateFinancialAccounts($params, 'changedCurrency'); | ||
|
|
||
| CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($params['line_item'] ?? NULL, $params['contribution'], TRUE, 'changedCurrency'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ejegg The 'changedCurrency' context doesn't do anything here as there's nothing in createDeferredTrxn. |
||
| // Set trxnParams back to amounts for new currency | ||
| $params['trxnParams']['total_amount'] = $totalAmount; | ||
| $params['trxnParams']['fee_amount'] = $params['fee_amount'] ?? NULL; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ejegg Is something potentially changing the fee_amount somewhere? |
||
| $params['trxnParams']['net_amount'] = $params['net_amount'] ?? $totalAmount; | ||
|
|
||
| // Fix line items and financial items | ||
| if (empty($params['skipLineItem'])) { | ||
| if ($params['prevContribution']->net_amount) { | ||
| // Replace with new currency and amount, proportionally | ||
| $conversionFactor = $params['trxnParams']['net_amount'] / $params['prevContribution']->net_amount; | ||
| $fieldToFix = [ | ||
| 'subTotal' | ||
| ]; | ||
| foreach ($params['line_item'] as $index => &$lineItems) { | ||
| foreach ($lineItems as $lineItemID => &$lineItem) { | ||
| foreach ($fieldToFix as $field) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ejegg Could simplify this, as it is just the one field. |
||
| $lineItem[$field] = $lineItem[$field] * $conversionFactor; | ||
| } | ||
| CRM_Price_BAO_LineItem::create($lineItem); | ||
|
|
||
| $financialItem = \Civi\Api4\FinancialItem::get(FALSE) | ||
| ->addWhere('entity_table', '=', 'civicrm_line_item') | ||
| ->addWhere('entity_id', '=', $lineItemID) | ||
| ->execute()->first(); | ||
| $financialItemParams = [ | ||
| 'id' => $financialItem['id'], | ||
| 'amount' => $financialItem['amount'] * $conversionFactor, | ||
| 'currency' => $params['currency'], | ||
| 'description' => ($lineItem['qty'] != 1 ? $lineItem['qty'] . ' of ' : '') . $lineItem['label'], | ||
| ]; | ||
| CRM_Financial_BAO_FinancialItem::create($financialItemParams); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // Then we sent the currency back to the new currency for the next call to the financial functions | ||
| $params['trxnParams']['currency'] = $params['currency']; | ||
| // And zero out the previous amount, so the next block creates a new financial txn with the full amount | ||
| // in the new currency. | ||
| $params['prevContribution']->total_amount = $params['prevContribution']->net_amount = 0; | ||
| // We have wrangled the line items into shape already - don't create any more. | ||
| $params['skipLineItem'] = TRUE; | ||
| $updated = TRUE; | ||
| } | ||
|
|
||
| // Change contribution amount | ||
| if (isset($totalAmount) && bccomp($totalAmount, $params['prevContribution']->total_amount, 5) !== 0 ) { | ||
| // Update Financial Records | ||
| $params['trxnParams']['from_financial_account_id'] = NULL; | ||
| CRM_Contribute_BAO_FinancialProcessor::updateFinancialAccounts($params, 'changedAmount'); | ||
| CRM_Core_BAO_FinancialTrxn::createDeferredTrxn($params['line_item'] ?? NULL, $params['contribution'], TRUE, 'changedAmount'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ejegg I think it would make sense to move most of this here: https://github.com/civicrm/civicrm-core/blob/master/CRM/Contribute/BAO/FinancialProcessor.php#L213