22from datetime import datetime
33
44import frappe
5+ from frappe .query_builder import DocType
6+ from frappe .query_builder .functions import Sum
57from frappe .utils import (
68 flt ,
79 fmt_money ,
@@ -347,15 +349,15 @@ def update_appointment(appointment_data, sort_by="Appointment Time"):
347349
348350
349351def get_total_unallocated_advance_amount (customer = None ):
350- advance_balance = frappe . db . get_all (
351- "Payment Entry" ,
352- filters = {
353- "party" : customer ,
354- "docstatus" : 1 ,
355- "custom_reference_appointment" : [ "is" , "not set" ],
356- },
357- fields = [ "sum(unallocated_amount) as unallocated_amount" ],
358- )
352+ PE = DocType ( "Payment Entry" )
353+
354+ advance_balance = (
355+ frappe . qb . from_ ( PE )
356+ . select ( Sum ( PE . unallocated_amount ). as_ ( "unallocated_amount" ))
357+ . where ( PE . party == customer )
358+ . where ( PE . docstatus == 1 )
359+ . where ( PE . custom_reference_appointment . isnull ())
360+ ). run ( as_dict = True )
359361
360362 total_unallocated_advance_amt = 0
361363 if advance_balance and len (advance_balance ) and advance_balance [0 ].get ("unallocated_amount" ):
@@ -366,11 +368,14 @@ def get_total_unallocated_advance_amount(customer=None):
366368
367369@frappe .whitelist ()
368370def get_total_advance_amount (customer = None ):
369- advance_balance = frappe .db .get_all (
370- "Payment Entry" ,
371- filters = {"party" : customer , "docstatus" : 1 },
372- fields = ["sum(unallocated_amount) as unallocated_amount" ],
373- )
371+ PE = DocType ("Payment Entry" )
372+
373+ advance_balance = (
374+ frappe .qb .from_ (PE )
375+ .select (Sum (PE .unallocated_amount ).as_ ("unallocated_amount" ))
376+ .where (PE .party == customer )
377+ .where (PE .docstatus == 1 )
378+ ).run (as_dict = True )
374379
375380 total_unallocated_advance_amt = 0
376381 if advance_balance and len (advance_balance ) and advance_balance [0 ].get ("unallocated_amount" ):
@@ -1228,16 +1233,18 @@ def get_payments(appointment):
12281233
12291234 registration_paid_amount = 0
12301235 customer = frappe .db .get_value ("Patient" , appointment_doc .patient , "customer" )
1231- paid_reg_amount_details = frappe .db .get_all (
1232- "Payment Entry" ,
1233- filters = {
1234- "docstatus" : ["!=" , 2 ],
1235- "register_paid" : 1 ,
1236- "party" : customer ,
1237- "custom_reference_appointment" : appointment ,
1238- },
1239- fields = ["sum(paid_amount) as amount" , "posting_date" ],
1240- )
1236+
1237+ PE = DocType ("Payment Entry" )
1238+
1239+ paid_reg_amount_details = (
1240+ frappe .qb .from_ (PE )
1241+ .select (Sum (PE .paid_amount ).as_ ("amount" ), PE .posting_date )
1242+ .where (PE .docstatus != 2 )
1243+ .where (PE .register_paid == 1 )
1244+ .where (PE .party == customer )
1245+ .where (PE .custom_reference_appointment == appointment )
1246+ ).run (as_dict = True )
1247+
12411248 if paid_reg_amount_details and paid_reg_amount_details [0 ].get ("amount" ):
12421249 registration_paid_amount = paid_reg_amount_details [0 ].get ("amount" )
12431250 registration_pay_date = paid_reg_amount_details [0 ].get ("posting_date" )
@@ -1275,15 +1282,14 @@ def get_payments(appointment):
12751282
12761283 total_amount = consultation_charge + registration_fee
12771284
1278- paid_consultation_amount_details = frappe .db .get_all (
1279- "Payment Entry" ,
1280- filters = {
1281- "custom_reference_appointment" : appointment ,
1282- "docstatus" : 1 ,
1283- "register_paid" : 0 ,
1284- },
1285- fields = ["sum(paid_amount) as total_amount" , "posting_date" ],
1286- )
1285+ paid_consultation_amount_details = (
1286+ frappe .qb .from_ (PE )
1287+ .select (Sum (PE .paid_amount ).as_ ("total_amount" ), PE .posting_date )
1288+ .where (PE .docstatus == 1 )
1289+ .where (PE .register_paid == 0 )
1290+ .where (PE .custom_reference_appointment == appointment )
1291+ ).run (as_dict = True )
1292+
12871293 consultation_paid_amount = 0
12881294 if paid_consultation_amount_details and paid_consultation_amount_details [0 ].get ("total_amount" ):
12891295 consultation_paid_amount = paid_consultation_amount_details [0 ]["total_amount" ]
0 commit comments