2017-06-15 05:39:27 +00:00
|
|
|
import frappe
|
2018-10-08 12:49:53 +00:00
|
|
|
from frappe import _
|
2017-06-15 05:39:27 +00:00
|
|
|
|
2012-04-05 12:57:56 +00:00
|
|
|
install_docs = [
|
2022-03-28 13:22:46 +00:00
|
|
|
{"doctype": "Role", "role_name": "Stock Manager", "name": "Stock Manager"},
|
|
|
|
{"doctype": "Role", "role_name": "Item Manager", "name": "Item Manager"},
|
|
|
|
{"doctype": "Role", "role_name": "Stock User", "name": "Stock User"},
|
|
|
|
{"doctype": "Role", "role_name": "Quality Manager", "name": "Quality Manager"},
|
|
|
|
{"doctype": "Item Group", "item_group_name": "All Item Groups", "is_group": 1},
|
|
|
|
{
|
|
|
|
"doctype": "Item Group",
|
|
|
|
"item_group_name": "Default",
|
|
|
|
"parent_item_group": "All Item Groups",
|
|
|
|
"is_group": 0,
|
|
|
|
},
|
2012-04-05 12:57:56 +00:00
|
|
|
]
|
2017-06-15 05:39:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2019-03-08 05:43:35 +00:00
|
|
|
def get_warehouse_account_map(company=None):
|
2022-03-28 13:22:46 +00:00
|
|
|
company_warehouse_account_map = company and frappe.flags.setdefault(
|
|
|
|
"warehouse_account_map", {}
|
|
|
|
).get(company)
|
2020-03-31 10:01:15 +00:00
|
|
|
warehouse_account_map = frappe.flags.warehouse_account_map
|
|
|
|
|
|
|
|
if not warehouse_account_map or not company_warehouse_account_map or frappe.flags.in_test:
|
2017-06-15 05:39:27 +00:00
|
|
|
warehouse_account = frappe._dict()
|
|
|
|
|
2019-03-08 05:43:35 +00:00
|
|
|
filters = {}
|
|
|
|
if company:
|
2022-03-28 13:22:46 +00:00
|
|
|
filters["company"] = company
|
|
|
|
frappe.flags.setdefault("warehouse_account_map", {}).setdefault(company, {})
|
2019-03-08 05:43:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
for d in frappe.get_all(
|
|
|
|
"Warehouse",
|
|
|
|
fields=["name", "account", "parent_warehouse", "company", "is_group"],
|
|
|
|
filters=filters,
|
|
|
|
order_by="lft, rgt",
|
|
|
|
):
|
2017-06-15 05:39:27 +00:00
|
|
|
if not d.account:
|
2018-04-02 05:00:17 +00:00
|
|
|
d.account = get_warehouse_account(d, warehouse_account)
|
2017-06-15 05:39:27 +00:00
|
|
|
|
|
|
|
if d.account:
|
2022-03-28 13:22:46 +00:00
|
|
|
d.account_currency = frappe.db.get_value("Account", d.account, "account_currency", cache=True)
|
2017-06-15 05:39:27 +00:00
|
|
|
warehouse_account.setdefault(d.name, d)
|
2020-03-31 10:01:15 +00:00
|
|
|
if company:
|
|
|
|
frappe.flags.warehouse_account_map[company] = warehouse_account
|
|
|
|
else:
|
|
|
|
frappe.flags.warehouse_account_map = warehouse_account
|
2021-02-26 12:04:53 +00:00
|
|
|
|
2020-03-31 10:01:15 +00:00
|
|
|
return frappe.flags.warehouse_account_map.get(company) or frappe.flags.warehouse_account_map
|
2017-06-15 05:39:27 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-04-02 05:00:17 +00:00
|
|
|
def get_warehouse_account(warehouse, warehouse_account=None):
|
|
|
|
account = warehouse.account
|
|
|
|
if not account and warehouse.parent_warehouse:
|
|
|
|
if warehouse_account:
|
2018-10-08 12:49:53 +00:00
|
|
|
if warehouse_account.get(warehouse.parent_warehouse):
|
|
|
|
account = warehouse_account.get(warehouse.parent_warehouse).account
|
|
|
|
else:
|
|
|
|
from frappe.utils.nestedset import rebuild_tree
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2018-10-08 12:49:53 +00:00
|
|
|
rebuild_tree("Warehouse", "parent_warehouse")
|
2018-04-02 05:00:17 +00:00
|
|
|
else:
|
2022-03-28 13:22:46 +00:00
|
|
|
account = frappe.db.sql(
|
|
|
|
"""
|
2018-04-02 05:00:17 +00:00
|
|
|
select
|
|
|
|
account from `tabWarehouse`
|
|
|
|
where
|
|
|
|
lft <= %s and rgt >= %s and company = %s
|
|
|
|
and account is not null and ifnull(account, '') !=''
|
2022-03-28 13:22:46 +00:00
|
|
|
order by lft desc limit 1""",
|
|
|
|
(warehouse.lft, warehouse.rgt, warehouse.company),
|
|
|
|
as_list=1,
|
|
|
|
)
|
2018-04-02 05:00:17 +00:00
|
|
|
|
|
|
|
account = account[0][0] if account else None
|
|
|
|
|
2018-08-16 08:28:44 +00:00
|
|
|
if not account and warehouse.company:
|
2018-04-02 05:00:17 +00:00
|
|
|
account = get_company_default_inventory_account(warehouse.company)
|
|
|
|
|
2021-02-26 12:04:53 +00:00
|
|
|
if not account and warehouse.company:
|
2022-03-28 13:22:46 +00:00
|
|
|
account = frappe.db.get_value(
|
|
|
|
"Account", {"account_type": "Stock", "is_group": 0, "company": warehouse.company}, "name"
|
|
|
|
)
|
2021-02-26 12:04:53 +00:00
|
|
|
|
2021-02-16 09:27:00 +00:00
|
|
|
if not account and warehouse.company and not warehouse.is_group:
|
2022-03-28 13:22:46 +00:00
|
|
|
frappe.throw(
|
|
|
|
_("Please set Account in Warehouse {0} or Default Inventory Account in Company {1}").format(
|
|
|
|
warehouse.name, warehouse.company
|
|
|
|
)
|
|
|
|
)
|
2017-06-15 05:39:27 +00:00
|
|
|
return account
|
2019-03-08 05:43:35 +00:00
|
|
|
|
2022-03-28 13:22:46 +00:00
|
|
|
|
2017-06-15 05:39:27 +00:00
|
|
|
def get_company_default_inventory_account(company):
|
2022-03-28 13:22:46 +00:00
|
|
|
return frappe.get_cached_value("Company", company, "default_inventory_account")
|