// Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com) // // MIT License (MIT) // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // // ReportContainer Contains ReportBuilder objects for all DocTypes // - Only one ReportContainer exists // - Page header is als a part // - New ReportBuilder is made here _r.ReportContainer = function() { if(user=='Guest') { msgprint("Not Allowed"); return; } var page = wn.container.add_page("Report Builder"); this.wrapper = $a(page, 'div', 'layout-wrapper', {padding: '0px'}); this.appframe = new wn.views.AppFrame(this.wrapper); this.appframe.$titlebar.append(''); this.rb_area = $a(this.wrapper, 'div', '', {padding: '15px'}); var me = this; this.rb_dict = {}; var run_fn = function() { if(me.cur_rb){ me.cur_rb.dt.start_rec = 1; me.cur_rb.dt.run(); } } var runbtn = this.appframe.add_button('Run', run_fn, 'icon-refresh'); // refresh btn this.appframe.add_button('Export', function() { me.cur_rb && me.cur_rb.dt.do_export(); }, 'icon-download-alt'); this.appframe.add_button('Print', function() { me.cur_rb && me.cur_rb.dt.do_print(); }, 'icon-print'); this.appframe.add_button('Calc', function() { me.cur_rb && me.cur_rb.dt.do_calc(); }, 'icon-plus'); // new if(has_common(['Administrator', 'System Manager'], user_roles)) { // save var savebtn = this.appframe.add_button('Save', function() {if(me.cur_rb) me.cur_rb.save_criteria(); }); // advanced var fn = function() { if(me.cur_rb) { if(!me.cur_rb.current_loaded) { msgprint("error:You must save the report before you can set Advanced features"); return; } loaddoc('Search Criteria', me.cur_rb.sc_dict[me.cur_rb.current_loaded]); } }; var advancedbtn = this.appframe.add_button('Advanced Settings', fn, 'icon-cog'); } // set a type this.set_dt = function(dt, onload) { my_onload = function(f) { if(!f.forbidden) { me.cur_rb = f; me.cur_rb.mytabs.items['Result'].expand(); if(onload)onload(f); } } if(me.cur_rb) me.cur_rb.hide(); if(me.rb_dict[dt]){ me.rb_dict[dt].show(my_onload); } else { me.rb_dict[dt] = new _r.ReportBuilder(me.rb_area, dt, my_onload); } } } // =================================================================================== // + ReportBuilder // + Datatable (grid) // + ColumnPicker // + ReportFilter // // - Contains all methods relating to saving, loading and executing Search Criteria // - Contains ui objects of the report including tabs _r.ReportBuilder = function(parent, doctype, onload) { this.menuitems = {}; this.has_primary_filters = false; this.doctype = doctype; this.forbidden = 0; this.filter_fields = []; this.filter_fields_dict = {}; var me = this; this.fn_list = ['beforetableprint','beforerowprint','afterrowprint','aftertableprint','customize_filters','get_query']; this.wrapper = $a(parent, 'div', 'finder_wrapper'); this.make_tabs(); this.current_loaded = null; this.setup_doctype(onload); this.hide = function() { $dh(me.wrapper); } this.show = function(my_onload) { $ds(me.wrapper); // reset main title this.set_main_title('Report: ' + get_doctype_label(me.doctype)); if(my_onload)my_onload(me); } } // ------------------------------------------------------------------------------------- _r.ReportBuilder.prototype.make_tabs = function() { this.tab_wrapper = $a(this.wrapper, 'div', 'finder_tab_area'); this.mytabs = new TabbedPage(this.tab_wrapper); this.mytabs.add_item('Result', null, null, 1); this.mytabs.add_item('More Filters', null, null, 1); this.mytabs.add_item('Select Columns', null, null, 1); this.mytabs.tabs = this.mytabs.items; } // ------------------------------------------------------------------------------------- _r.ReportBuilder.prototype.make_body = function() { this.set_main_title('Report: ' + get_doctype_label(this.doctype)); var me = this; this.make_save_criteria(); this.column_picker = new _r.ReportColumnPicker(this); this.report_filters = new _r.ReportFilters(this); } // // Make list of all Criterias relating to this DocType // ------------------------------------------------------------------------------------- // Search Criterias are loaded with the DocType - put them in a list and dict _r.ReportBuilder.prototype.make_save_criteria = function() { var me = this; // make_list // --------- this.sc_list = []; this.sc_dict = {}; for(var n in locals['Search Criteria']) { var d = locals['Search Criteria'][n]; if(d.doc_type==this.doctype) { this.sc_list[this.sc_list.length] = d.criteria_name; this.sc_dict[d.criteria_name] = n; } } } // Save Criteria // ------------------------------------------------------------------------------------- _r.ReportBuilder.prototype.save_criteria = function(save_as) { var overwrite = 0; // is loaded? if(this.current_loaded && (!save_as)) { var overwrite = confirm('Do you want to overwrite the saved criteria "'+this.current_loaded+'"'); if(overwrite) { var doc = locals['Search Criteria'][this.sc_dict[this.current_loaded]]; var criteria_name = this.current_loaded; } } // new criteria if(!overwrite) { var criteria_name = prompt('Select a name for the criteria:', ''); if(!criteria_name) return; var dn = createLocal('Search Criteria'); var doc = locals['Search Criteria'][dn]; doc.criteria_name = criteria_name; doc.doc_type = this.doctype; } var cl = []; var fl = {}; // save columns var t = this.column_picker.get_selected(); for(var i=0;i