2013-11-20 07:29:58 +00:00
|
|
|
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
|
2013-08-05 09:29:54 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
2013-02-25 12:47:51 +00:00
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import flt
|
2013-12-12 13:42:19 +00:00
|
|
|
from erpnext.stock.utils import get_buying_amount, get_sales_bom_buying_amount
|
2013-02-25 12:47:51 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
|
|
|
if not filters: filters = {}
|
|
|
|
|
2013-03-06 13:20:53 +00:00
|
|
|
stock_ledger_entries = get_stock_ledger_entries(filters)
|
2013-03-21 14:54:10 +00:00
|
|
|
source = get_source_data(filters)
|
2013-03-22 07:46:10 +00:00
|
|
|
item_sales_bom = get_item_sales_bom()
|
|
|
|
|
2013-03-26 13:13:10 +00:00
|
|
|
columns = ["Delivery Note/Sales Invoice::120", "Link::30", "Posting Date:Date", "Posting Time",
|
2013-02-25 12:47:51 +00:00
|
|
|
"Item Code:Link/Item", "Item Name", "Description", "Warehouse:Link/Warehouse",
|
2013-03-29 06:56:47 +00:00
|
|
|
"Qty:Float", "Selling Rate:Currency", "Avg. Buying Rate:Currency",
|
|
|
|
"Selling Amount:Currency", "Buying Amount:Currency",
|
2013-02-25 12:47:51 +00:00
|
|
|
"Gross Profit:Currency", "Gross Profit %:Percent", "Project:Link/Project"]
|
|
|
|
data = []
|
2013-03-21 14:54:10 +00:00
|
|
|
for row in source:
|
2014-02-10 13:50:15 +00:00
|
|
|
selling_amount = flt(row.base_amount)
|
2013-08-30 17:18:19 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
item_sales_bom_map = item_sales_bom.get(row.parenttype, {}).get(row.name, frappe._dict())
|
2013-08-30 17:18:19 +00:00
|
|
|
|
|
|
|
if item_sales_bom_map.get(row.item_code):
|
|
|
|
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)
|
|
|
|
else:
|
|
|
|
buying_amount = get_buying_amount(row.parenttype, row.name, row.item_row,
|
|
|
|
stock_ledger_entries.get((row.item_code, row.warehouse), []))
|
2013-03-21 13:15:02 +00:00
|
|
|
|
2013-03-19 06:31:46 +00:00
|
|
|
buying_amount = buying_amount > 0 and buying_amount or 0
|
2013-03-28 11:10:30 +00:00
|
|
|
|
|
|
|
gross_profit = selling_amount - buying_amount
|
2013-02-25 12:47:51 +00:00
|
|
|
if selling_amount:
|
|
|
|
gross_profit_percent = (gross_profit / selling_amount) * 100.0
|
|
|
|
else:
|
2013-03-28 11:10:30 +00:00
|
|
|
gross_profit_percent = 0.0
|
2013-02-25 12:47:51 +00:00
|
|
|
|
2013-03-26 13:13:10 +00:00
|
|
|
icon = """<a href="%s"><i class="icon icon-share" style="cursor: pointer;"></i></a>""" \
|
|
|
|
% ("/".join(["#Form", row.parenttype, row.name]),)
|
|
|
|
data.append([row.name, icon, row.posting_date, row.posting_time, row.item_code, row.item_name,
|
2014-02-10 12:56:49 +00:00
|
|
|
row.description, row.warehouse, row.qty, row.base_rate,
|
2014-02-10 13:50:15 +00:00
|
|
|
row.qty and (buying_amount / row.qty) or 0, row.base_amount, buying_amount,
|
2013-02-25 12:47:51 +00:00
|
|
|
gross_profit, gross_profit_percent, row.project])
|
|
|
|
|
|
|
|
return columns, data
|
|
|
|
|
2013-03-06 13:20:53 +00:00
|
|
|
def get_stock_ledger_entries(filters):
|
2013-02-25 12:47:51 +00:00
|
|
|
query = """select item_code, voucher_type, voucher_no,
|
2013-02-25 13:27:41 +00:00
|
|
|
voucher_detail_no, posting_date, posting_time, stock_value,
|
|
|
|
warehouse, actual_qty as qty
|
2013-09-25 05:02:51 +00:00
|
|
|
from `tabStock Ledger Entry`"""
|
2013-02-25 12:47:51 +00:00
|
|
|
|
|
|
|
if filters.get("company"):
|
2013-09-27 11:03:22 +00:00
|
|
|
query += """ where company=%(company)s"""
|
2013-02-25 12:47:51 +00:00
|
|
|
|
|
|
|
query += " order by item_code desc, warehouse desc, posting_date desc, posting_time desc, name desc"
|
2013-08-07 13:57:30 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
res = frappe.db.sql(query, filters, as_dict=True)
|
2013-08-07 13:57:30 +00:00
|
|
|
|
|
|
|
out = {}
|
|
|
|
for r in res:
|
|
|
|
if (r.item_code, r.warehouse) not in out:
|
|
|
|
out[(r.item_code, r.warehouse)] = []
|
|
|
|
|
|
|
|
out[(r.item_code, r.warehouse)].append(r)
|
2013-02-25 12:47:51 +00:00
|
|
|
|
2013-08-07 13:57:30 +00:00
|
|
|
return out
|
2013-03-21 14:54:10 +00:00
|
|
|
|
|
|
|
def get_item_sales_bom():
|
|
|
|
item_sales_bom = {}
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
for d in frappe.db.sql("""select parenttype, parent, parent_item,
|
2013-03-26 13:13:10 +00:00
|
|
|
item_code, warehouse, -1*qty as total_qty, parent_detail_docname
|
2013-10-18 11:30:53 +00:00
|
|
|
from `tabPacked Item` where docstatus=1""", as_dict=True):
|
2014-02-14 10:17:51 +00:00
|
|
|
item_sales_bom.setdefault(d.parenttype, frappe._dict()).setdefault(d.parent,
|
|
|
|
frappe._dict()).setdefault(d.parent_item, []).append(d)
|
2013-03-21 14:54:10 +00:00
|
|
|
|
|
|
|
return item_sales_bom
|
|
|
|
|
|
|
|
def get_source_data(filters):
|
|
|
|
conditions = ""
|
|
|
|
if filters.get("company"):
|
|
|
|
conditions += " and company=%(company)s"
|
2013-03-22 07:46:10 +00:00
|
|
|
if filters.get("from_date"):
|
|
|
|
conditions += " and posting_date>=%(from_date)s"
|
|
|
|
if filters.get("to_date"):
|
|
|
|
conditions += " and posting_date<=%(to_date)s"
|
2013-03-21 14:54:10 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
delivery_note_items = frappe.db.sql("""select item.parenttype, dn.name,
|
2013-03-21 14:54:10 +00:00
|
|
|
dn.posting_date, dn.posting_time, dn.project_name,
|
|
|
|
item.item_code, item.item_name, item.description, item.warehouse,
|
2014-02-10 13:50:15 +00:00
|
|
|
item.qty, item.base_rate, item.base_amount, item.name as "item_row",
|
2013-03-21 14:54:10 +00:00
|
|
|
timestamp(dn.posting_date, dn.posting_time) as posting_datetime
|
|
|
|
from `tabDelivery Note` dn, `tabDelivery Note Item` item
|
|
|
|
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)
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
sales_invoice_items = frappe.db.sql("""select item.parenttype, si.name,
|
2013-03-21 14:54:10 +00:00
|
|
|
si.posting_date, si.posting_time, si.project_name,
|
|
|
|
item.item_code, item.item_name, item.description, item.warehouse,
|
2014-02-10 13:50:15 +00:00
|
|
|
item.qty, item.base_rate, item.base_amount, item.name as "item_row",
|
2013-03-21 14:54:10 +00:00
|
|
|
timestamp(si.posting_date, si.posting_time) as posting_datetime
|
|
|
|
from `tabSales Invoice` si, `tabSales Invoice Item` item
|
|
|
|
where item.parent = si.name and si.docstatus = 1 %s
|
2013-05-30 12:13:30 +00:00
|
|
|
and si.update_stock = 1
|
2013-03-21 14:54:10 +00:00
|
|
|
order by si.posting_date desc, si.posting_time desc""" % (conditions,), filters, as_dict=1)
|
|
|
|
|
|
|
|
source = delivery_note_items + sales_invoice_items
|
|
|
|
if len(source) > len(delivery_note_items):
|
|
|
|
source.sort(key=lambda d: d.posting_datetime, reverse=True)
|
|
|
|
|
|
|
|
return source
|