Merge pull request #5605 from saurabh6790/warehouse_tree_fix_1
while creating warehouse tree check for non company warehouse and is_group default value fix
This commit is contained in:
commit
5b681666a0
@ -4,22 +4,98 @@ from frappe.utils import cint
|
|||||||
from frappe.utils.nestedset import rebuild_tree
|
from frappe.utils.nestedset import rebuild_tree
|
||||||
|
|
||||||
def execute():
|
def execute():
|
||||||
|
"""
|
||||||
|
Patch Reference:
|
||||||
|
1. check whether warehouse is associated to company or not
|
||||||
|
2. if warehouse is associated with company
|
||||||
|
a. create warehouse group for company
|
||||||
|
b. set warehouse group as parent to other warehouses and set is_group as 0
|
||||||
|
3. if warehouses is not associated with company
|
||||||
|
a. get distinct companies from stock ledger entries
|
||||||
|
b. if sle have only company,
|
||||||
|
i. set default company to all warehouse
|
||||||
|
ii. repeat 2.a and 2.b
|
||||||
|
c. if have multiple companies,
|
||||||
|
i. create group warehouse without company
|
||||||
|
ii. repeat 2.b
|
||||||
|
"""
|
||||||
|
|
||||||
frappe.reload_doc("stock", "doctype", "warehouse")
|
frappe.reload_doc("stock", "doctype", "warehouse")
|
||||||
|
|
||||||
for company in frappe.get_all("Company", fields=["name", "abbr"]):
|
if check_is_warehouse_associated_with_company():
|
||||||
validate_parent_account_for_warehouse(company)
|
for company in frappe.get_all("Company", fields=["name", "abbr"]):
|
||||||
|
make_warehouse_nestedset(company)
|
||||||
|
else:
|
||||||
|
sle_against_companies = frappe.db.sql_list("""select distinct company from `tabStock Ledger Entry`""")
|
||||||
|
company = frappe.defaults.get_defaults().company
|
||||||
|
|
||||||
if not frappe.db.get_value("Warehouse", "{0} - {1}".format(_("All Warehouses"), company.abbr)):
|
if len(sle_against_companies) == 1:
|
||||||
create_default_warehouse_group(company)
|
set_company_to_warehouse(company)
|
||||||
|
make_warehouse_nestedset(company)
|
||||||
|
|
||||||
set_parent_to_warehouse(company)
|
elif len(sle_against_companies) > 1:
|
||||||
if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
|
make_warehouse_nestedset()
|
||||||
set_parent_to_warehouse_acount(company)
|
|
||||||
|
|
||||||
def set_parent_to_warehouse(company):
|
def check_is_warehouse_associated_with_company():
|
||||||
frappe.db.sql(""" update tabWarehouse set parent_warehouse = %s
|
warehouse_associcated_with_company = False
|
||||||
where (is_group = 0 or is_group is null or is_group = '') and company = %s
|
|
||||||
""",("{0} - {1}".format(_("All Warehouses"), company.abbr), company.name))
|
for warehouse in frappe.get_all("Warehouse", fields=["name", "company"]):
|
||||||
|
if warehouse.company:
|
||||||
|
warehouse_associcated_with_company = True
|
||||||
|
|
||||||
|
return warehouse_associcated_with_company
|
||||||
|
|
||||||
|
def make_warehouse_nestedset(company=None):
|
||||||
|
validate_parent_account_for_warehouse(company)
|
||||||
|
|
||||||
|
if company:
|
||||||
|
warehouse_group = "{0} - {1}".format(_("All Warehouses"), company.abbr)
|
||||||
|
ignore_mandatory = False
|
||||||
|
else:
|
||||||
|
warehouse_group = _("All Warehouses")
|
||||||
|
ignore_mandatory = True
|
||||||
|
|
||||||
|
if not frappe.db.get_value("Warehouse", warehouse_group):
|
||||||
|
create_default_warehouse_group(company, ignore_mandatory)
|
||||||
|
|
||||||
|
set_parent_to_warehouse(warehouse_group, company)
|
||||||
|
if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
|
||||||
|
set_parent_to_warehouse_acount(company)
|
||||||
|
|
||||||
|
def validate_parent_account_for_warehouse(company=None):
|
||||||
|
if not company:
|
||||||
|
return
|
||||||
|
|
||||||
|
if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
|
||||||
|
parent_account = frappe.db.sql("""select name from tabAccount
|
||||||
|
where account_type='Stock' and company=%s and is_group=1
|
||||||
|
and (warehouse is null or warehouse = '')""", company.name)
|
||||||
|
|
||||||
|
if not parent_account:
|
||||||
|
current_parent_accounts_for_warehouse = frappe.db.sql("""select parent_account from tabAccount
|
||||||
|
where account_type='Warehouse' and (warehouse is not null or warehouse != '') """)
|
||||||
|
|
||||||
|
if current_parent_accounts_for_warehouse:
|
||||||
|
frappe.db.set_value("Account", current_parent_accounts_for_warehouse[0][0], "account_type", "Stock")
|
||||||
|
|
||||||
|
def create_default_warehouse_group(company=None, ignore_mandatory=False):
|
||||||
|
wh = frappe.get_doc({
|
||||||
|
"doctype": "Warehouse",
|
||||||
|
"warehouse_name": _("All Warehouses"),
|
||||||
|
"is_group": 1,
|
||||||
|
"company": company.name if company else "",
|
||||||
|
"parent_warehouse": ""
|
||||||
|
})
|
||||||
|
|
||||||
|
if ignore_mandatory:
|
||||||
|
wh.flags.ignore_mandatory = ignore_mandatory
|
||||||
|
|
||||||
|
wh.insert(ignore_permissions=True)
|
||||||
|
|
||||||
|
def set_parent_to_warehouse(warehouse_group, company=None):
|
||||||
|
frappe.db.sql(""" update tabWarehouse set parent_warehouse = %s, is_group = 0
|
||||||
|
where (is_group = 0 or is_group is null or is_group = '') and ifnull(company, '') = %s
|
||||||
|
""",(warehouse_group, company.name if company else ""))
|
||||||
|
|
||||||
rebuild_tree("Warehouse", "parent_warehouse")
|
rebuild_tree("Warehouse", "parent_warehouse")
|
||||||
|
|
||||||
@ -31,25 +107,5 @@ def set_parent_to_warehouse_acount(company):
|
|||||||
|
|
||||||
rebuild_tree("Account", "parent_account")
|
rebuild_tree("Account", "parent_account")
|
||||||
|
|
||||||
def create_default_warehouse_group(company):
|
def set_company_to_warehouse(company):
|
||||||
frappe.get_doc({
|
frappe.db.sql("update tabWahouse set company=%s", company)
|
||||||
"doctype": "Warehouse",
|
|
||||||
"warehouse_name": _("All Warehouses"),
|
|
||||||
"is_group": 1,
|
|
||||||
"company": company.name,
|
|
||||||
"parent_warehouse": ""
|
|
||||||
}).insert(ignore_permissions=True)
|
|
||||||
|
|
||||||
def validate_parent_account_for_warehouse(company):
|
|
||||||
if cint(frappe.defaults.get_global_default("auto_accounting_for_stock")):
|
|
||||||
|
|
||||||
parent_account = frappe.db.sql("""select name from tabAccount
|
|
||||||
where account_type='Stock' and company=%s and is_group=1
|
|
||||||
and (warehouse is null or warehouse = '')""", company.name)
|
|
||||||
|
|
||||||
if not parent_account:
|
|
||||||
current_parent_accounts_for_warehouse = frappe.db.sql("""select parent_account from tabAccount
|
|
||||||
where account_type='Warehouse' and (warehouse is not null or warehouse != '') """)
|
|
||||||
|
|
||||||
if current_parent_accounts_for_warehouse:
|
|
||||||
frappe.db.set_value("Account", current_parent_accounts_for_warehouse[0][0], "account_type", "Stock")
|
|
||||||
|
@ -39,7 +39,7 @@
|
|||||||
"allow_on_submit": 0,
|
"allow_on_submit": 0,
|
||||||
"bold": 0,
|
"bold": 0,
|
||||||
"collapsible": 0,
|
"collapsible": 0,
|
||||||
"default": "1",
|
"default": "0",
|
||||||
"fieldname": "is_group",
|
"fieldname": "is_group",
|
||||||
"fieldtype": "Check",
|
"fieldtype": "Check",
|
||||||
"hidden": 0,
|
"hidden": 0,
|
||||||
@ -588,8 +588,8 @@
|
|||||||
"issingle": 0,
|
"issingle": 0,
|
||||||
"istable": 0,
|
"istable": 0,
|
||||||
"max_attachments": 0,
|
"max_attachments": 0,
|
||||||
"modified": "2016-06-26 17:39:16.856800",
|
"modified": "2016-06-29 19:47:24.336215",
|
||||||
"modified_by": "s@s.com",
|
"modified_by": "Administrator",
|
||||||
"module": "Stock",
|
"module": "Stock",
|
||||||
"name": "Warehouse",
|
"name": "Warehouse",
|
||||||
"owner": "Administrator",
|
"owner": "Administrator",
|
||||||
|
@ -11,9 +11,12 @@ class Warehouse(NestedSet):
|
|||||||
nsm_parent_field = 'parent_warehouse'
|
nsm_parent_field = 'parent_warehouse'
|
||||||
|
|
||||||
def autoname(self):
|
def autoname(self):
|
||||||
suffix = " - " + frappe.db.get_value("Company", self.company, "abbr")
|
if self.company:
|
||||||
if not self.warehouse_name.endswith(suffix):
|
suffix = " - " + frappe.db.get_value("Company", self.company, "abbr")
|
||||||
self.name = self.warehouse_name + suffix
|
if not self.warehouse_name.endswith(suffix):
|
||||||
|
self.name = self.warehouse_name + suffix
|
||||||
|
else:
|
||||||
|
self.name = self.warehouse_name
|
||||||
|
|
||||||
def onload(self):
|
def onload(self):
|
||||||
'''load account name for General Ledger Report'''
|
'''load account name for General Ledger Report'''
|
||||||
@ -31,6 +34,7 @@ class Warehouse(NestedSet):
|
|||||||
|
|
||||||
def update_parent_account(self):
|
def update_parent_account(self):
|
||||||
if not getattr(self, "__islocal", None) \
|
if not getattr(self, "__islocal", None) \
|
||||||
|
and cint(frappe.defaults.get_global_default("auto_accounting_for_stock")) \
|
||||||
and (self.create_account_under != frappe.db.get_value("Warehouse", self.name, "create_account_under")):
|
and (self.create_account_under != frappe.db.get_value("Warehouse", self.name, "create_account_under")):
|
||||||
|
|
||||||
self.validate_parent_account()
|
self.validate_parent_account()
|
||||||
@ -251,9 +255,10 @@ def get_children():
|
|||||||
is_group as expandable
|
is_group as expandable
|
||||||
from `tab{doctype}`
|
from `tab{doctype}`
|
||||||
where docstatus < 2
|
where docstatus < 2
|
||||||
and ifnull(`{parent_field}`,'') = %s and `company` = %s
|
and ifnull(`{parent_field}`,'') = %s
|
||||||
order by name""".format(doctype=frappe.db.escape(doctype), parent_field=frappe.db.escape(parent_field)),
|
and (`company` = %s or company is null or company = '')
|
||||||
(parent, company), as_dict=1)
|
order by name""".format(doctype=frappe.db.escape(doctype),
|
||||||
|
parent_field=frappe.db.escape(parent_field)), (parent, company), as_dict=1)
|
||||||
|
|
||||||
# return warehouses
|
# return warehouses
|
||||||
for wh in warehouses:
|
for wh in warehouses:
|
||||||
|
@ -13,7 +13,7 @@ frappe.treeview_settings['Warehouse'] = {
|
|||||||
fields:[
|
fields:[
|
||||||
{fieldtype:'Data', fieldname: 'name_field',
|
{fieldtype:'Data', fieldname: 'name_field',
|
||||||
label:__('New Warehouse Name'), reqd:true},
|
label:__('New Warehouse Name'), reqd:true},
|
||||||
{fieldtype:'Check', fieldname:'is_group', label:__('Group Node'),
|
{fieldtype:'Check', fieldname:'is_group', label:__('Is Group'),
|
||||||
description: __("Further nodes can be only created under 'Group' type nodes")}
|
description: __("Further nodes can be only created under 'Group' type nodes")}
|
||||||
],
|
],
|
||||||
onrender: function(node) {
|
onrender: function(node) {
|
||||||
|
Loading…
Reference in New Issue
Block a user