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.
 
 
 
 
 
 

155 line
4.7 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. wn.provide('wn.meta.docfield_map');
  23. wn.provide('wn.meta.docfield_copy');
  24. wn.provide('wn.meta.docfield_list');
  25. wn.provide('wn.meta.doctypes');
  26. wn.provide("wn.meta.precision_map");
  27. $.extend(wn.meta, {
  28. // build docfield_map and docfield_list
  29. add_field: function(df) {
  30. wn.provide('wn.meta.docfield_map.' + df.parent);
  31. wn.meta.docfield_map[df.parent][df.fieldname || df.label] = df;
  32. if(!wn.meta.docfield_list[df.parent])
  33. wn.meta.docfield_list[df.parent] = [];
  34. // check for repeat
  35. for(var i in wn.meta.docfield_list[df.parent]) {
  36. var d = wn.meta.docfield_list[df.parent][i];
  37. if(df.fieldname==d.fieldname)
  38. return; // no repeat
  39. }
  40. wn.meta.docfield_list[df.parent].push(df);
  41. },
  42. make_docfield_copy_for: function(doctype, docname) {
  43. var c = wn.meta.docfield_copy;
  44. if(!c[doctype])
  45. c[doctype] = {};
  46. if(!c[doctype][docname])
  47. c[doctype][docname] = {};
  48. $.each(wn.meta.docfield_list[doctype], function(i, df) {
  49. c[doctype][docname][df.fieldname || df.label] = copy_dict(df);
  50. })
  51. },
  52. get_docfield: function(dt, fn, dn) {
  53. if(dn && wn.meta.docfield_copy[dt] && wn.meta.docfield_copy[dt][dn]){
  54. return wn.meta.docfield_copy[dt][dn][fn];
  55. } else {
  56. return wn.meta.docfield_map[dt][fn];
  57. }
  58. },
  59. has_field: function(dt, fn) {
  60. return wn.meta.docfield_map[dt][fn];
  61. },
  62. get_parentfield: function(parent_dt, child_dt) {
  63. var df = wn.model.get("DocField", {parent:parent_dt, fieldtype:"Table",
  64. options:child_dt})
  65. if(!df.length)
  66. throw "parentfield not found for " + parent_dt + ", " + child_dt;
  67. return df[0].fieldname;
  68. },
  69. get_label: function(dt, fn, dn) {
  70. if(fn==="owner") {
  71. return "Owner";
  72. } else {
  73. return this.get_docfield(dt, fn, dn).label || fn;
  74. }
  75. },
  76. get_print_formats: function(doctype) {
  77. // if default print format is given, use it
  78. var print_format_list = [];
  79. if(locals.DocType[doctype].default_print_format)
  80. print_format_list.push(locals.DocType[doctype].default_print_format)
  81. if(!in_list(print_format_list, "Standard"))
  82. print_format_list.push("Standard");
  83. var print_formats = wn.model.get("Print Format", {doc_type: doctype})
  84. .sort(function(a, b) { return (a > b) ? 1 : -1; });
  85. $.each(print_formats, function(i, d) {
  86. if(!in_list(print_format_list, d.name))
  87. print_format_list.push(d.name);
  88. });
  89. return print_format_list;
  90. },
  91. get_precision_map: function(doctype) {
  92. if(!wn.meta.precision_map[doctype]) {
  93. wn.meta.precision_map[doctype] = {};
  94. var fields = wn.model.get("DocField", {parent:doctype, fieldtype: "Currency"})
  95. .concat(wn.model.get("DocField", {parent: doctype, fieldtype: "Float"}));
  96. $.each(fields, function(i, df) {
  97. wn.meta.precision_map[doctype][df.fieldname] = df.precision;
  98. });
  99. }
  100. return wn.meta.precision_map[doctype];
  101. },
  102. sync_messages: function(doc) {
  103. if(doc.__messages) {
  104. $.extend(wn._messages, doc.__messages);
  105. }
  106. },
  107. get_field_currency: function(df, doc) {
  108. var currency = wn.boot.sysdefaults.currency;
  109. if(!doc && cur_frm)
  110. doc = cur_frm.doc;
  111. if(df && df.options) {
  112. if(doc && df.options.indexOf(":")!=-1) {
  113. var options = df.options.split(":");
  114. if(options.length==3) {
  115. // get reference record e.g. Company
  116. var docname = doc[options[1]];
  117. if(!docname && cur_frm) {
  118. docname = cur_frm.doc[options[1]]
  119. }
  120. currency = wn.model.get_value(options[0], doc[options[1]],
  121. options[2]) || currency;
  122. }
  123. } else if(doc && doc[df.options]) {
  124. currency = doc[df.options];
  125. } else if(cur_frm && cur_frm.doc[df.options]) {
  126. currency = cur_frm.doc[df.options];
  127. }
  128. }
  129. return currency;
  130. }
  131. });