refactor(Payment Entry): reduce indentation (#46864)

This commit is contained in:
Raffael Meyer
2025-04-05 16:41:23 +02:00
committed by GitHub
parent 7d12e9afd4
commit e25517a3ce

View File

@@ -339,7 +339,9 @@ class PaymentEntry(AccountsController):
reference_names.add(key) reference_names.add(key)
def set_bank_account_data(self): def set_bank_account_data(self):
if self.bank_account: if not self.bank_account:
return
bank_data = get_bank_account_details(self.bank_account) bank_data = get_bank_account_details(self.bank_account)
field = "paid_from" if self.payment_type == "Pay" else "paid_to" field = "paid_from" if self.payment_type == "Pay" else "paid_to"
@@ -366,7 +368,8 @@ class PaymentEntry(AccountsController):
if self.party_type in ("Customer", "Supplier"): if self.party_type in ("Customer", "Supplier"):
self.validate_allocated_amount_with_latest_data() self.validate_allocated_amount_with_latest_data()
else: return
fail_message = _("Row #{0}: Allocated Amount cannot be greater than outstanding amount.") fail_message = _("Row #{0}: Allocated Amount cannot be greater than outstanding amount.")
for d in self.get("references"): for d in self.get("references"):
if (flt(d.allocated_amount)) > 0 and flt(d.allocated_amount) > flt(d.outstanding_amount): if (flt(d.allocated_amount)) > 0 and flt(d.allocated_amount) > flt(d.outstanding_amount):
@@ -412,8 +415,10 @@ class PaymentEntry(AccountsController):
return False return False
def validate_allocated_amount_with_latest_data(self): def validate_allocated_amount_with_latest_data(self):
if self.references: if not self.references:
uniq_vouchers = set([(x.reference_doctype, x.reference_name) for x in self.references]) return
uniq_vouchers = {(x.reference_doctype, x.reference_name) for x in self.references}
vouchers = [frappe._dict({"voucher_type": x[0], "voucher_no": x[1]}) for x in uniq_vouchers] vouchers = [frappe._dict({"voucher_type": x[0], "voucher_no": x[1]}) for x in uniq_vouchers]
latest_references = get_outstanding_reference_documents( latest_references = get_outstanding_reference_documents(
{ {
@@ -455,9 +460,7 @@ class PaymentEntry(AccountsController):
# The reference has already been fully paid # The reference has already been fully paid
if not latest: if not latest:
frappe.throw( frappe.throw(
_("{0} {1} has already been fully paid.").format( _("{0} {1} has already been fully paid.").format(_(d.reference_doctype), d.reference_name)
_(d.reference_doctype), d.reference_name
)
) )
# The reference has already been partly paid # The reference has already been partly paid
elif ( elif (
@@ -481,9 +484,7 @@ class PaymentEntry(AccountsController):
and latest.payment_term_outstanding and latest.payment_term_outstanding
and (flt(d.allocated_amount) > flt(latest.payment_term_outstanding)) and (flt(d.allocated_amount) > flt(latest.payment_term_outstanding))
) )
and self.term_based_allocation_enabled_for_reference( and self.term_based_allocation_enabled_for_reference(d.reference_doctype, d.reference_name)
d.reference_doctype, d.reference_name
)
): ):
frappe.throw( frappe.throw(
_( _(
@@ -562,7 +563,9 @@ class PaymentEntry(AccountsController):
reference_exchange_details: dict | None = None, reference_exchange_details: dict | None = None,
) -> None: ) -> None:
for d in self.get("references"): for d in self.get("references"):
if d.allocated_amount: if not d.allocated_amount:
continue
if ( if (
update_ref_details_only_for update_ref_details_only_for
and (d.reference_doctype, d.reference_name) not in update_ref_details_only_for and (d.reference_doctype, d.reference_name) not in update_ref_details_only_for
@@ -604,8 +607,7 @@ class PaymentEntry(AccountsController):
frappe.throw(_("Payment Type must be one of Receive, Pay and Internal Transfer")) frappe.throw(_("Payment Type must be one of Receive, Pay and Internal Transfer"))
def validate_party_details(self): def validate_party_details(self):
if self.party: if self.party and not frappe.db.exists(self.party_type, self.party):
if not frappe.db.exists(self.party_type, self.party):
frappe.throw(_("{0} {1} does not exist").format(_(self.party_type), self.party)) frappe.throw(_("{0} {1} does not exist").format(_(self.party_type), self.party))
def set_exchange_rate(self, ref_doc=None): def set_exchange_rate(self, ref_doc=None):
@@ -616,12 +618,8 @@ class PaymentEntry(AccountsController):
if self.paid_from: if self.paid_from:
if self.paid_from_account_currency == self.company_currency: if self.paid_from_account_currency == self.company_currency:
self.source_exchange_rate = 1 self.source_exchange_rate = 1
else: elif ref_doc and self.paid_from_account_currency == ref_doc.currency:
if ref_doc: self.source_exchange_rate = ref_doc.get("exchange_rate") or ref_doc.get("conversion_rate")
if self.paid_from_account_currency == ref_doc.currency:
self.source_exchange_rate = ref_doc.get("exchange_rate") or ref_doc.get(
"conversion_rate"
)
if not self.source_exchange_rate: if not self.source_exchange_rate:
self.source_exchange_rate = get_exchange_rate( self.source_exchange_rate = get_exchange_rate(
@@ -632,8 +630,7 @@ class PaymentEntry(AccountsController):
if self.paid_from_account_currency == self.paid_to_account_currency: if self.paid_from_account_currency == self.paid_to_account_currency:
self.target_exchange_rate = self.source_exchange_rate self.target_exchange_rate = self.source_exchange_rate
elif self.paid_to and not self.target_exchange_rate: elif self.paid_to and not self.target_exchange_rate:
if ref_doc: if ref_doc and self.paid_to_account_currency == ref_doc.currency:
if self.paid_to_account_currency == ref_doc.currency:
self.target_exchange_rate = ref_doc.get("exchange_rate") or ref_doc.get("conversion_rate") self.target_exchange_rate = ref_doc.get("exchange_rate") or ref_doc.get("conversion_rate")
if not self.target_exchange_rate: if not self.target_exchange_rate:
@@ -665,7 +662,7 @@ class PaymentEntry(AccountsController):
elif d.reference_name: elif d.reference_name:
if not frappe.db.exists(d.reference_doctype, d.reference_name): if not frappe.db.exists(d.reference_doctype, d.reference_name):
frappe.throw(_("{0} {1} does not exist").format(d.reference_doctype, d.reference_name)) frappe.throw(_("{0} {1} does not exist").format(d.reference_doctype, d.reference_name))
else:
ref_doc = frappe.get_doc(d.reference_doctype, d.reference_name) ref_doc = frappe.get_doc(d.reference_doctype, d.reference_name)
if d.reference_doctype != "Journal Entry": if d.reference_doctype != "Journal Entry":
@@ -716,12 +713,10 @@ class PaymentEntry(AccountsController):
def get_valid_reference_doctypes(self): def get_valid_reference_doctypes(self):
if self.party_type == "Customer": if self.party_type == "Customer":
return ("Sales Order", "Sales Invoice", "Journal Entry", "Dunning", "Payment Entry") return ("Sales Order", "Sales Invoice", "Journal Entry", "Dunning", "Payment Entry")
elif self.party_type in ["Shareholder", "Employee"]:
return ("Journal Entry",)
elif self.party_type == "Supplier": elif self.party_type == "Supplier":
return ("Purchase Order", "Purchase Invoice", "Journal Entry", "Payment Entry") return ("Purchase Order", "Purchase Invoice", "Journal Entry", "Payment Entry")
elif self.party_type == "Shareholder":
return ("Journal Entry",)
elif self.party_type == "Employee":
return ("Journal Entry",)
def validate_paid_invoices(self): def validate_paid_invoices(self):
no_oustanding_refs = {} no_oustanding_refs = {}
@@ -787,7 +782,9 @@ class PaymentEntry(AccountsController):
invoice_paid_amount_map = {} invoice_paid_amount_map = {}
for ref in self.get("references"): for ref in self.get("references"):
if ref.payment_term and ref.reference_name: if not ref.payment_term or not ref.reference_name:
continue
key = (ref.payment_term, ref.reference_name, ref.reference_doctype) key = (ref.payment_term, ref.reference_name, ref.reference_doctype)
invoice_payment_amount_map.setdefault(key, 0.0) invoice_payment_amount_map.setdefault(key, 0.0)
invoice_payment_amount_map[key] += ref.allocated_amount invoice_payment_amount_map[key] += ref.allocated_amount
@@ -1064,11 +1061,11 @@ class PaymentEntry(AccountsController):
applicable_tax = 0 applicable_tax = 0
base_applicable_tax = 0 base_applicable_tax = 0
for tax in self.get("taxes"): for tax in self.get("taxes"):
if not tax.included_in_paid_amount: if tax.included_in_paid_amount:
continue
amount = -1 * tax.tax_amount if tax.add_deduct_tax == "Deduct" else tax.tax_amount amount = -1 * tax.tax_amount if tax.add_deduct_tax == "Deduct" else tax.tax_amount
base_amount = ( base_amount = -1 * tax.base_tax_amount if tax.add_deduct_tax == "Deduct" else tax.base_tax_amount
-1 * tax.base_tax_amount if tax.add_deduct_tax == "Deduct" else tax.base_tax_amount
)
applicable_tax += amount applicable_tax += amount
base_applicable_tax += base_amount base_applicable_tax += base_amount
@@ -1742,7 +1739,9 @@ class PaymentEntry(AccountsController):
def add_deductions_gl_entries(self, gl_entries): def add_deductions_gl_entries(self, gl_entries):
for d in self.get("deductions"): for d in self.get("deductions"):
if d.amount: if not d.amount:
continue
account_currency = get_account_currency(d.account) account_currency = get_account_currency(d.account)
if account_currency != self.company_currency: if account_currency != self.company_currency:
frappe.throw(_("Currency for {0} must be {1}").format(d.account, self.company_currency)) frappe.throw(_("Currency for {0} must be {1}").format(d.account, self.company_currency))
@@ -1777,10 +1776,12 @@ class PaymentEntry(AccountsController):
return flt(gl_dict.get(field, 0) / (conversion_rate or 1)) return flt(gl_dict.get(field, 0) / (conversion_rate or 1))
def update_advance_paid(self): def update_advance_paid(self):
if self.payment_type in ("Receive", "Pay") and self.party: if self.payment_type not in ("Receive", "Pay") or not self.party:
advance_payment_doctypes = frappe.get_hooks( return
"advance_payment_receivable_doctypes"
) + frappe.get_hooks("advance_payment_payable_doctypes") advance_payment_doctypes = frappe.get_hooks("advance_payment_receivable_doctypes") + frappe.get_hooks(
"advance_payment_payable_doctypes"
)
for d in self.get("references"): for d in self.get("references"):
if d.allocated_amount and d.reference_doctype in advance_payment_doctypes: if d.allocated_amount and d.reference_doctype in advance_payment_doctypes:
frappe.get_doc( frappe.get_doc(