from datetime import datetime from collections import Counter import re class DpdCalculationService: def dpdCalculation(accountTranDetailsID=None,Consumer_dpd_details=None,dpd_month_bucket_master=None): # print('dpdCalculation called',accountTranDetailsID,len(Consumer_dpd_details)) # print(dpd_month_bucket_master) # month_index = 0 currentDPD = 0; DPD0_6,DPD7_12,DPD13_24,DPD25_36 = [],[],[],[] # DPD0_6_BUCKET,DPD7_12_BUCKET,DPD13_24_BUCKET,DPD25_36_BUCKET = [{'name':'20-30','from':20,'to':30},{'name':'31-40','from':31,'to':40},{'name':'41-50','from':41,'to':60}],[],[],[] for idx,dpd in enumerate(Consumer_dpd_details): # if accountTranDetailsID == '388857': # print('#########################BREAK') # break #in future introduce if condition like idx <=36 if dpd['AccountTranDetailsID'] == accountTranDetailsID: # month_index+=1 # if month_index == 37:break # month = (datetime.strptime((dpd['StartDate'].split(" ")[0]), "%Y-%m-%d")).month month_bucket_index = int(dpd['month_bucket_index']) if month_bucket_index == 1: currentDPD = month_bucket_index month_bucket = '0_6' if month_bucket_index <= 6 else '7_12' if month_bucket_index >= 7 and month_bucket_index <= 12 else '13_24' if month_bucket_index >=13 and month_bucket_index <= 24 else '25_36' if month_bucket_index >= 25 and month_bucket_index <= 36 else None currentDPDcode = (int(dpd['Code'])) if dpd['Code'].isnumeric() else 0 dynamic_dpd_month_bucket = None if month_bucket is None else 'DPD'+month_bucket # dynamic_dpd_month_range_bucket = dynamic_dpd_month_bucket + '_BUCKET' # print(vars()[dynamic_dpd_month_bucket],vars()[dynamic_dpd_month_range_bucket]) # if len(vars()[dynamic_dpd_month_range_bucket]) > 0: # for single_date_range in vars()[dynamic_dpd_month_range_bucket]: # if currentDPDcode != 0 and currentDPDcode >= int(single_date_range['from']) and currentDPDcode <= int(single_date_range['to']): # print(currentDPDcode,'-',single_date_range['name']) # vars()[dynamic_dpd_month_bucket].append(single_date_range['name']) # break currentDPDMonth = 0 if dynamic_dpd_month_bucket is not None: currentDPDMonth = DpdCalculationService.dpdFindDaysBucket(dpd_month_bucket_master,currentDPDcode) if currentDPDMonth != 0: vars()[dynamic_dpd_month_bucket].append(currentDPDMonth) # print(vars()[dynamic_dpd_month_bucket]) # print(idx,dpd['AccountTranDetailsID'],dpd['StartDate'],month,month_bucket,dynamic_dpd_month_bucket,currentDPDMonth,currentDPDcode) # print(DPD0_6,DPD7_12,DPD13_24,DPD25_36) DPD0_6 = DpdCalculationService.findMaxCountofDPDDays(DPD0_6); DPD7_12 = DpdCalculationService.findMaxCountofDPDDays(DPD7_12); DPD13_24 = DpdCalculationService.findMaxCountofDPDDays(DPD13_24); DPD25_36 = DpdCalculationService.findMaxCountofDPDDays(DPD25_36); # print('final*****',DPD0_6) DPD0_6 = DPD0_6['days_range'] +'(' + DPD0_6['count'] +')' if 'days_range' in DPD0_6 else '0' DPD7_12 = DPD7_12['days_range'] +'(' + DPD7_12['count'] +')' if 'days_range' in DPD7_12 else '0' DPD13_24 = DPD13_24['days_range'] +'(' + DPD13_24['count'] +')' if 'days_range' in DPD13_24 else '0' DPD25_36 = DPD25_36['days_range'] +'(' + DPD25_36['count'] +')' if 'days_range' in DPD25_36 else '0' return {'currentDPD':currentDPD,'DPD0_6':DPD0_6,'DPD7_12':DPD7_12,'DPD13_24':DPD13_24,'DPD25_36':DPD25_36} def monthDiffBWDates(fromDate,endDate): # start = datetime.strptime((fromDate.split(" ")[0]), "%Y-%m-%d") # end = datetime.strptime((endDate.split(" ")[0]), "%Y-%m-%d") # months = (end.year - start.year) * 12 + (end.month - start.month ) # print('monhs',months) # print('') # return months str = 'xxx' try: val = int(str) print(type(val)) except IOError: print(type(str)) def dpdFindDaysBucket(month_bucket_master,factor): #DPD month bucket calculation start # print(dpd_month_bucket_master) for dpd_month_bucket in month_bucket_master: # print(dpd_month_bucket) # print(type(dpd_month_bucket['range_from']),'***',dpd_month_bucket['range_from'].isnumeric()) # print(type(dpd_month_bucket['range_to']),'***',dpd_month_bucket['range_to'].isnumeric()) range_from = int(dpd_month_bucket['range_from']) if (type(dpd_month_bucket['range_from']) is str and dpd_month_bucket['range_from'].isnumeric()) else None range_to = int(dpd_month_bucket['range_to']) if (type(dpd_month_bucket['range_to']) is str and dpd_month_bucket['range_to'].isnumeric()) else None # range_to = int(dpd_month_bucket['range_to']) if (dpd_month_bucket['range_to'] is str and dpd_month_bucket['range_to'].isnumeric()) else dpd_month_bucket['range_to'] factor = int(factor) if isinstance(factor,str) and factor.isnumeric() else factor if isinstance(factor,int) else 0 if range_from != None and range_to != None: # print((range_from),'###',(range_to),range_from != None,currentDPDcode) if factor >= (range_from) and factor <= (range_to): # vars()[dynamic_dpd_month_bucket].append(dpd_month_bucket['name']) return dpd_month_bucket['name'] # break elif range_to == None: if factor >= (range_from): # vars()[dynamic_dpd_month_bucket].append(dpd_month_bucket['name']) return dpd_month_bucket['name'] # break elif range_from == None: if factor >= (range_to): # vars()[dynamic_dpd_month_bucket].append(dpd_month_bucket['name']) return dpd_month_bucket['name'] # break else: return 0 # break #DPD month bucket calculation end def findMaxCountofDPDDays(listOfDaysCount): # print('findMaxCountofDPDDays******',listOfDaysCount) UniquelistOfDaysCount = Counter(listOfDaysCount) days_count_list = (list(UniquelistOfDaysCount.values())) if len(days_count_list) > 0: return {'days_range':(listOfDaysCount[days_count_list.index(max(days_count_list))]),'count': str(max(days_count_list))} else: return {} # DpdCalculationService.monthDiffBWDates('2014-02-03 00:00:00','2022-01-14 11:16:55.097000') def commercialDpdCalculation(creditFacilitySno = None,com_dpd_details=None,dpd_month_bucket_master=None): # print('dpdCalculation called',creditFacilitySno,len(com_dpd_details)) # print(dpd_month_bucket_master) month_index = 0 currentDPD = 0; DPD0_6,DPD7_12,DPD13_24,DPD25_36 = [],[],[],[] # DPD0_6_BUCKET,DPD7_12_BUCKET,DPD13_24_BUCKET,DPD25_36_BUCKET = [{'name':'20-30','from':20,'to':30},{'name':'31-40','from':31,'to':40},{'name':'41-50','from':41,'to':60}],[],[],[] for idx,dpd in enumerate(com_dpd_details): if creditFacilitySno[16:] == 2: print('#########################BREAK') break #in future introduce if condition like idx <=36 if dpd['BorrowerCurrentDetailsID'] == creditFacilitySno[16:]: month_index+=1 #find number in string i.e 25 in 25 days past tempDPD = re.findall('[0-9]+', dpd['CFHistory24MonthsACorDPD']) tempDPD = tempDPD[0] if len(tempDPD) > 0 else 0 if month_index == 1: currentDPD = tempDPD if month_index == 37:break # month = (datetime.strptime((dpd['StartDate'].split(" ")[0]), "%Y-%m-%d")).month month = month_index month_bucket = '0_6' if month <= 6 else '7_12' if month >= 7 and month <= 12 else '13_24' if month >=13 and month <= 24 else '25_36' if month >= 25 and month <= 36 else 'None' currentDPDcode = (int(tempDPD)) if tempDPD is not str else 0 dynamic_dpd_month_bucket = 'DPD'+month_bucket currentDPDMonth = DpdCalculationService.dpdFindDaysBucket(dpd_month_bucket_master,currentDPDcode) if currentDPDMonth != 0: vars()[dynamic_dpd_month_bucket].append(currentDPDMonth) # print(vars()[dynamic_dpd_month_bucket]) # print(idx,dpd['BorrowerCurrentDetailsID'],dpd['CFHistory24Monthsmonth'],month,month_bucket,dynamic_dpd_month_bucket,currentDPDMonth,currentDPDcode) # print(DPD0_6,DPD7_12,DPD13_24,DPD25_36) DPD0_6 = DpdCalculationService.findMaxCountofDPDDays(DPD0_6); DPD7_12 = DpdCalculationService.findMaxCountofDPDDays(DPD7_12); DPD13_24 = DpdCalculationService.findMaxCountofDPDDays(DPD13_24); DPD25_36 = DpdCalculationService.findMaxCountofDPDDays(DPD25_36); # print('final*****',DPD0_6) DPD0_6 = DPD0_6['days_range'] +'(' + DPD0_6['count'] +')' if 'days_range' in DPD0_6 else '0' DPD7_12 = DPD7_12['days_range'] +'(' + DPD7_12['count'] +')' if 'days_range' in DPD7_12 else '0' DPD13_24 = DPD13_24['days_range'] +'(' + DPD13_24['count'] +')' if 'days_range' in DPD13_24 else '0' DPD25_36 = DPD25_36['days_range'] +'(' + DPD25_36['count'] +')' if 'days_range' in DPD25_36 else '0' return {'currentDPD':currentDPD,'DPD0_6':DPD0_6,'DPD7_12':DPD7_12,'DPD13_24':DPD13_24,'DPD25_36':DPD25_36} def checkDateInLatest36Months(date,latest_36months_skeleton): print('checkDateInLatest36Months') temp_dpd_date = (datetime.datetime.strptime(date, "%Y-%m-%d")) # print(temp_dpd_date.month) return_data = 0 for month_index,skeleton_date in enumerate(latest_36months_skeleton): if (temp_dpd_date.year == skeleton_date.year) and (temp_dpd_date.month == skeleton_date.month): print(month_index,':',date, ' - is in - ', (month_index + 1), ' bucket') return_data = (month_index + 1) break return return_data def generateMonthSkeleton(date,month_size = 36,): latest_36months_skeleton = [] print('generateMonthSkeleton') date = datetime.datetime.strptime(date, "%Y-%m-%d") month_count = 1 while month_count <= 36 : day = date - relativedelta(months=month_count) latest_36months_skeleton.append((day)) month_count += 1 return latest_36months_skeleton