25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

209 satır
5.2 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[i];
  91. } else {
  92. var df = wn.meta.docfield_map[me.tabletype][fieldname];
  93. var label = df ? df.label : fieldname;
  94. }
  95. $("<td>").html(label)
  96. .appendTo(headrow)
  97. .css(me.head_cell_style)
  98. .css({"width": me.widths[ci] + "%"});
  99. })
  100. $.each(data, function(ri, row) {
  101. var allow = true;
  102. if(me.condition) {
  103. allow = me.condition(row);
  104. }
  105. if(allow) {
  106. var tr = $("<tr>").appendTo(table);
  107. $.each(me.columns, function(ci, fieldname) {
  108. if(ci==0)
  109. var value = ri + 1;
  110. else
  111. var value = row[fieldname];
  112. if(me.modifier && me.modifier[fieldname])
  113. value = me.modifier[fieldname](row);
  114. var df = wn.meta.docfield_map[me.tabletype][fieldname];
  115. value = wn.form.get_formatter(
  116. df && df.fieldtype || "Data")(value);
  117. $("<td>").html(value)
  118. .css(me.cell_style)
  119. .appendTo(tr);
  120. });
  121. }
  122. });
  123. this.tables.push(wrapper)
  124. },
  125. set_widths: function() {
  126. var me = this;
  127. if(!this.widths) {
  128. this.widths = $.map(this.columns, function(fieldname, ci) {
  129. df = wn.meta.docfield_map[me.tabletype][fieldname];
  130. return df && df.width ||
  131. (fieldname=="Sr" ? 20 : 80);
  132. })
  133. }
  134. var sum = 0;
  135. $.each(this.widths, function(i, w) {
  136. sum += cint(w);
  137. });
  138. this.widths = $.map(this.widths, function(w) {
  139. return (flt(w) / sum * 100).toFixed(0);
  140. });
  141. },
  142. get_tables: function() {
  143. if(this.tables.length > 1) {
  144. return $.map(this.tables, function(t) {
  145. return t.get(0);
  146. });
  147. } else {
  148. return this.tables[0].get(0);
  149. }
  150. },
  151. cell_style: {
  152. border: '1px solid #999',
  153. padding: '3px',
  154. 'vertical-align': 'top',
  155. 'word-wrap': 'break-word',
  156. },
  157. head_cell_style: {
  158. border: '1px solid #999',
  159. padding: '3px',
  160. 'vertical-align': 'top',
  161. 'background-color': '#ddd',
  162. 'font-weight': 'bold',
  163. 'word-wrap': 'break-word',
  164. },
  165. table_style: {
  166. width: '100%',
  167. 'border-collapse': 'collapse',
  168. 'margin-bottom': '10px',
  169. 'margin-top': '10px',
  170. 'table-layout': 'fixed'
  171. },
  172. })
  173. function print_table(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass, modifier, hide_empty) {
  174. return new wn.print.Table({
  175. doctype: dt,
  176. docname: dn,
  177. fieldname: fieldname,
  178. tabletype: tabletype,
  179. columns: cols,
  180. head_labels: head_labels,
  181. widths: widths,
  182. condition: condition,
  183. cssClass: cssClass,
  184. modifier: modifier
  185. }).get_tables();
  186. }