Minor fix in gross profit report

This commit is contained in:
Nabin Hait 2014-09-17 12:13:31 +05:30
parent 54938d431c
commit 556fbc487d

View File

@ -9,29 +9,29 @@ from erpnext.stock.utils import get_buying_amount, get_sales_bom_buying_amount
def execute(filters=None): def execute(filters=None):
if not filters: filters = {} if not filters: filters = {}
stock_ledger_entries = get_stock_ledger_entries(filters) stock_ledger_entries = get_stock_ledger_entries(filters)
source = get_source_data(filters) source = get_source_data(filters)
item_sales_bom = get_item_sales_bom() item_sales_bom = get_item_sales_bom()
columns = [__("Delivery Note/Sales Invoice") + "::120", _("Link") + "::30", _("Posting Date") + ":Date", _("Posting Time"), columns = [_("Delivery Note/Sales Invoice") + "::120", _("Link") + "::30", _("Posting Date") + ":Date", _("Posting Time"),
_("Item Code") + ":Link/Item", _("Item Name"), _("Description"), _("Warehouse") + ":Link/Warehouse", _("Item Code") + ":Link/Item", _("Item Name"), _("Description"), _("Warehouse") + ":Link/Warehouse",
_("Qty") + ":Float", _("Selling Rate") + ":Currency", _("Avg. Buying Rate") + ":Currency", _("Qty") + ":Float", _("Selling Rate") + ":Currency", _("Avg. Buying Rate") + ":Currency",
_("Selling Amount") + ":Currency", _("Buying Amount") + ":Currency", _("Selling Amount") + ":Currency", _("Buying Amount") + ":Currency",
_("Gross Profit") + ":Currency", _("Gross Profit %") + ":Percent", _("Project") + ":Link/Project"] _("Gross Profit") + ":Currency", _("Gross Profit %") + ":Percent", _("Project") + ":Link/Project"]
data = [] data = []
for row in source: for row in source:
selling_amount = flt(row.base_amount) selling_amount = flt(row.base_amount)
item_sales_bom_map = item_sales_bom.get(row.parenttype, {}).get(row.name, frappe._dict()) item_sales_bom_map = item_sales_bom.get(row.parenttype, {}).get(row.name, frappe._dict())
if item_sales_bom_map.get(row.item_code): if item_sales_bom_map.get(row.item_code):
buying_amount = get_sales_bom_buying_amount(row.item_code, row.warehouse, buying_amount = get_sales_bom_buying_amount(row.item_code, row.warehouse,
row.parenttype, row.name, row.item_row, stock_ledger_entries, item_sales_bom_map) row.parenttype, row.name, row.item_row, stock_ledger_entries, item_sales_bom_map)
else: else:
buying_amount = get_buying_amount(row.parenttype, row.name, row.item_row, buying_amount = get_buying_amount(row.parenttype, row.name, row.item_row,
stock_ledger_entries.get((row.item_code, row.warehouse), [])) stock_ledger_entries.get((row.item_code, row.warehouse), []))
buying_amount = buying_amount > 0 and buying_amount or 0 buying_amount = buying_amount > 0 and buying_amount or 0
gross_profit = selling_amount - buying_amount gross_profit = selling_amount - buying_amount
@ -39,41 +39,41 @@ def execute(filters=None):
gross_profit_percent = (gross_profit / selling_amount) * 100.0 gross_profit_percent = (gross_profit / selling_amount) * 100.0
else: else:
gross_profit_percent = 0.0 gross_profit_percent = 0.0
icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \ icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \
% ("/".join(["#Form", row.parenttype, row.name]),) % ("/".join(["#Form", row.parenttype, row.name]),)
data.append([row.name, icon, row.posting_date, row.posting_time, row.item_code, row.item_name, data.append([row.name, icon, row.posting_date, row.posting_time, row.item_code, row.item_name,
row.description, row.warehouse, row.qty, row.base_rate, row.description, row.warehouse, row.qty, row.base_rate,
row.qty and (buying_amount / row.qty) or 0, row.base_amount, buying_amount, row.qty and (buying_amount / row.qty) or 0, row.base_amount, buying_amount,
gross_profit, gross_profit_percent, row.project]) gross_profit, gross_profit_percent, row.project])
return columns, data return columns, data
def get_stock_ledger_entries(filters): def get_stock_ledger_entries(filters):
query = """select item_code, voucher_type, voucher_no, query = """select item_code, voucher_type, voucher_no,
voucher_detail_no, posting_date, posting_time, stock_value, voucher_detail_no, posting_date, posting_time, stock_value,
warehouse, actual_qty as qty warehouse, actual_qty as qty
from `tabStock Ledger Entry`""" from `tabStock Ledger Entry`"""
if filters.get("company"): if filters.get("company"):
query += """ where company=%(company)s""" query += """ where company=%(company)s"""
query += " order by item_code desc, warehouse desc, posting_date desc, posting_time desc, name desc" query += " order by item_code desc, warehouse desc, posting_date desc, posting_time desc, name desc"
res = frappe.db.sql(query, filters, as_dict=True) res = frappe.db.sql(query, filters, as_dict=True)
out = {} out = {}
for r in res: for r in res:
if (r.item_code, r.warehouse) not in out: if (r.item_code, r.warehouse) not in out:
out[(r.item_code, r.warehouse)] = [] out[(r.item_code, r.warehouse)] = []
out[(r.item_code, r.warehouse)].append(r) out[(r.item_code, r.warehouse)].append(r)
return out return out
def get_item_sales_bom(): def get_item_sales_bom():
item_sales_bom = {} item_sales_bom = {}
for d in frappe.db.sql("""select parenttype, parent, parent_item, for d in frappe.db.sql("""select parenttype, parent, parent_item,
item_code, warehouse, -1*qty as total_qty, parent_detail_docname item_code, warehouse, -1*qty as total_qty, parent_detail_docname
from `tabPacked Item` where docstatus=1""", as_dict=True): from `tabPacked Item` where docstatus=1""", as_dict=True):
@ -81,7 +81,7 @@ def get_item_sales_bom():
frappe._dict()).setdefault(d.parent_item, []).append(d) frappe._dict()).setdefault(d.parent_item, []).append(d)
return item_sales_bom return item_sales_bom
def get_source_data(filters): def get_source_data(filters):
conditions = "" conditions = ""
if filters.get("company"): if filters.get("company"):
@ -90,9 +90,9 @@ def get_source_data(filters):
conditions += " and posting_date>=%(from_date)s" conditions += " and posting_date>=%(from_date)s"
if filters.get("to_date"): if filters.get("to_date"):
conditions += " and posting_date<=%(to_date)s" conditions += " and posting_date<=%(to_date)s"
delivery_note_items = frappe.db.sql("""select item.parenttype, dn.name, delivery_note_items = frappe.db.sql("""select item.parenttype, dn.name,
dn.posting_date, dn.posting_time, dn.project_name, dn.posting_date, dn.posting_time, dn.project_name,
item.item_code, item.item_name, item.description, item.warehouse, item.item_code, item.item_name, item.description, item.warehouse,
item.qty, item.base_rate, item.base_amount, item.name as "item_row", item.qty, item.base_rate, item.base_amount, item.name as "item_row",
timestamp(dn.posting_date, dn.posting_time) as posting_datetime timestamp(dn.posting_date, dn.posting_time) as posting_datetime
@ -100,7 +100,7 @@ def get_source_data(filters):
where item.parent = dn.name and dn.docstatus = 1 %s where item.parent = dn.name and dn.docstatus = 1 %s
order by dn.posting_date desc, dn.posting_time desc""" % (conditions,), filters, as_dict=1) order by dn.posting_date desc, dn.posting_time desc""" % (conditions,), filters, as_dict=1)
sales_invoice_items = frappe.db.sql("""select item.parenttype, si.name, sales_invoice_items = frappe.db.sql("""select item.parenttype, si.name,
si.posting_date, si.posting_time, si.project_name, si.posting_date, si.posting_time, si.project_name,
item.item_code, item.item_name, item.description, item.warehouse, item.item_code, item.item_name, item.description, item.warehouse,
item.qty, item.base_rate, item.base_amount, item.name as "item_row", item.qty, item.base_rate, item.base_amount, item.name as "item_row",
@ -109,9 +109,9 @@ def get_source_data(filters):
where item.parent = si.name and si.docstatus = 1 %s where item.parent = si.name and si.docstatus = 1 %s
and si.update_stock = 1 and si.update_stock = 1
order by si.posting_date desc, si.posting_time desc""" % (conditions,), filters, as_dict=1) order by si.posting_date desc, si.posting_time desc""" % (conditions,), filters, as_dict=1)
source = delivery_note_items + sales_invoice_items source = delivery_note_items + sales_invoice_items
if len(source) > len(delivery_note_items): if len(source) > len(delivery_note_items):
source.sort(key=lambda d: d.posting_datetime, reverse=True) source.sort(key=lambda d: d.posting_datetime, reverse=True)
return source return source