diff --git a/erpnext/accounts/report/gross_profit/gross_profit.js b/erpnext/accounts/report/gross_profit/gross_profit.js index 1f7d24db1e..ba17a94e8d 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.js +++ b/erpnext/accounts/report/gross_profit/gross_profit.js @@ -23,6 +23,12 @@ frappe.query_reports["Gross Profit"] = { "fieldtype": "Date", "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"), diff --git a/erpnext/accounts/report/gross_profit/gross_profit.py b/erpnext/accounts/report/gross_profit/gross_profit.py index a5859e31b0..9a02d1b8e8 100644 --- a/erpnext/accounts/report/gross_profit/gross_profit.py +++ b/erpnext/accounts/report/gross_profit/gross_profit.py @@ -302,6 +302,12 @@ class GrossProfitGenerator(object): 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 8500aea415..4a9af490cf 100644 --- a/erpnext/accounts/report/utils.py +++ b/erpnext/accounts/report/utils.py @@ -135,3 +135,25 @@ def get_appropriate_company(filters): company = get_default_company() return company + +@frappe.whitelist() +def get_invoiced_item_gross_margin(sales_invoice=None, item_code=None, company=None, with_item_data=False): + 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) + result = gross_profit_data.grouped_data + if not with_item_data: + result = sum([d.gross_profit for d in result]) + + return result