2015-03-03 09:25:30 +00:00
|
|
|
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
|
2013-12-26 05:37:58 +00:00
|
|
|
# License: GNU General Public License v3. See license.txt
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
2014-02-14 10:17:51 +00:00
|
|
|
import frappe
|
2014-09-11 08:15:27 +00:00
|
|
|
from frappe import _
|
2020-07-08 13:53:13 +00:00
|
|
|
from frappe.utils import date_diff, flt, cint
|
2018-05-23 06:01:24 +00:00
|
|
|
from six import iteritems
|
2019-07-22 10:00:18 +00:00
|
|
|
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
2013-12-26 05:37:58 +00:00
|
|
|
|
|
|
|
def execute(filters=None):
|
2019-03-19 20:36:39 +00:00
|
|
|
columns = get_columns(filters)
|
2013-12-26 05:37:58 +00:00
|
|
|
item_details = get_fifo_queue(filters)
|
|
|
|
to_date = filters["to_date"]
|
2019-10-03 13:49:42 +00:00
|
|
|
_func = lambda x: x[1]
|
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
data = []
|
2018-05-23 06:01:24 +00:00
|
|
|
for item, item_dict in iteritems(item_details):
|
2020-10-19 05:09:20 +00:00
|
|
|
earliest_age, latest_age = 0, 0
|
2019-09-27 12:20:52 +00:00
|
|
|
|
2019-10-03 13:49:42 +00:00
|
|
|
fifo_queue = sorted(filter(_func, item_dict["fifo_queue"]), key=_func)
|
2013-12-26 05:37:58 +00:00
|
|
|
details = item_dict["details"]
|
2020-11-17 06:40:27 +00:00
|
|
|
|
|
|
|
if not fifo_queue: continue
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
average_age = get_average_age(fifo_queue, to_date)
|
|
|
|
earliest_age = date_diff(to_date, fifo_queue[0][1])
|
|
|
|
latest_age = date_diff(to_date, fifo_queue[-1][1])
|
2020-07-08 13:53:13 +00:00
|
|
|
range1, range2, range3, above_range3 = get_range_age(filters, fifo_queue, to_date)
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2019-03-19 20:36:39 +00:00
|
|
|
row = [details.name, details.item_name,
|
|
|
|
details.description, details.item_group, details.brand]
|
|
|
|
|
2019-07-13 16:28:32 +00:00
|
|
|
if filters.get("show_warehouse_wise_stock"):
|
2019-03-19 20:36:39 +00:00
|
|
|
row.append(details.warehouse)
|
|
|
|
|
|
|
|
row.extend([item_dict.get("total_qty"), average_age,
|
2020-07-08 13:53:13 +00:00
|
|
|
range1, range2, range3, above_range3,
|
2019-03-19 20:36:39 +00:00
|
|
|
earliest_age, latest_age, details.stock_uom])
|
|
|
|
|
|
|
|
data.append(row)
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2020-05-14 07:50:43 +00:00
|
|
|
chart_data = get_chart_data(data, filters)
|
|
|
|
|
|
|
|
return columns, data, None, chart_data
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_average_age(fifo_queue, to_date):
|
|
|
|
batch_age = age_qty = total_qty = 0.0
|
|
|
|
for batch in fifo_queue:
|
|
|
|
batch_age = date_diff(to_date, batch[1])
|
2019-07-16 10:38:50 +00:00
|
|
|
|
2021-04-04 12:57:01 +00:00
|
|
|
if isinstance(batch[0], (int, float)):
|
2019-07-16 10:38:50 +00:00
|
|
|
age_qty += batch_age * batch[0]
|
|
|
|
total_qty += batch[0]
|
|
|
|
else:
|
|
|
|
age_qty += batch_age * 1
|
|
|
|
total_qty += 1
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2020-05-19 13:38:30 +00:00
|
|
|
return flt(age_qty / total_qty, 2) if total_qty else 0.0
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2020-07-08 13:53:13 +00:00
|
|
|
def get_range_age(filters, fifo_queue, to_date):
|
|
|
|
range1 = range2 = range3 = above_range3 = 0.0
|
|
|
|
for item in fifo_queue:
|
|
|
|
age = date_diff(to_date, item[1])
|
2020-10-19 05:09:20 +00:00
|
|
|
|
2020-07-08 13:53:13 +00:00
|
|
|
if age <= filters.range1:
|
2020-07-23 15:39:27 +00:00
|
|
|
range1 += flt(item[0])
|
2020-07-08 13:53:13 +00:00
|
|
|
elif age <= filters.range2:
|
2020-07-23 15:39:27 +00:00
|
|
|
range2 += flt(item[0])
|
2020-07-08 13:53:13 +00:00
|
|
|
elif age <= filters.range3:
|
2020-07-23 15:39:27 +00:00
|
|
|
range3 += flt(item[0])
|
2020-07-08 13:53:13 +00:00
|
|
|
else:
|
2020-07-23 15:39:27 +00:00
|
|
|
above_range3 += flt(item[0])
|
2020-10-19 05:09:20 +00:00
|
|
|
|
2020-07-23 10:09:27 +00:00
|
|
|
return range1, range2, range3, above_range3
|
2020-07-08 13:53:13 +00:00
|
|
|
|
2019-03-19 20:36:39 +00:00
|
|
|
def get_columns(filters):
|
2020-07-08 13:53:13 +00:00
|
|
|
range_columns = []
|
|
|
|
setup_ageing_columns(filters, range_columns)
|
2019-07-10 09:19:25 +00:00
|
|
|
columns = [
|
|
|
|
{
|
|
|
|
"label": _("Item Code"),
|
|
|
|
"fieldname": "item_code",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Item",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Item Name"),
|
|
|
|
"fieldname": "item_name",
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Description"),
|
|
|
|
"fieldname": "description",
|
|
|
|
"fieldtype": "Data",
|
|
|
|
"width": 200
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Item Group"),
|
|
|
|
"fieldname": "item_group",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Item Group",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Brand"),
|
|
|
|
"fieldname": "brand",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Brand",
|
|
|
|
"width": 100
|
|
|
|
}]
|
2019-03-19 20:36:39 +00:00
|
|
|
|
2019-07-13 16:28:32 +00:00
|
|
|
if filters.get("show_warehouse_wise_stock"):
|
2019-07-10 09:19:25 +00:00
|
|
|
columns +=[{
|
|
|
|
"label": _("Warehouse"),
|
|
|
|
"fieldname": "warehouse",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Warehouse",
|
|
|
|
"width": 100
|
|
|
|
}]
|
|
|
|
|
|
|
|
columns.extend([
|
|
|
|
{
|
|
|
|
"label": _("Available Qty"),
|
|
|
|
"fieldname": "qty",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 100
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Average Age"),
|
|
|
|
"fieldname": "average_age",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 100
|
2020-07-08 13:53:13 +00:00
|
|
|
}])
|
|
|
|
columns.extend(range_columns)
|
|
|
|
columns.extend([
|
2019-07-10 09:19:25 +00:00
|
|
|
{
|
|
|
|
"label": _("Earliest"),
|
|
|
|
"fieldname": "earliest",
|
|
|
|
"fieldtype": "Int",
|
|
|
|
"width": 80
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Latest"),
|
|
|
|
"fieldname": "latest",
|
|
|
|
"fieldtype": "Int",
|
|
|
|
"width": 80
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("UOM"),
|
|
|
|
"fieldname": "uom",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "UOM",
|
|
|
|
"width": 100
|
|
|
|
}
|
|
|
|
])
|
2019-03-19 20:36:39 +00:00
|
|
|
|
|
|
|
return columns
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
def get_fifo_queue(filters, sle=None):
|
2013-12-26 05:37:58 +00:00
|
|
|
item_details = {}
|
2019-09-16 14:27:04 +00:00
|
|
|
transferred_item_details = {}
|
2019-07-16 10:38:50 +00:00
|
|
|
serial_no_batch_purchase_details = {}
|
2019-07-13 16:28:32 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
if sle == None:
|
|
|
|
sle = get_stock_ledger_entries(filters)
|
2019-07-13 16:28:32 +00:00
|
|
|
|
|
|
|
for d in sle:
|
2019-09-17 10:32:11 +00:00
|
|
|
key = (d.name, d.warehouse) if filters.get('show_warehouse_wise_stock') else d.name
|
2019-03-19 20:36:39 +00:00
|
|
|
item_details.setdefault(key, {"details": d, "fifo_queue": []})
|
|
|
|
fifo_queue = item_details[key]["fifo_queue"]
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2020-10-19 05:09:20 +00:00
|
|
|
transferred_item_key = (d.voucher_no, d.name, d.warehouse)
|
|
|
|
transferred_item_details.setdefault(transferred_item_key, [])
|
2019-07-13 16:28:32 +00:00
|
|
|
|
2014-10-07 09:32:58 +00:00
|
|
|
if d.voucher_type == "Stock Reconciliation":
|
2019-03-19 20:36:39 +00:00
|
|
|
d.actual_qty = flt(d.qty_after_transaction) - flt(item_details[key].get("qty_after_transaction", 0))
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2019-07-23 03:00:09 +00:00
|
|
|
serial_no_list = get_serial_nos(d.serial_no) if d.serial_no else []
|
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
if d.actual_qty > 0:
|
2020-10-19 05:09:20 +00:00
|
|
|
if transferred_item_details.get(transferred_item_key):
|
|
|
|
batch = transferred_item_details[transferred_item_key][0]
|
2019-07-22 10:00:18 +00:00
|
|
|
fifo_queue.append(batch)
|
2020-10-19 05:09:20 +00:00
|
|
|
transferred_item_details[transferred_item_key].pop(0)
|
2019-07-13 16:28:32 +00:00
|
|
|
else:
|
2019-07-23 03:00:09 +00:00
|
|
|
if serial_no_list:
|
|
|
|
for serial_no in serial_no_list:
|
|
|
|
if serial_no_batch_purchase_details.get(serial_no):
|
|
|
|
fifo_queue.append([serial_no, serial_no_batch_purchase_details.get(serial_no)])
|
|
|
|
else:
|
|
|
|
serial_no_batch_purchase_details.setdefault(serial_no, d.posting_date)
|
|
|
|
fifo_queue.append([serial_no, d.posting_date])
|
2013-12-26 05:37:58 +00:00
|
|
|
else:
|
2019-07-16 10:38:50 +00:00
|
|
|
fifo_queue.append([d.actual_qty, d.posting_date])
|
|
|
|
else:
|
2019-07-23 03:00:09 +00:00
|
|
|
if serial_no_list:
|
|
|
|
for serial_no in fifo_queue:
|
|
|
|
if serial_no[0] in serial_no_list:
|
|
|
|
fifo_queue.remove(serial_no)
|
2019-07-16 10:38:50 +00:00
|
|
|
else:
|
|
|
|
qty_to_pop = abs(d.actual_qty)
|
|
|
|
while qty_to_pop:
|
|
|
|
batch = fifo_queue[0] if fifo_queue else [0, None]
|
2020-06-12 11:53:24 +00:00
|
|
|
if 0 < flt(batch[0]) <= qty_to_pop:
|
2019-07-16 10:38:50 +00:00
|
|
|
# if batch qty > 0
|
|
|
|
# not enough or exactly same qty in current batch, clear batch
|
2020-06-12 11:53:24 +00:00
|
|
|
qty_to_pop -= flt(batch[0])
|
2020-10-19 05:09:20 +00:00
|
|
|
transferred_item_details[transferred_item_key].append(fifo_queue.pop(0))
|
2019-07-16 10:38:50 +00:00
|
|
|
else:
|
|
|
|
# all from current batch
|
2020-06-24 13:31:21 +00:00
|
|
|
batch[0] = flt(batch[0]) - qty_to_pop
|
2020-10-19 05:09:20 +00:00
|
|
|
transferred_item_details[transferred_item_key].append([qty_to_pop, batch[1]])
|
2019-07-16 10:38:50 +00:00
|
|
|
qty_to_pop = 0
|
2013-12-26 05:37:58 +00:00
|
|
|
|
2019-03-19 20:36:39 +00:00
|
|
|
item_details[key]["qty_after_transaction"] = d.qty_after_transaction
|
|
|
|
|
|
|
|
if "total_qty" not in item_details[key]:
|
|
|
|
item_details[key]["total_qty"] = d.actual_qty
|
|
|
|
else:
|
|
|
|
item_details[key]["total_qty"] += d.actual_qty
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
return item_details
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_stock_ledger_entries(filters):
|
2014-10-07 09:32:58 +00:00
|
|
|
return frappe.db.sql("""select
|
|
|
|
item.name, item.item_name, item_group, brand, description, item.stock_uom,
|
2019-07-16 10:38:50 +00:00
|
|
|
actual_qty, posting_date, voucher_type, voucher_no, serial_no, batch_no, qty_after_transaction, warehouse
|
2013-12-26 05:37:58 +00:00
|
|
|
from `tabStock Ledger Entry` sle,
|
|
|
|
(select name, item_name, description, stock_uom, brand, item_group
|
|
|
|
from `tabItem` {item_conditions}) item
|
|
|
|
where item_code = item.name and
|
|
|
|
company = %(company)s and
|
2021-01-21 12:52:35 +00:00
|
|
|
posting_date <= %(to_date)s and
|
|
|
|
is_cancelled != 1
|
2013-12-26 05:37:58 +00:00
|
|
|
{sle_conditions}
|
2019-07-16 10:38:50 +00:00
|
|
|
order by posting_date, posting_time, sle.creation, actual_qty""" #nosec
|
2013-12-26 05:37:58 +00:00
|
|
|
.format(item_conditions=get_item_conditions(filters),
|
|
|
|
sle_conditions=get_sle_conditions(filters)), filters, as_dict=True)
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_item_conditions(filters):
|
|
|
|
conditions = []
|
|
|
|
if filters.get("item_code"):
|
|
|
|
conditions.append("item_code=%(item_code)s")
|
|
|
|
if filters.get("brand"):
|
|
|
|
conditions.append("brand=%(brand)s")
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
return "where {}".format(" and ".join(conditions)) if conditions else ""
|
2014-10-07 09:32:58 +00:00
|
|
|
|
2013-12-26 05:37:58 +00:00
|
|
|
def get_sle_conditions(filters):
|
|
|
|
conditions = []
|
|
|
|
if filters.get("warehouse"):
|
2018-03-05 05:30:42 +00:00
|
|
|
lft, rgt = frappe.db.get_value('Warehouse', filters.get("warehouse"), ['lft', 'rgt'])
|
|
|
|
conditions.append("""warehouse in (select wh.name from `tabWarehouse` wh
|
|
|
|
where wh.lft >= {0} and rgt <= {1})""".format(lft, rgt))
|
2014-10-07 09:32:58 +00:00
|
|
|
|
|
|
|
return "and {}".format(" and ".join(conditions)) if conditions else ""
|
2020-05-14 07:50:43 +00:00
|
|
|
|
|
|
|
def get_chart_data(data, filters):
|
2020-05-15 07:51:58 +00:00
|
|
|
if not data:
|
|
|
|
return []
|
|
|
|
|
2020-05-14 07:50:43 +00:00
|
|
|
labels, datapoints = [], []
|
|
|
|
|
|
|
|
if filters.get("show_warehouse_wise_stock"):
|
|
|
|
return {}
|
|
|
|
|
2020-05-19 13:38:30 +00:00
|
|
|
data.sort(key = lambda row: row[6], reverse=True)
|
|
|
|
|
2020-05-14 07:50:43 +00:00
|
|
|
if len(data) > 10:
|
|
|
|
data = data[:10]
|
|
|
|
|
|
|
|
for row in data:
|
|
|
|
labels.append(row[0])
|
|
|
|
datapoints.append(row[6])
|
|
|
|
|
|
|
|
return {
|
|
|
|
"data" : {
|
|
|
|
"labels": labels,
|
|
|
|
"datasets": [
|
|
|
|
{
|
|
|
|
"name": _("Average Age"),
|
|
|
|
"values": datapoints
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
"type" : "bar"
|
2020-06-11 16:24:48 +00:00
|
|
|
}
|
2020-07-08 13:53:13 +00:00
|
|
|
|
|
|
|
def setup_ageing_columns(filters, range_columns):
|
|
|
|
for i, label in enumerate(["0-{range1}".format(range1=filters["range1"]),
|
|
|
|
"{range1}-{range2}".format(range1=cint(filters["range1"])+ 1, range2=filters["range2"]),
|
|
|
|
"{range2}-{range3}".format(range2=cint(filters["range2"])+ 1, range3=filters["range3"]),
|
|
|
|
"{range3}-{above}".format(range3=cint(filters["range3"])+ 1, above=_("Above"))]):
|
2020-07-23 15:03:07 +00:00
|
|
|
add_column(range_columns, label="Age ("+ label +")", fieldname='range' + str(i+1))
|
2020-07-08 13:53:13 +00:00
|
|
|
|
|
|
|
def add_column(range_columns, label, fieldname, fieldtype='Float', width=140):
|
|
|
|
range_columns.append(dict(
|
|
|
|
label=label,
|
|
|
|
fieldname=fieldname,
|
|
|
|
fieldtype=fieldtype,
|
|
|
|
width=width
|
2021-04-04 12:57:01 +00:00
|
|
|
))
|