Skip to content
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

refactor: Exchange rate revaluation to handle accounts with zero account balance (backport #33165) #33503

Merged

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Jan 2, 2023

This is an automatic backport of pull request #33165 done by Mergify.
Cherry-pick of 914b230 has failed:

On branch mergify/bp/version-14-hotfix/pr-33165
Your branch is up to date with 'origin/version-14-hotfix'.

You are currently cherry-picking commit 914b23038c.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.js
	modified:   erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.json
	modified:   erpnext/accounts/doctype/exchange_rate_revaluation_account/exchange_rate_revaluation_account.json
	modified:   erpnext/accounts/doctype/gl_entry/gl_entry.py
	modified:   erpnext/accounts/doctype/journal_entry/journal_entry.json
	modified:   erpnext/accounts/doctype/journal_entry/journal_entry.py
	modified:   erpnext/accounts/general_ledger.py
	modified:   erpnext/accounts/report/accounts_receivable/test_accounts_receivable.py
	modified:   erpnext/accounts/report/general_ledger/test_general_ledger.py
	modified:   erpnext/accounts/report/utils.py

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   erpnext/accounts/doctype/exchange_rate_revaluation/exchange_rate_revaluation.py

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/github/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally


Mergify commands and options

More conditions and actions can be found in the documentation.

You can also trigger Mergify actions by commenting on this pull request:

  • @Mergifyio refresh will re-evaluate the rules
  • @Mergifyio rebase will rebase this PR on its base branch
  • @Mergifyio update will merge the base branch into this PR
  • @Mergifyio backport <destination> will backport this PR on <destination> branch

Additionally, on Mergify dashboard you can:

  • look at your merge queues
  • generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com

@codecov
Copy link

codecov bot commented Jan 2, 2023

Codecov Report

Merging #33503 (914b230) into version-14-hotfix (a02ddc9) will increase coverage by 0.13%.
The diff coverage is 74.62%.

❗ Current head 914b230 differs from pull request most recent head 146733c. Consider uploading reports for the commit 146733c to get more accurate results

Additional details and impacted files
@@                  Coverage Diff                  @@
##           version-14-hotfix   #33503      +/-   ##
=====================================================
+ Coverage              63.91%   64.04%   +0.13%     
=====================================================
  Files                    817      818       +1     
  Lines                  58167    58840     +673     
=====================================================
+ Hits                   37180    37687     +507     
- Misses                 20987    21153     +166     
Impacted Files Coverage Δ
...count_balance_timeline/account_balance_timeline.py 0.00% <0.00%> (ø)
...next/accounts/doctype/bank_account/bank_account.py 77.27% <0.00%> (ø)
.../accounts/doctype/bank_guarantee/bank_guarantee.py 0.00% <0.00%> (ø)
...ype/bank_statement_import/bank_statement_import.py 0.00% <0.00%> (ø)
...ounts/doctype/bank_transaction/bank_transaction.py 70.37% <ø> (ø)
...octype/bank_transaction/bank_transaction_upload.py 0.00% <0.00%> (ø)
...of_accounts_importer/chart_of_accounts_importer.py 0.00% <0.00%> (ø)
...rpnext/accounts/doctype/cost_center/cost_center.py 39.24% <ø> (ø)
...ement_of_accounts/process_statement_of_accounts.py 0.00% <0.00%> (ø)
...ype/repost_payment_ledger/repost_payment_ledger.py 0.00% <0.00%> (ø)
... and 271 more

…unt balance (#33165)

* refactor: new type for JE - Exchange Gain or Loss

* refactor: skip few validations for Exchanage Gain Or Loss type Jour

* refactor: ERR create 2 journals for handling zero and non-zero compa

1. Additional check box accounts table to identify accounts with zero balance
2. Accounts with zero balance only in either of the 2 currencies will be handled on separate Journal

* refactor: skips few validation for allowing 0 debit/credit

* fix: General Ledger presentaion currency

* test: fix test case in general ledger

* test: fix failing test case in AR report

(cherry picked from commit 914b230)
@ruthra-kumar ruthra-kumar force-pushed the mergify/bp/version-14-hotfix/pr-33165 branch from c676545 to 146733c Compare January 2, 2023 09:35
@deepeshgarg007 deepeshgarg007 merged commit 648e979 into version-14-hotfix Jan 2, 2023
@mergify mergify bot deleted the mergify/bp/version-14-hotfix/pr-33165 branch January 2, 2023 11:08
@frappe-pr-bot
Copy link
Collaborator

🎉 This PR is included in version 14.12.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Comment on lines +186 to +250
for d in [x for x in account_details if not x.zero_balance]:
current_exchange_rate = (
d.balance / d.balance_in_account_currency if d.balance_in_account_currency else 0
)
new_exchange_rate = get_exchange_rate(d.account_currency, company_currency, posting_date)
new_balance_in_base_currency = flt(d.balance_in_account_currency * new_exchange_rate)
gain_loss = flt(new_balance_in_base_currency, precision) - flt(d.balance, precision)
if gain_loss:
accounts.append(
{
"account": d.account,
"party_type": d.party_type,
"party": d.party,
"account_currency": d.account_currency,
"balance_in_base_currency": d.balance,
"balance_in_account_currency": d.balance_in_account_currency,
"zero_balance": d.zero_balance,
"current_exchange_rate": current_exchange_rate,
"new_exchange_rate": new_exchange_rate,
"new_balance_in_base_currency": new_balance_in_base_currency,
"new_balance_in_account_currency": d.balance_in_account_currency,
"gain_loss": gain_loss,
}
)

# Handle Accounts with '0' balance in Account/Base Currency
for d in [x for x in account_details if x.zero_balance]:

# TODO: Set new balance in Base/Account currency
if d.balance > 0:
current_exchange_rate = new_exchange_rate = 0

new_balance_in_account_currency = 0 # this will be '0'
new_balance_in_base_currency = 0 # this will be '0'
gain_loss = flt(new_balance_in_base_currency, precision) - flt(d.balance, precision)
else:
new_exchange_rate = 0
new_balance_in_base_currency = 0
new_balance_in_account_currency = 0

current_exchange_rate = calculate_exchange_rate_using_last_gle(
company, d.account, d.party_type, d.party
)

gain_loss = new_balance_in_account_currency - (
current_exchange_rate * d.balance_in_account_currency
)

if gain_loss:
accounts.append(
{
"account": d.account,
"party_type": d.party_type,
"party": d.party,
"account_currency": d.account_currency,
"balance_in_base_currency": d.balance,
"balance_in_account_currency": d.balance_in_account_currency,
"zero_balance": d.zero_balance,
"current_exchange_rate": current_exchange_rate,
"new_exchange_rate": new_exchange_rate,
"new_balance_in_base_currency": new_balance_in_base_currency,
"new_balance_in_account_currency": new_balance_in_account_currency,
"gain_loss": gain_loss,
}
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruthra-kumar I would like to know why iterate twice account_details. Can I refactor it?

Copy link
Member

@ruthra-kumar ruthra-kumar Feb 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not processing all the account_details twice. First loop only processes accounts with balances, second one only processes accounts with zero balance in account/base currency. It's mentioned in the comment as well.

# Handle Accounts with balance in both Account/Base Currency
for d in [x for x in account_details if not x.zero_balance]:

This separation is so that code is easy to reason about.

Comment on lines +215 to +232
if d.balance > 0:
current_exchange_rate = new_exchange_rate = 0

new_balance_in_account_currency = 0 # this will be '0'
new_balance_in_base_currency = 0 # this will be '0'
gain_loss = flt(new_balance_in_base_currency, precision) - flt(d.balance, precision)
else:
new_exchange_rate = 0
new_balance_in_base_currency = 0
new_balance_in_account_currency = 0

current_exchange_rate = calculate_exchange_rate_using_last_gle(
company, d.account, d.party_type, d.party
)

gain_loss = new_balance_in_account_currency - (
current_exchange_rate * d.balance_in_account_currency
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ruthra-kumar Entries here are supposed to be either balance == 0 or balance_in_account_currency == 0, however
I have seen cases when d.balance < 0 which is normal, I think the logic here could be different. If we calculate current_exchange_rate, this would be negative also, mathematically reasonable but not in business. We could instead set it to zero and apply the same logic as if it were positive. gain_loss = flt(new_balance_in_base_currency, precision) - flt(d.balance, precision). In this case, the condition here is not necessary. Apologize me if I musunderstood the logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.

is a bug. It should've been !=.

@dj12djdjs has created a PR for this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants