@@ -182,10 +182,12 @@ def validate_allocated_amount_with_latest_data(self):
182182 latest_lookup = {}
183183 for d in latest_references :
184184 d = frappe ._dict (d )
185- latest_lookup .update ({ (d .voucher_type , d .voucher_no ): d })
185+ latest_lookup .setdefault ( (d .voucher_type , d .voucher_no ), frappe . _dict ())[ d . payment_term ] = d
186186
187187 for d in self .get ("references" ):
188- latest = latest_lookup .get ((d .reference_doctype , d .reference_name ))
188+ latest = (latest_lookup .get ((d .reference_doctype , d .reference_name )) or frappe ._dict ()).get (
189+ d .payment_term
190+ )
189191
190192 # The reference has already been fully paid
191193 if not latest :
@@ -208,6 +210,18 @@ def validate_allocated_amount_with_latest_data(self):
208210 if (flt (d .allocated_amount )) > 0 and flt (d .allocated_amount ) > flt (latest .outstanding_amount ):
209211 frappe .throw (fail_message .format (d .idx ))
210212
213+ if d .payment_term and (
214+ (flt (d .allocated_amount )) > 0
215+ and flt (d .allocated_amount ) > flt (latest .payment_term_outstanding )
216+ ):
217+ frappe .throw (
218+ _ (
219+ "Row #{0}: Allocated amount:{1} is greater than outstanding amount:{2} for Payment Term {3}"
220+ ).format (
221+ d .idx , d .allocated_amount , latest .payment_term_outstanding , d .payment_term
222+ )
223+ )
224+
211225 # Check for negative outstanding invoices as well
212226 if flt (d .allocated_amount ) < 0 and flt (d .allocated_amount ) < flt (latest .outstanding_amount ):
213227 frappe .throw (fail_message .format (d .idx ))
@@ -1466,6 +1480,7 @@ def split_invoices_based_on_payment_terms(outstanding_invoices):
14661480 "posting_date" : d .posting_date ,
14671481 "invoice_amount" : flt (d .invoice_amount ),
14681482 "outstanding_amount" : flt (d .outstanding_amount ),
1483+ "payment_term_outstanding" : flt (payment_term .outstanding ),
14691484 "payment_amount" : payment_term .payment_amount ,
14701485 "payment_term" : payment_term .payment_term ,
14711486 }
@@ -2240,6 +2255,7 @@ def get_reference_as_per_payment_terms(
22402255 "due_date" : doc .get ("due_date" ),
22412256 "total_amount" : grand_total ,
22422257 "outstanding_amount" : outstanding_amount ,
2258+ "payment_term_outstanding" : payment_term_outstanding ,
22432259 "payment_term" : payment_term .payment_term ,
22442260 "allocated_amount" : payment_term_outstanding ,
22452261 }
0 commit comments