2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-11-29 13:45:26 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2019-11-27 11:28:06 +00:00
|
|
|
from frappe import _
|
2020-07-10 12:41:04 +00:00
|
|
|
from frappe.utils import cint, flt
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-10-06 07:15:29 +00:00
|
|
|
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
2020-12-21 09:15:50 +00:00
|
|
|
from erpnext.stock.utils import (
|
|
|
|
is_reposting_item_valuation_in_progress,
|
|
|
|
update_included_uom_in_report,
|
2021-09-02 11:14:59 +00:00
|
|
|
)
|
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2013-11-29 13:45:26 +00:00
|
|
|
def execute(filters=None):
|
2020-12-21 09:15:50 +00:00
|
|
|
is_reposting_item_valuation_in_progress()
|
2018-10-18 12:29:47 +00:00
|
|
|
include_uom = filters.get("include_uom")
|
2013-12-05 10:38:30 +00:00
|
|
|
columns = get_columns()
|
2018-04-12 07:55:03 +00:00
|
|
|
items = get_items(filters)
|
|
|
|
sl_entries = get_stock_ledger_entries(filters, items)
|
2018-10-18 12:29:47 +00:00
|
|
|
item_details = get_item_details(items, sl_entries, include_uom)
|
2015-07-22 13:14:47 +00:00
|
|
|
opening_row = get_opening_balance(filters, columns)
|
2020-07-10 12:41:04 +00:00
|
|
|
precision = cint(frappe.db.get_single_value("System Settings", "float_precision"))
|
2017-04-20 09:51:01 +00:00
|
|
|
|
2013-12-05 10:38:30 +00:00
|
|
|
data = []
|
2018-10-18 12:29:47 +00:00
|
|
|
conversion_factors = []
|
2015-07-22 13:14:47 +00:00
|
|
|
if opening_row:
|
|
|
|
data.append(opening_row)
|
2021-08-23 08:06:13 +00:00
|
|
|
conversion_factors.append(0)
|
2015-07-22 13:14:47 +00:00
|
|
|
|
2019-11-08 09:20:48 +00:00
|
|
|
actual_qty = stock_value = 0
|
|
|
|
|
2020-10-06 07:15:29 +00:00
|
|
|
available_serial_nos = {}
|
2013-12-05 10:38:30 +00:00
|
|
|
for sle in sl_entries:
|
|
|
|
item_detail = item_details[sle.item_code]
|
2014-05-15 06:20:46 +00:00
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
sle.update(item_detail)
|
2019-11-08 09:20:48 +00:00
|
|
|
|
|
|
|
if filters.get("batch_no"):
|
2020-07-10 12:41:04 +00:00
|
|
|
actual_qty += flt(sle.actual_qty, precision)
|
2019-11-08 09:20:48 +00:00
|
|
|
stock_value += sle.stock_value_difference
|
|
|
|
|
2020-07-16 16:43:00 +00:00
|
|
|
if sle.voucher_type == 'Stock Reconciliation' and not sle.actual_qty:
|
2019-11-08 09:20:48 +00:00
|
|
|
actual_qty = sle.qty_after_transaction
|
|
|
|
stock_value = sle.stock_value
|
|
|
|
|
|
|
|
sle.update({
|
|
|
|
"qty_after_transaction": actual_qty,
|
2020-01-29 04:19:26 +00:00
|
|
|
"stock_value": stock_value
|
2019-11-27 11:28:06 +00:00
|
|
|
})
|
|
|
|
|
2020-01-28 10:34:49 +00:00
|
|
|
sle.update({
|
|
|
|
"in_qty": max(sle.actual_qty, 0),
|
|
|
|
"out_qty": min(sle.actual_qty, 0)
|
|
|
|
})
|
|
|
|
|
2020-10-06 07:15:29 +00:00
|
|
|
if sle.serial_no:
|
|
|
|
update_available_serial_nos(available_serial_nos, sle)
|
|
|
|
|
2019-07-03 05:04:31 +00:00
|
|
|
data.append(sle)
|
2017-04-20 09:51:01 +00:00
|
|
|
|
2018-10-18 12:29:47 +00:00
|
|
|
if include_uom:
|
|
|
|
conversion_factors.append(item_detail.conversion_factor)
|
|
|
|
|
|
|
|
update_included_uom_in_report(columns, data, include_uom, conversion_factors)
|
2013-12-05 10:38:30 +00:00
|
|
|
return columns, data
|
2014-05-15 06:20:46 +00:00
|
|
|
|
2020-10-06 07:15:29 +00:00
|
|
|
def update_available_serial_nos(available_serial_nos, sle):
|
|
|
|
serial_nos = get_serial_nos(sle.serial_no)
|
|
|
|
key = (sle.item_code, sle.warehouse)
|
|
|
|
if key not in available_serial_nos:
|
|
|
|
available_serial_nos.setdefault(key, [])
|
|
|
|
|
|
|
|
existing_serial_no = available_serial_nos[key]
|
|
|
|
for sn in serial_nos:
|
|
|
|
if sle.actual_qty > 0:
|
|
|
|
if sn in existing_serial_no:
|
|
|
|
existing_serial_no.remove(sn)
|
|
|
|
else:
|
|
|
|
existing_serial_no.append(sn)
|
|
|
|
else:
|
|
|
|
if sn in existing_serial_no:
|
|
|
|
existing_serial_no.remove(sn)
|
|
|
|
else:
|
|
|
|
existing_serial_no.append(sn)
|
|
|
|
|
|
|
|
sle.balance_serial_no = '\n'.join(existing_serial_no)
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2013-12-05 10:38:30 +00:00
|
|
|
def get_columns():
|
2017-08-04 10:45:09 +00:00
|
|
|
columns = [
|
2019-11-27 11:28:06 +00:00
|
|
|
{"label": _("Date"), "fieldname": "date", "fieldtype": "Datetime", "width": 150},
|
|
|
|
{"label": _("Item"), "fieldname": "item_code", "fieldtype": "Link", "options": "Item", "width": 100},
|
2018-10-18 12:29:47 +00:00
|
|
|
{"label": _("Item Name"), "fieldname": "item_name", "width": 100},
|
2019-11-27 11:28:06 +00:00
|
|
|
{"label": _("Stock UOM"), "fieldname": "stock_uom", "fieldtype": "Link", "options": "UOM", "width": 90},
|
|
|
|
{"label": _("In Qty"), "fieldname": "in_qty", "fieldtype": "Float", "width": 80, "convertible": "qty"},
|
|
|
|
{"label": _("Out Qty"), "fieldname": "out_qty", "fieldtype": "Float", "width": 80, "convertible": "qty"},
|
|
|
|
{"label": _("Balance Qty"), "fieldname": "qty_after_transaction", "fieldtype": "Float", "width": 100, "convertible": "qty"},
|
|
|
|
{"label": _("Voucher #"), "fieldname": "voucher_no", "fieldtype": "Dynamic Link", "options": "voucher_type", "width": 150},
|
|
|
|
{"label": _("Warehouse"), "fieldname": "warehouse", "fieldtype": "Link", "options": "Warehouse", "width": 150},
|
2018-10-18 12:29:47 +00:00
|
|
|
{"label": _("Item Group"), "fieldname": "item_group", "fieldtype": "Link", "options": "Item Group", "width": 100},
|
|
|
|
{"label": _("Brand"), "fieldname": "brand", "fieldtype": "Link", "options": "Brand", "width": 100},
|
|
|
|
{"label": _("Description"), "fieldname": "description", "width": 200},
|
2019-11-27 11:28:06 +00:00
|
|
|
{"label": _("Incoming Rate"), "fieldname": "incoming_rate", "fieldtype": "Currency", "width": 110, "options": "Company:company:default_currency", "convertible": "rate"},
|
|
|
|
{"label": _("Valuation Rate"), "fieldname": "valuation_rate", "fieldtype": "Currency", "width": 110, "options": "Company:company:default_currency", "convertible": "rate"},
|
|
|
|
{"label": _("Balance Value"), "fieldname": "stock_value", "fieldtype": "Currency", "width": 110, "options": "Company:company:default_currency"},
|
2018-10-18 12:29:47 +00:00
|
|
|
{"label": _("Voucher Type"), "fieldname": "voucher_type", "width": 110},
|
|
|
|
{"label": _("Voucher #"), "fieldname": "voucher_no", "fieldtype": "Dynamic Link", "options": "voucher_type", "width": 100},
|
|
|
|
{"label": _("Batch"), "fieldname": "batch_no", "fieldtype": "Link", "options": "Batch", "width": 100},
|
2020-10-06 07:15:29 +00:00
|
|
|
{"label": _("Serial No"), "fieldname": "serial_no", "fieldtype": "Link", "options": "Serial No", "width": 100},
|
|
|
|
{"label": _("Balance Serial No"), "fieldname": "balance_serial_no", "width": 100},
|
2018-10-18 12:29:47 +00:00
|
|
|
{"label": _("Project"), "fieldname": "project", "fieldtype": "Link", "options": "Project", "width": 100},
|
|
|
|
{"label": _("Company"), "fieldname": "company", "fieldtype": "Link", "options": "Company", "width": 110}
|
2014-09-12 06:41:38 +00:00
|
|
|
]
|
2014-05-15 06:20:46 +00:00
|
|
|
|
2017-08-04 10:45:09 +00:00
|
|
|
return columns
|
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2018-04-12 07:55:03 +00:00
|
|
|
def get_stock_ledger_entries(filters, items):
|
2018-04-02 18:07:33 +00:00
|
|
|
item_conditions_sql = ''
|
2018-04-12 07:55:03 +00:00
|
|
|
if items:
|
|
|
|
item_conditions_sql = 'and sle.item_code in ({})'\
|
2021-06-11 13:10:22 +00:00
|
|
|
.format(', '.join(frappe.db.escape(i) for i in items))
|
2018-04-12 07:55:03 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
sl_entries = frappe.db.sql("""
|
|
|
|
SELECT
|
|
|
|
concat_ws(" ", posting_date, posting_time) AS date,
|
|
|
|
item_code,
|
|
|
|
warehouse,
|
|
|
|
actual_qty,
|
|
|
|
qty_after_transaction,
|
|
|
|
incoming_rate,
|
|
|
|
valuation_rate,
|
|
|
|
stock_value,
|
|
|
|
voucher_type,
|
|
|
|
voucher_no,
|
|
|
|
batch_no,
|
|
|
|
serial_no,
|
|
|
|
company,
|
|
|
|
project,
|
|
|
|
stock_value_difference
|
|
|
|
FROM
|
|
|
|
`tabStock Ledger Entry` sle
|
|
|
|
WHERE
|
|
|
|
company = %(company)s
|
2021-03-10 08:16:42 +00:00
|
|
|
AND is_cancelled = 0 AND posting_date BETWEEN %(from_date)s AND %(to_date)s
|
2019-11-27 11:28:06 +00:00
|
|
|
{sle_conditions}
|
|
|
|
{item_conditions_sql}
|
|
|
|
ORDER BY
|
|
|
|
posting_date asc, posting_time asc, creation asc
|
|
|
|
""".format(sle_conditions=get_sle_conditions(filters), item_conditions_sql=item_conditions_sql),
|
|
|
|
filters, as_dict=1)
|
|
|
|
|
|
|
|
return sl_entries
|
|
|
|
|
2013-11-29 13:45:26 +00:00
|
|
|
|
2018-04-12 07:55:03 +00:00
|
|
|
def get_items(filters):
|
2013-11-29 13:45:26 +00:00
|
|
|
conditions = []
|
|
|
|
if filters.get("item_code"):
|
2017-08-16 06:05:04 +00:00
|
|
|
conditions.append("item.name=%(item_code)s")
|
2018-04-12 07:55:03 +00:00
|
|
|
else:
|
|
|
|
if filters.get("brand"):
|
|
|
|
conditions.append("item.brand=%(brand)s")
|
|
|
|
if filters.get("item_group"):
|
|
|
|
conditions.append(get_item_group_condition(filters.get("item_group")))
|
|
|
|
|
|
|
|
items = []
|
|
|
|
if conditions:
|
|
|
|
items = frappe.db.sql_list("""select name from `tabItem` item where {}"""
|
|
|
|
.format(" and ".join(conditions)), filters)
|
|
|
|
return items
|
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2018-10-18 12:29:47 +00:00
|
|
|
def get_item_details(items, sl_entries, include_uom):
|
2018-04-12 07:55:03 +00:00
|
|
|
item_details = {}
|
|
|
|
if not items:
|
2021-06-11 13:10:22 +00:00
|
|
|
items = list(set(d.item_code for d in sl_entries))
|
2014-05-15 06:20:46 +00:00
|
|
|
|
2018-04-17 15:15:06 +00:00
|
|
|
if not items:
|
|
|
|
return item_details
|
|
|
|
|
2018-10-18 12:29:47 +00:00
|
|
|
cf_field = cf_join = ""
|
|
|
|
if include_uom:
|
|
|
|
cf_field = ", ucd.conversion_factor"
|
2019-11-29 07:56:52 +00:00
|
|
|
cf_join = "left join `tabUOM Conversion Detail` ucd on ucd.parent=item.name and ucd.uom=%s" \
|
|
|
|
% frappe.db.escape(include_uom)
|
2019-01-25 11:37:29 +00:00
|
|
|
|
|
|
|
res = frappe.db.sql("""
|
|
|
|
select
|
|
|
|
item.name, item.item_name, item.description, item.item_group, item.brand, item.stock_uom {cf_field}
|
|
|
|
from
|
|
|
|
`tabItem` item
|
|
|
|
{cf_join}
|
|
|
|
where
|
|
|
|
item.name in ({item_codes})
|
2019-03-30 10:22:48 +00:00
|
|
|
""".format(cf_field=cf_field, cf_join=cf_join, item_codes=','.join(['%s'] *len(items))), items, as_dict=1)
|
2019-01-25 11:37:29 +00:00
|
|
|
|
|
|
|
for item in res:
|
|
|
|
item_details.setdefault(item.name, item)
|
2018-04-12 07:55:03 +00:00
|
|
|
|
|
|
|
return item_details
|
2014-05-15 06:20:46 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2013-11-29 13:45:26 +00:00
|
|
|
def get_sle_conditions(filters):
|
|
|
|
conditions = []
|
|
|
|
if filters.get("warehouse"):
|
2017-08-21 02:05:16 +00:00
|
|
|
warehouse_condition = get_warehouse_condition(filters.get("warehouse"))
|
|
|
|
if warehouse_condition:
|
|
|
|
conditions.append(warehouse_condition)
|
2013-11-29 13:45:26 +00:00
|
|
|
if filters.get("voucher_no"):
|
|
|
|
conditions.append("voucher_no=%(voucher_no)s")
|
2017-04-20 09:51:01 +00:00
|
|
|
if filters.get("batch_no"):
|
|
|
|
conditions.append("batch_no=%(batch_no)s")
|
2018-03-21 12:22:41 +00:00
|
|
|
if filters.get("project"):
|
|
|
|
conditions.append("project=%(project)s")
|
2014-05-15 06:20:46 +00:00
|
|
|
|
|
|
|
return "and {}".format(" and ".join(conditions)) if conditions else ""
|
2015-07-22 13:14:47 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2015-07-22 13:14:47 +00:00
|
|
|
def get_opening_balance(filters, columns):
|
|
|
|
if not (filters.item_code and filters.warehouse and filters.from_date):
|
|
|
|
return
|
|
|
|
|
|
|
|
from erpnext.stock.stock_ledger import get_previous_sle
|
|
|
|
last_entry = get_previous_sle({
|
|
|
|
"item_code": filters.item_code,
|
2018-02-01 09:28:50 +00:00
|
|
|
"warehouse_condition": get_warehouse_condition(filters.warehouse),
|
2015-07-22 13:14:47 +00:00
|
|
|
"posting_date": filters.from_date,
|
|
|
|
"posting_time": "00:00:00"
|
|
|
|
})
|
2019-11-27 11:28:06 +00:00
|
|
|
|
|
|
|
row = {
|
|
|
|
"item_code": _("'Opening'"),
|
|
|
|
"qty_after_transaction": last_entry.get("qty_after_transaction", 0),
|
|
|
|
"valuation_rate": last_entry.get("valuation_rate", 0),
|
|
|
|
"stock_value": last_entry.get("stock_value", 0)
|
|
|
|
}
|
2017-04-20 09:51:01 +00:00
|
|
|
|
2016-06-21 06:26:08 +00:00
|
|
|
return row
|
2017-04-20 09:51:01 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2016-06-21 06:26:08 +00:00
|
|
|
def get_warehouse_condition(warehouse):
|
2016-07-27 06:12:38 +00:00
|
|
|
warehouse_details = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt"], as_dict=1)
|
|
|
|
if warehouse_details:
|
|
|
|
return " exists (select name from `tabWarehouse` wh \
|
2018-02-01 09:28:50 +00:00
|
|
|
where wh.lft >= %s and wh.rgt <= %s and warehouse = wh.name)"%(warehouse_details.lft,
|
2016-07-27 06:12:38 +00:00
|
|
|
warehouse_details.rgt)
|
|
|
|
|
|
|
|
return ''
|
2017-08-16 06:05:04 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2017-08-16 06:05:04 +00:00
|
|
|
def get_item_group_condition(item_group):
|
|
|
|
item_group_details = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"], as_dict=1)
|
|
|
|
if item_group_details:
|
|
|
|
return "item.item_group in (select ig.name from `tabItem Group` ig \
|
|
|
|
where ig.lft >= %s and ig.rgt <= %s and item.item_group = ig.name)"%(item_group_details.lft,
|
|
|
|
item_group_details.rgt)
|
|
|
|
|
|
|
|
return ''
|