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-06-06 13:03:26 +00:00
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
|
|
|
from frappe.utils import flt
|
2013-06-06 13:03:26 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
|
|
|
if not filters: filters = {}
|
|
|
|
|
|
|
|
columns = get_columns(filters)
|
|
|
|
item_map = get_item_details()
|
|
|
|
pl = get_price_list()
|
2013-06-21 07:17:51 +00:00
|
|
|
last_purchase_rate = get_last_purchase_rate()
|
2013-06-06 13:03:26 +00:00
|
|
|
bom_rate = get_item_bom_rate()
|
|
|
|
val_rate_map = get_valuation_rate()
|
2013-06-21 07:17:51 +00:00
|
|
|
|
2014-01-24 16:14:36 +00:00
|
|
|
from erpnext.accounts.utils import get_currency_precision
|
2014-01-17 13:27:21 +00:00
|
|
|
precision = get_currency_precision() or 2
|
2013-06-06 13:03:26 +00:00
|
|
|
data = []
|
|
|
|
for item in sorted(item_map):
|
|
|
|
data.append([item, item_map[item]["item_name"],
|
|
|
|
item_map[item]["description"], item_map[item]["stock_uom"],
|
2013-06-21 07:17:51 +00:00
|
|
|
flt(last_purchase_rate.get(item, 0), precision),
|
|
|
|
flt(val_rate_map.get(item, 0), precision),
|
2013-09-11 07:35:24 +00:00
|
|
|
pl.get(item, {}).get("Selling"),
|
|
|
|
pl.get(item, {}).get("Buying"),
|
2013-06-21 07:17:51 +00:00
|
|
|
flt(bom_rate.get(item, 0), precision),
|
2013-06-21 07:39:18 +00:00
|
|
|
flt(item_map[item]["standard_rate"], precision)
|
2013-06-06 13:03:26 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
return columns, data
|
|
|
|
|
|
|
|
def get_columns(filters):
|
|
|
|
"""return columns based on filters"""
|
|
|
|
|
|
|
|
columns = ["Item:Link/Item:100", "Item Name::150", "Description::150", "UOM:Link/UOM:80",
|
|
|
|
"Last Purchase Rate:Currency:90", "Valuation Rate:Currency:80", "Sales Price List::80",
|
|
|
|
"Purchase Price List::80", "BOM Rate:Currency:90", "Standard Rate:Currency:100"]
|
|
|
|
|
|
|
|
return columns
|
|
|
|
|
|
|
|
def get_item_details():
|
|
|
|
"""returns all items details"""
|
|
|
|
|
|
|
|
item_map = {}
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
for i in frappe.db.sql("select name, item_name, description, \
|
2013-06-21 07:17:51 +00:00
|
|
|
stock_uom, standard_rate from tabItem \
|
2013-06-06 13:03:26 +00:00
|
|
|
order by item_code", as_dict=1):
|
|
|
|
item_map.setdefault(i.name, i)
|
|
|
|
|
|
|
|
return item_map
|
|
|
|
|
|
|
|
def get_price_list():
|
|
|
|
"""Get selling & buying price list of every item"""
|
|
|
|
|
|
|
|
rate = {}
|
2013-09-11 07:35:24 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
price_list = frappe.db.sql("""select ip.item_code, ip.buying, ip.selling,
|
2014-02-10 12:24:04 +00:00
|
|
|
concat(ip.price_list, " - ", ip.currency, " ", ip.price_list_rate) as price
|
2014-01-20 10:39:45 +00:00
|
|
|
from `tabItem Price` ip, `tabPrice List` pl
|
|
|
|
where ip.price_list=pl.name and pl.enabled=1""", as_dict=1)
|
2013-06-06 13:03:26 +00:00
|
|
|
|
|
|
|
for j in price_list:
|
2013-06-19 08:43:01 +00:00
|
|
|
if j.price:
|
2014-01-07 13:07:38 +00:00
|
|
|
rate.setdefault(j.item_code, {}).setdefault("Buying" if j.buying else "Selling", []).append(j.price)
|
2013-06-06 13:03:26 +00:00
|
|
|
item_rate_map = {}
|
|
|
|
|
|
|
|
for item in rate:
|
2013-09-11 07:35:24 +00:00
|
|
|
for buying_or_selling in rate[item]:
|
|
|
|
item_rate_map.setdefault(item, {}).setdefault(buying_or_selling,
|
|
|
|
", ".join(rate[item].get(buying_or_selling, [])))
|
2013-06-06 13:03:26 +00:00
|
|
|
|
|
|
|
return item_rate_map
|
|
|
|
|
2013-06-21 07:17:51 +00:00
|
|
|
def get_last_purchase_rate():
|
|
|
|
|
|
|
|
item_last_purchase_rate_map = {}
|
|
|
|
|
|
|
|
query = """select * from (select
|
|
|
|
result.item_code,
|
2014-02-10 12:56:49 +00:00
|
|
|
result.base_rate
|
2013-06-21 07:17:51 +00:00
|
|
|
from (
|
|
|
|
(select
|
|
|
|
po_item.item_code,
|
|
|
|
po_item.item_name,
|
|
|
|
po.transaction_date as posting_date,
|
2014-02-10 12:24:04 +00:00
|
|
|
po_item.base_price_list_rate,
|
|
|
|
po_item.discount_percentage,
|
2014-02-10 12:56:49 +00:00
|
|
|
po_item.base_rate
|
2013-06-21 07:17:51 +00:00
|
|
|
from `tabPurchase Order` po, `tabPurchase Order Item` po_item
|
|
|
|
where po.name = po_item.parent and po.docstatus = 1)
|
|
|
|
union
|
|
|
|
(select
|
|
|
|
pr_item.item_code,
|
|
|
|
pr_item.item_name,
|
|
|
|
pr.posting_date,
|
2014-02-10 12:24:04 +00:00
|
|
|
pr_item.base_price_list_rate,
|
|
|
|
pr_item.discount_percentage,
|
2014-02-10 12:56:49 +00:00
|
|
|
pr_item.base_rate
|
2013-06-21 07:17:51 +00:00
|
|
|
from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
|
|
|
|
where pr.name = pr_item.parent and pr.docstatus = 1)
|
|
|
|
) result
|
|
|
|
order by result.item_code asc, result.posting_date desc) result_wrapper
|
|
|
|
group by item_code"""
|
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
for d in frappe.db.sql(query, as_dict=1):
|
2014-02-10 12:56:49 +00:00
|
|
|
item_last_purchase_rate_map.setdefault(d.item_code, d.base_rate)
|
2013-06-21 07:17:51 +00:00
|
|
|
|
|
|
|
return item_last_purchase_rate_map
|
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
def get_item_bom_rate():
|
|
|
|
"""Get BOM rate of an item from BOM"""
|
|
|
|
|
2013-06-21 07:17:51 +00:00
|
|
|
item_bom_map = {}
|
2013-06-06 13:03:26 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
for b in frappe.db.sql("""select item, (total_cost/quantity) as bom_rate
|
2013-06-06 13:03:26 +00:00
|
|
|
from `tabBOM` where is_active=1 and is_default=1""", as_dict=1):
|
2013-06-21 07:17:51 +00:00
|
|
|
item_bom_map.setdefault(b.item, flt(b.bom_rate))
|
2013-06-06 13:03:26 +00:00
|
|
|
|
2013-06-21 07:17:51 +00:00
|
|
|
return item_bom_map
|
2013-06-06 13:03:26 +00:00
|
|
|
|
|
|
|
def get_valuation_rate():
|
|
|
|
"""Get an average valuation rate of an item from all warehouses"""
|
|
|
|
|
2013-06-21 07:17:51 +00:00
|
|
|
item_val_rate_map = {}
|
2013-06-06 13:03:26 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
for d in frappe.db.sql("""select item_code,
|
2013-06-20 12:02:00 +00:00
|
|
|
sum(actual_qty*valuation_rate)/sum(actual_qty) as val_rate
|
2013-06-20 11:49:51 +00:00
|
|
|
from tabBin where actual_qty > 0 group by item_code""", as_dict=1):
|
2013-06-21 07:39:18 +00:00
|
|
|
item_val_rate_map.setdefault(d.item_code, d.val_rate)
|
2013-06-06 13:03:26 +00:00
|
|
|
|
2013-06-21 07:39:18 +00:00
|
|
|
return item_val_rate_map
|