@@ -1588,35 +1588,72 @@ def get_users():
15881588 if len (rows ) <= 6 :
15891589 # update ClientMeetings,Assets,Retirement tables sample data to current date
15901590 cursor = conn .cursor ()
1591- cursor .execute (
1592- """select DATEDIFF(d,CAST(max(StartTime) AS Date),CAST(GETDATE() AS Date)) + 3 as ndays from ClientMeetings"""
1593- )
1594- rows = cursor .fetchall ()
1595- ndays = 0
1596- for row in rows :
1597- ndays = row ["ndays" ]
1598- sql_stmt1 = f"UPDATE ClientMeetings SET StartTime = dateadd(day,{ ndays } ,StartTime), EndTime = dateadd(day,{ ndays } ,EndTime)"
1599- cursor .execute (sql_stmt1 )
1600- conn .commit ()
1601- nmonths = int (ndays / 30 )
1602- if nmonths > 0 :
1603- sql_stmt1 = (
1604- f"UPDATE Assets SET AssetDate = dateadd(MONTH,{ nmonths } ,AssetDate)"
1591+ combined_stmt = """
1592+ WITH MaxDates AS (
1593+ SELECT
1594+ MAX(CAST(StartTime AS Date)) AS MaxClientMeetingDate,
1595+ MAX(AssetDate) AS MaxAssetDate,
1596+ MAX(StatusDate) AS MaxStatusDate
1597+ FROM
1598+ (SELECT StartTime, NULL AS AssetDate, NULL AS StatusDate FROM ClientMeetings
1599+ UNION ALL
1600+ SELECT NULL AS StartTime, AssetDate, NULL AS StatusDate FROM Assets
1601+ UNION ALL
1602+ SELECT NULL AS StartTime, NULL AS AssetDate, StatusDate FROM Retirement) AS Combined
1603+ ),
1604+ Today AS (
1605+ SELECT GETDATE() AS TodayDate
1606+ ),
1607+ DaysDifference AS (
1608+ SELECT
1609+ DATEDIFF(DAY, MaxClientMeetingDate, TodayDate) + 3 AS ClientMeetingDaysDifference,
1610+ DATEDIFF(DAY, MaxAssetDate, TodayDate) - 30 AS AssetDaysDifference,
1611+ DATEDIFF(DAY, MaxStatusDate, TodayDate) - 30 AS StatusDaysDifference
1612+ FROM MaxDates, Today
16051613 )
1606- cursor .execute (sql_stmt1 )
1614+ SELECT
1615+ ClientMeetingDaysDifference,
1616+ AssetDaysDifference / 30 AS AssetMonthsDifference,
1617+ StatusDaysDifference / 30 AS StatusMonthsDifference
1618+ FROM DaysDifference
1619+ """
1620+ cursor .execute (combined_stmt )
1621+ date_diff_rows = cursor .fetchall ()
1622+
1623+ client_days = (
1624+ date_diff_rows [0 ]["ClientMeetingDaysDifference" ]
1625+ if date_diff_rows
1626+ else 0
1627+ )
1628+ asset_months = (
1629+ int (date_diff_rows [0 ]["AssetMonthsDifference" ]) if date_diff_rows else 0
1630+ )
1631+ status_months = (
1632+ int (date_diff_rows [0 ]["StatusMonthsDifference" ])
1633+ if date_diff_rows
1634+ else 0
1635+ )
1636+
1637+ # Update ClientMeetings
1638+ if client_days > 0 :
1639+ client_update_stmt = f"UPDATE ClientMeetings SET StartTime = DATEADD(day, { client_days } , StartTime), EndTime = DATEADD(day, { client_days } , EndTime)"
1640+ cursor .execute (client_update_stmt )
16071641 conn .commit ()
16081642
1609- sql_stmt1 = f"UPDATE Retirement SET StatusDate = dateadd(MONTH,{ nmonths } ,StatusDate)"
1610- cursor .execute (sql_stmt1 )
1643+ # Update Assets
1644+ if asset_months > 0 :
1645+ asset_update_stmt = f"UPDATE Assets SET AssetDate = DATEADD(month, { asset_months } , AssetDate)"
1646+ cursor .execute (asset_update_stmt )
16111647 conn .commit ()
16121648
1613- cursor = conn .cursor ()
1614- cursor .execute (sql_stmt )
1615- rows = cursor .fetchall ()
1649+ # Update Retirement
1650+ if status_months > 0 :
1651+ retire_update_stmt = f"UPDATE Retirement SET StatusDate = DATEADD(month, { status_months } , StatusDate)"
1652+ cursor .execute (retire_update_stmt )
1653+ conn .commit ()
16161654
16171655 users = []
16181656 for row in rows :
1619- # print(row)
16201657 user = {
16211658 "ClientId" : row ["ClientId" ],
16221659 "ClientName" : row ["Client" ],
@@ -1631,7 +1668,6 @@ def get_users():
16311668 "ClientSummary" : row ["ClientSummary" ],
16321669 }
16331670 users .append (user )
1634- # print(users)
16351671
16361672 return jsonify (users )
16371673
0 commit comments