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.
 
 
 
 
 
 

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