refactor: remove balance from Warehouse Tree

This commit is contained in:
s-aga-r 2022-12-01 19:44:49 +05:30
parent 2106de0fa5
commit 3ce8386ca3
2 changed files with 2 additions and 64 deletions

View File

@ -2,12 +2,10 @@
# License: GNU General Public License v3. See license.txt
from collections import defaultdict
import frappe
from frappe import _, throw
from frappe.contacts.address_and_contact import load_address_and_contact
from frappe.utils import cint, flt
from frappe.utils import cint
from frappe.utils.nestedset import NestedSet
from pypika.terms import ExistsCriterion
@ -166,60 +164,7 @@ def get_children(doctype, parent=None, company=None, is_root=False):
["company", "in", (company, None, "")],
]
warehouses = frappe.get_list(doctype, fields=fields, filters=filters, order_by="name")
company_currency = ""
if company:
company_currency = frappe.get_cached_value("Company", company, "default_currency")
warehouse_wise_value = get_warehouse_wise_stock_value(company)
# return warehouses
for wh in warehouses:
wh["balance"] = warehouse_wise_value.get(wh.value)
if company_currency:
wh["company_currency"] = company_currency
return warehouses
def get_warehouse_wise_stock_value(company):
warehouses = frappe.get_all(
"Warehouse", fields=["name", "parent_warehouse"], filters={"company": company}
)
parent_warehouse = {d.name: d.parent_warehouse for d in warehouses}
filters = {"warehouse": ("in", [data.name for data in warehouses])}
bin_data = frappe.get_all(
"Bin",
fields=["sum(stock_value) as stock_value", "warehouse"],
filters=filters,
group_by="warehouse",
)
warehouse_wise_stock_value = defaultdict(float)
for row in bin_data:
if not row.stock_value:
continue
warehouse_wise_stock_value[row.warehouse] = row.stock_value
update_value_in_parent_warehouse(
warehouse_wise_stock_value, parent_warehouse, row.warehouse, row.stock_value
)
return warehouse_wise_stock_value
def update_value_in_parent_warehouse(
warehouse_wise_stock_value, parent_warehouse_dict, warehouse, stock_value
):
parent_warehouse = parent_warehouse_dict.get(warehouse)
if not parent_warehouse:
return
warehouse_wise_stock_value[parent_warehouse] += flt(stock_value)
update_value_in_parent_warehouse(
warehouse_wise_stock_value, parent_warehouse_dict, parent_warehouse, stock_value
)
return frappe.get_list(doctype, fields=fields, filters=filters, order_by="name")
@frappe.whitelist()

View File

@ -17,11 +17,4 @@ frappe.treeview_settings['Warehouse'] = {
description: __("Child nodes can be only created under 'Group' type nodes")}
],
ignore_fields:["parent_warehouse"],
onrender: function(node) {
if (node.data && node.data.balance!==undefined) {
$('<span class="balance-area pull-right">'
+ format_currency((node.data.balance), node.data.company_currency)
+ '</span>').insertBefore(node.$ul);
}
}
}