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-05 06:06:24 +00:00
|
|
|
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2021-09-01 09:23:21 +00:00
|
|
|
from operator import itemgetter
|
2022-04-14 08:22:07 +00:00
|
|
|
from typing import Any, Dict, List, Optional, TypedDict
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2020-07-17 05:54:40 +00:00
|
|
|
import frappe
|
2014-04-15 09:06:12 +00:00
|
|
|
from frappe import _
|
2022-04-11 10:04:53 +00:00
|
|
|
from frappe.query_builder.functions import CombineDatetime
|
2019-09-16 14:27:04 +00:00
|
|
|
from frappe.utils import cint, date_diff, flt, getdate
|
2022-04-11 10:04:53 +00:00
|
|
|
from frappe.utils.nestedset import get_descendants_of
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
import erpnext
|
2022-06-22 06:54:08 +00:00
|
|
|
from erpnext.stock.doctype.inventory_dimension.inventory_dimension import get_inventory_dimensions
|
2022-10-17 08:48:55 +00:00
|
|
|
from erpnext.stock.doctype.warehouse.warehouse import apply_warehouse_filter
|
2021-12-20 16:23:47 +00:00
|
|
|
from erpnext.stock.report.stock_ageing.stock_ageing import FIFOSlots, get_average_age
|
2020-12-21 09:15:50 +00:00
|
|
|
from erpnext.stock.utils import add_additional_uom_columns, is_reposting_item_valuation_in_progress
|
2019-09-16 14:27:04 +00:00
|
|
|
|
2018-02-15 05:58:55 +00:00
|
|
|
|
2022-04-11 09:01:15 +00:00
|
|
|
class StockBalanceFilter(TypedDict):
|
|
|
|
company: Optional[str]
|
|
|
|
from_date: str
|
|
|
|
to_date: str
|
|
|
|
item_group: Optional[str]
|
|
|
|
item: Optional[str]
|
|
|
|
warehouse: Optional[str]
|
|
|
|
warehouse_type: Optional[str]
|
|
|
|
include_uom: Optional[str] # include extra info in converted UOM
|
|
|
|
show_stock_ageing_data: bool
|
|
|
|
show_variant_attributes: bool
|
|
|
|
|
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
SLEntry = Dict[str, Any]
|
|
|
|
|
|
|
|
|
2022-04-11 09:01:15 +00:00
|
|
|
def execute(filters: Optional[StockBalanceFilter] = None):
|
2020-12-21 09:15:50 +00:00
|
|
|
is_reposting_item_valuation_in_progress()
|
2013-06-05 06:06:24 +00:00
|
|
|
if not filters:
|
|
|
|
filters = {}
|
2016-07-14 11:29:58 +00:00
|
|
|
|
2020-07-17 05:54:40 +00:00
|
|
|
if filters.get("company"):
|
|
|
|
company_currency = erpnext.get_company_currency(filters.get("company"))
|
|
|
|
else:
|
|
|
|
company_currency = frappe.db.get_single_value("Global Defaults", "default_currency")
|
|
|
|
|
2018-10-18 12:29:47 +00:00
|
|
|
include_uom = filters.get("include_uom")
|
2019-09-16 14:27:04 +00:00
|
|
|
columns = get_columns(filters)
|
2018-04-12 07:55:03 +00:00
|
|
|
items = get_items(filters)
|
|
|
|
sle = get_stock_ledger_entries(filters, items)
|
2018-07-23 06:01:04 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
if filters.get("show_stock_ageing_data"):
|
|
|
|
filters["show_warehouse_wise_stock"] = True
|
2021-12-21 07:02:55 +00:00
|
|
|
item_wise_fifo_queue = FIFOSlots(filters, sle).generate()
|
2019-09-16 14:27:04 +00:00
|
|
|
|
2018-07-23 06:01:04 +00:00
|
|
|
# if no stock ledger entry found return
|
|
|
|
if not sle:
|
|
|
|
return columns, []
|
|
|
|
|
2018-04-12 07:55:03 +00:00
|
|
|
iwb_map = get_item_warehouse_map(filters, sle)
|
|
|
|
item_map = get_item_details(items, sle, filters)
|
|
|
|
item_reorder_detail_map = get_item_reorder_details(item_map.keys())
|
2014-04-15 09:06:12 +00:00
|
|
|
|
2013-06-05 06:06:24 +00:00
|
|
|
data = []
|
2019-09-16 14:27:04 +00:00
|
|
|
conversion_factors = {}
|
2019-10-20 14:17:52 +00:00
|
|
|
|
2021-09-01 09:23:21 +00:00
|
|
|
_func = itemgetter(1)
|
2019-10-20 14:17:52 +00:00
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
to_date = filters.get("to_date")
|
2022-06-22 06:54:08 +00:00
|
|
|
|
|
|
|
for group_by_key in iwb_map:
|
|
|
|
item = group_by_key[1]
|
|
|
|
warehouse = group_by_key[2]
|
|
|
|
company = group_by_key[0]
|
|
|
|
|
2018-07-09 11:26:41 +00:00
|
|
|
if item_map.get(item):
|
2022-06-22 06:54:08 +00:00
|
|
|
qty_dict = iwb_map[group_by_key]
|
2018-07-09 11:26:41 +00:00
|
|
|
item_reorder_level = 0
|
|
|
|
item_reorder_qty = 0
|
|
|
|
if item + warehouse in item_reorder_detail_map:
|
|
|
|
item_reorder_level = item_reorder_detail_map[item + warehouse]["warehouse_reorder_level"]
|
|
|
|
item_reorder_qty = item_reorder_detail_map[item + warehouse]["warehouse_reorder_qty"]
|
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
report_data = {
|
2020-07-17 05:54:40 +00:00
|
|
|
"currency": company_currency,
|
2019-09-16 14:27:04 +00:00
|
|
|
"item_code": item,
|
|
|
|
"warehouse": warehouse,
|
|
|
|
"company": company,
|
2019-12-27 07:05:58 +00:00
|
|
|
"reorder_level": item_reorder_level,
|
2019-09-16 14:27:04 +00:00
|
|
|
"reorder_qty": item_reorder_qty,
|
|
|
|
}
|
|
|
|
report_data.update(item_map[item])
|
|
|
|
report_data.update(qty_dict)
|
2018-07-09 11:26:41 +00:00
|
|
|
|
2018-10-18 12:29:47 +00:00
|
|
|
if include_uom:
|
2019-09-16 14:27:04 +00:00
|
|
|
conversion_factors.setdefault(item, item_map[item].conversion_factor)
|
2018-10-18 12:29:47 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
if filters.get("show_stock_ageing_data"):
|
|
|
|
fifo_queue = item_wise_fifo_queue[(item, warehouse)].get("fifo_queue")
|
2017-10-22 21:58:44 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
stock_ageing_data = {"average_age": 0, "earliest_age": 0, "latest_age": 0}
|
|
|
|
if fifo_queue:
|
2019-10-20 14:17:52 +00:00
|
|
|
fifo_queue = sorted(filter(_func, fifo_queue), key=_func)
|
|
|
|
if not fifo_queue:
|
|
|
|
continue
|
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
stock_ageing_data["average_age"] = get_average_age(fifo_queue, to_date)
|
|
|
|
stock_ageing_data["earliest_age"] = date_diff(to_date, fifo_queue[0][1])
|
|
|
|
stock_ageing_data["latest_age"] = date_diff(to_date, fifo_queue[-1][1])
|
2017-11-15 09:13:06 +00:00
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
report_data.update(stock_ageing_data)
|
|
|
|
|
|
|
|
data.append(report_data)
|
|
|
|
|
|
|
|
add_additional_uom_columns(columns, data, include_uom, conversion_factors)
|
2013-06-05 06:06:24 +00:00
|
|
|
return columns, data
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-04-11 09:01:15 +00:00
|
|
|
def get_columns(filters: StockBalanceFilter):
|
2016-03-07 07:41:59 +00:00
|
|
|
"""return columns"""
|
2016-02-04 11:52:52 +00:00
|
|
|
columns = [
|
2018-10-18 12:29:47 +00:00
|
|
|
{
|
|
|
|
"label": _("Item"),
|
|
|
|
"fieldname": "item_code",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Item",
|
|
|
|
"width": 100,
|
|
|
|
},
|
|
|
|
{"label": _("Item Name"), "fieldname": "item_name", "width": 150},
|
|
|
|
{
|
|
|
|
"label": _("Item Group"),
|
|
|
|
"fieldname": "item_group",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Item Group",
|
|
|
|
"width": 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Warehouse"),
|
|
|
|
"fieldname": "warehouse",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Warehouse",
|
|
|
|
"width": 100,
|
|
|
|
},
|
2016-02-04 11:52:52 +00:00
|
|
|
]
|
2013-06-05 06:06:24 +00:00
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
for dimension in get_inventory_dimensions():
|
|
|
|
columns.append(
|
|
|
|
{
|
2022-06-30 13:42:06 +00:00
|
|
|
"label": _(dimension.doctype),
|
2022-06-22 06:54:08 +00:00
|
|
|
"fieldname": dimension.fieldname,
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": dimension.doctype,
|
|
|
|
"width": 110,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
columns.extend(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"label": _("Stock UOM"),
|
|
|
|
"fieldname": "stock_uom",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "UOM",
|
|
|
|
"width": 90,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Balance Qty"),
|
|
|
|
"fieldname": "bal_qty",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 100,
|
|
|
|
"convertible": "qty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Balance Value"),
|
|
|
|
"fieldname": "bal_val",
|
|
|
|
"fieldtype": "Currency",
|
|
|
|
"width": 100,
|
|
|
|
"options": "currency",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Opening Qty"),
|
|
|
|
"fieldname": "opening_qty",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 100,
|
|
|
|
"convertible": "qty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Opening Value"),
|
|
|
|
"fieldname": "opening_val",
|
|
|
|
"fieldtype": "Currency",
|
|
|
|
"width": 110,
|
|
|
|
"options": "currency",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("In Qty"),
|
|
|
|
"fieldname": "in_qty",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 80,
|
|
|
|
"convertible": "qty",
|
|
|
|
},
|
|
|
|
{"label": _("In Value"), "fieldname": "in_val", "fieldtype": "Float", "width": 80},
|
|
|
|
{
|
|
|
|
"label": _("Out Qty"),
|
|
|
|
"fieldname": "out_qty",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 80,
|
|
|
|
"convertible": "qty",
|
|
|
|
},
|
|
|
|
{"label": _("Out Value"), "fieldname": "out_val", "fieldtype": "Float", "width": 80},
|
|
|
|
{
|
|
|
|
"label": _("Valuation Rate"),
|
|
|
|
"fieldname": "val_rate",
|
|
|
|
"fieldtype": "Currency",
|
|
|
|
"width": 90,
|
|
|
|
"convertible": "rate",
|
|
|
|
"options": "currency",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Reorder Level"),
|
|
|
|
"fieldname": "reorder_level",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 80,
|
|
|
|
"convertible": "qty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Reorder Qty"),
|
|
|
|
"fieldname": "reorder_qty",
|
|
|
|
"fieldtype": "Float",
|
|
|
|
"width": 80,
|
|
|
|
"convertible": "qty",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Company"),
|
|
|
|
"fieldname": "company",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Company",
|
|
|
|
"width": 100,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2019-09-16 14:27:04 +00:00
|
|
|
if filters.get("show_stock_ageing_data"):
|
|
|
|
columns += [
|
|
|
|
{"label": _("Average Age"), "fieldname": "average_age", "width": 100},
|
|
|
|
{"label": _("Earliest Age"), "fieldname": "earliest_age", "width": 100},
|
|
|
|
{"label": _("Latest Age"), "fieldname": "latest_age", "width": 100},
|
|
|
|
]
|
|
|
|
|
|
|
|
if filters.get("show_variant_attributes"):
|
|
|
|
columns += [
|
|
|
|
{"label": att_name, "fieldname": att_name, "width": 100}
|
|
|
|
for att_name in get_variants_attributes()
|
|
|
|
]
|
|
|
|
|
2013-06-05 06:06:24 +00:00
|
|
|
return columns
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
def apply_conditions(query, filters):
|
|
|
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
|
|
|
warehouse_table = frappe.qb.DocType("Warehouse")
|
|
|
|
|
2013-06-05 06:06:24 +00:00
|
|
|
if not filters.get("from_date"):
|
2014-04-15 09:06:12 +00:00
|
|
|
frappe.throw(_("'From Date' is required"))
|
2013-06-05 06:06:24 +00:00
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
if to_date := filters.get("to_date"):
|
|
|
|
query = query.where(sle.posting_date <= to_date)
|
2013-06-05 06:06:24 +00:00
|
|
|
else:
|
2014-04-15 09:06:12 +00:00
|
|
|
frappe.throw(_("'To Date' is required"))
|
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
if company := filters.get("company"):
|
|
|
|
query = query.where(sle.company == company)
|
|
|
|
|
2022-10-17 08:48:55 +00:00
|
|
|
if filters.get("warehouse"):
|
|
|
|
query = apply_warehouse_filter(query, sle, filters)
|
2022-04-11 10:04:53 +00:00
|
|
|
elif warehouse_type := filters.get("warehouse_type"):
|
|
|
|
query = (
|
|
|
|
query.join(warehouse_table)
|
|
|
|
.on(warehouse_table.name == sle.warehouse)
|
|
|
|
.where(warehouse_table.warehouse_type == warehouse_type)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-05-28 05:38:14 +00:00
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
return query
|
2013-06-05 06:06:24 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
def get_stock_ledger_entries(filters: StockBalanceFilter, items: List[str]) -> List[SLEntry]:
|
2022-04-11 10:04:53 +00:00
|
|
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
|
|
|
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(sle)
|
|
|
|
.select(
|
|
|
|
sle.item_code,
|
|
|
|
sle.warehouse,
|
|
|
|
sle.posting_date,
|
|
|
|
sle.actual_qty,
|
|
|
|
sle.valuation_rate,
|
|
|
|
sle.company,
|
|
|
|
sle.voucher_type,
|
|
|
|
sle.qty_after_transaction,
|
|
|
|
sle.stock_value_difference,
|
|
|
|
sle.item_code.as_("name"),
|
|
|
|
sle.voucher_no,
|
|
|
|
sle.stock_value,
|
|
|
|
sle.batch_no,
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2022-04-11 10:04:53 +00:00
|
|
|
.where((sle.docstatus < 2) & (sle.is_cancelled == 0))
|
|
|
|
.orderby(CombineDatetime(sle.posting_date, sle.posting_time))
|
|
|
|
.orderby(sle.creation)
|
|
|
|
.orderby(sle.actual_qty)
|
|
|
|
)
|
2018-04-12 07:55:03 +00:00
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
inventory_dimension_fields = get_inventory_dimension_fields()
|
|
|
|
if inventory_dimension_fields:
|
|
|
|
for fieldname in inventory_dimension_fields:
|
2022-06-30 13:42:06 +00:00
|
|
|
query = query.select(fieldname)
|
2022-06-22 06:54:08 +00:00
|
|
|
if fieldname in filters and filters.get(fieldname):
|
|
|
|
query = query.where(sle[fieldname].isin(filters.get(fieldname)))
|
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
if items:
|
|
|
|
query = query.where(sle.item_code.isin(items))
|
2018-04-12 07:55:03 +00:00
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
query = apply_conditions(query, filters)
|
2022-07-15 10:13:38 +00:00
|
|
|
return query.run(as_dict=True)
|
2022-06-22 06:54:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_inventory_dimension_fields():
|
|
|
|
return [dimension.fieldname for dimension in get_inventory_dimensions()]
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2013-06-05 06:06:24 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
def get_item_warehouse_map(filters: StockBalanceFilter, sle: List[SLEntry]):
|
2013-06-05 06:06:24 +00:00
|
|
|
iwb_map = {}
|
2017-04-14 08:18:55 +00:00
|
|
|
from_date = getdate(filters.get("from_date"))
|
|
|
|
to_date = getdate(filters.get("to_date"))
|
2016-02-11 15:08:01 +00:00
|
|
|
|
2020-04-23 04:14:58 +00:00
|
|
|
float_precision = cint(frappe.db.get_default("float_precision")) or 3
|
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
inventory_dimensions = get_inventory_dimension_fields()
|
|
|
|
|
2013-06-05 06:06:24 +00:00
|
|
|
for d in sle:
|
2022-06-30 13:42:06 +00:00
|
|
|
group_by_key = get_group_by_key(d, filters, inventory_dimensions)
|
2022-06-22 06:54:08 +00:00
|
|
|
if group_by_key not in iwb_map:
|
|
|
|
iwb_map[group_by_key] = frappe._dict(
|
2016-02-11 15:08:01 +00:00
|
|
|
{
|
2014-09-29 06:05:52 +00:00
|
|
|
"opening_qty": 0.0,
|
|
|
|
"opening_val": 0.0,
|
|
|
|
"in_qty": 0.0,
|
|
|
|
"in_val": 0.0,
|
|
|
|
"out_qty": 0.0,
|
|
|
|
"out_val": 0.0,
|
|
|
|
"bal_qty": 0.0,
|
|
|
|
"bal_val": 0.0,
|
2017-01-10 10:40:40 +00:00
|
|
|
"val_rate": 0.0,
|
2016-02-11 15:08:01 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
qty_dict = iwb_map[group_by_key]
|
|
|
|
for field in inventory_dimensions:
|
|
|
|
qty_dict[field] = d.get(field)
|
2014-09-11 11:17:19 +00:00
|
|
|
|
2021-05-04 09:33:10 +00:00
|
|
|
if d.voucher_type == "Stock Reconciliation" and not d.batch_no:
|
2020-04-23 04:14:58 +00:00
|
|
|
qty_diff = flt(d.qty_after_transaction) - flt(qty_dict.bal_qty)
|
2014-10-10 12:32:23 +00:00
|
|
|
else:
|
|
|
|
qty_diff = flt(d.actual_qty)
|
2014-10-14 06:11:44 +00:00
|
|
|
|
2021-03-13 07:18:14 +00:00
|
|
|
value_diff = flt(d.stock_value_difference)
|
2016-02-11 15:08:01 +00:00
|
|
|
|
2021-10-23 15:34:42 +00:00
|
|
|
if d.posting_date < from_date or (
|
|
|
|
d.posting_date == from_date
|
|
|
|
and d.voucher_type == "Stock Reconciliation"
|
|
|
|
and frappe.db.get_value("Stock Reconciliation", d.voucher_no, "purpose") == "Opening Stock"
|
|
|
|
):
|
2014-10-10 12:32:23 +00:00
|
|
|
qty_dict.opening_qty += qty_diff
|
|
|
|
qty_dict.opening_val += value_diff
|
2016-02-11 15:08:01 +00:00
|
|
|
|
|
|
|
elif d.posting_date >= from_date and d.posting_date <= to_date:
|
2020-04-23 04:14:58 +00:00
|
|
|
if flt(qty_diff, float_precision) >= 0:
|
2014-10-10 12:32:23 +00:00
|
|
|
qty_dict.in_qty += qty_diff
|
|
|
|
qty_dict.in_val += value_diff
|
2013-06-05 06:06:24 +00:00
|
|
|
else:
|
2014-10-10 12:32:23 +00:00
|
|
|
qty_dict.out_qty += abs(qty_diff)
|
|
|
|
qty_dict.out_val += abs(value_diff)
|
2016-02-11 15:08:01 +00:00
|
|
|
|
2015-10-06 13:16:56 +00:00
|
|
|
qty_dict.val_rate = d.valuation_rate
|
2014-10-10 12:32:23 +00:00
|
|
|
qty_dict.bal_qty += qty_diff
|
|
|
|
qty_dict.bal_val += value_diff
|
2018-09-21 04:50:52 +00:00
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
iwb_map = filter_items_with_no_transactions(iwb_map, float_precision, inventory_dimensions)
|
2017-01-10 10:40:40 +00:00
|
|
|
|
|
|
|
return iwb_map
|
2018-09-21 04:50:52 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-06-30 13:42:06 +00:00
|
|
|
def get_group_by_key(row, filters, inventory_dimension_fields) -> tuple:
|
2022-06-22 06:54:08 +00:00
|
|
|
group_by_key = [row.company, row.item_code, row.warehouse]
|
|
|
|
|
|
|
|
for fieldname in inventory_dimension_fields:
|
2022-06-30 13:42:06 +00:00
|
|
|
if filters.get(fieldname):
|
|
|
|
group_by_key.append(row.get(fieldname))
|
2022-06-22 06:54:08 +00:00
|
|
|
|
|
|
|
return tuple(group_by_key)
|
|
|
|
|
|
|
|
|
|
|
|
def filter_items_with_no_transactions(iwb_map, float_precision: float, inventory_dimensions: list):
|
2022-06-30 13:42:06 +00:00
|
|
|
pop_keys = []
|
2022-06-22 06:54:08 +00:00
|
|
|
for group_by_key in iwb_map:
|
|
|
|
qty_dict = iwb_map[group_by_key]
|
2018-09-21 04:50:52 +00:00
|
|
|
|
2017-01-10 10:40:40 +00:00
|
|
|
no_transactions = True
|
2018-05-23 06:01:24 +00:00
|
|
|
for key, val in qty_dict.items():
|
2022-06-22 06:54:08 +00:00
|
|
|
if key in inventory_dimensions:
|
|
|
|
continue
|
|
|
|
|
2017-06-13 13:28:47 +00:00
|
|
|
val = flt(val, float_precision)
|
2017-01-10 10:40:40 +00:00
|
|
|
qty_dict[key] = val
|
|
|
|
if key != "val_rate" and val:
|
|
|
|
no_transactions = False
|
2018-09-21 04:50:52 +00:00
|
|
|
|
2017-01-10 10:40:40 +00:00
|
|
|
if no_transactions:
|
2022-06-30 13:42:06 +00:00
|
|
|
pop_keys.append(group_by_key)
|
|
|
|
|
|
|
|
for key in pop_keys:
|
|
|
|
iwb_map.pop(key)
|
2013-06-05 06:06:24 +00:00
|
|
|
|
|
|
|
return iwb_map
|
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
def get_items(filters: StockBalanceFilter) -> List[str]:
|
2021-08-10 15:07:09 +00:00
|
|
|
"Get items based on item code, item group or brand."
|
2022-04-11 10:04:53 +00:00
|
|
|
if item_code := filters.get("item_code"):
|
|
|
|
return [item_code]
|
2018-04-12 07:55:03 +00:00
|
|
|
else:
|
2022-04-11 10:04:53 +00:00
|
|
|
item_filters = {}
|
|
|
|
if item_group := filters.get("item_group"):
|
|
|
|
children = get_descendants_of("Item Group", item_group, ignore_permissions=True)
|
|
|
|
item_filters["item_group"] = ("in", children + [item_group])
|
|
|
|
if brand := filters.get("brand"):
|
|
|
|
item_filters["brand"] = brand
|
|
|
|
|
2022-04-12 07:21:40 +00:00
|
|
|
return frappe.get_all("Item", filters=item_filters, pluck="name", order_by=None)
|
2018-04-12 07:55:03 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
def get_item_details(items: List[str], sle: List[SLEntry], filters: StockBalanceFilter):
|
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 sle))
|
2018-07-09 11:26:41 +00:00
|
|
|
|
2019-01-31 10:23:25 +00:00
|
|
|
if not items:
|
|
|
|
return item_details
|
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
item_table = frappe.qb.DocType("Item")
|
2019-01-31 10:23:25 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
query = (
|
|
|
|
frappe.qb.from_(item_table)
|
|
|
|
.select(
|
|
|
|
item_table.name,
|
|
|
|
item_table.item_name,
|
|
|
|
item_table.description,
|
|
|
|
item_table.item_group,
|
|
|
|
item_table.brand,
|
|
|
|
item_table.stock_uom,
|
|
|
|
)
|
|
|
|
.where(item_table.name.isin(items))
|
2019-03-30 10:22:48 +00:00
|
|
|
)
|
2019-01-31 10:23:25 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
if uom := filters.get("include_uom"):
|
|
|
|
uom_conv_detail = frappe.qb.DocType("UOM Conversion Detail")
|
|
|
|
query = (
|
|
|
|
query.left_join(uom_conv_detail)
|
|
|
|
.on((uom_conv_detail.parent == item_table.name) & (uom_conv_detail.uom == uom))
|
|
|
|
.select(uom_conv_detail.conversion_factor)
|
|
|
|
)
|
|
|
|
|
|
|
|
result = query.run(as_dict=1)
|
|
|
|
|
|
|
|
for item_table in result:
|
|
|
|
item_details.setdefault(item_table.name, item_table)
|
2017-11-15 09:13:06 +00:00
|
|
|
|
2022-04-11 10:04:53 +00:00
|
|
|
if filters.get("show_variant_attributes"):
|
2018-05-23 06:01:24 +00:00
|
|
|
variant_values = get_variant_values_for(list(item_details))
|
2018-02-15 05:58:55 +00:00
|
|
|
item_details = {k: v.update(variant_values.get(k, {})) for k, v in item_details.items()}
|
2017-11-15 09:13:06 +00:00
|
|
|
|
2017-10-22 21:58:44 +00:00
|
|
|
return item_details
|
2017-07-17 11:58:44 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-04-12 07:55:03 +00:00
|
|
|
def get_item_reorder_details(items):
|
2018-04-17 15:15:06 +00:00
|
|
|
item_reorder_details = frappe._dict()
|
|
|
|
|
|
|
|
if items:
|
2022-04-14 08:22:07 +00:00
|
|
|
item_reorder_details = frappe.get_all(
|
|
|
|
"Item Reorder",
|
|
|
|
["parent", "warehouse", "warehouse_reorder_qty", "warehouse_reorder_level"],
|
|
|
|
filters={"parent": ("in", items)},
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2017-07-17 11:58:44 +00:00
|
|
|
|
|
|
|
return dict((d.parent + d.warehouse, d) for d in item_reorder_details)
|
2016-03-14 09:29:21 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2022-04-14 08:22:07 +00:00
|
|
|
def get_variants_attributes() -> List[str]:
|
2017-10-22 21:58:44 +00:00
|
|
|
"""Return all item variant attributes."""
|
2022-04-11 10:04:53 +00:00
|
|
|
return frappe.get_all("Item Attribute", pluck="name")
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-10-22 21:58:44 +00:00
|
|
|
|
2017-11-15 09:13:06 +00:00
|
|
|
def get_variant_values_for(items):
|
2017-10-22 21:58:44 +00:00
|
|
|
"""Returns variant values for items."""
|
2017-10-23 09:04:00 +00:00
|
|
|
attribute_map = {}
|
2022-04-14 08:22:07 +00:00
|
|
|
|
|
|
|
attribute_info = frappe.get_all(
|
|
|
|
"Item Variant Attribute",
|
|
|
|
["parent", "attribute", "attribute_value"],
|
|
|
|
{
|
|
|
|
"parent": ("in", items),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
for attr in attribute_info:
|
2017-11-15 09:13:06 +00:00
|
|
|
attribute_map.setdefault(attr["parent"], {})
|
|
|
|
attribute_map[attr["parent"]].update({attr["attribute"]: attr["attribute_value"]})
|
|
|
|
|
2018-10-19 13:27:07 +00:00
|
|
|
return attribute_map
|