I am writing a simple Python program to check for expired service contracts in a Firebird database and create a report based on that query which will be used to send automatic e-mails to the customers.
The function is looking for about to be expiring contracts between two dates: date_from = today + 5days and date_to = today + 10days.
My code looks like this:
t = (date_from, date_to) # Create tuple with both dates
print()
cur = con.cursor()
cur.execute("SELECT C_CUST_BULSTAT, C_CUST_NAME, C_OBJ_PHONE,C_ECR_NUMB, C_FROMDATE, C_TODATE, C_NUMBER, C_TYPE FROM CONTRACTS "
"WHERE (C_TYPE = 2) "
"AND (C_TODATE BETWEEN ? AND ?);", t) # Make query matching the tuple
The problem I found is that if the customer comes today 22.01 and signs a new contract from 30.01.2022 to 30.01.2023 the system will have 2 contracts - one expiring and one renewed. According to my code - they will be notified about contract expiry which is wrong. So basically from in case of two contract records with two C_TODATE only the higher should be taken into account and checked against dates matching criteria.
So when doing the checks I should take the contract with latest C_TODATE from the list. How I can do that?