From 06cbd3427f4f378c28ccb2e927c0a743499aba14 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 12 Dec 2012 16:26:39 +0530 Subject: [PATCH] dynamic query for root in sales browser, validation for tree structure document --- accounts/doctype/account/account.js | 3 +-- accounts/doctype/cost_center/cost_center.js | 2 +- selling/page/sales_browser/sales_browser.js | 22 +++++++-------- selling/page/sales_browser/sales_browser.py | 5 +++- .../doctype/customer_group/customer_group.js | 2 +- .../doctype/customer_group/customer_group.py | 27 ++++++++++++------- setup/doctype/item_group/item_group.js | 15 +++++++++++ setup/doctype/sales_person/sales_person.js | 2 +- setup/doctype/sales_person/sales_person.py | 3 +++ setup/doctype/territory/territory.js | 8 +----- setup/doctype/territory/territory.py | 3 +++ 11 files changed, 59 insertions(+), 33 deletions(-) diff --git a/accounts/doctype/account/account.js b/accounts/doctype/account/account.js index d35d6ceb85..14e35d3094 100644 --- a/accounts/doctype/account/account.js +++ b/accounts/doctype/account/account.js @@ -39,8 +39,7 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { 'is_pl_account', 'company'], false); // read-only for root accounts - root_acc = doc.parent ? false : true; - if(in_list(root_acc, doc.account_name)) { + if(!doc.parent_account) { cur_frm.perm = [[1,0,0], [1,0,0]]; cur_frm.set_intro("This is a root account and cannot be edited."); } else { diff --git a/accounts/doctype/cost_center/cost_center.js b/accounts/doctype/cost_center/cost_center.js index 08253837e7..3dde661b92 100644 --- a/accounts/doctype/cost_center/cost_center.js +++ b/accounts/doctype/cost_center/cost_center.js @@ -20,7 +20,7 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { cur_frm.toggle_enable(['group_or_ledger', 'company_name'], doc.__islocal); if(!doc.__islocal && doc.group_or_ledger=='Group') { - intro_txt += '

Note: This is Cost Center is a Group, \ + intro_txt += '

Note: This Cost Center is a Group, \ Accounting Entries are not allowed against groups.

'; } diff --git a/selling/page/sales_browser/sales_browser.js b/selling/page/sales_browser/sales_browser.js index ce495dbebd..be43e67783 100644 --- a/selling/page/sales_browser/sales_browser.js +++ b/selling/page/sales_browser/sales_browser.js @@ -24,7 +24,14 @@ pscript['onload_Sales Browser'] = function(wrapper){ wrapper.make_tree = function() { var ctype = wn.get_route()[1] || 'Territory'; - erpnext.sales_chart = new erpnext.SalesChart(ctype, wrapper); + wn.call({ + method: 'selling.page.sales_browser.sales_browser.get_children', + args: {ctype: ctype}, + callback: function(r) { + var root = r.message[0]["value"]; + erpnext.sales_chart = new erpnext.SalesChart(ctype, root, wrapper); + } + }); } wrapper.make_tree(); @@ -42,20 +49,13 @@ pscript['onshow_Sales Browser'] = function(wrapper){ }; erpnext.SalesChart = Class.extend({ - init: function(ctype, wrapper) { - var root_nodes = { - 'Territory': 'All Territories', - 'Item Group': 'All Item Groups', - 'Customer Group': 'All Customer Groups', - 'Sales Person': 'All Sales Persons' - } - + init: function(ctype, root, wrapper) { $(wrapper).find('.tree-area').empty(); var me = this; me.ctype = ctype; this.tree = new wn.ui.Tree({ parent: $(wrapper).find('.tree-area'), - label: root_nodes[ctype], + label: root, args: {ctype: ctype}, method: 'selling.page.sales_browser.sales_browser.get_children', click: function(link) { @@ -72,7 +72,7 @@ erpnext.SalesChart = Class.extend({ } }); this.tree.rootnode.$a - .data('node-data', {value: root_nodes[ctype], expandable:1}) + .data('node-data', {value: root, expandable:1}) .click(); }, make_link_toolbar: function(link) { diff --git a/selling/page/sales_browser/sales_browser.py b/selling/page/sales_browser/sales_browser.py index 92c75e0fcd..49a05ff578 100644 --- a/selling/page/sales_browser/sales_browser.py +++ b/selling/page/sales_browser/sales_browser.py @@ -1,11 +1,14 @@ from __future__ import unicode_literals import webnotes + @webnotes.whitelist() def get_children(): ctype = webnotes.form_dict.get('ctype') webnotes.form_dict['parent_field'] = 'parent_' + ctype.lower().replace(' ', '_') - + if not webnotes.form_dict.get('parent'): + webnotes.form_dict['parent'] = '' + return webnotes.conn.sql("""select name as value, if(is_group='Yes', 1, 0) as expandable from `tab%(ctype)s` diff --git a/setup/doctype/customer_group/customer_group.js b/setup/doctype/customer_group/customer_group.js index 2b24c11efe..af0c6f0b44 100644 --- a/setup/doctype/customer_group/customer_group.js +++ b/setup/doctype/customer_group/customer_group.js @@ -20,7 +20,7 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { cur_frm.cscript.set_root_readonly = function(doc) { // read-only for root customer group - if(doc.name==='All Customer Groups') { + if(!doc.parent_customer_group) { cur_frm.perm = [[1,0,0], [1,0,0]]; cur_frm.set_intro("This is a root customer group and cannot be edited."); } else { diff --git a/setup/doctype/customer_group/customer_group.py b/setup/doctype/customer_group/customer_group.py index 0f68521198..cf126b8c07 100644 --- a/setup/doctype/customer_group/customer_group.py +++ b/setup/doctype/customer_group/customer_group.py @@ -31,20 +31,29 @@ class DocType(DocTypeNestedSet): self.nsm_parent_field = 'parent_customer_group'; def validate(self): - if sql("select name from `tabCustomer Group` where name = %s and docstatus = 2", (self.doc.customer_group_name)): + if sql("select name from `tabCustomer Group` where name = %s and docstatus = 2", + (self.doc.customer_group_name)): msgprint("""Another %s record is trashed. - To untrash please go to Setup & click on Trash."""%(self.doc.customer_group_name), raise_exception = 1) - self.validate_root_details("All Customer Groups", "parent_customer_group") + To untrash please go to Setup -> Recycle Bin.""" % + (self.doc.customer_group_name), raise_exception = 1) + + super(DocType, self).validate() + def on_trash(self): - cust = sql("select name from `tabCustomer` where ifnull(customer_group, '') = %s", self.doc.name) + cust = sql("select name from `tabCustomer` where ifnull(customer_group, '') = %s", + self.doc.name) cust = [d[0] for d in cust] if cust: - msgprint("""Customer Group: %s can not be trashed/deleted because it is used in customer: %s. - To trash/delete this, remove/change customer group in customer master""" % (self.doc.name, cust or ''), raise_exception=1) + msgprint("""Customer Group: %s can not be trashed/deleted \ + because it is used in customer: %s. + To trash/delete this, remove/change customer group in customer master""" % + (self.doc.name, cust or ''), raise_exception=1) - if sql("select name from `tabCustomer Group` where parent_customer_group = %s and docstatus != 2", self.doc.name): - msgprint("Child customer group exists for this customer group. You can not trash/cancel/delete this customer group.", raise_exception=1) + if sql("select name from `tabCustomer Group` where parent_customer_group = %s \ + and docstatus != 2", self.doc.name): + msgprint("Child customer group exists for this customer group. \ + You can not trash/cancel/delete this customer group.", raise_exception=1) # rebuild tree - super(DocType, self).on_trash() + super(DocType, self).on_trash() \ No newline at end of file diff --git a/setup/doctype/item_group/item_group.js b/setup/doctype/item_group/item_group.js index 3869eec00b..db894fa9d6 100644 --- a/setup/doctype/item_group/item_group.js +++ b/setup/doctype/item_group/item_group.js @@ -14,6 +14,21 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . + +cur_frm.cscript.refresh = function(doc, cdt, cdn) { + cur_frm.cscript.set_root_readonly(doc); +} + +cur_frm.cscript.set_root_readonly = function(doc) { + // read-only for root item group + if(!doc.parent_item_group) { + cur_frm.perm = [[1,0,0], [1,0,0]]; + cur_frm.set_intro("This is a root item group and cannot be edited."); + } else { + cur_frm.set_intro(null); + } +} + //get query select item group cur_frm.fields_dict['parent_item_group'].get_query = function(doc,cdt,cdn) { return 'SELECT `tabItem Group`.`name`,`tabItem Group`.`parent_item_group` FROM `tabItem Group` WHERE `tabItem Group`.`is_group` = "Yes" AND `tabItem Group`.`docstatus`!= 2 AND `tabItem Group`.`name` !="'+doc.item_group_name+'" AND `tabItem Group`.%(key)s LIKE "%s" ORDER BY `tabItem Group`.`name` ASC LIMIT 50'; diff --git a/setup/doctype/sales_person/sales_person.js b/setup/doctype/sales_person/sales_person.js index ba1a339ad2..c52948803f 100644 --- a/setup/doctype/sales_person/sales_person.js +++ b/setup/doctype/sales_person/sales_person.js @@ -20,7 +20,7 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { cur_frm.cscript.set_root_readonly = function(doc) { // read-only for root - if(doc.name==='All Sales Persons') { + if(!doc.parent_sales_person) { cur_frm.perm = [[1,0,0], [1,0,0]]; cur_frm.set_intro("This is a root sales person and cannot be edited."); } else { diff --git a/setup/doctype/sales_person/sales_person.py b/setup/doctype/sales_person/sales_person.py index 41d5042945..943e724c9b 100644 --- a/setup/doctype/sales_person/sales_person.py +++ b/setup/doctype/sales_person/sales_person.py @@ -33,3 +33,6 @@ class DocType(DocTypeNestedSet): if not flt(d.target_qty) and not flt(d.target_amount): webnotes.msgprint("Either target qty or target amount is mandatory.") raise Exception + + super(DocType, self).validate() + \ No newline at end of file diff --git a/setup/doctype/territory/territory.js b/setup/doctype/territory/territory.js index a04bea384f..2932006013 100644 --- a/setup/doctype/territory/territory.js +++ b/setup/doctype/territory/territory.js @@ -20,7 +20,7 @@ cur_frm.cscript.refresh = function(doc, cdt, cdn) { cur_frm.cscript.set_root_readonly = function(doc) { // read-only for root territory - if(doc.name==='All Territories') { + if(!doc.parent_territory) { cur_frm.perm = [[1,0,0], [1,0,0]]; cur_frm.set_intro("This is a root territory and cannot be edited."); } else { @@ -28,12 +28,6 @@ cur_frm.cscript.set_root_readonly = function(doc) { } } - -cur_frm.cscript.onload = function(){ - -} - - //get query select territory cur_frm.fields_dict['parent_territory'].get_query = function(doc,cdt,cdn) { return 'SELECT `tabTerritory`.`name`,`tabTerritory`.`parent_territory` FROM `tabTerritory` WHERE `tabTerritory`.`is_group` = "Yes" AND `tabTerritory`.`docstatus`!= 2 AND `tabTerritory`.`name` !="'+doc.territory_name+'" AND `tabTerritory`.%(key)s LIKE "%s" ORDER BY `tabTerritory`.`name` ASC LIMIT 50'; diff --git a/setup/doctype/territory/territory.py b/setup/doctype/territory/territory.py index 59c0b3a5d4..4b6468e663 100644 --- a/setup/doctype/territory/territory.py +++ b/setup/doctype/territory/territory.py @@ -33,3 +33,6 @@ class DocType(DocTypeNestedSet): if not flt(d.target_qty) and not flt(d.target_amount): msgprint("Either target qty or target amount is mandatory.") raise Exception + + super(DocType, self).validate() + \ No newline at end of file