@@ -1035,9 +1035,8 @@ def add_customer_filters(
10351035 self ,
10361036 ):
10371037 self .customer = qb .DocType ("Customer" )
1038-
10391038 if self .filters .get ("customer_group" ):
1040- groups = get_customer_group_with_children ( self .filters .customer_group )
1039+ groups = get_party_group_with_children ( "Customer" , self .filters .customer_group )
10411040 customers = (
10421041 qb .from_ (self .customer )
10431042 .select (self .customer .name )
@@ -1049,14 +1048,18 @@ def add_customer_filters(
10491048 self .get_hierarchical_filters ("Territory" , "territory" )
10501049
10511050 if self .filters .get ("payment_terms_template" ):
1052- self .qb_selection_filter .append (
1053- self .ple .party .isin (
1054- qb .from_ (self .customer )
1055- .select (self .customer .name )
1056- .where (self .customer .payment_terms == self .filters .get ("payment_terms_template" ))
1057- )
1051+ customer_ptt = self .ple .party .isin (
1052+ qb .from_ (self .customer )
1053+ .select (self .customer .name )
1054+ .where (self .customer .payment_terms == self .filters .get ("payment_terms_template" ))
10581055 )
10591056
1057+ si_ptt = self .add_payment_term_template_filters ("Sales Invoice" )
1058+
1059+ sales_ptt = self .ple .against_voucher_no .isin (si_ptt )
1060+
1061+ self .qb_selection_filter .append (Criterion .any ([customer_ptt , sales_ptt ]))
1062+
10601063 if self .filters .get ("sales_partner" ):
10611064 self .qb_selection_filter .append (
10621065 self .ple .party .isin (
@@ -1081,14 +1084,53 @@ def add_supplier_filters(self):
10811084 )
10821085
10831086 if self .filters .get ("payment_terms_template" ):
1084- self .qb_selection_filter .append (
1085- self .ple .party .isin (
1086- qb .from_ (supplier )
1087- .select (supplier .name )
1088- .where (supplier .payment_terms == self .filters .get ("supplier_group" ))
1089- )
1087+ supplier_ptt = self .ple .party .isin (
1088+ qb .from_ (supplier )
1089+ .select (supplier .name )
1090+ .where (supplier .payment_terms == self .filters .get ("payment_terms_template" ))
10901091 )
10911092
1093+ pi_ptt = self .add_payment_term_template_filters ("Purchase Invoice" )
1094+
1095+ purchase_ptt = self .ple .against_voucher_no .isin (pi_ptt )
1096+
1097+ self .qb_selection_filter .append (Criterion .any ([supplier_ptt , purchase_ptt ]))
1098+
1099+ def add_payment_term_template_filters (self , dtype ):
1100+ voucher_type = qb .DocType (dtype )
1101+
1102+ ptt = (
1103+ qb .from_ (voucher_type )
1104+ .select (voucher_type .name )
1105+ .where (voucher_type .payment_terms_template == self .filters .get ("payment_terms_template" ))
1106+ .where (voucher_type .company == self .filters .company )
1107+ )
1108+
1109+ if dtype == "Purchase Invoice" :
1110+ party = "Supplier"
1111+ party_group_type = "supplier_group"
1112+ acc_type = "credit_to"
1113+ else :
1114+ party = "Customer"
1115+ party_group_type = "customer_group"
1116+ acc_type = "debit_to"
1117+
1118+ if self .filters .get (party_group_type ):
1119+ party_groups = get_party_group_with_children (party , self .filters .get (party_group_type ))
1120+ ptt = ptt .where ((voucher_type [party_group_type ]).isin (party_groups ))
1121+
1122+ if self .filters .party :
1123+ ptt = ptt .where ((voucher_type [party .lower ()]).isin (self .filters .party ))
1124+
1125+ if self .filters .cost_center :
1126+ cost_centers = get_cost_centers_with_children (self .filters .cost_center )
1127+ ptt = ptt .where (voucher_type .cost_center .isin (cost_centers ))
1128+
1129+ if self .filters .party_account :
1130+ ptt = ptt .where (voucher_type [acc_type ] == self .filters .party_account )
1131+
1132+ return ptt
1133+
10921134 def get_hierarchical_filters (self , doctype , key ):
10931135 lft , rgt = frappe .db .get_value (doctype , self .filters .get (key ), ["lft" , "rgt" ])
10941136
@@ -1330,20 +1372,26 @@ def get_exchange_rate_revaluations(self):
13301372 self .err_journals = [x [0 ] for x in results ] if results else []
13311373
13321374
1333- def get_customer_group_with_children ( customer_groups ):
1334- if not isinstance ( customer_groups , list ):
1335- customer_groups = [ d . strip () for d in customer_groups . strip (). split ( "," ) if d ]
1375+ def get_party_group_with_children ( party , party_groups ):
1376+ if party not in ( "Customer" , "Supplier" ):
1377+ return [ ]
13361378
1337- all_customer_groups = []
1338- for d in customer_groups :
1339- if frappe .db .exists ("Customer Group" , d ):
1340- lft , rgt = frappe .db .get_value ("Customer Group" , d , ["lft" , "rgt" ])
1341- children = frappe .get_all ("Customer Group" , filters = {"lft" : [">=" , lft ], "rgt" : ["<=" , rgt ]})
1342- all_customer_groups += [c .name for c in children ]
1379+ group_dtype = f"{ party } Group"
1380+ if not isinstance (party_groups , list ):
1381+ party_groups = [d .strip () for d in party_groups .strip ().split ("," ) if d ]
1382+
1383+ all_party_groups = []
1384+ for d in party_groups :
1385+ if frappe .db .exists (group_dtype , d ):
1386+ lft , rgt = frappe .db .get_value (group_dtype , d , ["lft" , "rgt" ])
1387+ children = frappe .get_all (
1388+ group_dtype , filters = {"lft" : [">=" , lft ], "rgt" : ["<=" , rgt ]}, pluck = "name"
1389+ )
1390+ all_party_groups += children
13431391 else :
1344- frappe .throw (_ ("Customer Group : {0 } does not exist" ).format (d ))
1392+ frappe .throw (_ ("{0} : {1 } does not exist" ).format (group_dtype , d ))
13451393
1346- return list (set (all_customer_groups ))
1394+ return list (set (all_party_groups ))
13471395
13481396
13491397class InitSQLProceduresForAR :
0 commit comments