# Path: jasper_erpnext_report/utils/file.py
# def get_file(path, modes="r", raise_not_found=False):
# 	if modes != "r":
# 		content = read_file(path, modes=modes, raise_not_found=raise_not_found)
# 	else:
# 		content = frappe.read_file(path, raise_not_found=raise_not_found)
# 
# 	return content
# 
# def get_html_reports_images_path(report_path, where="images"):
# 	path = os.path.join(report_path, where)
# 	frappe.create_folder(path)
# 	return path
# 
# 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"):


from frappe.model.document import Document
from frappe import _
from io import BytesIO
from jasper_erpnext_report.utils.file import get_file, get_html_reports_images_path
from jasper_erpnext_report.utils.file import JasperXmlReport, get_html_reports_path
		from distutils.dir_util import copy_tree
		from jasper_erpnext_report.utils.utils import getFrappeVersion
import frappe
import uuid, os
import jasper_erpnext_report.utils.utils as utils
							import re
		import hashlib
			import frappe.utils.email_lib
			import frappe.email

	def get_session_from_db(self, tab="tabJasperClientHtmlDocs"):
		rec = frappe.db.sql("""select name, data
			from {0}""".format(tab))
		return rec

	def prepare_report_async(self, path, doc, data=None, params=None, pformat="pdf", ncopies=1, for_all_sites=0):
		resps = []
		frappe.local.fds = None
		frappe.local.batch = None
		data = self.run_report_async(doc, data=data, params=params)
		"""
		Run one report at a time for Form type report and many ids
		"""
		if (doc.jasper_report_type == "Form" or data.get('jasper_report_type', None) == "Form") and not data.get("is_doctype_id", None):
			ids = data.get('ids', [])[:]
			for id in ids:
				data['ids'] = [id]
				resps.append(self._run_report_async(path, doc, data=data, params=params, pformat=pformat, ncopies=ncopies, for_all_sites=for_all_sites))
		else:
			resps.append(self._run_report_async(path, doc, data=data, params=params, pformat=pformat, ncopies=ncopies, for_all_sites=for_all_sites))

		if self.report_origin == "local":
			resps = resps[:1]
			cresp = self.prepareCollectResponse(resps)
			cresp["origin"] = "local"
			cresp["pformat"] = pformat
			self.frappe_task.setResult([cresp])
			frappe.local.batch.batchReport.export()
			if frappe.local.batch.outtype == 7:#html file

# Path: jasper_erpnext_report/utils/file.py
# def get_file(path, modes="r", raise_not_found=False):
# 	if modes != "r":
# 		content = read_file(path, modes=modes, raise_not_found=raise_not_found)
# 	else:
# 		content = frappe.read_file(path, raise_not_found=raise_not_found)
# 
# 	return content
# 
# def get_html_reports_images_path(report_path, where="images"):
# 	path = os.path.join(report_path, where)
# 	frappe.create_folder(path)
# 	return path
# 
# 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/core/JasperBase.py
from frappe.model.document import Document
from frappe import _
from io import BytesIO
from jasper_erpnext_report.utils.file import get_file, get_html_reports_images_path
from jasper_erpnext_report.utils.file import JasperXmlReport, get_html_reports_path
		from distutils.dir_util import copy_tree
		from jasper_erpnext_report.utils.utils import getFrappeVersion
import frappe
import uuid, os
import jasper_erpnext_report.utils.utils as utils
							import re
		import hashlib
			import frappe.utils.email_lib
			import frappe.email


	def get_session_from_db(self, tab="tabJasperClientHtmlDocs"):
		rec = frappe.db.sql("""select name, data
			from {0}""".format(tab))
		return rec

	def prepare_report_async(self, path, doc, data=None, params=None, pformat="pdf", ncopies=1, for_all_sites=0):
		resps = []
		frappe.local.fds = None
		frappe.local.batch = None
		data = self.run_report_async(doc, data=data, params=params)
		"""
		Run one report at a time for Form type report and many ids
		"""
		if (doc.jasper_report_type == "Form" or data.get('jasper_report_type', None) == "Form") and not data.get("is_doctype_id", None):
			ids = data.get('ids', [])[:]
			for id in ids:
				data['ids'] = [id]
				resps.append(self._run_report_async(path, doc, data=data, params=params, pformat=pformat, ncopies=ncopies, for_all_sites=for_all_sites))
		else:
			resps.append(self._run_report_async(path, doc, data=data, params=params, pformat=pformat, ncopies=ncopies, for_all_sites=for_all_sites))

		if self.report_origin == "local":
			resps = resps[:1]
			cresp = self.prepareCollectResponse(resps)
			cresp["origin"] = "local"
			cresp["pformat"] = pformat
			self.frappe_task.setResult([cresp])
			frappe.local.batch.batchReport.export()
			if frappe.local.batch.outtype == 7:#html file

				content = get_file(frappe.local.batch.outputPath + os.sep + frappe.local.batch.reportName + ".html")