def generateAmortizationSchedule(loan, request): noOfPrincipalPaymentSchedules = loan.term.days / loan.term.principalPaymentPeriod.paymentCycle # noOfPaymentSchedules = loan.term.days / loan.term.paymentPeriod.paymentCycle noOfInterestPaymentSchedules = loan.term.days / loan.term.interestPaymentPeriod.paymentCycle if noOfPrincipalPaymentSchedules > noOfInterestPaymentSchedules: noOfPaymentSchedules = noOfPrincipalPaymentSchedules else: noOfPaymentSchedules = noOfInterestPaymentSchedules cycle = loan.term.interestPaymentPeriod.paymentCycle schedule = loan.dateReleased + timezone.timedelta(days=cycle) pmt = PMT() pmtInterest = PMT() print(pmt.payment) print(pmt.nextStartingValue) print(pmt.interest) print(pmt.principal) loanAmount = loan.amount amortization = Amortization( loan=loan, dateReleased=loan.dateReleased, # dateReleased = loan.dateReleased + timezone.timedelta(days=1), amortizationStatus=AmortizationStatus.objects.get(pk=1), createdBy=request.user, schedules=noOfPaymentSchedules, cycle=cycle, termDays=loan.term.days) amortization.save() currentCycle = 1 interestLoan = loan.amount for i in range(int(noOfPaymentSchedules)): pmt = pmt.getPayment(loanAmount, loan.interestRate.interestRate, loan.term.days, noOfPaymentSchedules, noOfPaymentSchedules - i) lastPayment = schedule dayTillCutOff = cycle - int(lastPayment.strftime('%d')) print("interest rate") print(Decimal((pmt.principal + pmt.nextStartingValue) - pmt.interest)) accruedInterest = Decimal((pmt.principal + pmt.nextStartingValue)) * ( loan.interestRate.interestRate / 100) * dayTillCutOff / 360 print(round(accruedInterest, 2)) amortizationItem = AmortizationItem( schedule=schedule, amortization=amortization, days=cycle, principal=pmt.principal, deductAccruedInterest=pmt.interest - accruedInterest, accruedInterest=accruedInterest, interest=pmt.interest, additionalInterest=0, penalty=0, vat=0, total=pmt.payment, principalBalance=pmt.nextStartingValue, amortizationStatus=AmortizationStatus.objects.get(pk=1), ) amortizationItem.save() schedule = schedule + timezone.timedelta(days=cycle) loanAmount = pmt.nextStartingValue
def generateUnevenAmortizationSchedule(loan, request): noOfPrincipalPaymentSchedules = loan.term.days / loan.term.principalPaymentPeriod.paymentCycle # noOfPaymentSchedules = loan.term.days / loan.term.paymentPeriod.paymentCycle noOfInterestPaymentSchedules = loan.term.days / loan.term.interestPaymentPeriod.paymentCycle if noOfPrincipalPaymentSchedules > noOfInterestPaymentSchedules: noOfPaymentSchedules = noOfPrincipalPaymentSchedules else: noOfPaymentSchedules = noOfInterestPaymentSchedules cycle = loan.term.interestPaymentPeriod.paymentCycle schedule = loan.dateReleased + timezone.timedelta(days=cycle) pmt = PMT() pmtInterest = PMT() print(pmt.payment) print(pmt.nextStartingValue) print(pmt.interest) print(pmt.principal) loanAmount = loan.amount amortization = Amortization( loan=loan, dateReleased=loan.dateReleased, # dateReleased = loan.dateReleased + timezone.timedelta(days=1), amortizationStatus=AmortizationStatus.objects.get(pk=1), createdBy=request.user, schedules=noOfPaymentSchedules, cycle=cycle, termDays=loan.term.days) amortization.save() currentCycle = 1 interestLoan = loan.amount print("CurrentCycle") print(currentCycle) print("interestLoan") print(interestLoan) print("Payment Schedules") print(int(noOfPaymentSchedules)) print("=============intitial") principalBalance = interestLoan for i in range(int(noOfPaymentSchedules)): pmt = pmt.getPayment(loanAmount, loan.interestRate.interestRate, loan.term.days, noOfPaymentSchedules, noOfPaymentSchedules - i) principaEntry = 0 pmtInterest = pmtInterest.getPayment(principalBalance, loan.interestRate.interestRate, loan.term.days, noOfPaymentSchedules, noOfPaymentSchedules - i) print(i) print("cycle") print(cycle) print("loan.term.principalPaymentPeriod.paymentCycle") print(loan.term.principalPaymentPeriod.paymentCycle) if (cycle * (i + 1)) == (loan.term.principalPaymentPeriod.paymentCycle * currentCycle): print("first if") # if i+1 == int(noOfPaymentSchedules)/ int(noOfPrincipalPaymentSchedules): # pmtPrincipal = pmt.getPayment(loanAmount,loan.interestRate.interestRate,loan.term.days,noOfPrincipalPaymentSchedules,noOfPrincipalPaymentSchedules - i) principaEntry = int(loan.amount) / noOfPrincipalPaymentSchedules print(i) lastPayment = schedule - timezone.timedelta(days=cycle) dayTillCutOff = cycle - int(lastPayment.strftime('%d')) accruedInterest = (int(pmt.principal)) * ( loan.interestRate.interestRate / 100) * dayTillCutOff / 360 principalBalance = int(principalBalance) - int(principaEntry) amortizationItem = AmortizationItem( schedule=schedule, amortization=amortization, days=cycle, principal=principaEntry, deductAccruedInterest=pmtInterest.interest - accruedInterest, accruedInterest=accruedInterest, interest=pmtInterest.interest, additionalInterest=0, penalty=0, vat=0, total=int(principaEntry) + pmtInterest.interest, principalBalance=principalBalance, amortizationStatus=AmortizationStatus.objects.get(pk=1), ) amortizationItem.save() schedule = schedule + timezone.timedelta(days=cycle) loanAmount = pmt.nextStartingValue if (cycle * (i + 1)) == (loan.term.principalPaymentPeriod.paymentCycle * currentCycle): # if i+1 == int(noOfPaymentSchedules)/ int(noOfPrincipalPaymentSchedules): # loanAmount = pmt.nextStartingValue currentCycle = currentCycle + 1 interestLoan = int(principaEntry)
def post(self, request): params = request.data.get("params") print(params) dateStart = datetime.strptime(params["dateStart"], '%m/%d/%Y') principalPaymentCycle = params["principalPaymentCycle"] interestPaymentCycle = params["interestPaymentCycle"] termDays = params["termDays"] loanId = params["loanId"] loan = Loan.objects.get(pk=loanId) Amortization.objects.filter(loan_id=loanId, amortizationStatus__id=4).delete() #DRAFT #loan principal balance cycle = interestPaymentCycle schedules = int(termDays) / int(cycle) loan.latestPayment = loan.getLatestPayment() if loan.latestPayment: loanAmount = loan.latestPayment.outStandingBalance else: loanAmount = loan.amount pmt = PMT() # loanAmount = loan.amount print(loanAmount) # days = loan.term.paymentPeriod.paymentCycle - delta.days days = cycle noOfPaymentSchedules = schedules print(noOfPaymentSchedules) schedule = dateStart + timezone.timedelta(days=days) amortization = Amortization( loan=loan, dateReleased=dateStart, amortizationStatus=AmortizationStatus.objects.get(pk=4), #DRAFT createdBy=request.user, schedules=schedules, termDays=termDays, cycle=cycle) amortization.save() if principalPaymentCycle == interestPaymentCycle: for i in range(int(noOfPaymentSchedules)): pmt = pmt.getPayment(loanAmount, loan.interestRate.interestRate, termDays, noOfPaymentSchedules, noOfPaymentSchedules - i) lastPayment = schedule - timezone.timedelta(days=cycle) dayTillCutOff = cycle - int(lastPayment.strftime('%d')) accruedInterest = (int(pmt.principal)) * ( loan.interestRate.interestRate / 100) * dayTillCutOff / 360 amortizationItem = AmortizationItem( schedule=schedule, amortization=amortization, days=days, principal=pmt.principal, deductAccruedInterest=pmt.interest - accruedInterest, accruedInterest=accruedInterest, interest=pmt.interest, additionalInterest=0, penalty=0, vat=0, total=pmt.payment, principalBalance=pmt.nextStartingValue, amortizationStatus=AmortizationStatus.objects.get(pk=1), ) amortizationItem.save() schedule = schedule + timezone.timedelta(days=days) loanAmount = pmt.nextStartingValue else: noOfPrincipalPaymentSchedules = loan.term.days / principalPaymentCycle # noOfPaymentSchedules = loan.term.days / loan.term.paymentPeriod.paymentCycle noOfInterestPaymentSchedules = loan.term.days / interestPaymentCycle if noOfPrincipalPaymentSchedules > noOfInterestPaymentSchedules: noOfPaymentSchedules = noOfPrincipalPaymentSchedules else: noOfPaymentSchedules = noOfInterestPaymentSchedules pmtInterest = PMT() currentCycle = 1 if loan.latestPayment: interestLoan = loan.latestPayment.outStandingBalance else: interestLoan = loan.amount principalBalance = interestLoan for i in range(int(noOfPaymentSchedules)): pmt = pmt.getPayment(loanAmount, loan.interestRate.interestRate, loan.term.days, noOfPaymentSchedules, noOfPaymentSchedules - i) principaEntry = 0 pmtInterest = pmtInterest.getPayment( principalBalance, loan.interestRate.interestRate, loan.term.days, noOfPaymentSchedules, noOfPaymentSchedules - i) if (cycle * (i + 1)) == ( loan.term.principalPaymentPeriod.paymentCycle * currentCycle): # if i+1 == int(noOfPaymentSchedules)/ int(noOfPrincipalPaymentSchedules): # pmtPrincipal = pmt.getPayment(loanAmount,loan.interestRate.interestRate,loan.term.days,noOfPrincipalPaymentSchedules,noOfPrincipalPaymentSchedules - i) principaEntry = int( loan.amount) / noOfPrincipalPaymentSchedules print(i) lastPayment = schedule - timezone.timedelta(days=cycle) # dayTillCutOff = cycle - int(timezone.localtime(lastPayment).strftime ('%d') ) dayTillCutOff = cycle - int(lastPayment.strftime('%d')) accruedInterest = (int(pmt.principal)) * ( loan.interestRate.interestRate / 100) * dayTillCutOff / 360 principalBalance = int(principalBalance) - int(principaEntry) amortizationItem = AmortizationItem( schedule=schedule, amortization=amortization, days=cycle, principal=principaEntry, deductAccruedInterest=pmtInterest.interest - accruedInterest, accruedInterest=accruedInterest, interest=pmtInterest.interest, additionalInterest=0, penalty=0, vat=0, total=int(principaEntry) + pmtInterest.interest, principalBalance=principalBalance, amortizationStatus=AmortizationStatus.objects.get(pk=1), ) amortizationItem.save() schedule = schedule + timezone.timedelta(days=cycle) loanAmount = pmt.nextStartingValue if (cycle * (i + 1)) == ( loan.term.principalPaymentPeriod.paymentCycle * currentCycle): # if i+1 == int(noOfPaymentSchedules)/ int(noOfPrincipalPaymentSchedules): # loanAmount = pmt.nextStartingValue currentCycle = currentCycle + 1 interestLoan = int(principaEntry) # excludeWeekends(amortization.amortizationItems) return Response( { 'success': 'true', 'message': 'Restructured Amortization Generated' }, status=status.HTTP_202_ACCEPTED)
def generateAmortizationSchedule(loan, lastPayment, currentAmortization): paidItems = loan.amortizations.filter( amortizationStatus__name="PAID").count() cycle = currentAmortization.cycle termDays = currentAmortization.termDays schedule = loan.dateReleased + timezone.timedelta( days=loan.term.principalPaymentPeriod.paymentCycle) noOfPaymentSchedules = termDays / cycle pmt = PMT() print(pmt.payment) print(pmt.nextStartingValue) print(pmt.interest) print(pmt.principal) loanAmount = loan.amount amortization = Amortization( loan=loan, # dateReleased = loan.dateReleased + timezone.timedelta(days=1), dateReleased=loan.dateReleased, amortizationStatus=AmortizationStatus.objects.get(pk=1), createdBy=CustomUser.objects.get(pk=1), schedules=noOfPaymentSchedules, cycle=cycle, termDays=termDays, ) amortization.save() currentAmortization.amortizationItems.all() # for i in range(int(noOfPaymentSchedules)): i = 1 loanAmount = lastPayment.outStandingBalance for amortizationItem in currentAmortization.amortizationItems.order_by( "id").all(): print(currentAmortization.amortizationItems.all().count()) print("count") print(amortizationItem.total) print(paidItems) print(i) print("heres") if i <= (paidItems): print(loanAmount) print("pasok") amortizationItem.amortization = amortization payments = amortizationItem.payments.all() amortizationItem.pk = None if i == (paidItems): amortizationItem.principalBalance = loanAmount # amortizationItem.total = lastPayment.total - ( lastPayment.additionalInterest + lastPayment.penalty ) # amortizationItem.days = lastPayment.days amortizationItem.days = cycle amortizationItem.schedule = schedule # amortizationItem.principal = lastPayment.principal # amortizationItem.interest = lastPayment.interest amortizationItem.daysExceed = lastPayment.daysExceed amortizationItem.daysAdvanced = lastPayment.daysAdvanced amortizationItem.additionalInterest = lastPayment.additionalInterest amortizationItem.penalty = lastPayment.penalty lastCheck = amortizationItem.getPDC() print(lastCheck) if lastCheck: lastCheck.amortizationItem = amortizationItem lastCheck.save() # print(lastPayment.balance) # if lastPayment.balance > 0: # print(lastPayment.balance) # print("balance2") # amortizationItem.amortizationStatus = AmortizationStatus.objects.get(pk=3) #partial # amortizationItem.principal = lastPayment.balance # amortizationItem.interest = principalBalance * (loan.interestRate.interestRate/100) * loan.term.paymentPeriod.paymentCycle/360 amortizationItem.save() lastPayment.amortizationItem = amortizationItem lastPayment.save() for payment in payments: payment.amortizationItem = amortizationItem payment.save() # schedule = lastPayment.datePayment else: if loanAmount > 0: pmt = pmt.getPayment( loanAmount, loan.interestRate.interestRate, termDays, noOfPaymentSchedules, noOfPaymentSchedules - (i - 1), ) lastPayment = schedule - timezone.timedelta(days=cycle) dayTillCutOff = cycle - int(lastPayment.strftime("%d")) accruedInterest = (int(pmt.principal)) * ( loan.interestRate.interestRate / 100) * dayTillCutOff / 360 amortizationItem = AmortizationItem( schedule=schedule, amortization=amortization, days=cycle, principal=pmt.principal, deductAccruedInterest=pmt.interest - accruedInterest, accruedInterest=accruedInterest, interest=pmt.interest, additionalInterest=0, penalty=0, vat=0, total=pmt.payment, principalBalance=pmt.nextStartingValue, amortizationStatus=AmortizationStatus.objects.get(pk=1), ) amortizationItem.save() loanAmount = pmt.nextStartingValue else: amortizationItem = AmortizationItem( schedule=schedule, amortization=amortization, days=cycle, principal=0, accruedInterest=0, deductAccruedInterest=0, interest=0, vat=0, total=0, principalBalance=0, amortizationStatus=AmortizationStatus.objects.get(pk=1), ) amortizationItem.save() schedule = schedule + timezone.timedelta(days=cycle) i = i + 1