From e3a3306b309f4a355b9469d4bfbd70ade679794c Mon Sep 17 00:00:00 2001 From: Rohan Bansal Date: Wed, 12 Jun 2019 17:42:24 +0530 Subject: [PATCH 1/2] feat(customer): Add report to show item prices per Customer --- .../customer_wise_item_price/__init__.py | 0 .../customer_wise_item_price.js | 27 +++++ .../customer_wise_item_price.json | 43 ++++++++ .../customer_wise_item_price.py | 101 ++++++++++++++++++ 4 files changed, 171 insertions(+) create mode 100644 erpnext/selling/report/customer_wise_item_price/__init__.py create mode 100644 erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.js create mode 100644 erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.json create mode 100644 erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py diff --git a/erpnext/selling/report/customer_wise_item_price/__init__.py b/erpnext/selling/report/customer_wise_item_price/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.js b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.js new file mode 100644 index 0000000000..d333c8be65 --- /dev/null +++ b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.js @@ -0,0 +1,27 @@ +// Copyright (c) 2016, Frappe Technologies Pvt. Ltd. and contributors +// For license information, please see license.txt +/* eslint-disable */ + +frappe.query_reports["Customer-wise Item Price"] = { + "filters": [ + { + "label": __("Customer"), + "fieldname": "customer", + "fieldtype": "Link", + "options": "Customer", + "reqd": 1 + }, + { + "label": __("Item"), + "fieldname": "item", + "fieldtype": "Link", + "options": "Item", + "get_query": () => { + return { + query: "erpnext.controllers.queries.item_query", + filters: { 'is_sales_item': 1 } + } + } + } + ] +} diff --git a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.json b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.json new file mode 100644 index 0000000000..998ba94a8b --- /dev/null +++ b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.json @@ -0,0 +1,43 @@ +{ + "add_total_row": 0, + "creation": "2019-06-12 03:25:36.263179", + "disable_prepared_report": 0, + "disabled": 0, + "docstatus": 0, + "doctype": "Report", + "idx": 0, + "is_standard": "Yes", + "letter_head": "Delta9", + "modified": "2019-06-12 03:25:36.263179", + "modified_by": "Administrator", + "module": "Selling", + "name": "Customer-wise Item Price", + "owner": "Administrator", + "prepared_report": 0, + "ref_doctype": "Customer", + "report_name": "Customer-wise Item Price", + "report_type": "Script Report", + "roles": [ + { + "role": "Sales User" + }, + { + "role": "Stock Manager" + }, + { + "role": "Accounts User" + }, + { + "role": "Accounts Manager" + }, + { + "role": "Sales Manager" + }, + { + "role": "Sales Master Manager" + }, + { + "role": "Stock User" + } + ] +} \ No newline at end of file diff --git a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py new file mode 100644 index 0000000000..bdd39229df --- /dev/null +++ b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py @@ -0,0 +1,101 @@ +# Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and contributors +# For license information, please see license.txt + +from __future__ import unicode_literals + +import frappe +from erpnext import get_default_company +from erpnext.accounts.party import get_party_details +from erpnext.stock.get_item_details import get_price_list_rate_for +from frappe import _ + + +def execute(filters=None): + if not filters: + filters = {} + + if not filters.get("customer"): + frappe.throw(_("Please select a Customer")) + + columns = get_columns(filters) + data = get_data(filters) + + return columns, data + + +def get_columns(filters=None): + return [ + { + "label": _("Item Code"), + "fieldname": "item_code", + "fieldtype": "Link", + "options": "Item", + "width": 150 + }, + { + "label": _("Item Name"), + "fieldname": "item_name", + "fieldtype": "Data", + "width": 200 + }, + { + "label": _("Selling Rate"), + "fieldname": "selling_rate", + "fieldtype": "Currency" + }, + { + "label": _("Available Stock"), + "fieldname": "available_stock", + "fieldtype": "Float", + "width": 150 + }, + { + "label": _("Price List"), + "fieldname": "price_list", + "fieldtype": "Link", + "options": "Price List", + "width": 120 + } + ] + + +def get_data(filters=None): + data = [] + customer_details = get_customer_details(filters) + items = get_selling_items(filters) + + for item in items: + price_list_rate = get_price_list_rate_for(customer_details, item.item_code) or 0.0 + available_stock = frappe.db.sql("SELECT sum(actual_qty) FROM `tabBin` WHERE item_code = %s", item.item_code) + available_stock = available_stock[0][0] if available_stock else None + + data.append({ + "item_code": item.item_code, + "item_name": item.item_name, + "selling_rate": price_list_rate, + "price_list": customer_details.get("price_list"), + "available_stock": available_stock, + }) + + return data + + +def get_customer_details(filters): + customer_details = get_party_details(party=filters.get("customer"), party_type="Customer") + customer_details.update({ + "company": get_default_company(), + "price_list": customer_details.get("selling_price_list") + }) + + return customer_details + + +def get_selling_items(filters): + if filters.get("item"): + item_filters = {"item_code": filters.get("item"), "is_sales_item": 1, "disabled": 0} + else: + item_filters = {"is_sales_item": 1, "disabled": 0} + + items = frappe.get_all("Item", filters=item_filters, fields=["item_code", "item_name"], order_by="item_name") + + return items \ No newline at end of file From 0426636a3270f23c6cccaa9ea519c850798c95fc Mon Sep 17 00:00:00 2001 From: Rohan Bansal Date: Mon, 17 Jun 2019 12:11:04 +0530 Subject: [PATCH 2/2] fix(customer): Improve performance by reducing queries --- .../customer_wise_item_price/customer_wise_item_price.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py index bdd39229df..eb9273a562 100644 --- a/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py +++ b/erpnext/selling/report/customer_wise_item_price/customer_wise_item_price.py @@ -62,12 +62,14 @@ def get_columns(filters=None): def get_data(filters=None): data = [] customer_details = get_customer_details(filters) + items = get_selling_items(filters) + item_stock_map = frappe.get_all("Bin", fields=["item_code", "sum(actual_qty) AS available"], group_by="item_code") + item_stock_map = {item.item_code: item.available for item in item_stock_map} for item in items: price_list_rate = get_price_list_rate_for(customer_details, item.item_code) or 0.0 - available_stock = frappe.db.sql("SELECT sum(actual_qty) FROM `tabBin` WHERE item_code = %s", item.item_code) - available_stock = available_stock[0][0] if available_stock else None + available_stock = item_stock_map.get(item.item_code) data.append({ "item_code": item.item_code, @@ -98,4 +100,4 @@ def get_selling_items(filters): items = frappe.get_all("Item", filters=item_filters, fields=["item_code", "item_name"], order_by="item_name") - return items \ No newline at end of file + return items