-
Notifications
You must be signed in to change notification settings - Fork 36
feat: create SEPA Payment Order from Expense Claim #352
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
Open
MarcCon
wants to merge
15
commits into
version-15-hotfix
Choose a base branch
from
create-sepa-from-expense
base: version-15-hotfix
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4a8ec11
feat(Expense Claim): create sepa payment
MarcCon ba5350f
feat(Expense Claim): create bulk sepa from list view
MarcCon 7ff8c8f
feat(Expense Claim): add button for creating sepa payment
MarcCon 96f2768
style: formatting
MarcCon 2a80b54
feat(Expense Claim): add custom field and hook for sepa status
MarcCon 9027a5a
feat(Expense Claim): add sepa payment order status change
MarcCon 5a5bdc4
feat: adjust filters for sepa status
MarcCon e47420c
style: linter fix
MarcCon 56bc3ca
style: linter fix 2
MarcCon 4d597fd
fix: call patch
MarcCon 3727e5d
fix(Expense Claim): get iban from employee
MarcCon 1bd4270
fix(Expense Claim): prevent crash when hrms is absent
MarcCon 82d175f
fix(Expense Claim): prevent duplicate SEPA Payment Order creation
MarcCon c938636
fix(Expense Claim): prevent payment order with amount 0
MarcCon 801083a
Merge branch 'version-15-hotfix' into create-sepa-from-expense
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,33 @@ | ||
| frappe.ui.form.on("Expense Claim", { | ||
| setup(frm) { | ||
| frm.make_methods = frm.make_methods || {}; | ||
| frm.make_methods["SEPA Payment Order"] = () => { | ||
| frm.trigger("make_sepa_payment_order"); | ||
| }; | ||
| }, | ||
|
|
||
| refresh(frm) { | ||
| const has_outstanding = | ||
| frm.doc.status !== "Paid" && | ||
| frm.doc.docstatus === 1 && | ||
| frm.doc.approval_status === "Approved" && | ||
| !frm.doc.sepa_payment_order_status && | ||
| flt(frm.doc.grand_total) - flt(frm.doc.total_amount_reimbursed) > 0; | ||
|
|
||
| if (has_outstanding) { | ||
| frm.add_custom_button( | ||
| __("SEPA Payment Order"), | ||
| () => frm.trigger("make_sepa_payment_order"), | ||
| __("Create") | ||
| ); | ||
| } | ||
| }, | ||
|
|
||
| make_sepa_payment_order(frm) { | ||
| frappe.model.open_mapped_doc({ | ||
| method: "banking.custom.expense_claim.make_sepa_payment_order", | ||
| frm: frm, | ||
| freeze_message: __("Creating SEPA Payment Order ..."), | ||
| }); | ||
| }, | ||
| }); |
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,144 @@ | ||
| from datetime import date | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import frappe | ||
| from frappe import _ | ||
| from frappe.model.document import Document | ||
| from frappe.model.mapper import get_mapped_doc | ||
| from frappe.utils import flt | ||
| from frappe.utils.data import getdate | ||
|
|
||
| from banking.ebics.doctype.sepa_payment_order.sepa_payment_order import PaymentOrderStatus | ||
|
|
||
| if TYPE_CHECKING: | ||
| from hrms.hr.doctype.expense_claim.expense_claim import ExpenseClaim | ||
| from hrms.hr.doctype.expense_claim_detail.expense_claim_detail import ExpenseClaimDetail | ||
|
|
||
| from banking.ebics.doctype.sepa_payment.sepa_payment import SEPAPayment | ||
|
|
||
|
|
||
| @frappe.whitelist() | ||
| def make_sepa_payment_order(source_name: str, target_doc: str | Document | None = None): | ||
| if frappe.db.get_value("Expense Claim", source_name, "sepa_payment_order_status"): | ||
| frappe.throw(_("A SEPA Payment Order already exists for Expense Claim {0}.").format(source_name)) | ||
|
|
||
| def set_missing_values(source, target): | ||
| if not target.bank_account: | ||
| bank_account = frappe.db.get_value( | ||
| "Bank Account", | ||
| {"is_company_account": 1, "company": source.company, "disabled": 0}, | ||
| ["name", "iban", "bank"], | ||
| order_by="is_default DESC", | ||
| as_dict=True, | ||
| ) | ||
| if bank_account: | ||
| target.bank_account = bank_account.get("name") | ||
| target.iban = bank_account.get("iban") | ||
| target.bank = bank_account.get("bank") | ||
|
|
||
| def process_payment(source: "ExpenseClaimDetail", target: "SEPAPayment", source_parent: "ExpenseClaim"): | ||
| claim = source_parent | ||
| target.recipient = claim.employee_name | ||
| target.purpose = _get_employee_purpose(claim) | ||
|
|
||
| bank_account = _get_recipients_bank_account(claim) | ||
|
|
||
| if bank_account and bank_account.get("iban"): | ||
| target.iban = bank_account.get("iban") | ||
| if bank_account.get("bank"): | ||
| swift_number, bank_name = frappe.db.get_value( | ||
| "Bank", bank_account["bank"], ["swift_number", "bank_name"] | ||
| ) | ||
| target.swift_number = swift_number | ||
| target.bank_name = bank_name | ||
| else: | ||
| employee_iban = frappe.db.get_value("Employee", claim.employee, "iban") | ||
| if employee_iban: | ||
| target.iban = employee_iban | ||
| else: | ||
| frappe.throw(_("No IBAN found for Employee {0}.").format(claim.employee)) | ||
|
|
||
| target.currency = frappe.db.get_value("Company", claim.company, "default_currency") | ||
| target.eref = target.reference_name | ||
| target.amount = get_sepa_payment_amount( | ||
| claim, | ||
| method="get_mapped_doc", | ||
| reference_row_name=source.name, | ||
| execution_date=getdate(), | ||
| ) | ||
|
|
||
| return get_mapped_doc( | ||
| "Expense Claim", | ||
| source_name, | ||
| { | ||
| "Expense Claim": { | ||
| "doctype": "SEPA Payment Order", | ||
| "validation": { | ||
| "docstatus": ("=", 1), | ||
| "approval_status": ("=", "Approved"), | ||
| "status": ("!=", "Paid"), | ||
| }, | ||
| }, | ||
| "Expense Claim Detail": { | ||
| "doctype": "SEPA Payment", | ||
| "field_map": { | ||
| "name": "reference_row_name", | ||
| "parent": "reference_name", | ||
| "parenttype": "reference_doctype", | ||
| }, | ||
| "condition": lambda row: row.idx == 1 | ||
| and flt(row.parent_doc.grand_total) - flt(row.parent_doc.total_amount_reimbursed) > 0.0, | ||
| "postprocess": process_payment, | ||
| }, | ||
| }, | ||
| target_doc, | ||
| postprocess=set_missing_values, | ||
| ) | ||
MarcCon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def _get_employee_purpose(claim: "ExpenseClaim"): | ||
| """Return the bank transfer purpose for an expense claim reimbursement. | ||
|
|
||
| Example: "EC-00001, 2025-03-01" | ||
| """ | ||
| reference = ", ".join(str(ref).strip() for ref in [claim.name, claim.posting_date] if ref) | ||
| return reference.strip() | ||
|
|
||
|
|
||
| def _get_recipients_bank_account(claim: "ExpenseClaim"): | ||
| return frappe.db.get_value( | ||
| "Bank Account", | ||
| {"party_type": "Employee", "party": claim.employee, "disabled": 0}, | ||
| ["iban", "bank"], | ||
| order_by="is_default DESC", | ||
| as_dict=True, | ||
| ) | ||
|
|
||
|
|
||
| @frappe.whitelist() | ||
| def make_bulk_sepa_payment_order(source_names: str): | ||
| target_doc = None | ||
| for source_name in frappe.parse_json(source_names): | ||
| if not isinstance(source_name, str): | ||
| raise TypeError | ||
|
|
||
| target_doc = make_sepa_payment_order(source_name, target_doc) | ||
|
|
||
| return target_doc | ||
|
|
||
|
|
||
| def sepa_payment_order_status_changed( | ||
| doc: "ExpenseClaim", method: str, reference_row_name: str, status: PaymentOrderStatus | ||
| ): | ||
| """Called via hooks when a linked SEPA Payment Order changes.""" | ||
| doc.sepa_payment_order_status = status.value | ||
| doc.save(ignore_permissions=True) | ||
|
|
||
|
|
||
| def get_sepa_payment_amount( | ||
| doc: "ExpenseClaim", method: str, reference_row_name: str, execution_date: date | ||
| ) -> float: | ||
| """Return outstanding amount. No discount logic.""" | ||
| precision = doc.precision("grand_total") | ||
| outstanding = flt(doc.grand_total, precision) - flt(doc.total_amount_reimbursed, precision) | ||
| return max(0, outstanding) | ||
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,57 @@ | ||
| frappe.listview_settings["Expense Claim"] = | ||
| frappe.listview_settings["Expense Claim"] || {}; | ||
| const old_onload = frappe.listview_settings["Expense Claim"].onload; | ||
| const old_add_fields = | ||
| frappe.listview_settings["Expense Claim"].add_fields || []; | ||
| frappe.listview_settings["Expense Claim"].add_fields = [ | ||
MarcCon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ...old_add_fields, | ||
| "approval_status", | ||
| "sepa_payment_order_status", | ||
| ]; | ||
|
|
||
| frappe.listview_settings["Expense Claim"].onload = function (listview) { | ||
| if (old_onload) old_onload(listview); | ||
|
|
||
| if (frappe.model.can_create("SEPA Payment Order")) { | ||
| listview.page.add_action_item(__("SEPA Payment Order"), () => { | ||
| const claims_to_pay = listview | ||
| .get_checked_items() | ||
| .filter( | ||
| (item) => | ||
| item.status !== "Paid" && | ||
| item.docstatus === 1 && | ||
| item.approval_status === "Approved" && | ||
| !item.sepa_payment_order_status | ||
| ) | ||
| .map((item) => item.name); | ||
| if (!claims_to_pay.length) { | ||
| frappe.msgprint( | ||
| __( | ||
| "Only submitted, approved and unpaid Expense Claims without an existing SEPA Payment Order can be used. Rejected, paid or already linked claims were ignored." | ||
| ), | ||
| __("SEPA Payment Order") | ||
| ); | ||
| return; | ||
| } | ||
| frappe.call({ | ||
| type: "POST", | ||
| method: "banking.custom.expense_claim.make_bulk_sepa_payment_order", | ||
| args: { | ||
| source_names: claims_to_pay, | ||
| }, | ||
| freeze: true, | ||
| freeze_message: __("Creating SEPA Payment Order ..."), | ||
| callback: function (r) { | ||
| if (!r.exc && r.message) { | ||
| frappe.model.sync(r.message); | ||
| frappe.get_doc( | ||
| r.message.doctype, | ||
| r.message.name | ||
| ).__run_link_triggers = true; | ||
| frappe.set_route("Form", r.message.doctype, r.message.name); | ||
| } | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
| }; | ||
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
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
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.
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.