Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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