# Path: jasper_erpnext_report/jasper_reports/compile_reports.py
# def jasper_compile(jrxml, destFileName):
# 	try:
# 		compiler = jr.ReportCompiler()
# 		compiler.compile(jrxml,destFileName)
# 	except Exception as e:
# 		import jasper_erpnext_report as jer
# 		if jer.pyjnius == False:
# 			frappe.throw(_("Please install pyjnius python module."))
# 			return
# 		frappe.throw(_("Error while compiling report %s, error is: %s." % (jrxml, e)))
# 
# Path: jasper_erpnext_report/utils/file.py
# def get_image_name(iname):
# def get_images_path(compiled_path):
# def get_jasper_path(for_all_sites = False):
# def write_StringIO_to_file(file_path, output):
# def write_file(content, file_path, modes="w+"):
# def check_extension(fname):
# def get_extension(fname):
# def remove_from_doc(dt, dn, field, where_field = "name"):
# def delete_from_doc(dt, dn, field, value, where_field):
# def delete_from_FileData(dt, dn, file_url):
# def remove_directory(path, ignore_errors=True):
# def remove_compiled_report(root_path):
# def get_file(path, modes="r", raise_not_found=False):
# def read_file(path, modes="r", raise_not_found=False):
# def get_html_reports_path(report_name, where="reports", hash=None, localsite=None):
# def get_html_reports_images_path(report_path, where="images"):


import frappe
import os
import StringIO
			import shutil
from frappe import _
from io import BytesIO
from jasper_erpnext_report.jasper_reports.compile_reports import jasper_compile
from jasper_erpnext_report.utils.file import check_extension, get_jasper_path, get_extension, JasperXmlReport,\
	write_file
from frappe.utils.file_manager import check_max_file_size, get_content_hash
from PIL import Image, ImageOps
					from .file import remove_directory
		return f

	def process(self, dn=None):

		if self.ext != "jrxml":
			self.process_childs()
		else:
			self.process_jrxmls()

		self.rel_path = os.path.relpath(self.file_path, self.jasper_path)

		f = self.insert_report_doc(dn=dn)

		try:
			self.save()

			if self.ext == "jrxml":
				self.compile()
		except Exception, e:
			frappe.delete_doc("File", f.name)
			print "Remove this doc: doctype {} docname {} error: {}".format(f.doctype, f.name, e)
			frappe.throw(_("Error in report doctype %s docname %s, error is: %s." % (f.doctype, f.name, e)))

		return f

	def process_childs(self):
		docs = frappe.get_all("File", fields=["file_name", "file_url"], filters={"attached_to_name": self.dn, "attached_to_doctype": self.dt, "name": self.parent})
		if not docs:
			frappe.msgprint(_("Add a report first."), raise_exception=True)
		for doc in docs:

# Path: jasper_erpnext_report/jasper_reports/compile_reports.py
# def jasper_compile(jrxml, destFileName):
# 	try:
# 		compiler = jr.ReportCompiler()
# 		compiler.compile(jrxml,destFileName)
# 	except Exception as e:
# 		import jasper_erpnext_report as jer
# 		if jer.pyjnius == False:
# 			frappe.throw(_("Please install pyjnius python module."))
# 			return
# 		frappe.throw(_("Error while compiling report %s, error is: %s." % (jrxml, e)))
# 
# Path: jasper_erpnext_report/utils/file.py
# def get_image_name(iname):
# def get_images_path(compiled_path):
# def get_jasper_path(for_all_sites = False):
# def write_StringIO_to_file(file_path, output):
# def write_file(content, file_path, modes="w+"):
# def check_extension(fname):
# def get_extension(fname):
# def remove_from_doc(dt, dn, field, where_field = "name"):
# def delete_from_doc(dt, dn, field, value, where_field):
# def delete_from_FileData(dt, dn, file_url):
# def remove_directory(path, ignore_errors=True):
# def remove_compiled_report(root_path):
# def get_file(path, modes="r", raise_not_found=False):
# def read_file(path, modes="r", raise_not_found=False):
# def get_html_reports_path(report_name, where="reports", hash=None, localsite=None):
# def get_html_reports_images_path(report_path, where="images"):


# Path: jasper_erpnext_report/utils/jasper_file_jrxml.py
import frappe
import os
import StringIO
			import shutil
from frappe import _
from io import BytesIO
from jasper_erpnext_report.jasper_reports.compile_reports import jasper_compile
from jasper_erpnext_report.utils.file import check_extension, get_jasper_path, get_extension, JasperXmlReport,\
	write_file
from frappe.utils.file_manager import check_max_file_size, get_content_hash
from PIL import Image, ImageOps
					from .file import remove_directory

		return f

	def process(self, dn=None):

		if self.ext != "jrxml":
			self.process_childs()
		else:
			self.process_jrxmls()

		self.rel_path = os.path.relpath(self.file_path, self.jasper_path)

		f = self.insert_report_doc(dn=dn)

		try:
			self.save()

			if self.ext == "jrxml":
				self.compile()
		except Exception, e:
			frappe.delete_doc("File", f.name)
			print "Remove this doc: doctype {} docname {} error: {}".format(f.doctype, f.name, e)
			frappe.throw(_("Error in report doctype %s docname %s, error is: %s." % (f.doctype, f.name, e)))

		return f

	def process_childs(self):
		docs = frappe.get_all("File", fields=["file_name", "file_url"], filters={"attached_to_name": self.dn, "attached_to_doctype": self.dt, "name": self.parent})
		if not docs:
			frappe.msgprint(_("Add a report first."), raise_exception=True)
		for doc in docs:

			jrxml_ext = get_extension(doc.file_name)