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

feat: migrate validation to doc_events from doctype override #127

Merged
merged 5 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 8 additions & 7 deletions check_run/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,26 @@
# ---------------
# Override standard doctype classes

override_doctype_class = {"Bank": "check_run.overrides.bank.CustomBank"}
# override_doctype_class = {"Bank": "check_run.overrides.bank.CustomBank"}

# Document Events
# ---------------
# Hook on document methods and events

doc_events = {
"Payment Entry": {
"on_submit": "check_run.overrides.payment_entry.update_check_number",
},
"Purchase Invoice": {
"before_cancel": ["check_run.check_run.disallow_cancellation_if_in_check_run"]
},
"Bank": {"validate": ["check_run.overrides.bank.validate"]},
"Expense Claim": {
"before_cancel": ["check_run.check_run.disallow_cancellation_if_in_check_run"]
},
"Journal Entry": {
"before_cancel": ["check_run.check_run.disallow_cancellation_if_in_check_run"]
},
"Payment Entry": {
"on_submit": "check_run.overrides.payment_entry.update_check_number",
},
"Purchase Invoice": {
"before_cancel": ["check_run.check_run.disallow_cancellation_if_in_check_run"]
},
}

# Scheduled Tasks
Expand Down
44 changes: 20 additions & 24 deletions check_run/overrides/bank.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import frappe
from frappe import _
from frappe.utils import cint, flt

from erpnext.accounts.doctype.bank.bank import Bank


class CustomBank(Bank):
def validate(self):
# Canadian banking institutions limit DFI Routing Numbers to 8 characters
countries = frappe.db.sql(
"""
select a.country
from `tabAddress` a, `tabDynamic Link` dl
where dl.parent = a.name and dl.parenttype = 'Address'
and dl.link_doctype = 'Bank' and dl.link_name = %s
""",
(self.name),
as_dict=True,
)
if "Canada" in [c["country"] for c in countries]:
if len(self.aba_number) > 8:
frappe.throw(
_(
"This Bank is linked to at least one Canadian address. Canadian banking institutions require the ABA Number must not exceed 8 characters."
)
@frappe.whitelist()
def validate(doc, method=None):
# Canadian banking institutions limit DFI Routing Numbers to 8 characters
countries = frappe.db.sql(
"""
select a.country
from `tabAddress` a, `tabDynamic Link` dl
where dl.parent = a.name and dl.parenttype = 'Address'
and dl.link_doctype = 'Bank' and dl.link_name = %s
""",
(doc.name),
as_dict=True,
)
if "Canada" in [c["country"] for c in countries]:
if len(doc.aba_number) > 8:
frappe.throw(
frappe._(
"This Bank is linked to at least one Canadian address. Canadian banking institutions require the ABA Number must not exceed 8 characters."
)
return
)
return