brotherton-erpnext/erpnext/stock/report/stock_ageing/stock_ageing.py

126 lines
4.2 KiB
Python
Raw Normal View History

# 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 _
2014-10-07 09:32:58 +00:00
from frappe.utils import date_diff, flt
from six import iteritems
2013-12-26 05:37:58 +00:00
def execute(filters=None):
2014-10-07 09:32:58 +00:00
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"]
data = []
for item, item_dict in iteritems(item_details):
2013-12-26 05:37:58 +00:00
fifo_queue = item_dict["fifo_queue"]
details = item_dict["details"]
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])
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]
if filters.get("show_ageing_warehouse_wise"):
row.append(details.warehouse)
row.extend([item_dict.get("total_qty"), average_age,
earliest_age, latest_age, details.stock_uom])
data.append(row)
2014-10-07 09:32:58 +00:00
2013-12-26 05:37:58 +00:00
return columns, 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])
age_qty += batch_age * batch[0]
total_qty += batch[0]
2014-10-07 09:32:58 +00:00
2013-12-26 05:37:58 +00:00
return (age_qty / total_qty) if total_qty else 0.0
2014-10-07 09:32:58 +00:00
2019-03-19 20:36:39 +00:00
def get_columns(filters):
columns = [_("Item Code") + ":Link/Item:100", _("Item Name") + "::100", _("Description") + "::200",
_("Item Group") + ":Link/Item Group:100", _("Brand") + ":Link/Brand:100"]
if filters.get("show_ageing_warehouse_wise"):
columns.extend([_("Warehouse") + ":Link/Warehouse:100"])
columns.extend([_("Available Qty") + ":Float:100", _("Average Age") + ":Float:100",
_("Earliest") + ":Int:80", _("Latest") + ":Int:80", _("UOM") + ":Link/UOM:100"])
return columns
2014-10-07 09:32:58 +00:00
2013-12-26 05:37:58 +00:00
def get_fifo_queue(filters):
item_details = {}
for d in get_stock_ledger_entries(filters):
2019-03-19 20:36:39 +00:00
key = (d.name, d.warehouse) if filters.get('show_ageing_warehouse_wise') else d.name
item_details.setdefault(key, {"details": d, "fifo_queue": []})
fifo_queue = item_details[key]["fifo_queue"]
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
2013-12-26 05:37:58 +00:00
if d.actual_qty > 0:
fifo_queue.append([d.actual_qty, d.posting_date])
else:
qty_to_pop = abs(d.actual_qty)
while qty_to_pop:
batch = fifo_queue[0] if fifo_queue else [0, None]
if 0 < batch[0] <= qty_to_pop:
2014-10-07 09:32:58 +00:00
# if batch qty > 0
2013-12-26 05:37:58 +00:00
# not enough or exactly same qty in current batch, clear batch
qty_to_pop -= batch[0]
fifo_queue.pop(0)
else:
# all from current batch
batch[0] -= qty_to_pop
qty_to_pop = 0
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-03-19 20:36:39 +00:00
actual_qty, posting_date, voucher_type, 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
posting_date <= %(to_date)s
{sle_conditions}
order by posting_date, posting_time, sle.creation"""\
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"):
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 ""