Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

165 righe
4.7 KiB

  1. // Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. // MIT License. See license.txt
  3. wn.provide('wn.meta.docfield_map');
  4. wn.provide('wn.meta.docfield_copy');
  5. wn.provide('wn.meta.docfield_list');
  6. wn.provide('wn.meta.doctypes');
  7. wn.provide("wn.meta.precision_map");
  8. $.extend(wn.meta, {
  9. // build docfield_map and docfield_list
  10. add_field: function(df) {
  11. wn.provide('wn.meta.docfield_map.' + df.parent);
  12. wn.meta.docfield_map[df.parent][df.fieldname || df.label] = df;
  13. if(!wn.meta.docfield_list[df.parent])
  14. wn.meta.docfield_list[df.parent] = [];
  15. // check for repeat
  16. for(var i in wn.meta.docfield_list[df.parent]) {
  17. var d = wn.meta.docfield_list[df.parent][i];
  18. if(df.fieldname==d.fieldname)
  19. return; // no repeat
  20. }
  21. wn.meta.docfield_list[df.parent].push(df);
  22. },
  23. make_docfield_copy_for: function(doctype, docname) {
  24. var c = wn.meta.docfield_copy;
  25. if(!c[doctype])
  26. c[doctype] = {};
  27. if(!c[doctype][docname])
  28. c[doctype][docname] = {};
  29. $.each(wn.meta.docfield_list[doctype] || [], function(i, df) {
  30. c[doctype][docname][df.fieldname || df.label] = copy_dict(df);
  31. })
  32. },
  33. get_docfield: function(dt, fn, dn) {
  34. return wn.meta.get_docfield_copy(dt, dn)[fn];
  35. },
  36. get_docfields: function(doctype, name, filters) {
  37. var docfield_map = wn.meta.get_docfield_copy(doctype, name);
  38. var docfields = wn.meta.sort_docfields(docfield_map);
  39. if(filters) {
  40. docfields = wn.utils.filter_dict(docfields, filters);
  41. }
  42. return docfields;
  43. },
  44. get_restricted_fields: function(doctype, name, restricted_types) {
  45. return $.map(wn.meta.get_docfields(doctype, name), function(df) {
  46. return (df.fieldtype==="Link" && df.ignore_restrictions!==1 &&
  47. restricted_types.indexOf(df.options)!==-1) ? df : null;
  48. });
  49. },
  50. sort_docfields: function(docs) {
  51. return values(docs).sort(function(a, b) { return a.idx - b.idx });
  52. },
  53. get_docfield_copy: function(doctype, name) {
  54. if(!name) return wn.meta.docfield_map[doctype];
  55. if(!(wn.meta.docfield_copy[doctype] && wn.meta.docfield_copy[doctype][name])) {
  56. wn.meta.make_docfield_copy_for(doctype, name);
  57. }
  58. return wn.meta.docfield_copy[doctype][name];
  59. },
  60. get_fieldnames: function(doctype, name, filters) {
  61. return $.map(wn.utils.filter_dict(wn.meta.docfield_map[doctype], filters),
  62. function(df) { return df.fieldname; });
  63. },
  64. has_field: function(dt, fn) {
  65. return wn.meta.docfield_map[dt][fn];
  66. },
  67. get_parentfield: function(parent_dt, child_dt) {
  68. var df = wn.model.get("DocField", {parent:parent_dt, fieldtype:"Table",
  69. options:child_dt})
  70. if(!df.length)
  71. throw "parentfield not found for " + parent_dt + ", " + child_dt;
  72. return df[0].fieldname;
  73. },
  74. get_label: function(dt, fn, dn) {
  75. if(fn==="owner") {
  76. return "Owner";
  77. } else {
  78. return this.get_docfield(dt, fn, dn).label || fn;
  79. }
  80. },
  81. get_print_formats: function(doctype) {
  82. var print_format_list = ["Standard"];
  83. var default_print_format = locals.DocType[doctype].default_print_format;
  84. var print_formats = wn.model.get("Print Format", {doc_type: doctype})
  85. .sort(function(a, b) { return (a > b) ? 1 : -1; });
  86. $.each(print_formats, function(i, d) {
  87. if(!in_list(print_format_list, d.name))
  88. print_format_list.push(d.name);
  89. });
  90. if(default_print_format && default_print_format != "Standard") {
  91. var index = print_format_list.indexOf(default_print_format) - 1;
  92. print_format_list.sort().splice(index, 1);
  93. print_format_list.unshift(default_print_format);
  94. }
  95. return print_format_list;
  96. },
  97. sync_messages: function(doc) {
  98. if(doc.__messages) {
  99. $.extend(wn._messages, doc.__messages);
  100. }
  101. },
  102. get_field_currency: function(df, doc) {
  103. var currency = wn.boot.sysdefaults.currency;
  104. if(!doc && cur_frm)
  105. doc = cur_frm.doc;
  106. if(df && df.options) {
  107. if(doc && df.options.indexOf(":")!=-1) {
  108. var options = df.options.split(":");
  109. if(options.length==3) {
  110. // get reference record e.g. Company
  111. var docname = doc[options[1]];
  112. if(!docname && cur_frm) {
  113. docname = cur_frm.doc[options[1]];
  114. }
  115. currency = wn.model.get_value(options[0], docname, options[2]) ||
  116. wn.model.get_value(":" + options[0], docname, options[2]) ||
  117. currency;
  118. }
  119. } else if(doc && doc[df.options]) {
  120. currency = doc[df.options];
  121. } else if(cur_frm && cur_frm.doc[df.options]) {
  122. currency = cur_frm.doc[df.options];
  123. }
  124. }
  125. return currency;
  126. },
  127. get_field_precision: function(df, doc) {
  128. var precision = wn.defaults.get_default("float_precision") || 3;
  129. if(df && df.fieldtype === "Currency") {
  130. var currency = this.get_field_currency(df, doc);
  131. var number_format = get_number_format(currency);
  132. var number_format_info = get_number_format_info(number_format);
  133. precision = number_format_info.precision || precision;
  134. }
  135. return precision;
  136. },
  137. });