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.

print_table.js 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. wn.provide("wn.print");
  23. // opts:
  24. // doctype (parent)
  25. // docname
  26. // tabletype
  27. // fieldname
  28. // show_all = false;
  29. wn.print.Table = Class.extend({
  30. init: function(opts) {
  31. $.extend(this, opts);
  32. if(!this.columns)
  33. this.columns = this.get_columns();
  34. this.data = this.get_data();
  35. this.remove_empty_cols();
  36. this.set_widths();
  37. this.make();
  38. },
  39. get_columns: function() {
  40. return ['Sr'].concat($.map(wn.meta.docfield_list[this.tabletype], function(df) {
  41. return cint(df.print_hide) ? null : df.fieldname;
  42. }));
  43. },
  44. get_data: function() {
  45. var children = wn.model.get(this.tabletype, {
  46. parent:this.docname, parenttype:this.doctype, parentfield: this.fieldname})
  47. var data = []
  48. for(var i=0; i<children.length; i++) {
  49. data.push(copy_dict(children[i]));
  50. }
  51. return data;
  52. },
  53. remove_empty_cols: function() {
  54. var cols_with_value = [];
  55. var widths = [];
  56. var head_labels = [];
  57. var me = this;
  58. $.each(this.data, function(i, row) {
  59. $.each(me.columns, function(ci, fieldname) {
  60. var value = row[fieldname];
  61. if((value!==null && value!=="") || ci==0) {
  62. if(!in_list(cols_with_value, fieldname)) {
  63. cols_with_value.push(fieldname);
  64. // also prepare a new list of widths and head labels
  65. me.widths && widths.push(me.widths[ci]);
  66. me.head_labels && head_labels.push(me.head_labels[ci]);
  67. }
  68. }
  69. });
  70. });
  71. this.columns = cols_with_value;
  72. if(this.widths) this.widths = widths;
  73. if(this.head_labels) this.head_labels = head_labels;
  74. },
  75. make: function() {
  76. var me = this;
  77. this.tables = [];
  78. var table_data = [];
  79. $.each(this.data, function(i, d) {
  80. table_data.push(d);
  81. if(d.page_break) {
  82. me.add_table(table_data);
  83. table_data = [];
  84. }
  85. });
  86. if(table_data)
  87. me.add_table(table_data);
  88. },
  89. add_table: function(data) {
  90. var me = this;
  91. var wrapper = $("<div>")
  92. var table = $("<table>").css(this.table_style).appendTo(wrapper);
  93. var headrow = $("<tr>").appendTo(table);
  94. $.each(me.columns, function(ci, fieldname) {
  95. var df = wn.meta.docfield_map[me.tabletype][fieldname];
  96. if(me.head_labels) {
  97. var label = me.head_labels[ci];
  98. } else {
  99. var label = df ? df.label : fieldname;
  100. }
  101. var td = $("<td>").html(label)
  102. .css(me.head_cell_style)
  103. .css({"width": me.widths[ci]})
  104. .appendTo(headrow)
  105. if(ci==0) {
  106. td.css({"min-width": "30px"});
  107. }
  108. if(df && in_list(['Float', 'Currency'], df.fieldtype)) {
  109. td.css({"text-align": "right"});
  110. }
  111. });
  112. $.each(data, function(ri, row) {
  113. var allow = true;
  114. if(me.condition) {
  115. allow = me.condition(row);
  116. }
  117. if(allow) {
  118. var tr = $("<tr>").appendTo(table);
  119. $.each(me.columns, function(ci, fieldname) {
  120. if(ci==0)
  121. var value = row.idx;
  122. else
  123. var value = row[fieldname];
  124. var df = wn.meta.docfield_map[me.tabletype][fieldname];
  125. value = wn.form.get_formatter(
  126. df && df.fieldtype || "Data")(value);
  127. if(me.modifier && me.modifier[fieldname])
  128. value = me.modifier[fieldname](row);
  129. var td = $("<td>").html(value)
  130. .css(me.cell_style)
  131. .css({width: me.widths[ci]})
  132. .appendTo(tr);
  133. if(ci==0) {
  134. td.css({"min-width": "30px"});
  135. }
  136. });
  137. }
  138. });
  139. this.tables.push(wrapper)
  140. },
  141. set_widths: function() {
  142. var me = this;
  143. // if widths not passed (like in standard),
  144. // get from doctype and redistribute to fit 100%
  145. if(!this.widths) {
  146. this.widths = $.map(this.columns, function(fieldname, ci) {
  147. df = wn.meta.docfield_map[me.tabletype][fieldname];
  148. return df && df.width || (fieldname=="Sr" ? 30 : 80);
  149. });
  150. var sum = 0;
  151. $.each(this.widths, function(i, w) {
  152. sum += cint(w);
  153. });
  154. this.widths = $.map(this.widths, function(w) {
  155. return (flt(w) / sum * 100).toFixed(0) + "%";
  156. });
  157. }
  158. },
  159. get_tables: function() {
  160. if(this.tables.length > 1) {
  161. return $.map(this.tables, function(t) {
  162. return t.get(0);
  163. });
  164. } else {
  165. return this.tables[0].get(0);
  166. }
  167. },
  168. cell_style: {
  169. border: '1px solid #999',
  170. padding: '3px',
  171. 'vertical-align': 'top',
  172. 'word-wrap': 'break-word',
  173. },
  174. head_cell_style: {
  175. border: '1px solid #999',
  176. padding: '3px',
  177. 'vertical-align': 'top',
  178. 'background-color': '#ddd',
  179. 'font-weight': 'bold',
  180. 'word-wrap': 'break-word',
  181. },
  182. table_style: {
  183. width: '100%',
  184. 'border-collapse': 'collapse',
  185. 'margin-bottom': '10px',
  186. 'margin-top': '10px',
  187. 'table-layout': 'fixed'
  188. },
  189. })
  190. function print_table(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier) {
  191. return new wn.print.Table({
  192. doctype: dt,
  193. docname: dn,
  194. fieldname: fieldname,
  195. tabletype: tabletype,
  196. columns: cols,
  197. head_labels: head_labels,
  198. widths: widths,
  199. condition: condition,
  200. cssClass: cssClass,
  201. modifier: modifier
  202. }).get_tables();
  203. }