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.

meta.js 4.5 KiB

13 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
12 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. sort_docfields: function(docs) {
  45. return values(docs).sort(function(a, b) { return a.idx - b.idx });
  46. },
  47. get_docfield_copy: function(doctype, name) {
  48. if(!name) return wn.meta.docfield_map[doctype];
  49. if(!(wn.meta.docfield_copy[doctype] && wn.meta.docfield_copy[doctype][name])) {
  50. wn.meta.make_docfield_copy_for(doctype, name);
  51. }
  52. return wn.meta.docfield_copy[doctype][name];
  53. },
  54. get_fieldnames: function(doctype, name, filters) {
  55. return $.map(wn.utils.filter_dict(wn.meta.docfield_map[doctype], filters),
  56. function(df) { return df.fieldname; });
  57. },
  58. has_field: function(dt, fn) {
  59. return wn.meta.docfield_map[dt][fn];
  60. },
  61. get_parentfield: function(parent_dt, child_dt) {
  62. var df = wn.model.get("DocField", {parent:parent_dt, fieldtype:"Table",
  63. options:child_dt})
  64. if(!df.length)
  65. throw "parentfield not found for " + parent_dt + ", " + child_dt;
  66. return df[0].fieldname;
  67. },
  68. get_label: function(dt, fn, dn) {
  69. if(fn==="owner") {
  70. return "Owner";
  71. } else {
  72. return this.get_docfield(dt, fn, dn).label || fn;
  73. }
  74. },
  75. get_print_formats: function(doctype) {
  76. var print_format_list = ["Standard"];
  77. var default_print_format = locals.DocType[doctype].default_print_format;
  78. var print_formats = wn.model.get("Print Format", {doc_type: doctype})
  79. .sort(function(a, b) { return (a > b) ? 1 : -1; });
  80. $.each(print_formats, function(i, d) {
  81. if(!in_list(print_format_list, d.name))
  82. print_format_list.push(d.name);
  83. });
  84. if(default_print_format && default_print_format != "Standard") {
  85. var index = print_format_list.indexOf(default_print_format) - 1;
  86. print_format_list.sort().splice(index, 1);
  87. print_format_list.unshift(default_print_format);
  88. }
  89. return print_format_list;
  90. },
  91. sync_messages: function(doc) {
  92. if(doc.__messages) {
  93. $.extend(wn._messages, doc.__messages);
  94. }
  95. },
  96. get_field_currency: function(df, doc) {
  97. var currency = wn.boot.sysdefaults.currency;
  98. if(!doc && cur_frm)
  99. doc = cur_frm.doc;
  100. if(df && df.options) {
  101. if(doc && df.options.indexOf(":")!=-1) {
  102. var options = df.options.split(":");
  103. if(options.length==3) {
  104. // get reference record e.g. Company
  105. var docname = doc[options[1]];
  106. if(!docname && cur_frm) {
  107. docname = cur_frm.doc[options[1]];
  108. }
  109. currency = wn.model.get_value(options[0], docname, options[2]) ||
  110. wn.model.get_value(":" + options[0], docname, options[2]) ||
  111. currency;
  112. }
  113. } else if(doc && doc[df.options]) {
  114. currency = doc[df.options];
  115. } else if(cur_frm && cur_frm.doc[df.options]) {
  116. currency = cur_frm.doc[df.options];
  117. }
  118. }
  119. return currency;
  120. },
  121. get_field_precision: function(df, doc) {
  122. var precision = wn.defaults.get_default("float_precision") || 3;
  123. if(df && df.fieldtype === "Currency") {
  124. var currency = this.get_field_currency(df, doc);
  125. var number_format = get_number_format(currency);
  126. var number_format_info = get_number_format_info(number_format);
  127. precision = number_format_info.precision || precision;
  128. }
  129. return precision;
  130. },
  131. });