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
|
|
|
|
|
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 _
|
2022-07-15 10:13:38 +00:00
|
|
|
from frappe.query_builder.functions import CombineDatetime
|
2020-07-10 12:41:04 +00:00
|
|
|
from frappe.utils import cint, flt
|
2021-09-02 11:14:59 +00:00
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
from erpnext.stock.doctype.inventory_dimension.inventory_dimension import get_inventory_dimensions
|
2020-10-06 07:15:29 +00:00
|
|
|
from erpnext.stock.doctype.serial_no.serial_no import get_serial_nos
|
2022-03-01 12:45:02 +00:00
|
|
|
from erpnext.stock.doctype.stock_reconciliation.stock_reconciliation import get_stock_balance_for
|
2022-10-17 08:48:55 +00:00
|
|
|
from erpnext.stock.doctype.warehouse.warehouse import apply_warehouse_filter
|
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")
|
2022-06-22 06:54:08 +00:00
|
|
|
columns = get_columns(filters)
|
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)
|
2021-10-23 15:34:42 +00:00
|
|
|
opening_row = get_opening_balance(filters, columns, sl_entries)
|
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 = {}
|
2022-06-22 06:54:08 +00:00
|
|
|
inventory_dimension_filters_applied = check_inventory_dimension_filters_applied(filters)
|
|
|
|
|
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
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
if filters.get("batch_no") or inventory_dimension_filters_applied:
|
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, "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
|
|
|
|
2022-03-28 13:22: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:
|
2022-03-01 12:45:02 +00:00
|
|
|
stock_balance = get_stock_balance_for(
|
2022-07-15 10:13:38 +00:00
|
|
|
sle.item_code, sle.warehouse, sle.posting_date, sle.posting_time
|
2022-03-14 04:41:38 +00:00
|
|
|
)
|
2022-03-01 12:45:02 +00:00
|
|
|
serials = get_serial_nos(stock_balance["serial_nos"]) if stock_balance["serial_nos"] else []
|
|
|
|
available_serial_nos.setdefault(key, serials)
|
2020-10-06 07:15:29 +00:00
|
|
|
|
|
|
|
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)
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
def get_columns(filters):
|
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,
|
|
|
|
},
|
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,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-07-15 10:13:38 +00:00
|
|
|
columns.extend(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"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},
|
|
|
|
{
|
|
|
|
"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",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Value Change"),
|
|
|
|
"fieldname": "stock_value_difference",
|
|
|
|
"fieldtype": "Currency",
|
|
|
|
"width": 110,
|
|
|
|
"options": "Company:company:default_currency",
|
|
|
|
},
|
|
|
|
{"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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Serial No"),
|
|
|
|
"fieldname": "serial_no",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Serial No",
|
|
|
|
"width": 100,
|
|
|
|
},
|
|
|
|
{"label": _("Balance Serial No"), "fieldname": "balance_serial_no", "width": 100},
|
|
|
|
{
|
|
|
|
"label": _("Project"),
|
|
|
|
"fieldname": "project",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Project",
|
|
|
|
"width": 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"label": _("Company"),
|
|
|
|
"fieldname": "company",
|
|
|
|
"fieldtype": "Link",
|
|
|
|
"options": "Company",
|
|
|
|
"width": 110,
|
|
|
|
},
|
|
|
|
]
|
2022-06-22 06:54:08 +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):
|
2022-07-15 10:13:38 +00:00
|
|
|
sle = frappe.qb.DocType("Stock Ledger Entry")
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(sle)
|
|
|
|
.select(
|
|
|
|
sle.item_code,
|
|
|
|
CombineDatetime(sle.posting_date, sle.posting_time).as_("date"),
|
|
|
|
sle.warehouse,
|
|
|
|
sle.posting_date,
|
|
|
|
sle.posting_time,
|
|
|
|
sle.actual_qty,
|
|
|
|
sle.incoming_rate,
|
|
|
|
sle.valuation_rate,
|
|
|
|
sle.company,
|
|
|
|
sle.voucher_type,
|
|
|
|
sle.qty_after_transaction,
|
|
|
|
sle.stock_value_difference,
|
|
|
|
sle.voucher_no,
|
|
|
|
sle.stock_value,
|
|
|
|
sle.batch_no,
|
|
|
|
sle.serial_no,
|
|
|
|
sle.project,
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2022-07-15 10:13:38 +00:00
|
|
|
.where(
|
|
|
|
(sle.docstatus < 2)
|
|
|
|
& (sle.is_cancelled == 0)
|
|
|
|
& (sle.posting_date[filters.from_date : filters.to_date])
|
|
|
|
)
|
|
|
|
.orderby(CombineDatetime(sle.posting_date, sle.posting_time))
|
|
|
|
.orderby(sle.creation)
|
2019-11-27 11:28:06 +00:00
|
|
|
)
|
|
|
|
|
2022-07-15 10:13:38 +00:00
|
|
|
inventory_dimension_fields = get_inventory_dimension_fields()
|
|
|
|
if inventory_dimension_fields:
|
|
|
|
for fieldname in inventory_dimension_fields:
|
|
|
|
query = query.select(fieldname)
|
|
|
|
if fieldname in filters and filters.get(fieldname):
|
|
|
|
query = query.where(sle[fieldname].isin(filters.get(fieldname)))
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2022-07-15 10:13:38 +00:00
|
|
|
if items:
|
|
|
|
query = query.where(sle.item_code.isin(items))
|
|
|
|
|
|
|
|
for field in ["voucher_no", "batch_no", "project", "company"]:
|
|
|
|
if filters.get(field):
|
|
|
|
query = query.where(sle[field] == filters.get(field))
|
|
|
|
|
2022-10-17 08:48:55 +00:00
|
|
|
query = apply_warehouse_filter(query, sle, filters)
|
2013-11-29 13:45:26 +00:00
|
|
|
|
2022-07-15 10:13:38 +00:00
|
|
|
return query.run(as_dict=True)
|
2022-06-22 06:54:08 +00:00
|
|
|
|
|
|
|
|
2022-07-15 10:13:38 +00:00
|
|
|
def get_inventory_dimension_fields():
|
|
|
|
return [dimension.fieldname for dimension in get_inventory_dimensions()]
|
2022-06-22 06:54:08 +00:00
|
|
|
|
|
|
|
|
2018-04-12 07:55:03 +00:00
|
|
|
def get_items(filters):
|
2022-10-14 08:34:43 +00:00
|
|
|
item = frappe.qb.DocType("Item")
|
|
|
|
query = frappe.qb.from_(item).select(item.name)
|
2013-11-29 13:45:26 +00:00
|
|
|
conditions = []
|
2022-10-14 08:34:43 +00:00
|
|
|
|
|
|
|
if item_code := filters.get("item_code"):
|
|
|
|
conditions.append(item.name == item_code)
|
2018-04-12 07:55:03 +00:00
|
|
|
else:
|
2022-10-14 08:34:43 +00:00
|
|
|
if brand := filters.get("brand"):
|
|
|
|
conditions.append(item.brand == brand)
|
|
|
|
if item_group := filters.get("item_group"):
|
|
|
|
if condition := get_item_group_condition(item_group, item):
|
|
|
|
conditions.append(condition)
|
2018-04-12 07:55:03 +00:00
|
|
|
|
|
|
|
items = []
|
|
|
|
if conditions:
|
2022-10-14 08:34:43 +00:00
|
|
|
for condition in conditions:
|
|
|
|
query = query.where(condition)
|
|
|
|
items = [r[0] for r in query.run()]
|
|
|
|
|
2018-04-12 07:55:03 +00:00
|
|
|
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
|
|
|
|
|
2022-10-14 08:34:43 +00:00
|
|
|
item = frappe.qb.DocType("Item")
|
|
|
|
query = (
|
|
|
|
frappe.qb.from_(item)
|
|
|
|
.select(item.name, item.item_name, item.description, item.item_group, item.brand, item.stock_uom)
|
|
|
|
.where(item.name.isin(items))
|
|
|
|
)
|
|
|
|
|
2018-10-18 12:29:47 +00:00
|
|
|
if include_uom:
|
2022-10-14 08:34:43 +00:00
|
|
|
ucd = frappe.qb.DocType("UOM Conversion Detail")
|
|
|
|
query = (
|
|
|
|
query.left_join(ucd)
|
|
|
|
.on((ucd.parent == item.name) & (ucd.uom == include_uom))
|
|
|
|
.select(ucd.conversion_factor)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2019-01-25 11:37:29 +00:00
|
|
|
|
2022-10-14 08:34:43 +00:00
|
|
|
res = query.run(as_dict=True)
|
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
|
|
|
|
2022-06-22 06:54:08 +00:00
|
|
|
for dimension in get_inventory_dimensions():
|
|
|
|
if filters.get(dimension.fieldname):
|
|
|
|
conditions.append(f"{dimension.fieldname} in %({dimension.fieldname})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
|
|
|
|
2021-10-23 15:34:42 +00:00
|
|
|
def get_opening_balance(filters, columns, sl_entries):
|
2015-07-22 13:14:47 +00:00
|
|
|
if not (filters.item_code and filters.warehouse and filters.from_date):
|
|
|
|
return
|
|
|
|
|
|
|
|
from erpnext.stock.stock_ledger import get_previous_sle
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2015-07-22 13:14:47 +00:00
|
|
|
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
|
|
|
|
2021-10-23 15:34:42 +00:00
|
|
|
# check if any SLEs are actually Opening Stock Reconciliation
|
2022-11-17 12:16:49 +00:00
|
|
|
for sle in list(sl_entries):
|
2021-10-23 15:34:42 +00:00
|
|
|
if (
|
|
|
|
sle.get("voucher_type") == "Stock Reconciliation"
|
2022-07-15 10:13:38 +00:00
|
|
|
and sle.posting_date == filters.from_date
|
2021-10-23 15:34:42 +00:00
|
|
|
and frappe.db.get_value("Stock Reconciliation", sle.voucher_no, "purpose") == "Opening Stock"
|
|
|
|
):
|
|
|
|
last_entry = sle
|
|
|
|
sl_entries.remove(sle)
|
|
|
|
|
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)"
|
2016-07-27 06:12:38 +00:00
|
|
|
% (warehouse_details.lft, warehouse_details.rgt)
|
2022-03-28 13:22:46 +00:00
|
|
|
)
|
2016-07-27 06:12:38 +00:00
|
|
|
|
|
|
|
return ""
|
2017-08-16 06:05:04 +00:00
|
|
|
|
2019-11-27 11:28:06 +00:00
|
|
|
|
2022-10-14 08:34:43 +00:00
|
|
|
def get_item_group_condition(item_group, item_table=None):
|
2017-08-16 06:05:04 +00:00
|
|
|
item_group_details = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"], as_dict=1)
|
|
|
|
if item_group_details:
|
2022-10-14 08:34:43 +00:00
|
|
|
if item_table:
|
|
|
|
ig = frappe.qb.DocType("Item Group")
|
|
|
|
return item_table.item_group.isin(
|
|
|
|
(
|
|
|
|
frappe.qb.from_(ig)
|
|
|
|
.select(ig.name)
|
|
|
|
.where(
|
|
|
|
(ig.lft >= item_group_details.lft)
|
|
|
|
& (ig.rgt <= item_group_details.rgt)
|
|
|
|
& (item_table.item_group == ig.name)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
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)
|
|
|
|
)
|
2022-06-22 06:54:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_inventory_dimension_filters_applied(filters) -> bool:
|
|
|
|
for dimension in get_inventory_dimensions():
|
|
|
|
if dimension.fieldname in filters and filters.get(dimension.fieldname):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|