// 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. // wn.provide("wn.print"); // opts: // doctype (parent) // docname // tabletype // fieldname // show_all = false; wn.print.Table = Class.extend({ init: function(opts) { $.extend(this, opts); if(!this.columns) this.columns = this.get_columns(); this.data = this.get_data(); this.remove_empty_cols(); this.set_widths(); this.make(); }, get_columns: function() { var perms = wn.perm.get_perm(this.doctype, this.docname); return ['Sr'].concat($.map(wn.meta.docfield_list[this.tabletype], function(df) { return (cint(df.print_hide) || !(perms[df.permlevel] && perms[df.permlevel][READ])) ? null : df.fieldname; })); }, get_data: function() { var children = wn.model.get(this.tabletype, { parent:this.docname, parenttype:this.doctype, parentfield: this.fieldname}) var data = [] for(var i=0; i") var table = $("").css(this.table_style).appendTo(wrapper); var headrow = $("").appendTo(table); $.each(me.columns, function(ci, fieldname) { var df = wn.meta.docfield_map[me.tabletype][fieldname]; if(me.head_labels) { var label = me.head_labels[ci]; } else { var label = df ? df.label : fieldname; } var td = $("").appendTo(table); $.each(me.columns, function(ci, fieldname) { if(ci==0) var value = row.idx; else var value = row[fieldname]; var df = wn.meta.docfield_map[me.tabletype][fieldname]; value = wn.format(value, df, {for_print:true}); // set formatted value back into data so that modifer can use it row[fieldname] = value; // modifier is called after formatting so that // modifier's changes do not get lost in formatting (eg. 3.45%) if(me.modifier && me.modifier[fieldname]) value = me.modifier[fieldname](row); var td = $("
").html(label) .css(me.head_cell_style) .css({"width": me.widths[ci]}) .appendTo(headrow) if(df && in_list(['Float', 'Currency'], df.fieldtype)) { td.css({"text-align": "right"}); } }); $.each(data, function(ri, row) { var allow = true; if(me.condition) { allow = me.condition(row); } if(allow) { var tr = $("
").html(value) .css(me.cell_style) .css({width: me.widths[ci]}) .appendTo(tr); }); } }); this.tables.push(wrapper) }, set_widths: function() { var me = this; // if widths not passed (like in standard), // get from doctype and redistribute to fit 100% if(!this.widths) { this.widths = $.map(this.columns, function(fieldname, ci) { df = wn.meta.docfield_map[me.tabletype][fieldname]; return df && df.print_width || (fieldname=="Sr" ? 30 : 80); }); var sum = 0; $.each(this.widths, function(i, w) { sum += cint(w); }); this.widths = $.map(this.widths, function(w) { w = (flt(w) / sum * 100).toFixed(0); return (w < 5 ? 5 : w) + "%"; }); } }, get_tables: function() { if(this.tables.length > 1) { return $.map(this.tables, function(t) { return t.get(0); }); } else { return this.tables[0].get(0); } }, cell_style: { border: '1px solid #999', padding: '3px', 'vertical-align': 'top', 'word-wrap': 'break-word', }, head_cell_style: { border: '1px solid #999', padding: '3px', 'vertical-align': 'top', 'background-color': '#ddd', 'font-weight': 'bold', 'word-wrap': 'break-word', }, table_style: { width: '100%', 'border-collapse': 'collapse', 'margin-bottom': '10px', 'margin-top': '10px', 'table-layout': 'fixed' }, }) function print_table(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier) { return new wn.print.Table({ doctype: dt, docname: dn, fieldname: fieldname, tabletype: tabletype, columns: cols, head_labels: head_labels, widths: widths, condition: condition, cssClass: cssClass, modifier: modifier }).get_tables(); }