示例#1
0
def get_reports_list_for_all():
    jasper_run_method("jasper_before_list_for_all")
    jsr = jasper_session_obj or Jr.JasperRoot()
    jasper_users_login(jsr.user)
    lall = jsr.get_reports_list_for_all()
    jasper_run_method("jasper_after_list_for_all", lall)
    return lall
def get_reports_list(doctype, docnames, report):
	jsr = jasper_session_obj or Jr.JasperRoot()
	jasper_run_method("jasper_before_list_for_doctype", doctype, docnames, report)
	jasper_users_login(jsr.user)
	l = jsr.get_reports_list(doctype, docnames, report)
	jasper_run_method("jasper_after_list_for_doctype", doctype, docnames, report, l)
	return l
def get_reports_list(doctype, docnames, report):
	jsr = jasper_session_obj or Jr.JasperRoot()
	jasper_run_method("jasper_before_list_for_doctype", doctype, docnames, report)
	jasper_users_login(jsr.user)
	l = jsr.get_reports_list(doctype, docnames, report)
	jasper_run_method("jasper_after_list_for_doctype", doctype, docnames, report, l)
	return l
def get_reports_list_for_all():
	jasper_run_method("jasper_before_list_for_all")
	jsr = jasper_session_obj or Jr.JasperRoot()
	jasper_users_login(jsr.user)
	lall = jsr.get_reports_list_for_all()
	jasper_run_method("jasper_after_list_for_all", lall)
	return lall
def _get_report(data):
	jasper_run_method("jasper_before_get_report", data)
	jsr = jasper_session_obj or Jr.JasperRoot()
	fileName, content, report_name = jsr.get_report_server(data)
	pformat = data.get("pformat")
	if pformat == "html":
		html_reports_path = get_html_reports_path(report_name, hash=jsr.html_hash)
		write_file(content[0], os.path.join(html_reports_path, fileName))

	return fileName, content, report_name
def _get_report(data):
	jasper_run_method("jasper_before_get_report", data)
	jsr = jasper_session_obj or Jr.JasperRoot()
	fileName, content, report_name = jsr.get_report_server(data)
	pformat = data.get("pformat")
	if pformat == "html":
		html_reports_path = get_html_reports_path(report_name, hash=jsr.html_hash)
		write_file(content[0], os.path.join(html_reports_path, fileName))

	return fileName, content, report_name
def make_pdf(fileName, content, pformat, report_name, reqId=None, merge_all=True, pages=None, email=False):

	if pformat == "html":
		filepath = getHtmlFilepath(report_name, fileName)
		g = "jasper_erpnext_report/reports/" + frappe.local.site
		path = os.path.normpath(os.path.relpath(filepath, g))
		url = "%s?jasper_doc_path=%s" % ("Jasper Reports", path)
		fileName = fileName[fileName.rfind(os.sep) + 1:]
		file_name = fileName.replace(" ", "-").replace("/", "-")
		jasper_run_method("jasper_after_get_report", file_name, content, url, filepath)
		if not email:
			return url
		return url, filepath

	jsr = jasper_session_obj or Jr.JasperRoot()
	file_name, output = jsr.make_pdf(fileName, content, pformat, merge_all, pages)

	if pformat == "pdf":
		filepath = get_email_pdf_path(report_name, reqId)
		path = getPdfFilePath(file_name, filepath)
		url = "%s?jasper_doc_path=%s" % ("Jasper Reports", path)
		jasper_run_method("jasper_after_get_report", file_name, output.getvalue(), url, filepath)
		if not email:
			file_path = os.path.join(filepath, file_name)
			jasper_save_email(file_path, output.getvalue())
			return url
		return file_name, filepath, output, url

	jasper_run_method("jasper_after_get_report", file_name, output.getvalue(), None, None)
	if not email:
		jsr.prepare_file_to_client(file_name, output.getvalue())
		return

	return file_name, output
def make_pdf(fileName, content, pformat, report_name, reqId=None, merge_all=True, pages=None, email=False):

	if pformat == "html":
		filepath = getHtmlFilepath(report_name, fileName)
		g = "jasper_erpnext_report/reports/" + frappe.local.site
		path = os.path.normpath(os.path.relpath(filepath, g))
		url = "%s?jasper_doc_path=%s" % ("Jasper Reports", path)
		fileName = fileName[fileName.rfind(os.sep) + 1:]
		file_name = fileName.replace(" ", "-").replace("/", "-")
		jasper_run_method("jasper_after_get_report", file_name, content, url, filepath)
		if not email:
			return url
		return url, filepath

	jsr = jasper_session_obj or Jr.JasperRoot()
	file_name, output = jsr.make_pdf(fileName, content, pformat, merge_all, pages)

	if pformat == "pdf":
		filepath = get_email_pdf_path(report_name, reqId)
		path = getPdfFilePath(file_name, filepath)
		url = "%s?jasper_doc_path=%s" % ("Jasper Reports", path)
		jasper_run_method("jasper_after_get_report", file_name, output.getvalue(), url, filepath)
		if not email:
			file_path = os.path.join(filepath, file_name)
			jasper_save_email(file_path, output.getvalue())
			return url
		return file_name, filepath, output, url

	jasper_run_method("jasper_after_get_report", file_name, output.getvalue(), None, None)
	if not email:
		jsr.prepare_file_to_client(file_name, output.getvalue())
		return

	return file_name, output
示例#9
0
def jasper_make_email(doctype=None,
                      name=None,
                      content=None,
                      subject=None,
                      sent_or_received="Sent",
                      sender=None,
                      recipients=None,
                      communication_medium="Email",
                      send_email=False,
                      print_html=None,
                      print_format=None,
                      attachments='[]',
                      send_me_a_copy=False,
                      set_lead=True,
                      date=None,
                      jasper_doc=None,
                      docdata=None):

    custom_print_html = print_html
    custom_print_format = print_format

    jasper_polling_time = frappe.db.get_value('JasperServerConfig',
                                              fieldname="jasper_polling_time")
    data = json.loads(jasper_doc)
    result = run_report(data, docdata)
    if result[0].get("status", "not ready") != "ready":
        poll_data = prepare_polling(result)
        result = report_polling(poll_data)
        limit = 0
        while limit <= 10 and result[0].get("status", "not ready") != "ready":
            time.sleep(cint(jasper_polling_time) / 1000)
            result = report_polling(poll_data)
            limit += 1

    pformat = data.get("pformat")
    #we have to remove the original and send only duplicate
    if result[0].get("status", "not ready") == "ready":
        rdoc = frappe.get_doc(data.get("doctype"), data.get('report_name'))
        ncopies = get_copies(rdoc, pformat)
        fileName, jasper_content, report_name = _get_report(result[0])
        merge_all = True
        pages = None
        if pformat == "pdf" and ncopies > 1:
            merge_all = False
            pages = get_pages(ncopies, len(jasper_content))

        sender = get_sender(sender)

        if pformat == "html":
            custom_print_html = True
            url, filepath = make_pdf(fileName,
                                     jasper_content,
                                     pformat,
                                     report_name,
                                     merge_all=merge_all,
                                     pages=pages,
                                     email=True)
            output = filepath
            file_name = output.rsplit("/", 1)
            if len(file_name) > 1:
                file_name = file_name[1]
            else:
                file_name = file_name[0]

        elif pformat == "pdf":
            custom_print_format = "pdf"
            file_name, filepath, output, url = make_pdf(
                fileName,
                jasper_content,
                pformat,
                report_name,
                reqId=result[0].get("requestId"),
                merge_all=merge_all,
                pages=pages,
                email=True)
            output = output.getvalue()

        else:
            file_name, output = make_pdf(fileName,
                                         jasper_content,
                                         pformat,
                                         report_name,
                                         merge_all=merge_all,
                                         pages=pages,
                                         email=True)
            filepath = url = get_email_other_path(data, file_name,
                                                  result[0].get("requestId"),
                                                  sender)
            output = output.getvalue()
            #remove name from filepath
            filepath = filepath.rsplit("/", 1)[0]

    else:
        frappe.throw(
            _("Error generating %s format, try again later.") % (pformat, ))
        frappe.errprint(frappe.get_traceback())
        return

    if not check_frappe_permission(
            data.get("doctype"), data.get('report_name'), ptypes=("read", )):
        raise frappe.PermissionError(
            (_("You are not allowed to send emails related to") +
             ": {doctype} {name}").format(doctype=data.get("doctype"),
                                          name=data.get('report_name')))

    jasper_run_method("jasper_before_sendmail",
                      data,
                      file_name,
                      output,
                      url,
                      doctype=doctype,
                      name=name,
                      content=content,
                      subject=subject,
                      sent_or_received=sent_or_received,
                      sender=sender,
                      recipients=recipients,
                      print_html=print_html,
                      print_format=print_format,
                      attachments=attachments,
                      send_me_a_copy=send_me_a_copy)

    version = getFrappeVersion().major
    if version >= 5:
        file_path = None

        if isinstance(attachments, basestring):
            attachments = json.loads(attachments)

        if pformat != "html":
            file_path = os.path.join(filepath, file_name)
            jasper_save_email(file_path, output)
            attachments.append(file_path)

        comm_name = sendmail_v5(url,
                                doctype=doctype,
                                name=name,
                                content=content,
                                subject=subject,
                                sent_or_received=sent_or_received,
                                sender=sender,
                                recipients=recipients,
                                send_email=send_email,
                                print_html=print_html,
                                print_format=print_format,
                                attachments=attachments)

        set_jasper_email_doctype(data.get('report_name'), recipients, sender,
                                 frappe.utils.now(), url, file_name)
        jasper_run_method("jasper_after_sendmail", data, url, file_name,
                          file_path)

        return comm_name

    print_format = custom_print_format
    print_html = custom_print_html

    sendmail(file_name,
             output,
             url,
             doctype=doctype,
             name=name,
             content=content,
             subject=subject,
             sent_or_received=sent_or_received,
             sender=sender,
             recipients=recipients,
             print_html=print_html,
             print_format=print_format,
             attachments=attachments,
             send_me_a_copy=send_me_a_copy)

    file_path = None
    if pformat != "html":
        file_path = os.path.join(filepath, file_name)
        jasper_save_email(file_path, output)

    set_jasper_email_doctype(data.get('report_name'), recipients, sender,
                             frappe.utils.now(), url, file_name)
    jasper_run_method("jasper_after_sendmail", data, url, file_name, file_path)
def jasper_make_email(doctype=None, name=None, content=None, subject=None, sent_or_received = "Sent",
	sender=None, recipients=None, communication_medium="Email", send_email=False,
	print_html=None, print_format=None, attachments='[]', send_me_a_copy=False, set_lead=True, date=None,
	jasper_doc=None, docdata=None):

	custom_print_html = print_html
	custom_print_format = print_format

	jasper_polling_time = frappe.db.get_value('JasperServerConfig', fieldname="jasper_polling_time")
	data = json.loads(jasper_doc)
	result = run_report(data, docdata)
	if result[0].get("status", "not ready") != "ready":
		poll_data = prepare_polling(result)
		result = report_polling(poll_data)
		limit = 0
		while limit <= 10 and result[0].get("status", "not ready") != "ready":
			time.sleep(cint(jasper_polling_time)/1000)
			result = report_polling(poll_data)
			limit += 1

	pformat = data.get("pformat")
	#we have to remove the original and send only duplicate
	if result[0].get("status", "not ready") == "ready":
		rdoc = frappe.get_doc(data.get("doctype"), data.get('report_name'))
		ncopies = get_copies(rdoc, pformat)
		fileName, jasper_content, report_name = _get_report(result[0])
		merge_all = True
		pages = None
		if pformat == "pdf" and ncopies > 1:
			merge_all = False
			pages = get_pages(ncopies, len(jasper_content))

		sender = get_sender(sender)

		if pformat == "html":
			custom_print_html = True
			url, filepath = make_pdf(fileName, jasper_content, pformat, report_name, merge_all=merge_all, pages=pages, email=True)
			output = filepath
			file_name = output.rsplit("/",1)
			if len(file_name) > 1:
				file_name = file_name[1]
			else:
				file_name = file_name[0]


		elif pformat == "pdf":
			custom_print_format = "pdf"
			file_name, filepath, output, url = make_pdf(fileName, jasper_content, pformat, report_name, reqId=result[0].get("requestId"), merge_all=merge_all, pages=pages, email=True)
			output = output.getvalue()

		else:
			file_name, output = make_pdf(fileName, jasper_content, pformat, report_name, merge_all=merge_all, pages=pages, email=True)
			filepath = url = get_email_other_path(data, file_name, result[0].get("requestId"), sender)
			output = output.getvalue()
			#remove name from filepath
			filepath = filepath.rsplit("/",1)[0]

	else:
		frappe.throw(_("Error generating %s format, try again later.") % (pformat,))
		frappe.errprint(frappe.get_traceback())
		return

	if not check_frappe_permission(data.get("doctype"), data.get('report_name'), ptypes=("read", )):
		raise frappe.PermissionError((_("You are not allowed to send emails related to") + ": {doctype} {name}").format(
			doctype=data.get("doctype"), name=data.get('report_name')))

	jasper_run_method("jasper_before_sendmail", data, file_name, output, url, doctype=doctype, name=name, content=content, subject=subject, sent_or_received=sent_or_received,
		sender=sender, recipients=recipients, print_html=print_html, print_format=print_format, attachments=attachments,
		send_me_a_copy=send_me_a_copy)

	version = getFrappeVersion().major
	if version >= 5:
		file_path = None

		if isinstance(attachments, basestring):
			attachments = json.loads(attachments)

		if pformat != "html":
			file_path = os.path.join(filepath, file_name)
			jasper_save_email(file_path, output)
			attachments.append(file_path)

		comm_name = sendmail_v5(url, doctype=doctype, name=name, content=content, subject=subject, sent_or_received=sent_or_received,
				sender=sender, recipients=recipients, send_email=send_email, print_html=print_html, print_format=print_format, attachments=attachments)

		set_jasper_email_doctype(data.get('report_name'), recipients, sender, frappe.utils.now(), url, file_name)
		jasper_run_method("jasper_after_sendmail", data, url, file_name, file_path)

		return comm_name

	print_format = custom_print_format
	print_html = custom_print_html

	sendmail(file_name, output, url, doctype=doctype, name=name, content=content, subject=subject, sent_or_received=sent_or_received,
		sender=sender, recipients=recipients, print_html=print_html, print_format=print_format, attachments=attachments,
		send_me_a_copy=send_me_a_copy)

	file_path = None
	if pformat != "html":
		file_path = os.path.join(filepath, file_name)
		jasper_save_email(file_path, output)

	set_jasper_email_doctype(data.get('report_name'), recipients, sender, frappe.utils.now(), url, file_name)
	jasper_run_method("jasper_after_sendmail", data, url, file_name, file_path)