Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print Preview #243

Open
wants to merge 22 commits into
base: version-14
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 7 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,6 @@ repos:
hooks:
- id: black

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
types_or: [javascript]
# Ignore any files that might contain jinja / bundles
exclude: |
(?x)^(
frappe/public/dist/.*|
.*node_modules.*|
.*boilerplate.*|
frappe/www/website_script.js|
frappe/templates/includes/.*|
frappe/public/js/lib/.*
)$

- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
Expand All @@ -68,6 +52,13 @@ repos:
language: system
types: [python]

- repo: local
hooks:
- id: prettier
name: prettier
entry: npx prettier -w . --config .prettierrc.cjs --ignore-path .prettierignore
language: system

ci:
autoupdate_schedule: weekly
skip: []
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions check_run/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Root editor config file
root = true

# Common settings
[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

# python, js indentation settings
[{*.py,*.js,*.vue,*.css,*.scss,*.html}]
indent_style = tab
indent_size = 2
max_line_length = 99
72 changes: 47 additions & 25 deletions check_run/check_run/doctype/check_run/check_run.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ frappe.ui.form.on('Check Run', {
show_progress_bar(frm, data, 'Processing')
})
frappe.realtime.on('render_check_progress', data => {
show_progress_bar(frm, data, 'Printing')
show_progress_bar(frm, data, 'Rendering')
})
},
validate: frm => {
Expand Down Expand Up @@ -64,6 +64,17 @@ frappe.ui.form.on('Check Run', {
frm.set_df_property('final_check_number', 'read_only', 1)
}
check_settings(frm)
$('[data-original-title="Print"]').hide()
if (frappe.model.can_print(null, frm)) {
frm.page.add_action_icon(
'printer',
() => {
frappe.set_route('print-check-run', frm.doc.name)
},
'',
__('Print')
)
}
},
onload_post_render: frm => {
frm.page.wrapper.find('.layout-side-section').hide()
Expand Down Expand Up @@ -283,6 +294,9 @@ function confirm_print(frm) {
}

function reprint_checks(frm) {
if (frm.settings.print_preview == 'Print from Print Preview') {
return
}
frm.set_value('status', 'Submitted')
let d = new frappe.ui.Dialog({
title: __('Re-Print'),
Expand Down Expand Up @@ -315,35 +329,43 @@ function reprint_checks(frm) {
}

function ach_only(frm) {
frappe
.xcall('check_run.check_run.doctype.check_run.check_run.ach_only', {
docname: frm.doc.name,
})
.then(r => {
if (!r.ach_only) {
if (frm.doc.docstatus == 1) {
if (frm.doc.print_count > 0 && frm.doc.status != 'Ready to Print') {
frm.add_custom_button(__('Re-Print Checks'), () => {
reprint_checks(frm)
if (frm.settings.print_preview != 'Print from Print Preview') {
frappe
.xcall('check_run.check_run.doctype.check_run.check_run.ach_only', {
docname: frm.doc.name,
})
.then(r => {
if (!r.ach_only) {
if (frm.doc.docstatus == 1) {
if (frm.doc.print_count > 0 && frm.doc.status != 'Ready to Print') {
frm.add_custom_button(__('Re-Print Checks'), () => {
reprint_checks(frm)
})
} else if (frm.doc.print_count == 0 && frm.doc.status == 'Submitted') {
render_checks(frm)
}
}
if (frm.doc.status == 'Ready to Print') {
frm.add_custom_button(__('Download Checks'), () => {
download_checks(frm)
})
} else if (frm.doc.print_count == 0 && frm.doc.status == 'Submitted') {
} else if (
frm.doc.print_count == 0 &&
frm.doc.status == 'Submitted' &&
frm.doc.__onload.print_preview == 'Automatically Render PDF after Submit'
) {
render_checks(frm)
}
}
if (frm.doc.status == 'Ready to Print') {
frm.add_custom_button(__('Download Checks'), () => {
download_checks(frm)
})
}
}
if (!r.print_checks_only) {
if (frm.doc.docstatus == 1) {
frm.add_custom_button(__('Download NACHA File'), () => {
download_nacha(frm)
})
if (!r.print_checks_only) {
if (frm.doc.docstatus == 1) {
frm.add_custom_button(__('Download NACHA File'), () => {
download_nacha(frm)
})
}
}
}
})
})
}
}

function validate_mode_of_payment_mandatory(frm) {
Expand Down
147 changes: 119 additions & 28 deletions check_run/check_run/doctype/check_run/check_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
from itertools import groupby, zip_longest
from io import StringIO
from typing_extensions import Self

from frappe.www.printview import validate_print_permission
from frappe.translate import print_language
from PyPDF2 import PdfFileWriter
from bs4 import BeautifulSoup

import frappe
from frappe.model.document import Document
from frappe.utils.data import flt
from frappe.utils.data import nowdate, getdate, now, get_datetime
from frappe.utils.print_format import read_multi_pdf
from frappe.permissions import has_permission
from frappe.utils.file_manager import save_file, remove_all
from frappe.utils.password import get_decrypted_password
Expand Down Expand Up @@ -415,14 +417,18 @@ def increment_print_count(self: Self, reprint_check_number: int | None = None) -
frappe.enqueue_doc(
self.doctype,
self.name,
"render_check_pdf",
"render_check_run",
reprint_check_number=reprint_check_number,
queue="short",
now=True,
)

@frappe.whitelist()
def render_check_pdf(self: Self, reprint_check_number: int | None = None) -> None:
def render_check_run(
self: Self, reprint_check_number: int | None = None, pdf: bool = True
) -> None | str:
from frappe.utils.print_format import read_multi_pdf # imported here to prevent circular imports

self.print_count = self.print_count + 1
self.set_status("Submitted")
if not frappe.db.exists("File", "Home/Check Run"):
Expand All @@ -438,6 +444,7 @@ def render_check_pdf(self: Self, reprint_check_number: int | None = None) -> Non
self.initial_check_number = int(reprint_check_number)
output = PdfFileWriter()
se_print_output = PdfFileWriter()
html = []
transactions = json.loads(self.transactions)
check_increment = 0
_transactions = []
Expand All @@ -455,19 +462,32 @@ def render_check_pdf(self: Self, reprint_check_number: int | None = None) -> Non
"Payment Entry",
pe,
settings.secondary_print_format or frappe.get_meta("Payment Entry").default_print_format,
as_pdf=True,
output=se_print_output,
as_pdf=pdf,
output=se_print_output if pdf else "",
no_letterhead=0,
)
if docstatus == 1 and frappe.db.get_value("Mode of Payment", mode_of_payment, "type") == "Bank":
output = frappe.get_print(
"Payment Entry",
pe,
settings.print_format or frappe.get_meta("Payment Entry").default_print_format,
as_pdf=True,
output=output,
no_letterhead=0,
)
if pdf:
output = frappe.get_print(
"Payment Entry",
pe,
settings.print_format or frappe.get_meta("Payment Entry").default_print_format,
as_pdf=True,
output=output,
no_letterhead=0,
)
else:
_output = frappe.get_print(
"Payment Entry",
pe,
settings.print_format or frappe.get_meta("Payment Entry").default_print_format,
as_pdf=False,
no_letterhead=0,
)
soup = BeautifulSoup(_output, "html.parser")
soup.find("div", class_="action-banner").decompose()
soup.find("div", class_="print-format-gutter").unwrap()
html.append(soup.prettify())
if initial_check_number != reprint_check_number:
frappe.db.set_value(
"Payment Entry", pe, "reference_no", self.initial_check_number + check_increment
Expand Down Expand Up @@ -500,20 +520,27 @@ def render_check_pdf(self: Self, reprint_check_number: int | None = None) -> Non
self.db_set("status", "Ready to Print")
self.db_set("print_count", self.print_count)
frappe.db.set_value("Bank Account", self.bank_account, "check_number", self.final_check_number)
save_file(
f"{self.name}.pdf", read_multi_pdf(output), "Check Run", self.name, "Home/Check Run", False, 0
)
save_file(
f"Chaque {self.name}.pdf",
read_multi_pdf(se_print_output),
"Check Run",
self.name,
"Home/Check Run",
False,
0,
)
frappe.db.commit()
frappe.publish_realtime("reload", "{}", doctype=self.doctype, docname=self.name)
if pdf:
save_file(
f"{self.name}.pdf", read_multi_pdf(output), "Check Run", self.name, "Home/Check Run", False, 0
)
# TODO: this is raising an issue
# save_file(
# f"{self.name}.pdf",
# read_multi_pdf(se_print_output),
# "Check Run",
# self.name,
# "Home/Check Run",
# False,
# 0,
# )
frappe.db.commit()
frappe.publish_realtime("reload", "{}", doctype=self.doctype, docname=self.name)
return None
else:
return '<p style="page-break-after: always;"><hr style="border: 1px dashed gray"></p>'.join(
html
)


@frappe.whitelist()
Expand Down Expand Up @@ -937,3 +964,67 @@ def ach_only(docname: str) -> dict:
def process_check_run(docname: str) -> None:
doc = frappe.get_doc("Check Run", docname)
doc.process_check_run()


@frappe.whitelist(allow_guest=True)
def download_pdf(
doctype,
name,
formattype=None,
print_format=None,
doc=None,
no_letterhead=0,
language=None,
letterhead=None,
baseurl=None,
printcss=None,
):
doc = doc or frappe.get_doc(doctype, name)
validate_print_permission(doc)
from check_run.www.print_check_run import get_check_run_format

out = get_check_run_format(
doc, name=doc.name, doctype_to_print=formattype, print_format=print_format
)
data = ""
for d in out.get("html"):
data += d[0]

style = out.get("style")

hide_image = """
@media print {
@print {
margin: 0;
}

.back_image {
background: none !important;
}
}
"""

html = """<style type='text/css'>{}</style><link href={}{} rel='stylesheet'>""".format(
style, baseurl, printcss
)

html += f"<div class='print-format print-format-preview'>{data}</div>"

modified_html_string = html.replace("</style>", hide_image + "</style>")
html = modified_html_string
with print_language(language):
pdf_file = frappe.get_print(
doctype,
name,
html=html,
doc=doc,
as_pdf=True,
letterhead=letterhead,
no_letterhead=no_letterhead,
)

frappe.local.response.filename = "{name}.pdf".format(
name=name.replace(" ", "-").replace("/", "-")
)
frappe.local.response.filecontent = pdf_file
frappe.local.response.type = "pdf"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"actions": [],
"allow_rename": 1,
"creation": "2024-07-31 17:10:07.841441",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": ["mode_of_payment"],
"fields": [
{
"fieldname": "mode_of_payment",
"fieldtype": "Link",
"in_list_view": 1,
"label": "Mode of Payment",
"options": "Mode of Payment"
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2024-07-31 17:10:56.744398",
"modified_by": "Administrator",
"module": "Check Run",
"name": "Check Run Printable MOP",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC",
"states": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2024, AgriTheory and contributors
# For license information, please see license.txt

# import frappe
from frappe.model.document import Document


class CheckRunPrintableMOP(Document):
pass
Loading
Loading