From 00866567484a475937fc352ba4c7ba1f2d909c27 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 31 Jan 2025 16:10:25 +0530 Subject: [PATCH] fix: Gross Profit Report with Correct Totals and Gross Margin (backport #45548) (#45597) * fix: Gross Profit Report with Correct Totals and Gross Margin (#45548) Co-authored-by: Sanket322 (cherry picked from commit aaf720ab61f8a8b3e3d03513dd4c650b6106413d) # Conflicts: # erpnext/accounts/report/gross_profit/test_gross_profit.py # erpnext/patches.txt * fix: conflicts --------- Co-authored-by: Sanket Shah <113279972+Sanket322@users.noreply.github.com> Co-authored-by: ljain112 --- .../report/gross_profit/gross_profit.json | 4 +-- .../report/gross_profit/gross_profit.py | 28 +++++++++++++++++ .../report/gross_profit/test_gross_profit.py | 30 +++++++++++++++++++ erpnext/patches.txt | 2 ++ .../v14_0/disable_add_row_in_gross_profit.py | 5 ++++ 5 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 erpnext/patches/v14_0/disable_add_row_in_gross_profit.py diff --git a/erpnext/accounts/report/gross_profit/gross_profit.json b/erpnext/accounts/report/gross_profit/gross_profit.json index 0730ffd77e5..dfb7a991e3e 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.json +++ b/erpnext/accounts/report/gross_profit/gross_profit.json @@ -1,5 +1,5 @@ { - "add_total_row": 1, + "add_total_row": 0, "columns": [], "creation": "2013-02-25 17:03:34", "disable_prepared_report": 0, @@ -9,7 +9,7 @@ "filters": [], "idx": 3, "is_standard": "Yes", - "modified": "2022-02-11 10:18:36.956558", + "modified": "2025-01-27 18:40:24.493829", "modified_by": "Administrator", "module": "Accounts", "name": "Gross Profit", diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index 5539954231a..07d85983a84 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -166,7 +166,14 @@ def get_data_when_grouped_by_invoice(columns, gross_profit_data, filters, group_ # removing Item Code and Item Name columns del columns[4:6] + total_base_amount = 0 + total_buying_amount = 0 + for src in gross_profit_data.si_list: + if src.indent == 1: + total_base_amount += src.base_amount or 0.0 + total_buying_amount += src.buying_amount or 0.0 + row = frappe._dict() row.indent = src.indent row.parent_invoice = src.parent_invoice @@ -177,6 +184,27 @@ def get_data_when_grouped_by_invoice(columns, gross_profit_data, filters, group_ data.append(row) + total_gross_profit = total_base_amount - total_buying_amount + data.append( + frappe._dict( + { + "sales_invoice": "Total", + "qty": None, + "avg._selling_rate": None, + "valuation_rate": None, + "selling_amount": total_base_amount, + "buying_amount": total_buying_amount, + "gross_profit": total_gross_profit, + "gross_profit_%": flt( + (total_gross_profit / total_base_amount) * 100.0, + cint(frappe.db.get_default("currency_precision")) or 3, + ) + if total_base_amount + else 0, + } + ) + ) + def get_data_when_not_grouped_by_invoice(gross_profit_data, filters, group_wise_columns, data): for src in gross_profit_data.grouped_data: diff --git a/erpnext/accounts/report/gross_profit/test_gross_profit.py b/erpnext/accounts/report/gross_profit/test_gross_profit.py index 8b1621ae782..94e44b76fcd 100644 --- a/erpnext/accounts/report/gross_profit/test_gross_profit.py +++ b/erpnext/accounts/report/gross_profit/test_gross_profit.py @@ -558,3 +558,33 @@ class TestGrossProfit(FrappeTestCase): } gp_entry = [x for x in data if x.parent_invoice == sinv.name] self.assertDictContainsSubset(expected_entry, gp_entry[0]) + + def test_gross_profit_groupby_invoices(self): + create_sales_invoice( + qty=1, + rate=100, + company=self.company, + customer=self.customer, + item_code=self.item, + item_name=self.item, + cost_center=self.cost_center, + warehouse=self.warehouse, + debit_to=self.debit_to, + parent_cost_center=self.cost_center, + update_stock=0, + currency="INR", + income_account=self.income_account, + expense_account=self.expense_account, + ) + + filters = frappe._dict( + company=self.company, from_date=nowdate(), to_date=nowdate(), group_by="Invoice" + ) + + _, data = execute(filters=filters) + total = data[-1] + + self.assertEqual(total.selling_amount, 100.0) + self.assertEqual(total.buying_amount, 0.0) + self.assertEqual(total.gross_profit, 100.0) + self.assertEqual(total.get("gross_profit_%"), 100.0) diff --git a/erpnext/patches.txt b/erpnext/patches.txt index 8cc7ada03c2..46fccd1bb6f 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -368,3 +368,5 @@ erpnext.patches.v14_0.remove_cancelled_asset_capitalization_from_asset erpnext.patches.v14_0.enable_set_priority_for_pricing_rules #1 erpnext.patches.v14_0.update_currency_exchange_settings_for_frankfurter erpnext.patches.v14_0.update_stock_uom_in_work_order_item +erpnext.patches.v14_0.disable_add_row_in_gross_profit + diff --git a/erpnext/patches/v14_0/disable_add_row_in_gross_profit.py b/erpnext/patches/v14_0/disable_add_row_in_gross_profit.py new file mode 100644 index 00000000000..d95503bef0a --- /dev/null +++ b/erpnext/patches/v14_0/disable_add_row_in_gross_profit.py @@ -0,0 +1,5 @@ +import frappe + + +def execute(): + frappe.db.set_value("Report", "Gross Profit", "add_total_row", 0)