Snippets
Created by
Nick Reilingh
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | DECLARE open_batch_users CURSOR LOCAL FAST_FORWARD FOR SELECT COUNT(*) AS num_batches, RTRIM(b.[owner]) AS [owner], MIN(b.create_dt) AS create_dt, ISNULL(u.fname, '') AS fname, ISNULL(u.lname, '') AS lname, ISNULL(u.email_address, '') AS email_address, ISNULL(u.location, '') AS location FROM dbo.T_BATCH b JOIN dbo.T_METUSER u ON b.[owner] = u.userid AND u.location <> 'System Resource' WHERE b.[status] NOT IN ('C', 'P') AND DATEDIFF(day, 0, b.create_dt) < DATEDIFF(day, 0, GETDATE()) AND u.email_address IS NOT NULL AND u.inactive <> 'Y' GROUP BY b.owner, u.fname, u.lname, u.email_address, u.location; DECLARE @payload nvarchar(max), @batchreport nvarchar(max); OPEN open_batch_users; DECLARE @num_batches int, @owner varchar(8), @create_dt datetime, @fname varchar(30), @lname varchar(30), @email_address varchar(80), @location varchar(30); WHILE 1 = 1 BEGIN; FETCH NEXT FROM open_batch_users INTO @num_batches, @owner, @create_dt, @fname, @lname, @email_address, @location; IF @@FETCH_STATUS <> 0 BREAK; -- Part time student employees should not be emailed about this. Instead, send a slack notification to ?box_office. IF @location = 'Box Office' BEGIN; SET @payload = @fname + ' ' + @lname + ' (' + @owner + ') has an unclosed batch from yesterday.'; EXEC dbo.LP_NOTIFY_SLACK @payload, '#box_office'; END; ELSE BEGIN; SET @batchreport = '<table> <thead><tr><th>batch_no</th><th>batch_type</th><th>status</th><th>owner</th><th>create_dt</th><th>open_loc</th></tr></thead> <tbody> ' + CONVERT(nvarchar(max), ( SELECT b.batch_no AS td, bt.description AS td, b.[status] AS td, b.[owner] AS td, b.create_dt AS td, ISNULL(b.open_loc, '') AS td FROM dbo.T_BATCH b JOIN dbo.TR_BATCH_TYPE bt ON b.batch_type = bt.id WHERE b.status NOT IN ('C', 'P') AND b.[owner] = @owner FOR XML RAW ('tr'), ELEMENTS )) + ' </tbody> </table>'; SET @payload = '<html> <head> <style> td, th { padding: 5px; } tbody td, thead th { border: 1px solid black; } table { border-collapse: collapse; } thead { text-align: left; } </style> </head> <body> <p>Hello ' + @fname + ',</p> <p>This email is to inform you that your Tessitura account (' + @owner + ') currently owns ' + IIF(@num_batches > 1, CONVERT(varchar, @num_batches) + ' unclosed batches', 'an unclosed batch') + ' since ' + CONVERT(varchar(6), @create_dt) + '.</p> <p>Please remember to close your batch at the end of the day, or whenever you are done using it. Multiple batches on the same day are fine, but accidentally processing a card transaction in a previous day''s batch is a Bad Thing™ to be avoided.</p> <p>Thank you!</p> <p>- The Happy Tessitura Batch Closing Enforcement Robot</p> ' + @batchreport + ' <img alt="Benedict Close-the-batch" src="https://misc.fc.bard.edu/assets/batch.jpg" /> </body> </html>'; EXEC msdb.dbo.sp_send_dbmail @profile_name = 'TessituraUserNotifications', @recipients = @email_address, @subject = 'Tessitura batch left open', @body_format = 'HTML', @body = @payload; END; END; CLOSE open_batch_users; DEALLOCATE open_batch_users; |
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.