feat: Get invoiced item's gross margin using API
diff --git a/erpnext/accounts/report/gross_profit/gross_profit.js b/erpnext/accounts/report/gross_profit/gross_profit.js
index 1f7d24d..ba17a94 100644
--- a/erpnext/accounts/report/gross_profit/gross_profit.js
+++ b/erpnext/accounts/report/gross_profit/gross_profit.js
@@ -24,6 +24,12 @@
"default": frappe.defaults.get_user_default("year_end_date")
},
{
+ "fieldname":"sales_invoice",
+ "label": __("Sales Invoice"),
+ "fieldtype": "Link",
+ "options": "Sales Invoice"
+ },
+ {
"fieldname":"group_by",
"label": __("Group By"),
"fieldtype": "Select",
diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py
index a5859e3..9a02d1b 100644
--- a/erpnext/accounts/report/gross_profit/gross_profit.py
+++ b/erpnext/accounts/report/gross_profit/gross_profit.py
@@ -302,6 +302,12 @@
sales_person_cols = ""
sales_team_table = ""
+ if self.filters.get("sales_invoice"):
+ conditions += " and `tabSales Invoice`.name = %(sales_invoice)s"
+
+ if self.filters.get("item_code"):
+ conditions += " and `tabSales Invoice Item`.item_code = %(item_code)s"
+
self.si_list = frappe.db.sql("""
select
`tabSales Invoice Item`.parenttype, `tabSales Invoice Item`.parent,
diff --git a/erpnext/accounts/report/utils.py b/erpnext/accounts/report/utils.py
index 8500aea..0b17c8f 100644
--- a/erpnext/accounts/report/utils.py
+++ b/erpnext/accounts/report/utils.py
@@ -135,3 +135,22 @@
company = get_default_company()
return company
+
+@frappe.whitelist()
+def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None):
+ from erpnext.accounts.report.gross_profit.gross_profit import GrossProfitGenerator
+
+ sales_invoice = sales_invoice or frappe.form_dict.get('sales_invoice')
+ item_code = item_code or frappe.form_dict.get('item_code')
+ company = company or frappe.get_cached_value("Sales Invoice", sales_invoice, 'company')
+
+ filters = {
+ 'sales_invoice': sales_invoice,
+ 'item_code': item_code,
+ 'company': company,
+ 'group_by': 'Invoice'
+ }
+
+ gross_profit_data = GrossProfitGenerator(filters)
+
+ return gross_profit_data.grouped_data