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.8 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. from __future__ import unicode_literals
  23. import webnotes
  24. import os, json
  25. import types
  26. from webnotes import _
  27. from webnotes.modules import scrub, get_module_path
  28. from webnotes.utils import flt, cint
  29. import webnotes.widgets.reportview
  30. @webnotes.whitelist()
  31. def get_script(report_name):
  32. report = webnotes.doc("Report", report_name)
  33. script_path = os.path.join(get_module_path(webnotes.conn.get_value("DocType", report.ref_doctype, "module")),
  34. "report", scrub(report.name), scrub(report.name) + ".js")
  35. if os.path.exists(script_path):
  36. with open(script_path, "r") as script:
  37. return script.read()
  38. else:
  39. return "wn.query_reports['%s']={}" % report_name
  40. @webnotes.whitelist()
  41. def run(report_name, filters=None):
  42. report = webnotes.doc("Report", report_name)
  43. if not webnotes.has_permission(report.ref_doctype, "report"):
  44. webnotes.msgprint(_("Must have report permission to access this report."),
  45. raise_exception=True)
  46. if report.report_type=="Query Report":
  47. if not report.query:
  48. webnotes.msgprint(_("Must specify a Query to run"), raise_exception=True)
  49. if not report.query.lower().startswith("select"):
  50. webnotes.msgprint(_("Query must be a SELECT"), raise_exception=True)
  51. result = [list(t) for t in webnotes.conn.sql(report.query)]
  52. columns = [c[0] for c in webnotes.conn.get_description()]
  53. else:
  54. if filters:
  55. filters = json.loads(filters)
  56. method_name = scrub(webnotes.conn.get_value("DocType", report.ref_doctype, "module")) \
  57. + ".report." + scrub(report.name) + "." + scrub(report.name) + ".execute"
  58. columns, result = webnotes.get_method(method_name)(filters or {})
  59. result = get_filtered_data(report.ref_doctype, columns, result)
  60. if cint(report.add_total_row) and result:
  61. result = add_total_row(result, columns)
  62. return {
  63. "result": result,
  64. "columns": columns
  65. }
  66. def add_total_row(result, columns):
  67. total_row = [""]*len(columns)
  68. for row in result:
  69. for i, col in enumerate(columns):
  70. col = col.split(":")
  71. if len(col) > 1 and col[1] in ["Currency", "Int", "Float"] and flt(row[i]):
  72. total_row[i] = flt(total_row[i]) + flt(row[i])
  73. first_col = columns[0].split(":")
  74. if len(first_col) > 1 and first_col[1] not in ["Currency", "Int", "Float"]:
  75. total_row[0] = "Total"
  76. result.append(total_row)
  77. return result
  78. def get_filtered_data(ref_doctype, columns, data):
  79. result = []
  80. linked_doctypes = get_linked_doctypes(columns)
  81. match_filters = get_user_match_filters(linked_doctypes, ref_doctype)
  82. if match_filters:
  83. matched_columns = get_matched_columns(linked_doctypes, match_filters)
  84. for row in data:
  85. match = True
  86. for col, idx in matched_columns.items():
  87. if row[idx] not in match_filters[col]:
  88. match = False
  89. if match:
  90. result.append(row)
  91. else:
  92. for row in data:
  93. result.append(row)
  94. return result
  95. def get_linked_doctypes(columns):
  96. linked_doctypes = {}
  97. for idx, col in enumerate(columns):
  98. if "Link" in col:
  99. link_dt = col.split(":")[1].split("/")[1]
  100. linked_doctypes[link_dt] = idx
  101. return linked_doctypes
  102. def get_user_match_filters(doctypes, ref_doctype):
  103. match_filters = {}
  104. doctypes_meta = {}
  105. tables = []
  106. doctypes[ref_doctype] = None
  107. for dt in doctypes:
  108. tables.append("`tab" + dt + "`")
  109. doctypes_meta[dt] = webnotes.model.doctype.get(dt)
  110. webnotes.widgets.reportview.tables = tables
  111. webnotes.widgets.reportview.doctypes = doctypes_meta
  112. for dt in doctypes:
  113. match_filters = webnotes.widgets.reportview.build_match_conditions(dt,
  114. None, False, match_filters)
  115. return match_filters
  116. def get_matched_columns(linked_doctypes, match_filters):
  117. col_idx_map = {}
  118. for dt, idx in linked_doctypes.items():
  119. link_field = dt.lower().replace(" ", "_")
  120. if link_field in match_filters:
  121. col_idx_map[link_field] = idx
  122. return col_idx_map