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.
 
 
 
 
 
 

809 line
20 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. /* Form page structure
  23. + this.parent (either FormContainer or Dialog)
  24. + this.wrapper
  25. + this.toolbar
  26. + this.form_wrapper
  27. + this.main
  28. + this.head
  29. + this.body
  30. + this.layout
  31. + this.sidebar
  32. + this.print_wrapper
  33. + this.head
  34. + this.footer
  35. */
  36. wn.provide('_f');
  37. wn.provide('wn.ui.form');
  38. wn.ui.form.Controller = Class.extend({
  39. init: function(opts) {
  40. $.extend(this, opts);
  41. this.setup && this.setup();
  42. }
  43. });
  44. _f.frms = {};
  45. _f.Frm = function(doctype, parent, in_form) {
  46. this.docname = '';
  47. this.doctype = doctype;
  48. this.display = 0;
  49. this.refresh_if_stale_for = 120;
  50. var me = this;
  51. this.last_view_is_edit = {};
  52. this.opendocs = {};
  53. this.sections = [];
  54. this.grids = [];
  55. this.cscript = new wn.ui.form.Controller({frm:this});
  56. this.pformat = {};
  57. this.fetch_dict = {};
  58. this.parent = parent;
  59. this.tinymce_id_list = [];
  60. this.setup_meta(doctype);
  61. // show in form instead of in dialog, when called using url (router.js)
  62. this.in_form = in_form ? true : false;
  63. // notify on rename
  64. var me = this;
  65. $(document).on('rename', function(event, dt, old_name, new_name) {
  66. if(dt==me.doctype)
  67. me.rename_notify(dt, old_name, new_name)
  68. });
  69. }
  70. _f.Frm.prototype.check_doctype_conflict = function(docname) {
  71. var me = this;
  72. if(this.doctype=='DocType' && docname=='DocType') {
  73. msgprint('Allowing DocType, DocType. Be careful!')
  74. } else if(this.doctype=='DocType') {
  75. if (wn.views.formview[docname] || wn.pages['List/'+docname]) {
  76. msgprint("Cannot open DocType when its instance is open")
  77. throw 'doctype open conflict'
  78. }
  79. } else {
  80. if (wn.views.formview.DocType && wn.views.formview.DocType.frm.opendocs[this.doctype]) {
  81. msgprint("Cannot open instance when its DocType is open")
  82. throw 'doctype open conflict'
  83. }
  84. }
  85. }
  86. _f.Frm.prototype.setup = function() {
  87. var me = this;
  88. this.fields = [];
  89. this.fields_dict = {};
  90. this.state_fieldname = wn.workflow.get_state_fieldname(this.doctype);
  91. // wrapper
  92. this.wrapper = this.parent;
  93. wn.ui.make_app_page({
  94. parent: this.wrapper,
  95. single_column: true
  96. });
  97. this.appframe = this.wrapper.appframe;
  98. this.layout_main = $(this.wrapper).find(".layout-main").get(0);
  99. this.toolbar = new wn.ui.form.Toolbar({
  100. frm: this,
  101. appframe: this.appframe
  102. });
  103. this.frm_head = this.toolbar;
  104. // create area for print fomrat
  105. this.setup_print_layout();
  106. // 2 column layout
  107. this.setup_std_layout();
  108. // client script must be called after "setup" - there are no fields_dict attached to the frm otherwise
  109. this.script_manager = new wn.ui.form.ScriptManager({
  110. frm: this
  111. })
  112. this.watch_model_updates();
  113. this.footer = new wn.ui.form.Footer({
  114. frm: this,
  115. parent: this.layout_main
  116. })
  117. this.setup_done = true;
  118. }
  119. _f.Frm.prototype.watch_model_updates = function() {
  120. // watch model updates
  121. var me = this;
  122. // on main doc
  123. wn.model.on(me.doctype, "*", function(fieldname, value, doc) {
  124. // set input
  125. if(doc.name===me.docname) {
  126. me.fields_dict[fieldname]
  127. && me.fields_dict[fieldname].set_input(value);
  128. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  129. me.refresh_dependency();
  130. }
  131. })
  132. // on table fields
  133. $.each(wn.model.get("DocField", {fieldtype:"Table", parent: me.doctype}), function(i, df) {
  134. wn.model.on(df.options, "*", function(fieldname, value, doc) {
  135. if(doc.parent===me.docname) {
  136. me.fields_dict[df.fieldname].grid.set_value(fieldname, value, doc);
  137. me.script_manager.trigger(fieldname, doc.doctype, doc.name);
  138. }
  139. })
  140. })
  141. }
  142. _f.Frm.prototype.setup_print_layout = function() {
  143. var me = this;
  144. this.print_wrapper = $('<div>\
  145. <div class="print-format-area clear-fix" style="min-height: 400px;"></div>\
  146. </div>').appendTo(this.layout_main).get(0);
  147. //appframe.add_ripped_paper_effect(this.print_wrapper);
  148. this.print_body = $(this.print_wrapper).find(".print-format-area").get(0);
  149. }
  150. _f.Frm.prototype.onhide = function() {
  151. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  152. }
  153. _f.Frm.prototype.setup_std_layout = function() {
  154. this.form_wrapper = $('<div></div>').appendTo(this.layout_main).get(0);
  155. $parent = $(this.form_wrapper);
  156. this.head = $parent.find(".layout-appframe").get(0);
  157. this.main = this.form_wrapper;
  158. this.body_header = $a(this.main, 'div');
  159. this.body = $a(this.main, 'div');
  160. if(this.heading) {
  161. this.page_head = new PageHeader(this.head, this);
  162. }
  163. // only tray
  164. this.meta.section_style='Simple'; // always simple!
  165. // layout
  166. this.layout = new wn.ui.form.Layout({
  167. parent: this.body,
  168. doctype: this.doctype,
  169. frm: this,
  170. })
  171. // state
  172. this.states = new wn.ui.form.States({
  173. frm: this
  174. });
  175. }
  176. _f.Frm.prototype.setup_print = function() {
  177. this.print_formats = wn.meta.get_print_formats(this.meta.name);
  178. this.print_sel = $a(null, 'select', '', {width:'160px'});
  179. add_sel_options(this.print_sel, this.print_formats);
  180. this.print_sel.value = this.print_formats[0];
  181. }
  182. _f.Frm.prototype.print_doc = function() {
  183. if(this.doc.docstatus==2) {
  184. msgprint("Cannot Print Cancelled Documents.");
  185. return;
  186. }
  187. _p.show_dialog(); // multiple options
  188. }
  189. // email the form
  190. _f.Frm.prototype.email_doc = function(message) {
  191. new wn.views.CommunicationComposer({
  192. doc: this.doc,
  193. subject: wn._(this.meta.name) + ': ' + this.docname,
  194. recipients: this.doc.email || this.doc.email_id || this.doc.contact_email,
  195. attach_document_print: true,
  196. message: message,
  197. real_name: this.doc.real_name || this.doc.contact_display || this.doc.contact_name
  198. });
  199. }
  200. // email the form
  201. _f.Frm.prototype.rename_doc = function() {
  202. wn.model.rename_doc(this.doctype, this.docname);
  203. }
  204. // notify this form of renamed records
  205. _f.Frm.prototype.rename_notify = function(dt, old, name) {
  206. // from form
  207. if(this.meta.istable)
  208. return;
  209. if(this.docname == old)
  210. this.docname = name;
  211. else
  212. return;
  213. // view_is_edit
  214. this.last_view_is_edit[name] = this.last_view_is_edit[old];
  215. delete this.last_view_is_edit[old];
  216. // cleanup
  217. if(this && this.opendocs[old]) {
  218. // delete docfield copy
  219. wn.meta.docfield_copy[dt][name] = wn.meta.docfield_copy[dt][old];
  220. delete wn.meta.docfield_copy[dt][old];
  221. }
  222. delete this.opendocs[old];
  223. this.opendocs[name] = true;
  224. if(this.meta.in_dialog || !this.in_form) {
  225. return;
  226. }
  227. wn.re_route[window.location.hash] = '#Form/' + encodeURIComponent(this.doctype) + '/' + encodeURIComponent(name);
  228. wn.set_route('Form', this.doctype, name);
  229. }
  230. // SETUP
  231. _f.Frm.prototype.setup_meta = function(doctype) {
  232. this.meta = wn.model.get_doc('DocType',this.doctype);
  233. this.perm = wn.perm.get_perm(this.doctype); // for create
  234. if(this.meta.istable) { this.meta.in_dialog = 1 }
  235. this.setup_print();
  236. }
  237. _f.Frm.prototype.set_intro = function(txt) {
  238. wn.utils.set_intro(this, this.body, txt);
  239. }
  240. _f.Frm.prototype.set_footnote = function(txt) {
  241. wn.utils.set_footnote(this, this.body, txt);
  242. }
  243. _f.Frm.prototype.add_custom_button = function(label, fn, icon) {
  244. this.appframe.add_button(label, fn, icon || "icon-arrow-right", true);
  245. }
  246. _f.Frm.prototype.clear_custom_buttons = function() {
  247. this.toolbar.refresh()
  248. }
  249. _f.Frm.prototype.add_fetch = function(link_field, src_field, tar_field) {
  250. if(!this.fetch_dict[link_field]) {
  251. this.fetch_dict[link_field] = {'columns':[], 'fields':[]}
  252. }
  253. this.fetch_dict[link_field].columns.push(src_field);
  254. this.fetch_dict[link_field].fields.push(tar_field);
  255. }
  256. _f.Frm.prototype.refresh_print_layout = function() {
  257. $ds(this.print_wrapper);
  258. $dh(this.form_wrapper);
  259. var me = this;
  260. var print_callback = function(print_html) {
  261. me.print_body.innerHTML = print_html;
  262. }
  263. // print head
  264. if(cur_frm.doc.select_print_heading)
  265. cur_frm.set_print_heading(cur_frm.doc.select_print_heading)
  266. if(user!='Guest') {
  267. $di(this.view_btn_wrapper);
  268. // archive
  269. if(cur_frm.doc.__archived) {
  270. $dh(this.view_btn_wrapper);
  271. }
  272. } else {
  273. $dh(this.view_btn_wrapper);
  274. $dh(this.print_close_btn);
  275. }
  276. // create print format here
  277. _p.build(this.$print_view_select.val(), print_callback, false, true, true);
  278. }
  279. _f.Frm.prototype.show_the_frm = function() {
  280. // show the dialog
  281. if(this.meta.in_dialog && !this.parent.dialog.display) {
  282. if(!this.meta.istable)
  283. this.parent.table_form = false;
  284. this.parent.dialog.show();
  285. }
  286. }
  287. _f.Frm.prototype.set_print_heading = function(txt) {
  288. this.pformat[cur_frm.docname] = txt;
  289. }
  290. _f.Frm.prototype.defocus_rest = function() {
  291. // deselect others
  292. if(_f.cur_grid_cell) _f.cur_grid_cell.grid.cell_deselect();
  293. }
  294. _f.Frm.prototype.refresh_header = function() {
  295. // set title
  296. // main title
  297. if(!this.meta.in_dialog || this.in_form) {
  298. set_title(this.meta.issingle ? this.doctype : this.docname);
  299. }
  300. if(wn.ui.toolbar.recent)
  301. wn.ui.toolbar.recent.add(this.doctype, this.docname, 1);
  302. // show / hide buttons
  303. if(this.frm_head) {
  304. this.frm_head.refresh();
  305. }
  306. }
  307. _f.Frm.prototype.check_doc_perm = function() {
  308. // get perm
  309. var dt = this.parent_doctype?this.parent_doctype : this.doctype;
  310. var dn = this.parent_docname?this.parent_docname : this.docname;
  311. this.perm = wn.perm.get_perm(dt, dn);
  312. if(!this.perm[0][READ]) {
  313. wn.set_route("403");
  314. return 0;
  315. }
  316. return 1
  317. }
  318. _f.Frm.prototype.refresh = function(docname) {
  319. // record switch
  320. if(docname) {
  321. if(this.docname != docname && (!this.meta.in_dialog || this.in_form) &&
  322. !this.meta.istable)
  323. scroll(0, 0);
  324. this.docname = docname;
  325. }
  326. cur_frm = this;
  327. if(this.docname) { // document to show
  328. // check permissions
  329. if(!this.check_doc_perm()) return;
  330. // read only (workflow)
  331. this.read_only = wn.workflow.is_read_only(this.doctype, this.docname);
  332. // set the doc
  333. this.doc = wn.model.get_doc(this.doctype, this.docname);
  334. // check if doctype is already open
  335. if (!this.opendocs[this.docname]) {
  336. this.check_doctype_conflict(this.docname);
  337. } else {
  338. if(this.doc && this.doc.__last_sync_on &&
  339. (new Date() - this.doc.__last_sync_on) > (this.refresh_if_stale_for * 1000)) {
  340. this.reload_doc();
  341. return;
  342. }
  343. }
  344. // do setup
  345. if(!this.setup_done) this.setup();
  346. // load the record for the first time, if not loaded (call 'onload')
  347. cur_frm.cscript.is_onload = false;
  348. if(!this.opendocs[this.docname]) {
  349. cur_frm.cscript.is_onload = true;
  350. this.setnewdoc(this.docname);
  351. }
  352. // view_is_edit
  353. if(this.doc.__islocal)
  354. this.last_view_is_edit[this.docname] = 1; // new is view_is_edit
  355. this.view_is_edit = this.last_view_is_edit[this.docname];
  356. if(this.view_is_edit || (!this.view_is_edit && this.meta.istable)) {
  357. if(this.print_wrapper) {
  358. $dh(this.print_wrapper);
  359. $ds(this.form_wrapper);
  360. }
  361. // header
  362. if(!this.meta.istable) {
  363. this.refresh_header();
  364. }
  365. // call trigger
  366. this.script_manager.trigger("refresh");
  367. // trigger global trigger
  368. // to use this
  369. $(document).trigger('form_refresh');
  370. // fields
  371. this.refresh_fields();
  372. // call onload post render for callbacks to be fired
  373. if(this.cscript.is_onload) {
  374. this.script_manager.trigger("onload_post_render");
  375. }
  376. // focus on first input
  377. if(this.doc.docstatus==0) {
  378. var first = $(this.form_wrapper).find('.form-layout-row :input:first');
  379. if(!in_list(["Date", "Datetime"], first.attr("data-fieldtype"))) {
  380. first.focus();
  381. }
  382. }
  383. } else {
  384. this.refresh_header();
  385. if(this.print_wrapper) {
  386. this.refresh_print_layout();
  387. }
  388. }
  389. $(cur_frm.wrapper).trigger('render_complete');
  390. }
  391. }
  392. _f.Frm.prototype.refresh_field = function(fname) {
  393. cur_frm.fields_dict[fname] && cur_frm.fields_dict[fname].refresh
  394. && cur_frm.fields_dict[fname].refresh();
  395. }
  396. _f.Frm.prototype.refresh_fields = function() {
  397. this.layout.refresh();
  398. // cleanup activities after refresh
  399. this.cleanup_refresh(this);
  400. // dependent fields
  401. this.refresh_dependency();
  402. }
  403. _f.Frm.prototype.cleanup_refresh = function() {
  404. var me = this;
  405. if(me.fields_dict['amended_from']) {
  406. if (me.doc.amended_from) {
  407. unhide_field('amended_from');
  408. if (me.fields_dict['amendment_date']) unhide_field('amendment_date');
  409. } else {
  410. hide_field('amended_from');
  411. if (me.fields_dict['amendment_date']) hide_field('amendment_date');
  412. }
  413. }
  414. if(me.fields_dict['trash_reason']) {
  415. if(me.doc.trash_reason && me.doc.docstatus == 2) {
  416. unhide_field('trash_reason');
  417. } else {
  418. hide_field('trash_reason');
  419. }
  420. }
  421. if(me.meta.autoname && me.meta.autoname.substr(0,6)=='field:' && !me.doc.__islocal) {
  422. var fn = me.meta.autoname.substr(6);
  423. cur_frm.toggle_display(fn, false);
  424. }
  425. if(me.meta.autoname=="naming_series:" && !me.doc.__islocal) {
  426. cur_frm.toggle_display("naming_series", false);
  427. }
  428. }
  429. // Resolve "depends_on" and show / hide accordingly
  430. _f.Frm.prototype.refresh_dependency = function() {
  431. var me = this;
  432. var doc = locals[this.doctype][this.docname];
  433. // build dependants' dictionary
  434. var has_dep = false;
  435. for(fkey in me.fields) {
  436. var f = me.fields[fkey];
  437. f.dependencies_clear = true;
  438. if(f.df.depends_on) {
  439. has_dep = true;
  440. }
  441. }
  442. if(!has_dep)return;
  443. // show / hide based on values
  444. for(var i=me.fields.length-1;i>=0;i--) {
  445. var f = me.fields[i];
  446. f.guardian_has_value = true;
  447. if(f.df.depends_on) {
  448. // evaluate guardian
  449. var v = doc[f.df.depends_on];
  450. if(f.df.depends_on.substr(0,5)=='eval:') {
  451. f.guardian_has_value = eval(f.df.depends_on.substr(5));
  452. } else if(f.df.depends_on.substr(0,3)=='fn:') {
  453. f.guardian_has_value = me.script_manager.trigger(f.df.depends_on.substr(3), me.doctype, me.docname);
  454. } else {
  455. if(!v) {
  456. f.guardian_has_value = false;
  457. }
  458. }
  459. // show / hide
  460. if(f.guardian_has_value) {
  461. if(f.df.hidden != 0) {
  462. f.df.hidden = 0;
  463. f.refresh();
  464. }
  465. } else {
  466. if(f.df.hidden != 1) {
  467. f.df.hidden = 1;
  468. f.refresh();
  469. }
  470. }
  471. }
  472. }
  473. }
  474. // setnewdoc is called when a record is loaded for the first time
  475. // ======================================================================================
  476. _f.Frm.prototype.setnewdoc = function(docname) {
  477. // moved this call to refresh function
  478. // this.check_doctype_conflict(docname);
  479. // if loaded
  480. if(this.opendocs[docname]) { // already exists
  481. this.docname=docname;
  482. return;
  483. }
  484. this.docname = docname;
  485. var me = this;
  486. var viewname = this.meta.issingle ? this.doctype : docname;
  487. // Client Script
  488. this.script_manager.trigger("onload");
  489. this.last_view_is_edit[docname] = 1;
  490. if(cint(this.meta.read_only_onload)) this.last_view_is_edit[docname] = 0;
  491. this.opendocs[docname] = true;
  492. }
  493. _f.Frm.prototype.edit_doc = function() {
  494. // set fields
  495. this.last_view_is_edit[this.docname] = true;
  496. this.refresh();
  497. }
  498. _f.Frm.prototype.runscript = function(scriptname, callingfield, onrefresh) {
  499. var me = this;
  500. if(this.docname) {
  501. // make doc list
  502. var doclist = wn.model.compress(make_doclist(this.doctype, this.docname));
  503. // send to run
  504. if(callingfield)
  505. $(callingfield.input).set_working();
  506. $c('runserverobj', {'docs':doclist, 'method':scriptname },
  507. function(r, rtxt) {
  508. // run refresh
  509. if(onrefresh)
  510. onrefresh(r,rtxt);
  511. // fields
  512. me.refresh_fields();
  513. // enable button
  514. if(callingfield)
  515. $(callingfield.input).done_working();
  516. }
  517. );
  518. }
  519. }
  520. _f.Frm.prototype.copy_doc = function(onload, from_amend) {
  521. if(!this.perm[0][CREATE]) {
  522. msgprint('You are not allowed to create '+this.meta.name);
  523. return;
  524. }
  525. var dn = this.docname;
  526. // copy parent
  527. var newdoc = wn.model.copy_doc(this.doctype, dn, from_amend);
  528. // copy chidren
  529. var dl = make_doclist(this.doctype, dn);
  530. // table fields dict - for no_copy check
  531. var tf_dict = {};
  532. for(var d in dl) {
  533. d1 = dl[d];
  534. // get tabel field
  535. if(d1.parentfield && !tf_dict[d1.parentfield]) {
  536. tf_dict[d1.parentfield] = wn.meta.get_docfield(d1.parenttype, d1.parentfield);
  537. }
  538. if(d1.parent==dn && cint(tf_dict[d1.parentfield].no_copy)!=1) {
  539. var ch = wn.model.copy_doc(d1.doctype, d1.name, from_amend);
  540. ch.parent = newdoc.name;
  541. ch.docstatus = 0;
  542. ch.owner = user;
  543. ch.creation = '';
  544. ch.modified_by = user;
  545. ch.modified = '';
  546. }
  547. }
  548. newdoc.__islocal = 1;
  549. newdoc.docstatus = 0;
  550. newdoc.owner = user;
  551. newdoc.creation = '';
  552. newdoc.modified_by = user;
  553. newdoc.modified = '';
  554. if(onload)onload(newdoc);
  555. loaddoc(newdoc.doctype, newdoc.name);
  556. }
  557. _f.Frm.prototype.reload_doc = function() {
  558. this.check_doctype_conflict(this.docname);
  559. var me = this;
  560. var onsave = function(r, rtxt) {
  561. me.refresh();
  562. }
  563. if(!me.doc.__islocal) {
  564. wn.model.remove_from_locals(me.doctype, me.docname);
  565. wn.model.with_doc(me.doctype, me.docname, function() {
  566. me.refresh();
  567. })
  568. }
  569. }
  570. var validated;
  571. _f.Frm.prototype.save = function(save_action, callback, btn, on_error) {
  572. $(document.activeElement).blur();
  573. var me = this;
  574. if((!this.meta.in_dialog || this.in_form) && !this.meta.istable)
  575. scroll(0, 0);
  576. // validate
  577. if(save_action!="Cancel") {
  578. validated = true;
  579. this.script_manager.trigger("validate");
  580. if(!validated) {
  581. if(on_error)
  582. on_error();
  583. return;
  584. }
  585. }
  586. var doclist = new wn.model.DocList(this.doctype, this.docname);
  587. doclist.save(save_action || "Save", function(r) {
  588. if(!r.exc) {
  589. me.refresh();
  590. } else {
  591. if(on_error)
  592. on_error();
  593. }
  594. callback && callback(r);
  595. }, btn);
  596. }
  597. _f.Frm.prototype.savesubmit = function(btn, on_error) {
  598. var me = this;
  599. wn.confirm("Permanently Submit "+this.docname+"?", function() {
  600. me.save('Submit', function(r) {
  601. if(!r.exc) {
  602. me.script_manager.trigger("on_submit");
  603. }
  604. }, btn, on_error);
  605. });
  606. }
  607. _f.Frm.prototype.savecancel = function(btn, on_error) {
  608. var me = this;
  609. wn.confirm("Permanently Cancel "+this.docname+"?", function() {
  610. validated = true;
  611. me.script_manager.trigger("before_cancel");
  612. if(!validated) {
  613. if(on_error)
  614. on_error();
  615. return;
  616. }
  617. var doclist = new wn.model.DocList(me.doctype, me.docname);
  618. doclist.cancel(function(r) {
  619. if(!r.exc) {
  620. me.refresh();
  621. me.script_manager.trigger("after_cancel");
  622. }
  623. }, btn, on_error);
  624. });
  625. }
  626. // delete the record
  627. _f.Frm.prototype.savetrash = function() {
  628. wn.model.delete_doc(this.doctype, this.docname, function(r) {
  629. window.history.back();
  630. })
  631. }
  632. _f.Frm.prototype.amend_doc = function() {
  633. if(!this.fields_dict['amended_from']) {
  634. alert('"amended_from" field must be present to do an amendment.');
  635. return;
  636. }
  637. var me = this;
  638. var fn = function(newdoc) {
  639. newdoc.amended_from = me.docname;
  640. if(me.fields_dict && me.fields_dict['amendment_date'])
  641. newdoc.amendment_date = dateutil.obj_to_str(new Date());
  642. }
  643. this.copy_doc(fn, 1);
  644. }
  645. _f.Frm.prototype.disable_save = function() {
  646. // IMPORTANT: this function should be called in refresh event
  647. cur_frm.save_disabled = true;
  648. cur_frm.footer.hide_save();
  649. cur_frm.appframe.buttons.Save.remove();
  650. delete cur_frm.appframe.buttons.Save
  651. }
  652. _f.get_value = function(dt, dn, fn) {
  653. if(locals[dt] && locals[dt][dn])
  654. return locals[dt][dn][fn];
  655. }
  656. _f.Frm.prototype.dirty = function() {
  657. this.doc.__unsaved = 1;
  658. $(this.wrapper).trigger('dirty')
  659. }
  660. _f.Frm.prototype.get_docinfo = function() {
  661. return wn.model.docinfo[this.doctype][this.docname];
  662. }