3
3
import frappe
4
4
from frappe import _ , qb
5
5
from frappe .model .document import Document
6
- from frappe .query_builder .functions import Sum
6
+ from frappe .query_builder .functions import Abs , Sum
7
7
from frappe .utils import flt , nowdate
8
8
from frappe .utils .background_jobs import enqueue
9
9
@@ -563,6 +563,8 @@ def make_payment_request(**args):
563
563
# fetches existing payment request `grand_total` amount
564
564
existing_payment_request_amount = get_existing_payment_request_amount (ref_doc .doctype , ref_doc .name )
565
565
566
+ existing_paid_amount = get_existing_paid_amount (ref_doc .doctype , ref_doc .name )
567
+
566
568
def validate_and_calculate_grand_total (grand_total , existing_payment_request_amount ):
567
569
grand_total -= existing_payment_request_amount
568
570
if not grand_total :
@@ -582,6 +584,15 @@ def validate_and_calculate_grand_total(grand_total, existing_payment_request_amo
582
584
else :
583
585
grand_total = validate_and_calculate_grand_total (grand_total , existing_payment_request_amount )
584
586
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
+
585
596
if draft_payment_request :
586
597
frappe .db .set_value (
587
598
"Payment Request" , draft_payment_request , "grand_total" , grand_total , update_modified = False
@@ -672,9 +683,11 @@ def get_amount(ref_doc, payment_account=None):
672
683
elif dt in ["Sales Invoice" , "Purchase Invoice" ]:
673
684
if not ref_doc .get ("is_pos" ):
674
685
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 )
676
687
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
+ )
678
691
elif dt == "Sales Invoice" :
679
692
for pay in ref_doc .payments :
680
693
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 =
756
769
return response [0 ][0 ] if response [0 ] else 0
757
770
758
771
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
+
759
793
def get_gateway_details (args ): # nosemgrep
760
794
"""
761
795
Return gateway and payment account of default payment gateway
0 commit comments