Skip to content

Commit 8290767

Browse files
committed
fix: reduce paid amount from grand total
1 parent bbe3bc9 commit 8290767

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

erpnext/accounts/doctype/payment_request/payment_request.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import frappe
44
from frappe import _, qb
55
from frappe.model.document import Document
6-
from frappe.query_builder.functions import Sum
6+
from frappe.query_builder.functions import Abs, Sum
77
from frappe.utils import flt, nowdate
88
from frappe.utils.background_jobs import enqueue
99

@@ -563,6 +563,8 @@ def make_payment_request(**args):
563563
# fetches existing payment request `grand_total` amount
564564
existing_payment_request_amount = get_existing_payment_request_amount(ref_doc.doctype, ref_doc.name)
565565

566+
existing_paid_amount = get_existing_paid_amount(ref_doc.doctype, ref_doc.name)
567+
566568
def validate_and_calculate_grand_total(grand_total, existing_payment_request_amount):
567569
grand_total -= existing_payment_request_amount
568570
if not grand_total:
@@ -582,6 +584,15 @@ def validate_and_calculate_grand_total(grand_total, existing_payment_request_amo
582584
else:
583585
grand_total = validate_and_calculate_grand_total(grand_total, existing_payment_request_amount)
584586

587+
if existing_paid_amount:
588+
if ref_doc.party_account_currency == ref_doc.currency:
589+
if ref_doc.conversion_rate:
590+
grand_total -= flt(existing_paid_amount / ref_doc.conversion_rate)
591+
else:
592+
grand_total -= flt(existing_paid_amount)
593+
else:
594+
grand_total -= flt(existing_paid_amount / ref_doc.conversion_rate)
595+
585596
if draft_payment_request:
586597
frappe.db.set_value(
587598
"Payment Request", draft_payment_request, "grand_total", grand_total, update_modified=False
@@ -672,9 +683,11 @@ def get_amount(ref_doc, payment_account=None):
672683
elif dt in ["Sales Invoice", "Purchase Invoice"]:
673684
if not ref_doc.get("is_pos"):
674685
if ref_doc.party_account_currency == ref_doc.currency:
675-
grand_total = flt(ref_doc.outstanding_amount)
686+
grand_total = flt(ref_doc.rounded_total or ref_doc.grand_total)
676687
else:
677-
grand_total = flt(flt(ref_doc.outstanding_amount) / ref_doc.conversion_rate)
688+
grand_total = flt(
689+
flt(ref_doc.base_rounded_total or ref_doc.base_grand_total) / ref_doc.conversion_rate
690+
)
678691
elif dt == "Sales Invoice":
679692
for pay in ref_doc.payments:
680693
if pay.type == "Phone" and pay.account == payment_account:
@@ -756,6 +769,27 @@ def get_existing_payment_request_amount(ref_dt, ref_dn, statuses: list | None =
756769
return response[0][0] if response[0] else 0
757770

758771

772+
def get_existing_paid_amount(doctype, name):
773+
PL = frappe.qb.DocType("Payment Ledger Entry")
774+
PER = frappe.qb.DocType("Payment Entry Reference")
775+
776+
query = (
777+
frappe.qb.from_(PL)
778+
.left_join(PER)
779+
.on(
780+
(PER.reference_doctype == PL.against_voucher_type) & (PER.reference_name == PL.against_voucher_no)
781+
)
782+
.select(Abs(Sum(PL.amount)).as_("total_paid_amount"))
783+
.where(PL.against_voucher_type.eq(doctype))
784+
.where(PL.against_voucher_no.eq(name))
785+
.where(PL.amount < 0)
786+
.where(PER.payment_request.isnull())
787+
)
788+
response = query.run()
789+
790+
return response[0][0] if response[0] else 0
791+
792+
759793
def get_gateway_details(args): # nosemgrep
760794
"""
761795
Return gateway and payment account of default payment gateway

0 commit comments

Comments
 (0)