From 6301b321d894db91df3efb51b6cb8f52d7eafb04 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 30 Jan 2025 11:21:52 +0530 Subject: [PATCH] fix: Gross Profit Report with Correct Totals and Gross Margin (backport #45548) (#45598) fix: Gross Profit Report with Correct Totals and Gross Margin (#45548) Co-authored-by: Sanket322 (cherry picked from commit aaf720ab61f8a8b3e3d03513dd4c650b6106413d) Co-authored-by: Sanket Shah <113279972+Sanket322@users.noreply.github.com> --- .../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 | 1 + .../v14_0/disable_add_row_in_gross_profit.py | 5 ++++ 5 files changed, 66 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 5df37603b9a..4802b0f35c1 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -178,7 +178,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 @@ -189,6 +196,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 721be79ed88..c6c8a0266ef 100644 --- a/erpnext/accounts/report/gross_profit/test_gross_profit.py +++ b/erpnext/accounts/report/gross_profit/test_gross_profit.py @@ -605,3 +605,33 @@ class TestGrossProfit(FrappeTestCase): item_from_sinv2 = [x for x in data if x.parent_invoice == sinv2.name] self.assertEqual(len(item_from_sinv2), 1) self.assertEqual(1800, item_from_sinv2[0].valuation_rate) + + 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 f40801e2776..d3651ee7060 100644 --- a/erpnext/patches.txt +++ b/erpnext/patches.txt @@ -391,3 +391,4 @@ erpnext.patches.v15_0.rename_manufacturing_settings_field erpnext.patches.v15_0.migrate_checkbox_to_select_for_reconciliation_effect erpnext.patches.v15_0.sync_auto_reconcile_config execute:frappe.db.set_single_value("Accounts Settings", "exchange_gain_loss_posting_date", "Payment") +erpnext.patches.v14_0.disable_add_row_in_gross_profit \ No newline at end of file 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)