From 4dae3256c532cb5b8d1a545959d04e0f48d213fa Mon Sep 17 00:00:00 2001 From: JohnKent Date: Tue, 5 Mar 2019 00:09:56 -0500 Subject: [PATCH] Updated the template to print the correct interest information. Fixed logic in script to send correct interest information to the template. Added ability to do a test run to a different email address with a real data file (the debug flag replaces the email address with a hard coded one). Added an aborting checking that the payments are listed in order. --- mortgage_template.py | 25 ++++++++++++++++++++----- statement.pdf.jinja | 2 +- testloan.json | 6 ++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mortgage_template.py b/mortgage_template.py index 69459c2..8565c46 100644 --- a/mortgage_template.py +++ b/mortgage_template.py @@ -114,6 +114,13 @@ def amortizeLoan(loan): payment_date = datetime.strptime((payment[0]), '%Y-%m-%d').date() payment_amount = Decimal(payment[1]).quantize(Decimal("1.00")) days_since_last_payment = (payment_date - interest_paid_through_date).days + + #check for out of order payments, generally a sign of a data entry problem, especially years + if days_since_last_payment < 0: + print "Payment Number %s appears out of order. The payment date '%s' is before the previous payment on '%s'." \ + % (payment_number, payment_date, interest_paid_through_date) + quit() + new_interest = (days_since_last_payment * remaining_principal * daily_interest_rate).quantize(Decimal("0.00")) new_principal = payment_amount - new_interest interest_paid_through_date = payment_date @@ -121,6 +128,7 @@ def amortizeLoan(loan): annual_interest = annual_interest + new_interest remaining_principal = remaining_principal - new_principal + # create the payment record for the template to render payment_record = {} payment_record['year']=next_bill_date.year @@ -143,6 +151,7 @@ def amortizeLoan(loan): if old_bill_date.month < 12: next_bill_date = date(year=old_bill_date.year, month=old_bill_date.month + 1, day=payment_day_of_month) else: + annual_interest = Decimal("0.00") next_bill_date = date(year=old_bill_date.year + 1, month=1, day = payment_day_of_month) loan["total_interest_paid_to_date"] = total_interest @@ -278,10 +287,14 @@ def main(): # at closing. The first payment will incur interest from one month before the bill is due. # read in the file + test_flag = True + #test_flag = False + test_address = 'jkent3rd@yahoo.com' + filename = "./testloan.json" - # filename = "./10Kloan.json" - # filename = "./dadmortgage.json" - # filename = "./brendamortgage.json" + #filename = "./10Kloan.json" + #filename = "./dadmortgage.json" + #filename = "./brendamortgage.json" #filename = "./greenfield_mortgage.json" template_filename = "statement.pdf.jinja" @@ -299,8 +312,10 @@ def main(): emailParameters = loan["email"] msg = generateEmail(emailParameters["from_address"], emailParameters["to_address"], emailParameters["subject"], emailParameters["body"], pdfAttachment, report) - sendEmail(msg, emailParameters["from_address"], emailParameters["to_address"], emailParameters['password']) - + if test_flag == False: + sendEmail(msg, emailParameters["from_address"], emailParameters["to_address"], emailParameters['password']) + else: + sendEmail(msg, emailParameters["from_address"], test_address, emailParameters['password']) if __name__ == '__main__': main() diff --git a/statement.pdf.jinja b/statement.pdf.jinja index 288e119..acd59c5 100644 --- a/statement.pdf.jinja +++ b/statement.pdf.jinja @@ -43,7 +43,7 @@ {{ "$%.2f"|format(item.interest_payment) }} {{ "$%.2f"|format(item.new_balance) }} - {% if item.month == '12' or loop.last %} + {% if item.month == 12 or loop.last %} Total interest paid in {{item.year}} is {{ "$%.2f"|format(item.annual_interest_to_date) }}. {% endif %} {% endfor %} diff --git a/testloan.json b/testloan.json index f0cc95c..8a7331b 100644 --- a/testloan.json +++ b/testloan.json @@ -4,7 +4,7 @@ "format": "html", "email": { "from_address": "jkent3rd@gmail.com", - "to_address": "jkent3rd@yahoo.com", + "to_address": "jkent3rd@gmail.com", "server": "smtp.gmail.com", "password": "pvyrbcnzrjoizprn", "template": "./template.txt", @@ -38,7 +38,9 @@ ["2018-09-26", "589.00"], ["2018-10-15", "0"], ["2018-11-29", "589.00"], - ["2018-12-31", "589.00"] + ["2018-12-31", "589.00"], + ["2019-01-28", "589.00"], + ["2019-03-15", "589.00"] ], "borrower": { "name": "Bear Houses, LLC",