Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

doclist.js 4.8 KiB

13 anos atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. function compress_doclist(list) {
  23. var kl = {}; var vl = []; var flx = {};
  24. for(var i=0; i<list.length;i++) {
  25. var o = list[i];
  26. var fl = [];
  27. if(!kl[o.doctype]) { // make key only once # doctype must be first
  28. var tfl = ['doctype', 'name', 'docstatus', 'owner', 'parent', 'parentfield', 'parenttype', 'idx', 'creation', 'modified', 'modified_by', '__islocal', '__newname', '__modified', '_user_tags']; // for text
  29. var fl = [].concat(tfl);
  30. for(key in wn.meta.docfield_map[o.doctype]) { // all other values
  31. if(!in_list(fl, key)
  32. && !in_list(no_value_fields, wn.meta.docfield_map[o.doctype][key].fieldtype)
  33. && !wn.meta.docfield_map[o.doctype][key].no_column) {
  34. fl[fl.length] = key; // save value list
  35. tfl[tfl.length] = key //.replace(/'/g, "\\'").replace(/\n/g, "\\n");
  36. }
  37. }
  38. flx[o.doctype] = fl;
  39. kl[o.doctype] = tfl
  40. }
  41. var nl = [];
  42. var fl = flx[o.doctype];
  43. // check all
  44. for(var j=0;j<fl.length;j++) {
  45. var v = o[fl[j]];
  46. nl.push(v);
  47. }
  48. vl.push(nl);
  49. }
  50. return JSON.stringify({'_vl':vl, '_kl':kl});
  51. }
  52. function expand_doclist(docs) {
  53. var l = [];
  54. for(var i=0;i<docs._vl.length;i++)
  55. l[l.length] = zip(docs._kl[docs._vl[i][0]], docs._vl[i]);
  56. return l;
  57. }
  58. function zip(k,v) {
  59. var obj = {};
  60. for(var i=0;i<k.length;i++) {
  61. obj[k[i]] = v[i];
  62. }
  63. return obj;
  64. }
  65. function save_doclist(dt, dn, save_action, onsave, onerr) {
  66. var doc = locals[dt][dn];
  67. var doctype = locals['DocType'][dt];
  68. var tmplist = [];
  69. // make doc list
  70. var doclist = make_doclist(dt, dn, 1);
  71. var all_reqd_ok = true;
  72. if(save_action!='Cancel') {
  73. for(var n in doclist) {
  74. // type / mandatory checking
  75. var reqd_ok = check_required(doclist[n].doctype, doclist[n].name, doclist[0].doctype);
  76. if(doclist[n].docstatus+''!='2' && all_reqd_ok) // if not deleted
  77. all_reqd_ok = reqd_ok;
  78. }
  79. }
  80. // mandatory not filled
  81. if(!all_reqd_ok) {
  82. onerr()
  83. return;
  84. }
  85. var _save = function() {
  86. //console.log(compress_doclist(doclist));
  87. $c('webnotes.widgets.form.save.savedocs', {'docs':compress_doclist(doclist), 'docname':dn, 'action': save_action, 'user':user },
  88. function(r, rtxt) {
  89. if(f){ f.savingflag = false;}
  90. if(r.saved) {
  91. if(onsave)onsave(r);
  92. } else {
  93. if(onerr)onerr(r);
  94. }
  95. }, function() {
  96. if(f){ f.savingflag = false; } /*time out*/
  97. },0,(f ? 'Saving...' : '')
  98. );
  99. }
  100. // ask for name
  101. if(doc.__islocal && (doctype && doctype.autoname && doctype.autoname.toLowerCase()=='prompt')) {
  102. var newname = prompt('Enter the name of the new '+ dt, '');
  103. if(newname) {
  104. doc.__newname = strip(newname); _save();
  105. } else {
  106. msgprint('Not Saved'); onerr();
  107. }
  108. } else {
  109. _save();
  110. }
  111. }
  112. function check_required(dt, dn, parent_dt) {
  113. var doc = locals[dt][dn];
  114. if(doc.docstatus>1)return true;
  115. var fl = wn.meta.docfield_list[dt];
  116. if(!fl)return true; // no doctype loaded
  117. var all_clear = true;
  118. var errfld = [];
  119. for(var i=0;i<fl.length;i++) {
  120. var key = fl[i].fieldname;
  121. var v = doc[key];
  122. if(fl[i].reqd && is_null(v) && fl[i].fieldname) {
  123. errfld[errfld.length] = fl[i].label;
  124. // Bring to front "Section"
  125. if(cur_frm) {
  126. // show as red
  127. var f = cur_frm.fields_dict[fl[i].fieldname];
  128. if(f) {
  129. // in form
  130. if(f.set_as_error) f.set_as_error(1);
  131. // switch to section
  132. if(!cur_frm.error_in_section && f.parent_section) {
  133. cur_frm.error_in_section = 1;
  134. }
  135. }
  136. }
  137. if(all_clear)all_clear = false;
  138. }
  139. }
  140. if(errfld.length)msgprint('<b>Mandatory fields required in '+
  141. (doc.parenttype ? (wn.meta.docfield_map[doc.parenttype][doc.parentfield].label + ' (Table)') : get_doctype_label(doc.doctype)) +
  142. ':</b>\n' + errfld.join('\n'));
  143. return all_clear;
  144. }