diff --git a/ClientAdvisor/App/app.py b/ClientAdvisor/App/app.py index e8221243..725dd1aa 100644 --- a/ClientAdvisor/App/app.py +++ b/ClientAdvisor/App/app.py @@ -1591,32 +1591,61 @@ def get_users(): cursor.execute( """select DATEDIFF(d,CAST(max(StartTime) AS Date),CAST(GETDATE() AS Date)) + 3 as ndays from ClientMeetings""" ) - rows = cursor.fetchall() - ndays = 0 - for row in rows: - ndays = row["ndays"] - sql_stmt1 = f"UPDATE ClientMeetings SET StartTime = dateadd(day,{ndays},StartTime), EndTime = dateadd(day,{ndays},EndTime)" - cursor.execute(sql_stmt1) + meeting_rows = cursor.fetchall() + ndays = meeting_rows[0].get("ndays") + + client_update_stmt = f"UPDATE ClientMeetings SET StartTime = dateadd(day,{ndays},StartTime), EndTime = dateadd(day,{ndays},EndTime)" + cursor.execute(client_update_stmt) conn.commit() - nmonths = int(ndays / 30) - if nmonths > 0: - sql_stmt1 = ( - f"UPDATE Assets SET AssetDate = dateadd(MONTH,{nmonths},AssetDate)" - ) - cursor.execute(sql_stmt1) - conn.commit() - sql_stmt1 = f"UPDATE Retirement SET StatusDate = dateadd(MONTH,{nmonths},StatusDate)" - cursor.execute(sql_stmt1) - conn.commit() + asset_stmt = """WITH MaxAssetDate AS ( + SELECT MAX(AssetDate) AS MaxDate + FROM Assets + ), + Today AS ( + SELECT GETDATE() AS TodayDate + ), + DaysDifference AS ( + SELECT DATEDIFF(DAY, MaxDate, TodayDate) - 30 AS DaysDifference + FROM MaxAssetDate, Today + ) + SELECT DaysDifference + FROM DaysDifference + """ + cursor.execute(asset_stmt) + asset_rows = cursor.fetchall() + nmonths = int(asset_rows[0].get("DaysDifference") / 30) + if nmonths > 0: + asset_update_stmt = ( + f"UPDATE Assets SET AssetDate = dateadd(MONTH,{nmonths},AssetDate)" + ) + cursor.execute(asset_update_stmt) + conn.commit() - cursor = conn.cursor() - cursor.execute(sql_stmt) - rows = cursor.fetchall() + retirement_stmt = """WITH MaxStatusDate AS ( + SELECT MAX(StatusDate) AS MaxDate + FROM Retirement + ), + Today AS ( + SELECT GETDATE() AS TodayDate + ), + DaysDifference AS ( + SELECT DATEDIFF(DAY, MaxDate, TodayDate) - 30 AS DaysDifference + FROM MaxStatusDate, Today + ) + SELECT DaysDifference + FROM DaysDifference + """ + cursor.execute(retirement_stmt) + retirement_rows = cursor.fetchall() + nmonths = int(retirement_rows[0].get("DaysDifference") / 30) + if nmonths > 0: + retire_update_stmt = f"UPDATE Retirement SET StatusDate = dateadd(MONTH,{nmonths},StatusDate)" + cursor.execute(retire_update_stmt) + conn.commit() users = [] for row in rows: - # print(row) user = { "ClientId": row["ClientId"], "ClientName": row["Client"], @@ -1631,7 +1660,6 @@ def get_users(): "ClientSummary": row["ClientSummary"], } users.append(user) - # print(users) return jsonify(users)