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.
 
 
 
 
 
 

692 lines
18 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. // default print style
  23. _p.def_print_style_body = "html, body, div, span, td, p { \
  24. font-family: inherit; \
  25. font-size: inherit; \
  26. }\
  27. .page-settings {\
  28. font-family: Arial, Helvetica Neue, Sans;\
  29. font-size: 9pt;\
  30. }\
  31. pre { margin:0; padding:0;}";
  32. _p.def_print_style_other = "\n.simpletable, .noborder { \
  33. border-collapse: collapse;\
  34. margin-bottom: 10px;\
  35. }\
  36. .simpletable td {\
  37. border: 1pt solid #777;\
  38. vertical-align: top;\
  39. padding: 4px;\
  40. }\
  41. .noborder td {\
  42. vertical-align: top;\
  43. }";
  44. _p.go = function(html) {
  45. var d = document.createElement('div')
  46. d.innerHTML = html
  47. $(d).printElement();
  48. }
  49. _p.preview = function(html) {
  50. var w = window.open('');
  51. if(!w) return;
  52. w.document.write(html)
  53. w.document.close();
  54. }
  55. // _p can be referenced as this inside $.extend
  56. $.extend(_p, {
  57. show_dialog: function() {
  58. if(!_p.dialog) {
  59. _p.make_dialog();
  60. }
  61. _p.dialog.show();
  62. },
  63. make_dialog: function() {
  64. // Prepare Dialog Box Layout
  65. var d = new Dialog(
  66. 360, // w
  67. 140, // h
  68. 'Print Formats', // title
  69. [ // content
  70. ['HTML', 'Select'],
  71. ['Check', 'No Letterhead'],
  72. ['HTML', 'Buttons']
  73. ]);
  74. //d.widgets['No Letterhead'].checked = 1;
  75. // Print Button
  76. $(d.widgets.Buttons).css({"height": "60px"})
  77. $btn(d.widgets.Buttons, 'Print', function() {
  78. _p.build(
  79. sel_val(cur_frm.print_sel), // fmtname
  80. _p.go, // onload
  81. d.widgets['No Letterhead'].checked // no_letterhead
  82. );
  83. },
  84. {
  85. cssFloat: 'right',
  86. marginBottom: '16px',
  87. marginLeft: '7px',
  88. }, 'green');
  89. // Print Preview
  90. $btn(d.widgets.Buttons, 'Preview', function() {
  91. _p.build(
  92. sel_val(cur_frm.print_sel), // fmtname
  93. _p.preview, // onload
  94. d.widgets['No Letterhead'].checked // no_letterhead
  95. );
  96. },
  97. {
  98. cssFloat: 'right',
  99. marginBottom: '16px'
  100. }, '');
  101. // Delete previous print format select list and Reload print format list from current form
  102. d.onshow = function() {
  103. var c = _p.dialog.widgets['Select'];
  104. if(c.cur_sel && c.cur_sel.parentNode == c) {
  105. c.removeChild(c.cur_sel);
  106. }
  107. c.appendChild(cur_frm.print_sel);
  108. c.cur_sel = cur_frm.print_sel;
  109. }
  110. _p.dialog = d;
  111. },
  112. // Define formats dict
  113. formats: {},
  114. /* args dict can contain:
  115. + fmtname --> print format name
  116. + onload
  117. + no_letterhead
  118. + only_body
  119. */
  120. build: function(fmtname, onload, no_letterhead, only_body, no_heading) {
  121. if(!fmtname) {
  122. fmtname= "Standard";
  123. }
  124. args = {
  125. fmtname: fmtname,
  126. onload: onload,
  127. no_letterhead: no_letterhead,
  128. only_body: only_body
  129. };
  130. if(!cur_frm) {
  131. alert('No Document Selected');
  132. return;
  133. }
  134. // Get current doc (record)
  135. var doc = locals[cur_frm.doctype][cur_frm.docname];
  136. if(args.fmtname == 'Standard') {
  137. args.onload(_p.render({
  138. body: _p.print_std(args.no_letterhead, no_heading),
  139. style: _p.print_style,
  140. doc: doc,
  141. title: doc.name,
  142. no_letterhead: args.no_letterhead,
  143. no_heading: no_heading,
  144. only_body: args.only_body
  145. }));
  146. } else {
  147. var print_format_doc = locals["Print Format"][args.fmtname];
  148. if(!print_format_doc) {
  149. msgprint("Unknown Print Format: " + args.fmtname);
  150. return;
  151. }
  152. args.onload(_p.render({
  153. body: print_format_doc.html,
  154. style: '',
  155. doc: doc,
  156. title: doc.name,
  157. no_letterhead: args.no_letterhead,
  158. no_heading: no_heading,
  159. only_body: args.only_body
  160. }));
  161. }
  162. },
  163. render: function(args) {
  164. var container = document.createElement('div');
  165. var stat = '';
  166. if(!args.no_heading) {
  167. // if draft/archived, show draft/archived banner
  168. stat += _p.show_draft(args);
  169. stat += _p.show_archived(args);
  170. stat += _p.show_cancelled(args);
  171. }
  172. // Append args.body's content as a child of container
  173. container.innerHTML = args.body;
  174. // Show letterhead?
  175. _p.show_letterhead(container, args);
  176. _p.run_embedded_js(container, args.doc);
  177. var style = _p.consolidate_css(container, args);
  178. _p.render_header_on_break(container, args);
  179. return _p.render_final(style, stat, container, args);
  180. },
  181. head_banner_format: function() {
  182. return "\
  183. <div style = '\
  184. text-align: center; \
  185. padding: 8px; \
  186. background-color: #CCC;'> \
  187. <div style = '\
  188. font-size: 20px; \
  189. font-weight: bold;'>\
  190. {{HEAD}}\
  191. </div>\
  192. {{DESCRIPTION}}\
  193. </div>"
  194. },
  195. /*
  196. Check if doc's status is not submitted (docstatus == 0)
  197. and submission is pending
  198. Display draft in header if true
  199. */
  200. show_draft: function(args) {
  201. var is_doctype_submittable = 0;
  202. var plist = locals['DocPerm'];
  203. for(var perm in plist) {
  204. var p = plist[perm];
  205. if((p.parent==args.doc.doctype) && (p.submit==1)){
  206. is_doctype_submittable = 1;
  207. break;
  208. }
  209. }
  210. if(args.doc && cint(args.doc.docstatus)==0 && is_doctype_submittable) {
  211. draft = _p.head_banner_format();
  212. draft = draft.replace("{{HEAD}}", "DRAFT");
  213. draft = draft.replace("{{DESCRIPTION}}", "This box will go away after the document is submitted.");
  214. return draft;
  215. } else {
  216. return "";
  217. }
  218. },
  219. /*
  220. Check if doc is archived
  221. Display archived in header if true
  222. */
  223. show_archived: function(args) {
  224. if(args.doc && args.doc.__archived) {
  225. archived = _p.head_banner_format();
  226. archived = archived.replace("{{HEAD}}", "ARCHIVED");
  227. archived = archived.replace("{{DESCRIPTION}}", "You must restore this document to make it editable.");
  228. return archived;
  229. } else {
  230. return "";
  231. }
  232. },
  233. /*
  234. Check if doc is cancelled
  235. Display cancelled in header if true
  236. */
  237. show_cancelled: function(args) {
  238. if(args.doc && args.doc.docstatus==2) {
  239. cancelled = _p.head_banner_format();
  240. cancelled = cancelled.replace("{{HEAD}}", "CANCELLED");
  241. cancelled = cancelled.replace("{{DESCRIPTION}}", "You must amend this document to make it editable.");
  242. return cancelled;
  243. } else {
  244. return "";
  245. }
  246. },
  247. consolidate_css: function(container, args) {
  248. // Extract <style> content from container
  249. var body_style = '';
  250. var style_list = container.getElementsByTagName('style');
  251. while(style_list && style_list.length>0) {
  252. for(i in style_list) {
  253. if(style_list[i] && style_list[i].innerHTML) {
  254. body_style += style_list[i].innerHTML;
  255. var parent = style_list[i].parentNode;
  256. if(parent) {
  257. parent.removeChild(style_list[i]);
  258. } else {
  259. container.removeChild(style_list[i]);
  260. }
  261. }
  262. }
  263. style_list = container.getElementsByTagName('style');
  264. }
  265. // Concatenate all styles
  266. style_concat = (args.only_body ? '' : _p.def_print_style_body)
  267. + _p.def_print_style_other + args.style + body_style;
  268. return style_concat;
  269. },
  270. // This is used to calculate and substitude values in the HTML
  271. run_embedded_js: function(container, doc) {
  272. script_list = $(container).find("script");
  273. for(var i=0; i<script_list.length; i++) {
  274. var element = script_list[i];
  275. var code = element.innerHTML;
  276. var new_html = code ? (eval(code) || "") : "";
  277. if(in_list(["string", "number"], typeof new_html)) {
  278. $(element).replaceWith(this.add_span(new_html + ""));
  279. }
  280. }
  281. },
  282. add_span: function(html) {
  283. var tags = ["<span[^>]>", "<p[^>]>", "<div[^>]>", "<br[^>]>", "<table[^>]>"];
  284. var match = false;
  285. for(var i=0; i<tags.length; i++) {
  286. if(html.match(tags[i])) {
  287. match = true;
  288. }
  289. }
  290. if(!match) {
  291. html = "<span>" + html + "</span>";
  292. }
  293. return html;
  294. },
  295. // Attach letterhead at top of container
  296. show_letterhead: function(container, args) {
  297. if(!(args.no_letterhead || args.only_body)) {
  298. container.innerHTML = '<div>' + _p.get_letter_head() + '</div>'
  299. + container.innerHTML;
  300. }
  301. },
  302. render_header_on_break: function(container, args) {
  303. var page_set = container.getElementsByClassName('page-settings');
  304. if(page_set.length) {
  305. for(var i = 0; i < page_set.length; i++) {
  306. var tmp = '';
  307. // if draft/archived, show draft/archived banner
  308. tmp += _p.show_draft(args);
  309. tmp += _p.show_archived(args);
  310. _p.show_letterhead(page_set[i], args);
  311. page_set[i].innerHTML = tmp + page_set[i].innerHTML;
  312. }
  313. }
  314. },
  315. // called by _p.render for final render of print
  316. render_final: function(style, stat, container, args) {
  317. if(!args.only_body) {
  318. var header = '<!DOCTYPE html>\
  319. <html>\
  320. <head>\
  321. <meta charset="utf-8" />\
  322. <title>' + args.title + '</title>\
  323. <style>' + style + '</style>\
  324. </head>\
  325. <body>';
  326. var footer = '\
  327. </body>\
  328. </html>';
  329. } else {
  330. var header = '';
  331. var footer = '';
  332. }
  333. var finished = header
  334. + '<div class="page-settings">'
  335. + stat
  336. + container.innerHTML
  337. + '</div>'
  338. + footer;
  339. // replace relative links by absolute links
  340. var prefix = window.location.href.split("app.html")[0]
  341. // find unique matches
  342. var matches = $.unique(finished.match(/src=['"]([^'"]*)['"]/g) || []);
  343. $.each(matches, function(i, v) {
  344. if(v.substr(0,4)=="src=") {
  345. var v = v.substr(5, v.length-6);
  346. if(v.substr(0,4)!="http")
  347. finished = finished.split(v).join(prefix + v);
  348. }
  349. });
  350. return finished;
  351. },
  352. // fetches letter head from current doc or control panel
  353. get_letter_head: function() {
  354. var cp = wn.control_panel;
  355. var lh = '';
  356. if(cur_frm.doc.letter_head) {
  357. lh = cstr(wn.boot.letter_heads[cur_frm.doc.letter_head]);
  358. } else if (cp.letter_head) {
  359. lh = cp.letter_head;
  360. }
  361. return lh;
  362. },
  363. // common print style setting
  364. print_style: "\
  365. .datalabelcell { \
  366. padding: 2px 0px; \
  367. width: 38%; \
  368. vertical-align: top; \
  369. } \
  370. .datainputcell { \
  371. padding: 2px 0px; \
  372. width: 62%; \
  373. text-align: left; \
  374. }\
  375. .sectionHeading { \
  376. font-size: 16px; \
  377. font-weight: bold; \
  378. margin: 8px 0px; \
  379. } \
  380. .columnHeading { \
  381. font-size: 14px; \
  382. font-weight: bold; \
  383. margin: 8px 0px; \
  384. }",
  385. print_std: function(no_letterhead, no_heading) {
  386. // Get doctype, docname, layout for a doctype
  387. var docname = cur_frm.docname;
  388. var doctype = cur_frm.doctype;
  389. var data = getchildren('DocField', doctype, 'fields', 'DocType');
  390. var layout = _p.add_layout(doctype);
  391. this.pf_list = [layout];
  392. var me = this;
  393. me.layout = layout;
  394. $.extend(this, {
  395. build_head: function(data, doctype, docname) {
  396. // Heading
  397. var h1_style = {
  398. fontSize: '22px',
  399. marginBottom: '8px'
  400. }
  401. var h1 = $a(me.layout.cur_row.header, 'h1', '', h1_style);
  402. // Get print heading
  403. if (cur_frm.pformat[docname]) {
  404. // first check in cur_frm.pformat
  405. h1.innerHTML = cur_frm.pformat[docname];
  406. } else {
  407. // then check if select print heading exists and has a value
  408. var val = null;
  409. for (var i = 0; i < data.length; i++) {
  410. if (data[i].fieldname === 'select_print_heading') {
  411. val = _f.get_value(doctype, docname, data[i].fieldname);
  412. break;
  413. }
  414. }
  415. // if not, just have doctype has heading
  416. h1.innerHTML = val ? val : wn._(doctype);
  417. }
  418. var h2_style = {
  419. fontSize: '16px',
  420. color: '#888',
  421. marginBottom: '8px',
  422. paddingBottom: '8px',
  423. borderBottom: (me.layout.with_border ? '0px' :
  424. '1px solid #000')
  425. }
  426. var h2 = $a(me.layout.cur_row.header, 'div', '', h2_style);
  427. h2.innerHTML = docname;
  428. if(cur_frm.state_fieldname) {
  429. $a(h2, 'br');
  430. var span = $a(h2, 'span', '',
  431. {padding: "3px", color: "#fff", backgroundColor: "#777",
  432. display:"inline-block"});
  433. span.innerHTML = cur_frm.doc[cur_frm.state_fieldname];
  434. }
  435. },
  436. build_data: function(data, doctype, docname) {
  437. // Start with a row and a cell in that row
  438. if(data[0] && data[0].fieldtype != "Section Break") {
  439. me.layout.addrow();
  440. if(data[0].fieldtype != "Column Break") {
  441. me.layout.addcell();
  442. }
  443. }
  444. $.extend(this, {
  445. generate_custom_html: function(field, doctype, docname) {
  446. var container = $a(me.layout.cur_cell, 'div');
  447. container.innerHTML = cur_frm.pformat[field.fieldname](locals[doctype][docname]);
  448. },
  449. render_normal: function(field, data, i) {
  450. switch(field.fieldtype) {
  451. case 'Section Break':
  452. me.layout.addrow();
  453. // Add column if no column break after this field
  454. if(data[i+1] && data[i+1].fieldtype !=
  455. 'Column Break') {
  456. me.layout.addcell();
  457. }
  458. break;
  459. case 'Column Break':
  460. me.layout.addcell(field.width, field.label);
  461. break;
  462. case 'Table':
  463. var table = print_table(
  464. doctype, // dt
  465. docname, // dn
  466. field.fieldname,
  467. field.options, // tabletype
  468. null, // cols
  469. null, // head_labels
  470. null, // widths
  471. null); // condition
  472. me.layout = _p.print_std_add_table(table, me.layout, me.pf_list, doctype, no_letterhead);
  473. break;
  474. case 'HTML':
  475. var div = $a(me.layout.cur_cell, 'div');
  476. div.innerHTML = field.options;
  477. break;
  478. case 'Code':
  479. var div = $a(me.layout.cur_cell, 'div');
  480. var val = _f.get_value(doctype, docname,
  481. field.fieldname);
  482. div.innerHTML = '<div>' + field.label +
  483. ': </div><pre style="font-family: Courier, Fixed;">' + (val ? val : '') +
  484. '</pre>';
  485. break;
  486. case 'Text Editor':
  487. var div = $a(me.layout.cur_cell, 'div');
  488. var val = _f.get_value(doctype, docname,
  489. field.fieldname);
  490. div.innerHTML = val ? val : '';
  491. break;
  492. default:
  493. // Add Cell Data
  494. _p.print_std_add_field(doctype, docname, field, me.layout);
  495. break;
  496. }
  497. }
  498. });
  499. // Then build each field
  500. for(var i = 0; i < data.length; i++) {
  501. var fieldname = data[i].fieldname ? data[i].fieldname :
  502. data[i].label;
  503. var field = fieldname ?
  504. wn.meta.get_docfield(doctype, fieldname, docname) : data[i];
  505. if(!field.print_hide) {
  506. if(cur_frm.pformat[field.fieldname]) {
  507. // If there is a custom method to generate the HTML, then use it
  508. this.generate_custom_html(field, doctype, docname);
  509. } else {
  510. // Do the normal rendering
  511. this.render_normal(field, data, i);
  512. }
  513. }
  514. }
  515. me.layout.close_borders();
  516. },
  517. build_html: function() {
  518. var html = '';
  519. for(var i = 0; i < me.pf_list.length; i++) {
  520. if(me.pf_list[i].wrapper) {
  521. html += me.pf_list[i].wrapper.innerHTML;
  522. } else if(me.pf_list[i].innerHTML) {
  523. html += me.pf_list[i].innerHTML;
  524. } else {
  525. html += me.pf_list[i];
  526. }
  527. }
  528. this.pf_list = [];
  529. return html;
  530. }
  531. });
  532. if(!no_heading) {
  533. this.build_head(data, doctype, docname);
  534. }
  535. this.build_data(data, doctype, docname);
  536. var html = this.build_html();
  537. return html;
  538. },
  539. add_layout: function(doctype) {
  540. var layout = new Layout();
  541. layout.addrow();
  542. if(locals['DocType'][doctype].print_outline == 'Yes') {
  543. layout.with_border = 1
  544. }
  545. return layout;
  546. },
  547. print_std_add_table: function(t, layout, pf_list, dt, no_letterhead) {
  548. if(t.appendChild) {
  549. // If only one table is passed
  550. layout.cur_cell.appendChild(t);
  551. } else {
  552. page_break = '\n\
  553. <div style = "page-break-after: always;" \
  554. class = "page_break"></div><div class="page-settings"></div>';
  555. // If a list of tables is passed
  556. for(var i = 0; i < t.length-1; i++) {
  557. // add to current page
  558. layout.cur_cell.appendChild(t[i]);
  559. layout.close_borders();
  560. pf_list.push(page_break);
  561. // Create new page
  562. layout = _p.add_layout(dt, no_letterhead);
  563. pf_list.push(layout);
  564. layout.addrow();
  565. layout.addcell();
  566. var div = $a(layout.cur_cell, 'div');
  567. div.innerHTML = 'Continued from previous page...';
  568. div.style.padding = '4px';
  569. }
  570. // Append last table
  571. layout.cur_cell.appendChild(t[t.length-1]);
  572. }
  573. return layout;
  574. },
  575. print_std_add_field: function(dt, dn, f, layout) {
  576. var val = _f.get_value(dt, dn, f.fieldname);
  577. if(f.fieldtype!='Button') {
  578. if(val || in_list(['Float', 'Int', 'Currency'], f.fieldtype)) {
  579. // If value or a numeric type then proceed
  580. // Add field table
  581. row = _p.field_tab(layout.cur_cell);
  582. // Add label
  583. row.cells[0].innerHTML = f.label ? f.label : f.fieldname;
  584. row.cells[1].innerHTML = wn.format(val, f, {for_print: true});
  585. // left align currency in normal display
  586. if(f.fieldtype == 'Currency') {
  587. $y(row.cells[1], { textAlign: 'left' });
  588. }
  589. }
  590. }
  591. },
  592. field_tab: function(layout_cell) {
  593. var tab = $a(layout_cell, 'table', '', {width:'100%'});
  594. var row = tab.insertRow(0);
  595. _p.row = row; // Don't know this line's purpose
  596. row.insertCell(0);
  597. row.insertCell(1);
  598. row.cells[0].className = 'datalabelcell';
  599. row.cells[1].className = 'datainputcell';
  600. return row;
  601. }
  602. });