-
Notifications
You must be signed in to change notification settings - Fork 36
feat(Bank Reconciliation): add rule-based auto booking #347
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
Merged
barredterra
merged 11 commits into
version-15-hotfix
from
split/bank-reconciliation-rule
Mar 30, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6da2c30
feat(Bank Reconciliation): add rule-based auto booking
barredterra bc74d14
chore(Bank Reconciliation): bump model timestamps for sync
barredterra 6da5a2e
fix: mark Journal Entry as system generated and reference Bank Transa…
barredterra 1f49dfb
fix: set Journal Entry clearance date
barredterra 4ef363e
fix: typo in description
barredterra e2b6edb
fix: order rules by priority and creation
barredterra 0f60cf3
fix: guard against missing company account
barredterra e3c8a76
Merge branch 'version-15-hotfix' into split/bank-reconciliation-rule
barredterra 0d9abbd
refactor: consolidate testing utils
barredterra ab9ec8c
test: scope delete to bank account
barredterra 693767c
fix: parse JSON to validate empty rule
barredterra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright (c) 2025, ALYF GmbH and Contributors | ||
| # See license.txt | ||
|
|
||
| from frappe.exceptions import ValidationError | ||
|
|
||
|
|
||
| class CurrencyMismatchError(ValidationError): | ||
| """Raised when two accounts unexpectedly have different currencies.""" | ||
|
|
||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
79 changes: 79 additions & 0 deletions
79
...ing/klarna_kosma_integration/doctype/bank_reconciliation_rule/bank_reconciliation_rule.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) 2025, ALYF GmbH and contributors | ||
| // For license information, please see license.txt | ||
|
|
||
| frappe.ui.form.on("Bank Reconciliation Rule", { | ||
| onload: function (frm) { | ||
| frm.trigger("render_bt_filters"); | ||
| }, | ||
| refresh(frm) { | ||
| frm.trigger("set_target_account_query"); | ||
| }, | ||
| bank_account(frm) { | ||
| frm.trigger("set_target_account_query"); | ||
| }, | ||
| async set_target_account_query(frm) { | ||
| if (!frm.doc.bank_account) { | ||
| return; | ||
| } | ||
|
|
||
| const { | ||
| message: { account }, | ||
| } = await frappe.db.get_value( | ||
| "Bank Account", | ||
| frm.doc.bank_account, | ||
| "account" | ||
| ); | ||
|
|
||
| if (!account) { | ||
| frappe.throw( | ||
| __("In {0} {1} the field {2} is not set.", [ | ||
| frappe.bold(__("Bank Account")), | ||
| frm.doc.bank_account, | ||
| __("Company Account"), | ||
| ]) | ||
| ); | ||
| } | ||
|
|
||
| const { | ||
| message: { account_currency: currency, company: company }, | ||
| } = await frappe.db.get_value("Account", account, [ | ||
| "account_currency", | ||
| "company", | ||
| ]); | ||
|
|
||
| frm.set_query("target_account", () => ({ | ||
| filters: { | ||
| account_currency: currency, | ||
| company: company, | ||
| }, | ||
| })); | ||
| }, | ||
| render_bt_filters(frm) { | ||
| const parent = frm.fields_dict.filter_area.$wrapper; | ||
| parent.empty(); | ||
|
|
||
| const filters = | ||
| frm.doc.filters && frm.doc.filters !== "[]" | ||
| ? JSON.parse(frm.doc.filters) | ||
| : []; | ||
|
|
||
| frappe.model.with_doctype("Bank Transaction", () => { | ||
| const filter_group = new frappe.ui.FilterGroup({ | ||
| parent: parent, | ||
| doctype: "Bank Transaction", | ||
| on_change: () => { | ||
| frm.set_value("filters", JSON.stringify(filter_group.get_filters())); | ||
| }, | ||
| }); | ||
|
|
||
| filter_group.add_filters_to_filter_group(filters); | ||
|
|
||
| if (frm.doc.docstatus === 1) { | ||
| parent.find(".filter-action-buttons").remove(); | ||
| parent.find(".divider").remove(); | ||
| parent.find(".remove-filter").remove(); | ||
| parent.find(".form-control").prop("disabled", true); | ||
| } | ||
| }); | ||
| }, | ||
| }); | ||
149 changes: 149 additions & 0 deletions
149
...g/klarna_kosma_integration/doctype/bank_reconciliation_rule/bank_reconciliation_rule.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| { | ||
| "actions": [], | ||
| "creation": "2025-10-23 13:00:49.226504", | ||
| "doctype": "DocType", | ||
| "engine": "InnoDB", | ||
| "field_order": [ | ||
| "account_settings_section", | ||
| "bank_account", | ||
| "target_account", | ||
| "column_break_dvyd", | ||
| "priority", | ||
| "disabled", | ||
| "filters_section", | ||
| "filter_description", | ||
| "filters", | ||
| "filter_area", | ||
| "amended_from" | ||
| ], | ||
| "fields": [ | ||
| { | ||
| "fieldname": "bank_account", | ||
| "fieldtype": "Link", | ||
| "in_list_view": 1, | ||
| "label": "Bank Account", | ||
| "link_filters": "[[\"Bank Account\",\"is_company_account\",\"=\",1],[\"Bank Account\",\"account\",\"is\",\"set\"]]", | ||
| "options": "Bank Account", | ||
| "reqd": 1 | ||
| }, | ||
| { | ||
| "fieldname": "account_settings_section", | ||
| "fieldtype": "Section Break", | ||
| "label": "Account Settings" | ||
| }, | ||
| { | ||
| "fieldname": "target_account", | ||
| "fieldtype": "Link", | ||
| "in_list_view": 1, | ||
| "label": "Target Account", | ||
| "options": "Account", | ||
| "reqd": 1 | ||
| }, | ||
| { | ||
| "fieldname": "filters_section", | ||
| "fieldtype": "Section Break", | ||
| "label": "Filters" | ||
| }, | ||
| { | ||
| "fieldname": "filters", | ||
| "fieldtype": "Code", | ||
| "hidden": 1, | ||
| "label": "Filters", | ||
| "options": "JSON", | ||
| "read_only": 1 | ||
| }, | ||
| { | ||
| "fieldname": "filter_area", | ||
| "fieldtype": "HTML" | ||
| }, | ||
| { | ||
| "fieldname": "filter_description", | ||
| "fieldtype": "HTML", | ||
| "options": "A <b>Journal Entry</b> is created automatically, when a submitted <b>Bank Transaction</b> matches these filters.<br>Please define these filters as strictly as possible to avoid accounting errors.<br><br>" | ||
| }, | ||
| { | ||
| "fieldname": "amended_from", | ||
| "fieldtype": "Link", | ||
| "label": "Amended From", | ||
| "no_copy": 1, | ||
| "options": "Bank Reconciliation Rule", | ||
| "print_hide": 1, | ||
| "read_only": 1, | ||
| "search_index": 1 | ||
| }, | ||
| { | ||
| "allow_on_submit": 1, | ||
| "default": "0", | ||
| "fieldname": "disabled", | ||
| "fieldtype": "Check", | ||
| "label": "Disabled" | ||
| }, | ||
| { | ||
| "fieldname": "column_break_dvyd", | ||
| "fieldtype": "Column Break" | ||
| }, | ||
| { | ||
| "description": "If multiple rules match a <b>Bank Transaction</b>, only the one with the highest priority is executed.", | ||
| "fieldname": "priority", | ||
| "fieldtype": "Int", | ||
| "in_list_view": 1, | ||
| "label": "Priority", | ||
| "non_negative": 1 | ||
| } | ||
| ], | ||
| "grid_page_length": 50, | ||
| "index_web_pages_for_search": 1, | ||
| "is_submittable": 1, | ||
| "links": [], | ||
barredterra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "modified": "2026-03-28 02:28:06.918430", | ||
| "modified_by": "Administrator", | ||
| "module": "Klarna Kosma Integration", | ||
| "name": "Bank Reconciliation Rule", | ||
| "owner": "Administrator", | ||
| "permissions": [ | ||
| { | ||
| "amend": 1, | ||
| "cancel": 1, | ||
| "create": 1, | ||
| "delete": 1, | ||
| "email": 1, | ||
| "export": 1, | ||
| "print": 1, | ||
| "read": 1, | ||
| "report": 1, | ||
| "role": "System Manager", | ||
| "share": 1, | ||
| "submit": 1, | ||
| "write": 1 | ||
| }, | ||
| { | ||
| "cancel": 1, | ||
| "create": 1, | ||
| "email": 1, | ||
| "export": 1, | ||
| "print": 1, | ||
| "read": 1, | ||
| "report": 1, | ||
| "role": "Accounts Manager", | ||
| "share": 1, | ||
| "submit": 1, | ||
| "write": 1 | ||
| }, | ||
| { | ||
| "create": 1, | ||
| "email": 1, | ||
| "export": 1, | ||
| "print": 1, | ||
| "read": 1, | ||
| "report": 1, | ||
| "role": "Accounts User", | ||
| "share": 1, | ||
| "write": 1 | ||
| } | ||
| ], | ||
| "row_format": "Dynamic", | ||
| "rows_threshold_for_grid_search": 20, | ||
| "sort_field": "modified", | ||
| "sort_order": "DESC", | ||
| "states": [] | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
...ing/klarna_kosma_integration/doctype/bank_reconciliation_rule/bank_reconciliation_rule.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright (c) 2025, ALYF GmbH and contributors | ||
| # For license information, please see license.txt | ||
|
|
||
| import json | ||
|
|
||
| import frappe | ||
| from frappe import _ | ||
| from frappe.exceptions import ValidationError | ||
| from frappe.model.document import Document | ||
|
|
||
| from banking.exceptions import CurrencyMismatchError | ||
|
|
||
|
|
||
| class NoFiltersError(ValidationError): | ||
| pass | ||
|
|
||
|
|
||
| class BankReconciliationRule(Document): | ||
| # begin: auto-generated types | ||
| # This code is auto-generated. Do not modify anything in this block. | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from frappe.types import DF | ||
|
|
||
| amended_from: DF.Link | None | ||
| bank_account: DF.Link | ||
| disabled: DF.Check | ||
| filters: DF.Code | None | ||
| priority: DF.Int | ||
| target_account: DF.Link | ||
|
|
||
| # end: auto-generated types | ||
| def validate(self): | ||
| self.validate_account_currencies() | ||
| self.validate_filters() | ||
|
|
||
| def validate_account_currencies(self): | ||
| bank_account = frappe.db.get_value("Bank Account", self.bank_account, "account") | ||
| bank_account_currency = frappe.db.get_value("Account", bank_account, "account_currency") | ||
| target_account_currency = frappe.db.get_value("Account", self.target_account, "account_currency") | ||
|
|
||
| if bank_account_currency != target_account_currency: | ||
| frappe.throw( | ||
| _("Bank Account and Target Account need to be in the same currency!"), CurrencyMismatchError | ||
| ) | ||
|
|
||
| def validate_filters(self): | ||
| if not self.filters or not json.loads(self.filters): | ||
| frappe.throw(_("Please define at least one filter!"), NoFiltersError) | ||
barredterra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
23 changes: 23 additions & 0 deletions
23
...larna_kosma_integration/doctype/bank_reconciliation_rule/bank_reconciliation_rule_list.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright (c) 2025, ALYF GmbH and contributors | ||
| // For license information, please see license.txt | ||
|
|
||
| frappe.listview_settings["Bank Reconciliation Rule"] = { | ||
| add_fields: ["docstatus", "disabled"], | ||
| has_indicator_for_draft: true, | ||
|
|
||
| get_indicator: function (doc) { | ||
| // Submitted documents | ||
| if (doc.docstatus === 1) { | ||
| if (doc.disabled === 1) { | ||
| return [__("Disabled"), "orange", "disabled,=,1"]; | ||
| } else { | ||
| return [__("Active"), "green", "docstatus,=,1"]; | ||
| } | ||
| } | ||
|
|
||
| // Draft documents | ||
| if (doc.docstatus === 0) { | ||
| return [__("Draft"), "blue", "docstatus,=,0"]; | ||
| } | ||
| }, | ||
| }; |
40 changes: 40 additions & 0 deletions
40
...larna_kosma_integration/doctype/bank_reconciliation_rule/test_bank_reconciliation_rule.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Copyright (c) 2025, ALYF GmbH and Contributors | ||
| # See license.txt | ||
|
|
||
| import frappe | ||
| from frappe.tests.utils import FrappeTestCase | ||
|
|
||
| from banking.exceptions import CurrencyMismatchError | ||
| from banking.klarna_kosma_integration.doctype.bank_reconciliation_rule.bank_reconciliation_rule import ( | ||
| NoFiltersError, | ||
| ) | ||
| from banking.testing_utils import TEST_COMPANY, create_bank_account, create_currency_account | ||
|
|
||
|
|
||
| class TestBankReconciliationRule(FrappeTestCase): | ||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
|
|
||
| parent_account = frappe.db.get_value("Account", {"is_group": 1, "company": TEST_COMPANY}) | ||
| bank_account = create_currency_account("EUR", parent_account, "_Test_Account_EUR") | ||
| cls.target_account = create_currency_account("USD", parent_account, "_Test_Account_USD") | ||
| cls.ba = create_bank_account(bank_account.name) | ||
|
|
||
| def test_validate_account_currencies(self): | ||
| brr_doc = frappe.new_doc("Bank Reconciliation Rule") | ||
| brr_doc.bank_account = self.ba.name | ||
| brr_doc.target_account = self.target_account.name | ||
|
|
||
| with self.assertRaises(CurrencyMismatchError): | ||
| brr_doc.validate_account_currencies() | ||
|
|
||
| def test_validate_filters(self): | ||
| brr_doc = frappe.new_doc("Bank Reconciliation Rule") | ||
| brr_doc.filters = None | ||
| with self.assertRaises(NoFiltersError): | ||
| brr_doc.validate_filters() | ||
|
|
||
| brr_doc.filters = "[]" | ||
| with self.assertRaises(NoFiltersError): | ||
| brr_doc.validate_filters() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.