Skip to content

Conversation

@Navin-S-R
Copy link
Member

Issue: Net Profit value in the Gross and Net Profit Report shows incorrect value because total income and expense amounts are calculated using only the zero-indexed row.
This leads a discrepancy, when the Net Profit value is compared with the Profit and Loss Statement, especially when multiple root-level accounts exist.

Ref: 56322

Solution: Calculate income and expense totals by summing all root-level accounts.

Before:

Screenshot 2026-01-05 at 14 21 14

After:

Screenshot 2026-01-05 at 14 17 32

Bacport Needed v15

@github-actions github-actions bot added the needs-tests This PR needs automated unit-tests. label Jan 5, 2026
@codecov
Copy link

codecov bot commented Jan 5, 2026

Codecov Report

❌ Patch coverage is 0% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.10%. Comparing base (8d186d6) to head (c84986d).
⚠️ Report is 239 commits behind head on develop.

Files with missing lines Patch % Lines
...d_net_profit_report/gross_and_net_profit_report.py 0.00% 8 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #51513      +/-   ##
===========================================
- Coverage    79.11%   79.10%   -0.01%     
===========================================
  Files         1179     1179              
  Lines       121360   121359       -1     
===========================================
- Hits         96009    96007       -2     
- Misses       25351    25352       +1     
Files with missing lines Coverage Δ
...d_net_profit_report/gross_and_net_profit_report.py 0.00% <0.00%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Navin-S-R Navin-S-R marked this pull request as ready for review January 5, 2026 14:06
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 5, 2026

📝 Walkthrough

Walkthrough

The get_net_profit method in the Gross and Net Profit Report has been refactored to calculate profit/loss by aggregating root rows rather than accessing single row values. The method now filters non-indented entries from gross_income, non_gross_income, gross_expense, and non_gross_expense categories, then sums across the corresponding root rows for each period instead of relying on the first element in each list. This logic change maintains the overall control flow while improving how per-period totals are derived.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Suggested labels

backport version-15-hotfix

Suggested reviewers

  • ruthra-kumar
  • diptanilsaha

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing net profit calculation by using root node accounts instead of a single indexed row.
Description check ✅ Passed The description is directly related to the changeset, explaining the issue, solution, and providing visual evidence with before/after screenshots.
✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between afc5dda and c84986d.

📒 Files selected for processing (1)
  • erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-12-16T05:33:58.723Z
Learnt from: Abdeali099
Repo: frappe/erpnext PR: 51078
File: erpnext/accounts/doctype/financial_report_template/financial_report_engine.py:486-491
Timestamp: 2025-12-16T05:33:58.723Z
Learning: In ERPNext/Frappe codebase, query.run(as_dict=True) returns frappe._dict objects that support both dict-style access (obj["key"]) and attribute-style access (obj.key). Therefore, attribute access on query results is valid and will not raise AttributeError. When reviewing Python code, prefer attribute access (obj.key) for readability where the key is known to exist, but ensure existence checks or fallback handling if there is any doubt about missing keys.

Applied to files:

  • erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Summary
🔇 Additional comments (2)
erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py (2)

230-233: Aggregation logic correctly sums across all root accounts.

The change from single-row access (gross_income[0]) to summing all roots (sum(flt(row.get(key, 0)) for row in gross_income_roots)) correctly addresses the issue described in the PR objectives.

The implementation is safe:

  • Empty root lists: sum() returns 0
  • Missing period keys: .get(key, 0) provides safe fallback
  • None values: flt(None) returns 0.0

222-225: LGTM! Root filtering logic correctly identifies root-level accounts.

The filtering using not flt(row.get("indent")) correctly captures rows with indent == 0 (root accounts). The logic handles edge cases well:

  • Missing indent field: treated as root (indent 0)
  • Empty lists: handled by or [] fallback
  • None values: flt(None) returns 0.0

The commit message confirms this is an intentional fix: "calculate net profit amount from root node accounts", so the behavior is as designed.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py (1)

175-201: Apply the same aggregation fix to get_profit for consistency.

The get_profit function still uses the old single-row approach on lines 189-190:

gross_income_for_period = flt(gross_income[0].get(key, 0)) if gross_income else 0
gross_expense_for_period = flt(gross_expense[0].get(key, 0)) if gross_expense else 0

This mirrors the bug fixed in get_net_profit. If a company has multiple root-level accounts marked with include_in_gross=1, only the first is included in the Gross Profit calculation. The get_net_profit function already demonstrates the correct approach (lines 222-233):

gross_income_roots = [row for row in (gross_income or []) if not flt(row.get("indent"))]
gross_income_for_period = sum(flt(row.get(key, 0)) for row in gross_income_roots)

Apply this same pattern to get_profit to aggregate all root accounts correctly.

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between afc5dda and c84986d.

📒 Files selected for processing (1)
  • erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-12-16T05:33:58.723Z
Learnt from: Abdeali099
Repo: frappe/erpnext PR: 51078
File: erpnext/accounts/doctype/financial_report_template/financial_report_engine.py:486-491
Timestamp: 2025-12-16T05:33:58.723Z
Learning: In ERPNext/Frappe codebase, query.run(as_dict=True) returns frappe._dict objects that support both dict-style access (obj["key"]) and attribute-style access (obj.key). Therefore, attribute access on query results is valid and will not raise AttributeError. When reviewing Python code, prefer attribute access (obj.key) for readability where the key is known to exist, but ensure existence checks or fallback handling if there is any doubt about missing keys.

Applied to files:

  • erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Summary
🔇 Additional comments (2)
erpnext/accounts/report/gross_and_net_profit_report/gross_and_net_profit_report.py (2)

230-233: Aggregation logic correctly sums across all root accounts.

The change from single-row access (gross_income[0]) to summing all roots (sum(flt(row.get(key, 0)) for row in gross_income_roots)) correctly addresses the issue described in the PR objectives.

The implementation is safe:

  • Empty root lists: sum() returns 0
  • Missing period keys: .get(key, 0) provides safe fallback
  • None values: flt(None) returns 0.0

222-225: LGTM! Root filtering logic correctly identifies root-level accounts.

The filtering using not flt(row.get("indent")) correctly captures rows with indent == 0 (root accounts). The logic handles edge cases well:

  • Missing indent field: treated as root (indent 0)
  • Empty lists: handled by or [] fallback
  • None values: flt(None) returns 0.0

The commit message confirms this is an intentional fix: "calculate net profit amount from root node accounts", so the behavior is as designed.

@ruthra-kumar ruthra-kumar self-assigned this Jan 6, 2026
@ruthra-kumar ruthra-kumar added backport version-15-hotfix backport version-16-hotfix Backport to the Version 16 Hotfix branch labels Jan 19, 2026
@ruthra-kumar ruthra-kumar merged commit e7f6125 into frappe:develop Jan 19, 2026
15 checks passed
ruthra-kumar added a commit that referenced this pull request Jan 19, 2026
…-51513

fix: calculate net profit amount from root node accounts (backport #51513)
ruthra-kumar added a commit that referenced this pull request Jan 19, 2026
…-51513

fix: calculate net profit amount from root node accounts (backport #51513)
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

backport version-15-hotfix backport version-16-hotfix Backport to the Version 16 Hotfix branch needs-tests This PR needs automated unit-tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants