// ERPNext - web based ERP (http://erpnext.com) // Copyright (C) 2012 Web Notes Technologies Pvt Ltd // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . pscript['onload_Sales Browser'] = function(wrapper){ wn.require('lib/js/wn/ui/tree.js'); wrapper.appframe = new wn.ui.AppFrame($(wrapper).find('.appframe-area')); wrapper.appframe.add_button('Refresh', function() { wrapper.make_tree(); }, 'icon-refresh'); wrapper.make_tree = function() { var ctype = wn.get_route()[1] || 'Territory'; erpnext.sales_chart = new erpnext.SalesChart(ctype, wrapper); } wrapper.make_tree(); } pscript['onshow_Sales Browser'] = function(wrapper){ // set route var ctype = wn.get_route()[1] || 'Territory'; wrapper.appframe.title(ctype + ' Tree'); if(erpnext.sales_chart && erpnext.sales_chart.ctype != ctype) { wrapper.make_tree(); } }; 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' } $(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], args: {ctype: ctype}, method: 'selling.page.sales_browser.sales_browser.get_children', click: function(link) { if(me.cur_toolbar) $(me.cur_toolbar).toggle(false); if(!link.toolbar) me.make_link_toolbar(link); if(link.toolbar) { me.cur_toolbar = link.toolbar; $(me.cur_toolbar).toggle(true); } } }); this.tree.rootnode.$a .data('node-data', {value: root_nodes[ctype], expandable:1}) .click(); }, make_link_toolbar: function(link) { var data = $(link).data('node-data'); if(!data) return; link.toolbar = $('').insertAfter(link); // edit $('Edit').appendTo(link.toolbar); if(data.expandable) { link.toolbar.append(' | Add Child'); } }, new_node: function() { var me = this; // the dialog var d = new wn.ui.Dialog({ title:'New ' + me.ctype, fields: [ {fieldtype:'Data', fieldname: 'name_field', label:'New ' + me.ctype + ' Name', reqd:true}, {fieldtype:'Select', fieldname:'is_group', label:'Group Node', options:'No\nYes', description:'Entries can made only against non-group (leaf) nodes'}, {fieldtype:'Button', fieldname:'create_new', label:'Create New' } ] }) // create $(d.fields_dict.create_new.input).click(function() { var btn = this; $(btn).set_working(); var v = d.get_values(); if(!v) return; var node = me.selected_node(); v.parent = node.data('label'); v.ctype = me.ctype; wn.call({ method: 'selling.page.sales_browser.sales_browser.add_node', args: v, callback: function() { $(btn).done_working(); d.hide(); node.trigger('reload'); } }) }); d.show(); }, selected_node: function() { return this.tree.$w.find('.tree-link.selected'); } }); /* pscript['onshow_Sales Browser'] = function(){ wn.require('lib/js/legacy/widgets/tree.js'); var route = decodeURIComponent(location.hash); if(route.indexOf('/')!=-1) { var chart_type = route.split('/')[1]; new SalesBrowser().set_val(chart_type) return; } var parent = $i('tr_body'); parent.innerHTML = 'Please select your chart: ' var sel = $a(parent,'select'); add_sel_options(sel, ['Territory', 'Customer Group', 'Item Group', 'Sales Person'], 'Territory'); var btn = $btn(parent, 'Go', function() { new SalesBrowser().set_val(sel_val(sel)) }, {marginTop:'8px'}); } //================================= SalesBrowser Class ====================================== SalesBrowser = function(){ this.make_body = function(){ $i('tr_header').innerHTML = ''; $i('tr_body').innerHTML = ''; //make header var desc = this.sel; var me = this; var h = new PageHeader($i('tr_header'),desc); h.add_button('New '+this.sel, function() { me.set_dialog(1); }, 0, 'ui-icon-plus', 1); h.add_button('Refresh', function() { me.refresh_tree(); }, 0, 'ui-icon-refresh'); var div_body = $a($i('tr_body'),'div'); var tr_main_grid = make_table(div_body,1,2,'100%',['60%','40%'],{width: "100%", tableLayout: "fixed", borderCollapse: "collapse", border:"0px", padding:"4px 4px 4px 4px"}); $y($td(tr_main_grid,0,0),{border: "1px solid #dddddd", padding: "8px", width: "60%"}); this.tree_area = $a($td(tr_main_grid,0,0),'div','',{minHeight:'400px'}); $y($td(tr_main_grid,0,1),{border: "1px solid #DDD"}); this.detail_area = $a($td(tr_main_grid,0,1),'div'); this.make_tree_body(this.tree_area); this.refresh_tree(); } this.set_val = function(b){ var me = this; me.sel = b; me.make_body(); } } //================================================================================================================================= SalesBrowser.prototype.make_tree_body = function(parent){ //this.tab2 =make_table(this.wrapper,1,2,'100%',['60%','40%']); this.make_tree(); this.make_rgt_sect(); } //================================================================================================================================= SalesBrowser.prototype.make_rgt_sect=function(){ //var d = $a($td(this.tab2,0,1),'div','',{border:'1px solid #000'}); this.rgt_tab =make_table(this.detail_area,4,1,'','',{padding:"4px",spacing:"4px"}); this.dtl = $a($td(this.rgt_tab,0,0),'div'); this.btn = $a($td(this.rgt_tab,1,0),'div','span'); this.help = $a($td(this.rgt_tab,2,0),'div'); this.help.innerHTML = "Note: Explore and click on the tree node to see details." this.set_btn(); } //================================================================================================================================= SalesBrowser.prototype.set_btn = function(){ var me = this; this.edit_btn = $btn(this.btn,'Edit',function(){ me.set_dialog(2); }); this.trash_btn = $btn(this.btn,'Trash',null); this.trash_btn.onclick = function(){ var check = confirm("Are you sure you want to trash "+me.cur_node.rec.name+" node?"); if(check){ var arg = [me.cur_node.rec.name, me.sel]; $c_obj('Sales Browser Control','trash_record',arg.join(','),function(r,rt){ me.refresh_tree();}); } } } //===================================================== SalesBrowser.prototype.set_dialog = function(f){ if(this.sel == 'Territory') new MakeDialog('Territory','territory',f,this); //Territory Dialog if(this.sel == 'Customer Group') new MakeDialog('Customer Group','customer_group',f,this); //Customer Group Dialog if(this.sel == 'Item Group') new MakeDialog('Item Group','item_group',f,this); //Item Group Dialog if(this.sel == 'Sales Person') new MakeDialog('Sales Person','sales_person',f,this);//Sales Person Dialog } //=====================================================Make Tree============================================================================ SalesBrowser.prototype.make_tree = function() { var me = this; this.tree = new Tree(this.tree_area, '100%'); //--------------------------------------------------------------------------------------------------------------------------------- // on click this.tree.std_onclick = function(node) { me.cur_node = node; if(node.rec.name =='All Customer Groups' || node.rec.name =='All Sales Persons' || node.rec.name =='All Item Groups' || node.rec.name =='All Territories'){ //$di(me.add_btn); $dh(me.edit_btn); $dh(me.trash_btn); } else{ //$di(me.add_btn); //if(node.has_children == false) //$dh(me.add_btn); $di(me.edit_btn); $di(me.trash_btn); } me.make_details(); } //--------------------------------------------------------------------------------------------------------------------------------- // on expand this.tree.std_onexp = function(node) { if(node.expanded_once)return; $di(node.loading_div); var callback = function(r,rt) { $dh(node.loading_div); var n = me.tree.allnodes[r.message.parent]; var cl = r.message.cl; for(var i=0;i