mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-23 23:19:20 +00:00
Optimisation of warehouse_account_map (#13454)
This commit is contained in:
@@ -101,7 +101,7 @@ def _make_test_records(verbose):
|
|||||||
def get_inventory_account(company, warehouse=None):
|
def get_inventory_account(company, warehouse=None):
|
||||||
account = None
|
account = None
|
||||||
if warehouse:
|
if warehouse:
|
||||||
account = get_warehouse_account(warehouse, company)
|
account = get_warehouse_account(frappe.get_doc("Warehouse", warehouse))
|
||||||
else:
|
else:
|
||||||
account = get_company_default_inventory_account(company)
|
account = get_company_default_inventory_account(company)
|
||||||
|
|
||||||
|
|||||||
@@ -15,33 +15,39 @@ def get_warehouse_account_map():
|
|||||||
if not frappe.flags.warehouse_account_map or frappe.flags.in_test:
|
if not frappe.flags.warehouse_account_map or frappe.flags.in_test:
|
||||||
warehouse_account = frappe._dict()
|
warehouse_account = frappe._dict()
|
||||||
|
|
||||||
for d in frappe.get_all('Warehouse', filters = {"is_group": 0},
|
for d in frappe.get_all('Warehouse',
|
||||||
fields = ["name", "account", "parent_warehouse", "company"]):
|
fields = ["name", "account", "parent_warehouse", "company"],
|
||||||
|
order_by="lft, rgt"):
|
||||||
if not d.account:
|
if not d.account:
|
||||||
d.account = get_warehouse_account(d.name, d.company)
|
d.account = get_warehouse_account(d, warehouse_account)
|
||||||
|
|
||||||
if d.account:
|
if d.account:
|
||||||
d.account_currency = frappe.db.get_value('Account', d.account, 'account_currency')
|
d.account_currency = frappe.db.get_value('Account', d.account, 'account_currency', cache=True)
|
||||||
warehouse_account.setdefault(d.name, d)
|
warehouse_account.setdefault(d.name, d)
|
||||||
|
|
||||||
frappe.flags.warehouse_account_map = warehouse_account
|
frappe.flags.warehouse_account_map = warehouse_account
|
||||||
|
|
||||||
return frappe.flags.warehouse_account_map
|
return frappe.flags.warehouse_account_map
|
||||||
|
|
||||||
def get_warehouse_account(warehouse, company):
|
def get_warehouse_account(warehouse, warehouse_account=None):
|
||||||
lft, rgt = frappe.db.get_value("Warehouse", warehouse, ["lft", "rgt"])
|
account = warehouse.account
|
||||||
account = frappe.db.sql("""
|
if not account and warehouse.parent_warehouse:
|
||||||
select
|
if warehouse_account:
|
||||||
account from `tabWarehouse`
|
account = warehouse_account.get(warehouse.parent_warehouse).account
|
||||||
where
|
else:
|
||||||
lft <= %s and rgt >= %s and company = %s
|
account = frappe.db.sql("""
|
||||||
and account is not null and ifnull(account, '') !=''
|
select
|
||||||
order by lft desc limit 1""", (lft, rgt, company), as_list=1)
|
account from `tabWarehouse`
|
||||||
|
where
|
||||||
|
lft <= %s and rgt >= %s and company = %s
|
||||||
|
and account is not null and ifnull(account, '') !=''
|
||||||
|
order by lft desc limit 1""", (warehouse.lft, warehouse.rgt, warehouse.company), as_list=1)
|
||||||
|
|
||||||
|
account = account[0][0] if account else None
|
||||||
|
|
||||||
account = account[0][0] if account else None
|
|
||||||
|
|
||||||
if not account:
|
if not account:
|
||||||
account = get_company_default_inventory_account(company)
|
account = get_company_default_inventory_account(warehouse.company)
|
||||||
|
|
||||||
return account
|
return account
|
||||||
|
|
||||||
def get_company_default_inventory_account(company):
|
def get_company_default_inventory_account(company):
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import frappe, erpnext
|
import frappe, erpnext
|
||||||
from frappe.utils import cint, validate_email_add
|
from frappe.utils import cint
|
||||||
from frappe import throw, _
|
from frappe import throw, _
|
||||||
from frappe.utils.nestedset import NestedSet
|
from frappe.utils.nestedset import NestedSet
|
||||||
from erpnext.stock import get_warehouse_account
|
from erpnext.stock import get_warehouse_account
|
||||||
@@ -22,7 +22,7 @@ class Warehouse(NestedSet):
|
|||||||
|
|
||||||
def onload(self):
|
def onload(self):
|
||||||
'''load account name for General Ledger Report'''
|
'''load account name for General Ledger Report'''
|
||||||
account = self.account or get_warehouse_account(self.name, self.company)
|
account = self.account or get_warehouse_account(self)
|
||||||
|
|
||||||
if account:
|
if account:
|
||||||
self.set_onload('account', account)
|
self.set_onload('account', account)
|
||||||
|
|||||||
Reference in New Issue
Block a user