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.
 
 
 
 
 
 

417 lines
12 KiB

  1. _p.show_dialog = function() {
  2. if(!_p.dialog) {
  3. _p.make_dialog();
  4. }
  5. _p.dialog.show();
  6. }
  7. _p.make_dialog = function() {
  8. var d = new Dialog(360, 140, "Print Formats");
  9. d.make_body(
  10. [['HTML','Select']
  11. ,['Check','No Letterhead','Will ignore letterhead if it can be set. May not work for all formats']
  12. ,['HTML', 'Buttons']
  13. ]);
  14. d.widgets['No Letterhead'].checked = 1;
  15. // prints
  16. $btn(d.widgets.Buttons, 'Print',
  17. function() { _p.build(sel_val(cur_frm.print_sel), _p.go, d.widgets['No Letterhead'].checked); },
  18. {cssFloat:'right', marginBottom:'16px', marginLeft:'7px'}, 'green');
  19. // preview
  20. $btn(d.widgets.Buttons, 'Preview',
  21. function() { _p.build(sel_val(cur_frm.print_sel), _p.preview, d.widgets['No Letterhead'].checked); },
  22. {cssFloat:'right', marginBottom:'16px'}, '');
  23. _p.dialog = d;
  24. d.onshow = function() {
  25. var c = d.widgets['Select'];
  26. if(c.cur_sel && c.cur_sel.parentNode == c)
  27. c.removeChild(c.cur_sel);
  28. c.appendChild(cur_frm.print_sel);
  29. c.cur_sel = cur_frm.print_sel;
  30. }
  31. }
  32. _p.field_tab = function(layout_cell) {
  33. var t = $a(layout_cell, 'table', '', {width:'100%'});
  34. var r = t.insertRow(0); this.r = r;
  35. r.insertCell(0); r.insertCell(1);
  36. r.cells[0].className='datalabelcell';
  37. r.cells[1].className='datainputcell';
  38. return r
  39. }
  40. // standard layout
  41. // ==========================================================================
  42. _p.print_std_add_table = function(t, layout, pf_list, dt, no_letterhead) {
  43. if(t.appendChild) {
  44. // one table only
  45. layout.cur_cell.appendChild(t);
  46. } else {
  47. // multiple tables
  48. for(var ti=0;ti<t.length-1;ti++) {
  49. // add to current page
  50. layout.cur_cell.appendChild(t[ti]);
  51. layout.close_borders();
  52. pf_list[pf_list.length] = '<div style="page-break-after: always;" class="page_break"></div>';
  53. // new page
  54. layout = _p.add_layout(dt, no_letterhead);
  55. pf_list[pf_list.length]=layout;
  56. layout.addrow();
  57. layout.addcell();
  58. var div = $a(layout.cur_cell, 'div');
  59. div.innerHTML = 'Continued from previous page...';
  60. div.style.padding = '4px';
  61. }
  62. // last table
  63. layout.cur_cell.appendChild(t[t.length-1]);
  64. }
  65. return layout;
  66. }
  67. // --------------------------------------------------------------------
  68. _p.print_std_add_field = function(dt, dn, f, layout) {
  69. var v = _f.get_value(dt,dn,f.fieldname);
  70. if(f.fieldtype!="Button") {
  71. if(!v && !in_list(['Float','Int','Currency'], f.fieldtype)) {
  72. // no value and non-numberic - do nothing
  73. } else {
  74. r = _p.field_tab(layout.cur_cell)
  75. // label
  76. r.cells[0].innerHTML=f.label?f.label:f.fieldname;
  77. $s(r.cells[1], v, f.fieldtype);
  78. // left align currency in normal display
  79. if(f.fieldtype=='Currency')
  80. $y(r.cells[1],{textAlign: 'left'});
  81. }
  82. }
  83. }
  84. _p.get_letter_head = function() {
  85. // add letter head
  86. var cp = locals['Control Panel']['Control Panel'];
  87. if(cur_frm.doc.letter_head)
  88. var lh= cstr(_p.letter_heads[cur_frm.doc.letter_head]);
  89. else if(cp.letter_head)
  90. var lh= cp.letter_head;
  91. else
  92. var lh= '';
  93. return lh;
  94. }
  95. // --------------------------------------------------------------------
  96. _p.add_layout = function(dt, no_letterhead) {
  97. var l = new Layout();
  98. l.addrow();
  99. if(locals['DocType'][dt].print_outline=='Yes') l.with_border = 1;
  100. return l;
  101. }
  102. // --------------------------------------------------------------------
  103. _p.print_std = function(no_letterhead) {
  104. var dn = cur_frm.docname;
  105. var dt = cur_frm.doctype;
  106. var pf_list = [];
  107. var layout = _p.add_layout(dt, no_letterhead);
  108. pf_list[pf_list.length]=layout;
  109. // heading
  110. var h1 = $a(layout.cur_row.header, 'h1', '', {fontSize:'22px', marginBottom:'8px'});
  111. h1.innerHTML = cur_frm.pformat[dn] ? cur_frm.pformat[dn] : get_doctype_label(dt);
  112. var h2 = $a(layout.cur_row.header, 'div', '', {fontSize:'16px', color:'#888', marginBottom:'8px', paddingBottom:'8px', borderBottom:(layout.with_border ? '0px' : '1px solid #000' )});
  113. h2.innerHTML = dn;
  114. var fl = getchildren('DocField', dt, 'fields', 'DocType');
  115. if(fl[0]&&fl[0].fieldtype!="Section Break") {
  116. layout.addrow(); // default section break
  117. if(fl[0].fieldtype!="Column Break") // without column too
  118. layout.addcell();
  119. }
  120. // build each field
  121. for(var i=0;i<fl.length;i++) {
  122. var fn = fl[i].fieldname?fl[i].fieldname:fl[i].label;
  123. if(fn)
  124. var f = get_field(dt, fn, dn);
  125. else
  126. var f = fl[i];
  127. if(!f.print_hide){
  128. // if there is a custom method to generate the HTML then use it
  129. if(cur_frm.pformat[f.fieldname]) {
  130. var tmp = $a(layout.cur_cell, 'div');
  131. tmp.innerHTML = cur_frm.pformat[f.fieldname](locals[dt][dn]);
  132. } else {
  133. // do the normal thing
  134. switch(f.fieldtype){
  135. case 'Section Break':
  136. layout.addrow();
  137. // if no column break after this field then add a column
  138. if(fl[i+1]&&(fl[i+1].fieldtype!='Column Break')) {
  139. layout.addcell(); }
  140. // add label ---- no labels for section breaks!
  141. //if(f.label) layout.cur_row.header.innerHTML = '<div class="sectionHeading">'+f.label+'</div>';
  142. break;
  143. case 'Column Break':
  144. layout.addcell(f.width, f.label);
  145. //if(f.label) layout.cur_cell.header.innerHTML = '<div class="columnHeading">'+f.label+'</div>';
  146. break;
  147. case 'Table':
  148. var t = print_table(dt, dn,f.fieldname,f.options,null,null,null,null);
  149. layout = _p.print_std_add_table(t, layout, pf_list, dt, no_letterhead);
  150. break;
  151. case 'HTML':
  152. var tmp = $a(layout.cur_cell, 'div');
  153. tmp.innerHTML = f.options;
  154. break;
  155. case 'Code':
  156. var tmp = $a(layout.cur_cell, 'div');
  157. var v= _f.get_value(dt,dn,f.fieldname);
  158. tmp.innerHTML = '<div>'+ f.label + ': </div>' + '<pre style="font-family: Courier, Fixed;">'+(v?v:'')+'</pre>';
  159. break;
  160. case 'Text Editor':
  161. var tmp = $a(layout.cur_cell, 'div');
  162. var v= _f.get_value(dt,dn,f.fieldname);
  163. tmp.innerHTML = v?v:'';
  164. break;
  165. default:
  166. // add cell data
  167. _p.print_std_add_field(dt, dn, f, layout);
  168. }
  169. }
  170. }
  171. }
  172. layout.close_borders();
  173. // build html for each page
  174. var html = '';
  175. for(var i=0;i<pf_list.length;i++) {
  176. if(pf_list[i].wrapper) {
  177. html += pf_list[i].wrapper.innerHTML;
  178. } else if(pf_list[i].innerHTML) {
  179. html += pf_list[i].innerHTML;
  180. } else {
  181. html += pf_list[i];
  182. }
  183. }
  184. pf_list = []; // cleanup
  185. return html;
  186. }
  187. _p.print_style = ".datalabelcell {padding: 2px 0px; width: 38%;vertical-align:top; }"
  188. +".datainputcell { padding: 2px 0px; width: 62%; text-align:left; }"
  189. +".sectionHeading { font-size: 16px; font-weight: bold; margin: 8px 0px }"
  190. +".columnHeading { font-size: 14px; font-weight: bold; margin: 8px 0px; }"
  191. _p.formats = {}
  192. _p.build = function(fmtname, onload, no_letterhead, only_body) {
  193. if(!cur_frm) { alert('No Document Selected'); return; }
  194. var doc = locals[cur_frm.doctype][cur_frm.docname];
  195. if(fmtname=='Standard') {
  196. onload(_p.render(_p.print_std(no_letterhead), _p.print_style, doc, doc.name, no_letterhead, only_body));
  197. } else {
  198. if(!_p.formats[fmtname]) // not loaded, get data
  199. $c('webnotes.widgets.form.get_print_format', {'name':fmtname },
  200. function(r,rt) {
  201. _p.formats[fmtname] = r.message;
  202. onload(_p.render(_p.formats[fmtname], '', doc, doc.name, no_letterhead, only_body));
  203. }
  204. );
  205. else // loaded
  206. onload(_p.render(_p.formats[fmtname], '', doc, doc.name, no_letterhead, only_body));
  207. }
  208. }
  209. _p.render = function(body, style, doc, title, no_letterhead, only_body) {
  210. var block = document.createElement('div');
  211. var tmp_html = '';
  212. if(doc && cint(doc.docstatus)==0 && cur_frm.perm[0][SUBMIT]) {
  213. var tmp_html = '<div style="text-align: center; padding: 8px; background-color: #CCC; "><div style="font-size: 20px; font-weight: bold; ">DRAFT</div>This box will go away after the document is submitted.</div>';
  214. }
  215. if(doc && doc.__archived) {
  216. var tmp_html = '<div style="text-align: center; padding: 8px; background-color: #CCC; "><div style="font-size: 20px; font-weight: bold; ">ARCHIVED</div>You must restore this document to make it editable.</div>';
  217. }
  218. style = (only_body ? '' : _p.def_print_style_body) + _p.def_print_style_other + style;
  219. block.innerHTML = body;
  220. // run embedded javascript
  221. var jslist = block.getElementsByTagName('script');
  222. while(jslist.length>0) {
  223. for(var i=0; i<jslist.length; i++) {
  224. var code = jslist[i].innerHTML;
  225. var p = jslist[i].parentNode;
  226. var sp = $a(p, 'span');
  227. p.replaceChild(sp, jslist[i]);
  228. var h = eval(code); if(!h)h='';
  229. sp.innerHTML = h;
  230. }
  231. jslist = block.getElementsByTagName('script');
  232. }
  233. // show letterhead?
  234. if(only_body)
  235. show_lh = false;
  236. else {
  237. if(!no_letterhead) show_lh = true;
  238. else show_lh = false;
  239. }
  240. // add letter head
  241. if(show_lh) {
  242. block.innerHTML = '<div>' + _p.get_letter_head() + '</div>' + block.innerHTML;
  243. }
  244. if(only_body) {
  245. return tmp_html + block.innerHTML.replace(/<td/g, '\n<td').replace(/<div/g, '\n<div');
  246. } else {
  247. // print block
  248. return '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n'
  249. + '<html><head>'
  250. + '<title>'+title+'</title>'
  251. + '<style>'+style+'</style>'
  252. + '</head><body>'
  253. + tmp_html
  254. + block.innerHTML.replace(/<td/g, '\n<td')
  255. + '</body></html>';
  256. }
  257. }
  258. print_table = function(dt, dn, fieldname, tabletype, cols, head_labels, widths, condition, cssClass) {
  259. var fl = fields_list[tabletype];
  260. var ds = getchildren(tabletype, dn, fieldname, dt);
  261. var tl = [];
  262. var cell_style = {border:'1px solid #000', padding:'2px', verticalAlign:'top'};
  263. var head_cell_style = {border:'1px solid #000', padding:'2px', verticalAlign:'top', backgroundColor:'#ddd'};
  264. var make_table = function(fl) {
  265. var w = document.createElement('div');
  266. var t = $a(w, 'table', '', {width:'100%', borderCollapse:'collapse', marginBottom:'10px'});
  267. t.wrapper = w;
  268. // head row
  269. t.insertRow(0);
  270. var c_start = 0;
  271. if(fl[0]=='SR') {
  272. var cell = t.rows[0].insertCell(0)
  273. cell.innerHTML = head_labels?head_labels[0]:'<b>Sr</b>';
  274. $y(cell, {width:'30px'});
  275. $y(cell, head_cell_style)
  276. c_start = 1;
  277. }
  278. for(var c=c_start;c<fl.length;c++) {
  279. var cell = t.rows[0].insertCell(c);
  280. $y(cell, head_cell_style)
  281. if(head_labels)
  282. cell.innerHTML = head_labels[c];
  283. else
  284. cell.innerHTML = fl[c].label;
  285. if(fl[c].width)
  286. $y(cell, {width:fl[c].width});
  287. if(widths)
  288. $y(cell, {width: widths[c]});
  289. if(fl[c].fieldtype=='Currency')
  290. $y(cell,{textAlign: 'right'});
  291. cell.style.fontWeight = 'bold';
  292. }
  293. return t;
  294. }
  295. // no headings if not entries
  296. if(!ds.length) return document.createElement('div');
  297. // make column list
  298. var newfl = [];
  299. if(cols&&cols.length) { // custom
  300. if(cols[0]=='SR')newfl[0]='SR';
  301. for(var i=0;i<cols.length;i++) {
  302. for(var j=0;j<fl.length;j++) {
  303. if(fl[j].fieldname==cols[i]) {
  304. newfl[newfl.length] = fl[j];
  305. break;
  306. }
  307. }
  308. }
  309. } else { // remove hidden cols
  310. newfl = ['SR']
  311. for(var j=0;j<fl.length;j++) {
  312. if(!fl[j].print_hide) {
  313. newfl[newfl.length] = fl[j];
  314. }
  315. }
  316. }
  317. fl = newfl;
  318. var t = make_table(fl);
  319. tl.push(t.wrapper);
  320. // setup for auto "Sr No" -> SR
  321. var c_start = 0;
  322. if(fl[0]=='SR') { c_start = 1; }
  323. // data
  324. var sr = 0;
  325. for(var r=0;r<ds.length;r++) {
  326. if((!condition)||(condition(ds[r]))) {
  327. // check for page break
  328. if(ds[r].page_break) { var t = make_table(fl); tl.push(t.wrapper); }
  329. var rowidx = t.rows.length;
  330. sr++
  331. var row = t.insertRow(rowidx);
  332. if(c_start) {
  333. var cell = row.insertCell(0);
  334. cell.innerHTML = sr;
  335. $y(cell, cell_style);
  336. }
  337. // add values
  338. for(var c=c_start;c<fl.length;c++) {
  339. var cell = row.insertCell(c);
  340. $y(cell, cell_style);
  341. $s(cell, ds[r][fl[c].fieldname], fl[c].fieldtype);
  342. if(fl[c].fieldtype=='Currency')
  343. cell.style.textAlign = 'right';
  344. }
  345. }
  346. }
  347. if(tl.length>1) return tl; // multiple tables with page breakes
  348. else return tl[0];
  349. }