diff --git a/erpnext/accounts/doctype/payment_request/payment_request.py b/erpnext/accounts/doctype/payment_request/payment_request.py index 9b2b3c8a824ec60699ac0e11c20e5dc19a7b4f95..32020b88772754c4d427f05d0c30dd146742b2cd 100644 --- a/erpnext/accounts/doctype/payment_request/payment_request.py +++ b/erpnext/accounts/doctype/payment_request/payment_request.py @@ -365,9 +365,7 @@ class PaymentRequest(Document): {"payment_gateway": self.payment_gateway, "customer_id": customer_id}, ) if not customer.mode_of_payment: - customer.mode_of_payment = frappe.get_cached_value( - "Mode of Payment", dict(payment_gateway=self.payment_gateway, enabled=True) - ) + customer.mode_of_payment = get_mode_of_payment_for_company(self.company, self.payment_gateway) customer.flags.ignore_permissions = True customer.save() @@ -1009,3 +1007,26 @@ def make_payment_order(source_name, target_doc=None): ) return doclist + +def get_mode_of_payment_for_company(company, payment_gateway): + mode_of_payment = frappe.qb.DocType("Mode of Payment") + mode_of_payment_account = frappe.qb.DocType("Mode of Payment Account") + payment_gateway_dt = frappe.qb.DocType("Payment Gateway") + query = ( + frappe.qb.from_(mode_of_payment_account) + .left_join(mode_of_payment) + .on(mode_of_payment_account.parent == mode_of_payment.name) + .left_join(payment_gateway_dt) + .on(mode_of_payment_account.payment_gateway == payment_gateway_dt.name) + .select(mode_of_payment.name) + .where( + mode_of_payment.enabled + & (mode_of_payment_account.company == company) + & (mode_of_payment_account.payment_gateway == payment_gateway) + & (payment_gateway_dt.disabled == 0) + ) + ) + + mode_of_payments = query.run(as_dict=True, pluck=True) + + return mode_of_payments[0] if mode_of_payments else None diff --git a/erpnext/accounts/doctype/subscription/patches/refactor_subscriptions_model.py b/erpnext/accounts/doctype/subscription/patches/refactor_subscriptions_model.py index 619fe1fcc2dd6c62686463aed6d8d2e6283a6422..0fc76a62b2780f489e2c73275d6a9dd2f0d8d352 100644 --- a/erpnext/accounts/doctype/subscription/patches/refactor_subscriptions_model.py +++ b/erpnext/accounts/doctype/subscription/patches/refactor_subscriptions_model.py @@ -3,6 +3,7 @@ from collections import defaultdict import frappe from frappe import _ +from erpnext.accounts.doctype.payment_request.payment_request import get_mode_of_payment_for_company from erpnext.setup.setup_wizard.operations.install_fixtures import create_recurrence_periods @@ -15,7 +16,6 @@ def execute(): frappe.reload_doc("selling", "doctype", "Recurrence Period") for dt in ["Subscription Template", "Subscription"]: - fields = [ "billing_interval", "billing_interval_count", @@ -134,9 +134,7 @@ def fix_new_orders_status(): if si := frappe.db.get_value( "Sales Invoice", - dict( - subscription=doc.name, from_date=doc.current_invoice_start, to_date=doc.current_invoice_end - ), + dict(subscription=doc.name, from_date=doc.current_invoice_start, to_date=doc.current_invoice_end), ): doc.set_state("sales_invoice", si) @@ -150,10 +148,10 @@ def fix_new_orders_status(): if prs := frappe.get_all( "Payment Request", filters=dict(subscription=doc.name, status="Paid"), - fields=["name", "payment_gateway"], + fields=["name", "payment_gateway", "company"], order_by="transaction_date DESC", ): - if mop := frappe.db.get_value("Mode of Payment", dict(payment_gateway=prs[0].payment_gateway)): + if mop := get_mode_of_payment_for_company(prs[0].company, prs[0].payment_gateway): doc.payment_gateway = mop doc.db_set("payment_gateway", mop) diff --git a/erpnext/venue/doctype/event_registration/event_registration.py b/erpnext/venue/doctype/event_registration/event_registration.py index b273249bd0353ec28d27bdfe0739b52c51044bbd..1436399324a51a5af4d296a72d1262710e557f1f 100644 --- a/erpnext/venue/doctype/event_registration/event_registration.py +++ b/erpnext/venue/doctype/event_registration/event_registration.py @@ -8,6 +8,7 @@ from frappe import _ from frappe.model.document import Document from frappe.utils import cint, is_desk +from erpnext.accounts.doctype.payment_request.payment_request import get_mode_of_payment_for_company from erpnext.utilities import webshop_app_import_guard @@ -425,8 +426,8 @@ class EventRegistration(Document): fee_amount = 0.0 # exchange_rate = 1.0 # TODO - mode_of_payment = mode_of_payment or frappe.db.get_value( - "Mode of Payment", dict(payment_gateway=payment_gateway.name, enabled=1) + mode_of_payment = mode_of_payment or get_mode_of_payment_for_company( + details.company, payment_gateway.name )