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.
 
 
 
 
 
 

90 lines
3.2 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. from webnotes import _
  26. from webnotes.modules import scrub, get_module_path
  27. from webnotes.utils import flt, cint
  28. @webnotes.whitelist()
  29. def get_script(report_name):
  30. report = webnotes.doc("Report", report_name)
  31. script_path = os.path.join(get_module_path(webnotes.conn.get_value("DocType", report.ref_doctype, "module")),
  32. "report", scrub(report.name), scrub(report.name) + ".js")
  33. if os.path.exists(script_path):
  34. with open(script_path, "r") as script:
  35. return script.read()
  36. else:
  37. return "wn.query_reports['%s']={}" % report_name
  38. @webnotes.whitelist()
  39. def run(report_name, filters=None):
  40. report = webnotes.doc("Report", report_name)
  41. if not webnotes.has_permission(report.ref_doctype, "report"):
  42. webnotes.msgprint(_("Must have report permission to access this report."),
  43. raise_exception=True)
  44. if report.report_type=="Query Report":
  45. if not report.query:
  46. webnotes.msgprint(_("Must specify a Query to run"), raise_exception=True)
  47. if not report.query.lower().startswith("select"):
  48. webnotes.msgprint(_("Query must be a SELECT"), raise_exception=True)
  49. result = [list(t) for t in webnotes.conn.sql(report.query)]
  50. columns = [c[0] for c in webnotes.conn.get_description()]
  51. else:
  52. if filters:
  53. filters = json.loads(filters)
  54. method_name = scrub(webnotes.conn.get_value("DocType", report.ref_doctype, "module")) \
  55. + ".report." + scrub(report.name) + "." + scrub(report.name) + ".execute"
  56. columns, result = webnotes.get_method(method_name)(filters or {})
  57. if cint(report.add_total_row) and result:
  58. result = add_total_row(result, columns)
  59. return {
  60. "result": result,
  61. "columns": columns
  62. }
  63. def add_total_row(result, columns):
  64. total_row = [""]*len(columns)
  65. for row in result:
  66. for i, col in enumerate(columns):
  67. if col.split(":")[1] in ["Currency", "Int", "Float"] and flt(row[i]):
  68. total_row[i] = flt(total_row[i]) + flt(row[i])
  69. if columns[0].split(":")[1] not in ["Currency", "Int", "Float"]:
  70. total_row[0] = "Total"
  71. result.append(total_row)
  72. return result