You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

180 lines
5.5 KiB

  1. // Copyright (c) 2012 Web Notes Technologies Pvt Ltd (http://erpnext.com)
  2. //
  3. // MIT License (MIT)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a
  6. // copy of this software and associated documentation files (the "Software"),
  7. // to deal in the Software without restriction, including without limitation
  8. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. // and/or sell copies of the Software, and to permit persons to whom the
  10. // Software is furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  16. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
  20. // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. //
  22. _p.PrintQuery = function() {
  23. this.args = {};
  24. }
  25. _p.PrintQuery.prototype.show_dialog = function(args) {
  26. this.args = args;
  27. var me = this;
  28. if(!this.dialog) {
  29. var d = new Dialog(400, 300, "Print");
  30. d.make_body([
  31. ['Data', 'Max rows', 'Blank to print all rows'],
  32. ['Data', 'Rows per page'],
  33. ['Button', 'Go'],
  34. ]);
  35. d.widgets['Go'].onclick = function() {
  36. d.hide();
  37. me.render(cint(d.widgets['Max rows'].value), cint(d.widgets['Rows per page'].value))
  38. }
  39. d.onshow = function() {
  40. this.widgets['Rows per page'].value = '35';
  41. this.widgets['Max rows'].value = '500';
  42. }
  43. this.dialog = d;
  44. }
  45. this.dialog.show();
  46. }
  47. _p.PrintQuery.prototype.render = function(max_rows, page_len) {
  48. //q, title, colnames, colwidths, coltypes, has_index, check_limit, is_simple
  49. var me = this;
  50. var args = me.args;
  51. // limit for max rows
  52. if(cint(max_rows)!=0) args.query += ' LIMIT 0,' + cint(max_rows);
  53. if(!args.query) return;
  54. var callback = function(r, rt) {
  55. if(!r.values) { return; }
  56. if(!page_len) page_len = r.values.length;
  57. // add serial num column
  58. if(r.colnames && r.colnames.length)
  59. args.colnames = args.has_index ? add_lists(['Sr'],r.colnames) : r.colnames;
  60. if(r.colwidths && r.colwidths.length)
  61. args.colwidths = args.has_index ? add_lists(['25px'],r.colwidths) : r.colwidths;
  62. if(r.coltypes)
  63. args.coltypes = args.has_index ? add_lists(['Data'],r.coltypes) : r.coltypes;
  64. if(args.coltypes) {
  65. for(var i in args.coltypes)
  66. if(args.coltypes[i]=='Link') args.coltypes[i]='Data';
  67. }
  68. // fix widths to %
  69. if(args.colwidths) {
  70. var tw = 0;
  71. for(var i=0; i<args.colwidths.length; i++) tw+=cint(args.colwidths[i] ? args.colwidths[i]: 100);
  72. for(var i=0; i<args.colwidths.length; i++) args.colwidths[i]= cint(cint(args.colwidths[i] ? args.colwidths[i] : 100) / tw * 100) + '%';
  73. }
  74. var has_heading = args.colnames ? 1 : 0;
  75. if(!args.has_headings) has_heading = 0;
  76. var tl = []
  77. for(var st=0; st< r.values.length; st = st + page_len) {
  78. tl.push(me.build_table(r, st, page_len, has_heading, args.rb))
  79. }
  80. var html = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">'
  81. + '<html><head>'
  82. +'<title>'+args.title+'</title>'
  83. +'<style>'+_p.def_print_style_body + _p.def_print_style_other+'</style>'
  84. +'</head><body>'
  85. + (r.header_html ? r.header_html : '')
  86. + tl.join('\n<div style="page-break-after: always;"></div>\n')
  87. + (r.footer_html ? r.footer_html : '')
  88. +'</body></html>';
  89. _p.preview(html);
  90. }
  91. var out_args = copy_dict(args);
  92. if(args.is_simple) {
  93. out_args.simple_query = args.query;
  94. delete out_args.query;
  95. }
  96. // add filter values
  97. if(args.filter_values)
  98. out_args.filter_values = args.filter_values;
  99. $c('webnotes.widgets.query_builder.runquery', out_args, callback);
  100. }
  101. _p.PrintQuery.prototype.build_table = function(r, start, page_len, has_heading, rb) {
  102. // print a table
  103. var div = document.createElement('div');
  104. if(!r.page_template) {
  105. var head = $a(div,'div',null,{fontSize:'20px', fontWeight:'bold', margin:'16px 0px', borderBottom: '1px solid #CCC', paddingBottom:'8px'});
  106. head.innerHTML = args.title;
  107. }
  108. var m = start + page_len;
  109. if(m>r.values.length) m = r.values.length
  110. var t = make_table(div, m + has_heading - start, r.values[0].length + args.has_index, '100%', null);
  111. t.className = 'simpletable';
  112. if(args.colwidths)
  113. $y(t,{tableLayout:'fixed'});
  114. if(has_heading) {
  115. for(var i=0; i < args.colnames.length; i++) {
  116. $td(t,0,i).innerHTML = args.colnames[i].bold();
  117. if(args.colwidths && args.colwidths[i]) {
  118. $w($td(t,0,i),args.colwidths[i]);
  119. }
  120. }
  121. }
  122. for(var ri=start; ri<m; ri++) {
  123. // Sr No
  124. if(args.has_index)
  125. $td(t,ri+has_heading-start,0).innerHTML=ri+1;
  126. for(var ci=0; ci<r.values[0].length; ci++) {
  127. if(ri-start==0 && args.colwidths && args.colwidths[i]){
  128. $w($td(t,0,i), args.colwidths[i]); // colwidths for all
  129. }
  130. var c = $td(t,ri+has_heading-start,ci + args.has_index)
  131. c.div = $a(c, 'div','', {whiteSpace:'normal'});
  132. $s(
  133. c.div,
  134. r.values[ri][ci],
  135. args.coltypes ? args.coltypes[ci + args.has_index] : null
  136. );
  137. }
  138. }
  139. // user style
  140. if(r.style) {
  141. for(var i=0;i<r.style.length;i++) {
  142. $yt(t,r.style[i][0],r.style[i][1],r.style[i][2]);
  143. }
  144. }
  145. if(rb && rb.aftertableprint) {
  146. rb.aftertableprint(t);
  147. }
  148. if(r.page_template) return repl(r.page_template, {table:div.innerHTML});
  149. else return div.innerHTML;
  150. }