Skip to content

Commit

Permalink
fix: Gross Profit Report with Correct Totals and Gross Margin (#45548)
Browse files Browse the repository at this point in the history
Co-authored-by: Sanket322 <shahsanket322003.com>
(cherry picked from commit aaf720a)

# Conflicts:
#	erpnext/accounts/report/gross_profit/test_gross_profit.py
#	erpnext/patches.txt
  • Loading branch information
Sanket322 authored and mergify[bot] committed Jan 29, 2025
1 parent 39e82df commit df11103
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 2 deletions.
4 changes: 2 additions & 2 deletions erpnext/accounts/report/gross_profit/gross_profit.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"add_total_row": 1,
"add_total_row": 0,
"columns": [],
"creation": "2013-02-25 17:03:34",
"disable_prepared_report": 0,
Expand All @@ -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",
Expand Down
28 changes: 28 additions & 0 deletions erpnext/accounts/report/gross_profit/gross_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
82 changes: 82 additions & 0 deletions erpnext/accounts/report/gross_profit/test_gross_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,86 @@ def test_different_rates_in_si_and_dn(self):
"gross_profit_%": 12.5,
}
gp_entry = [x for x in data if x.parent_invoice == sinv.name]
<<<<<<< HEAD
self.assertDictContainsSubset(expected_entry, gp_entry[0])
=======
report_output = {k: v for k, v in gp_entry[0].items() if k in expected_entry}
self.assertEqual(report_output, expected_entry)

def test_valuation_rate_without_previous_sle(self):
"""
Test Valuation rate calculation when stock ledger is empty and invoices are against different warehouses
"""
stock_settings = frappe.get_doc("Stock Settings")
stock_settings.valuation_method = "FIFO"
stock_settings.save()

item = create_item(
item_code="_Test Wirebound Notebook",
is_stock_item=1,
)
item.allow_negative_stock = True
item.save()
self.item = item.item_code

item.reload()
item.valuation_rate = 1900
item.save()
sinv1 = self.create_sales_invoice(qty=1, rate=2000, posting_date=nowdate(), do_not_submit=True)
sinv1.update_stock = 1
sinv1.set_warehouse = self.warehouse
sinv1.items[0].warehouse = self.warehouse
sinv1.save().submit()

item.reload()
item.valuation_rate = 1800
item.save()
sinv2 = self.create_sales_invoice(qty=1, rate=2000, posting_date=nowdate(), do_not_submit=True)
sinv2.update_stock = 1
sinv2.set_warehouse = self.finished_warehouse
sinv2.items[0].warehouse = self.finished_warehouse
sinv2.save().submit()

filters = frappe._dict(
company=self.company, from_date=nowdate(), to_date=nowdate(), group_by="Invoice"
)
columns, data = execute(filters=filters)

item_from_sinv1 = [x for x in data if x.parent_invoice == sinv1.name]
self.assertEqual(len(item_from_sinv1), 1)
self.assertEqual(1900, item_from_sinv1[0].valuation_rate)

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)
>>>>>>> aaf720ab61 (fix: Gross Profit Report with Correct Totals and Gross Margin (#45548))
12 changes: 12 additions & 0 deletions erpnext/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,15 @@ 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
<<<<<<< HEAD
=======
erpnext.patches.v15_0.enable_allow_existing_serial_no
erpnext.patches.v15_0.update_cc_in_process_statement_of_accounts
erpnext.patches.v15_0.refactor_closing_stock_balance #5
erpnext.patches.v15_0.update_asset_status_to_work_in_progress
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
>>>>>>> aaf720ab61 (fix: Gross Profit Report with Correct Totals and Gross Margin (#45548))
5 changes: 5 additions & 0 deletions erpnext/patches/v14_0/disable_add_row_in_gross_profit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import frappe


def execute():
frappe.db.set_value("Report", "Gross Profit", "add_total_row", 0)

0 comments on commit df11103

Please sign in to comment.