2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe 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
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-09-11 08:15:27 +00:00
|
|
|
from frappe import _
|
2014-02-14 10:17:51 +00:00
|
|
|
from frappe.utils import flt
|
2013-06-06 13:03:26 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
def execute(filters=None):
|
|
|
|
if not filters:
|
|
|
|
filters = {}
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
columns = get_columns(filters)
|
2018-09-26 10:28:06 +00:00
|
|
|
conditions = get_condition(filters)
|
|
|
|
item_map = get_item_details(conditions)
|
2013-06-06 13:03:26 +00:00
|
|
|
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
|
2022-03-28 13:22:46 +00:00
|
|
|
|
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):
|
2016-03-10 00:49:38 +00:00
|
|
|
data.append(
|
2022-03-28 13:22:46 +00:00
|
|
|
[
|
2016-03-10 00:49:38 +00:00
|
|
|
item,
|
|
|
|
item_map[item]["item_name"],
|
|
|
|
item_map[item]["item_group"],
|
2018-05-03 10:53:57 +00:00
|
|
|
item_map[item]["brand"],
|
|
|
|
item_map[item]["description"],
|
|
|
|
item_map[item]["stock_uom"],
|
2014-05-29 10:48:49 +00:00
|
|
|
flt(last_purchase_rate.get(item, 0), precision),
|
|
|
|
flt(val_rate_map.get(item, 0), precision),
|
|
|
|
pl.get(item, {}).get("Selling"),
|
|
|
|
pl.get(item, {}).get("Buying"),
|
|
|
|
flt(bom_rate.get(item, 0), precision),
|
2013-06-06 13:03:26 +00:00
|
|
|
]
|
|
|
|
)
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
return columns, data
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
def get_columns(filters):
|
|
|
|
"""return columns based on filters"""
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2018-05-03 10:53:57 +00:00
|
|
|
columns = [
|
|
|
|
_("Item") + ":Link/Item:100",
|
|
|
|
_("Item Name") + "::150",
|
|
|
|
_("Item Group") + ":Link/Item Group:125",
|
|
|
|
_("Brand") + "::100",
|
|
|
|
_("Description") + "::150",
|
|
|
|
_("UOM") + ":Link/UOM:80",
|
2016-03-10 00:49:38 +00:00
|
|
|
_("Last Purchase Rate") + ":Currency:90",
|
|
|
|
_("Valuation Rate") + ":Currency:80",
|
|
|
|
_("Sales Price List") + "::180",
|
|
|
|
_("Purchase Price List") + "::180",
|
|
|
|
_("BOM Rate") + ":Currency:90",
|
|
|
|
]
|
2013-06-06 13:03:26 +00:00
|
|
|
|
|
|
|
return columns
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-09-26 10:28:06 +00:00
|
|
|
def get_item_details(conditions):
|
2013-06-06 13:03:26 +00:00
|
|
|
"""returns all items details"""
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
item_map = {}
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2018-05-03 10:53:57 +00:00
|
|
|
for i in frappe.db.sql(
|
|
|
|
"""select name, item_group, item_name, description,
|
2018-09-26 10:28:06 +00:00
|
|
|
brand, stock_uom from tabItem %s
|
|
|
|
order by item_code, item_group"""
|
|
|
|
% (conditions),
|
|
|
|
as_dict=1,
|
|
|
|
):
|
2013-06-06 13:03:26 +00:00
|
|
|
item_map.setdefault(i.name, i)
|
|
|
|
|
|
|
|
return item_map
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
def get_price_list():
|
|
|
|
"""Get selling & buying price list of every item"""
|
|
|
|
|
|
|
|
rate = {}
|
2013-09-11 07:35:24 +00:00
|
|
|
|
2014-05-29 10:48:49 +00:00
|
|
|
price_list = frappe.db.sql(
|
|
|
|
"""select ip.item_code, ip.buying, ip.selling,
|
2016-03-16 08:37:28 +00:00
|
|
|
concat(ifnull(cu.symbol,ip.currency), " ", round(ip.price_list_rate,2), " - ", ip.price_list) as price
|
2016-03-10 00:49:38 +00:00
|
|
|
from `tabItem Price` ip, `tabPrice List` pl, `tabCurrency` cu
|
|
|
|
where ip.price_list=pl.name and pl.currency=cu.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 = {}
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
for item in rate:
|
2013-09-11 07:35:24 +00:00
|
|
|
for buying_or_selling in rate[item]:
|
2014-05-29 10:48:49 +00:00
|
|
|
item_rate_map.setdefault(item, {}).setdefault(
|
2013-09-11 07:35:24 +00:00
|
|
|
buying_or_selling, ", ".join(rate[item].get(buying_or_selling, []))
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2013-06-06 13:03:26 +00:00
|
|
|
return item_rate_map
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-06-21 07:17:51 +00:00
|
|
|
def get_last_purchase_rate():
|
|
|
|
item_last_purchase_rate_map = {}
|
|
|
|
|
2020-10-12 11:39:09 +00:00
|
|
|
query = """select * from (
|
|
|
|
(select
|
|
|
|
po_item.item_code,
|
|
|
|
po.transaction_date as posting_date,
|
|
|
|
po_item.base_rate
|
|
|
|
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.posting_date,
|
|
|
|
pr_item.base_rate
|
|
|
|
from `tabPurchase Receipt` pr, `tabPurchase Receipt Item` pr_item
|
|
|
|
where pr.name = pr_item.parent and pr.docstatus = 1)
|
|
|
|
union
|
|
|
|
(select
|
|
|
|
pi_item.item_code,
|
|
|
|
pi.posting_date,
|
|
|
|
pi_item.base_rate
|
|
|
|
from `tabPurchase Invoice` pi, `tabPurchase Invoice Item` pi_item
|
|
|
|
where pi.name = pi_item.parent and pi.docstatus = 1 and pi.update_stock = 1)
|
|
|
|
) result order by result.item_code asc, result.posting_date asc"""
|
2013-06-21 07:17:51 +00:00
|
|
|
|
2014-02-26 07:05:33 +00:00
|
|
|
for d in frappe.db.sql(query, as_dict=1):
|
2020-10-12 11:39:09 +00:00
|
|
|
item_last_purchase_rate_map[d.item_code] = d.base_rate
|
2013-06-21 07:17:51 +00:00
|
|
|
|
|
|
|
return item_last_purchase_rate_map
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
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 = {}
|
2014-05-29 10:48:49 +00:00
|
|
|
|
2014-12-23 12:08: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
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
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 = {}
|
2014-05-29 10:48:49 +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
|
2018-09-26 10:28:06 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-09-26 10:28:06 +00:00
|
|
|
def get_condition(filters):
|
|
|
|
"""Get Filter Items"""
|
|
|
|
|
|
|
|
if filters.get("items") == "Enabled Items only":
|
|
|
|
conditions = " where disabled=0 "
|
|
|
|
elif filters.get("items") == "Disabled Items only":
|
|
|
|
conditions = " where disabled=1 "
|
|
|
|
else:
|
|
|
|
conditions = ""
|
|
|
|
|
|
|
|
return conditions
|