-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReport.cs
199 lines (173 loc) · 8.81 KB
/
Report.cs
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// Copyright (c) 2022 by Acentus
// Developed by: Luis Cabrera
//
using System;
using System.Configuration;
using System.Data;
using System.Threading.Tasks;
namespace CoreOrderConfirmationEmailToPatient
{
class Report
{
JobCenterHistoryLogger logHeader = new JobCenterHistoryLogger();
JobCenterHistoryLoggerDetails logDetails = new JobCenterHistoryLoggerDetails();
private DataAccess db = new DataAccess();
public async Task RunReport()
{
try
{
//
// START LOGGING
//
logHeader = new JobCenterHistoryLogger(DateTime.Now, DateTime.Now, JobCenterHistoryLogger.StatusEnum.Running, "Report started");
logDetails = new JobCenterHistoryLoggerDetails(logHeader.HistoryId, "Test Mode: " + ConfigurationManager.AppSettings["TestMode"]);
logDetails = new JobCenterHistoryLoggerDetails(logHeader.HistoryId, "Subject: " + ConfigurationManager.AppSettings["subject"]);
logDetails = new JobCenterHistoryLoggerDetails(logHeader.HistoryId, "Email From: " + ConfigurationManager.AppSettings["emailFrom"]);
logDetails = new JobCenterHistoryLoggerDetails(logHeader.HistoryId, "Email To: " + ConfigurationManager.AppSettings["emailTo"]);
logDetails = new JobCenterHistoryLoggerDetails(logHeader.HistoryId, "Report Date: " + DateTime.Today.ToShortDateString());
// Get HTML template for report
string htmlPatient = Utils.GetTemplate("EmailTemplate.html");
string htmlList = Utils.GetTemplate("EmailList.html");
// Get data
DataTable dt = db.GetData();
// Populate template
await PopulateTemplate(dt, htmlList);
logDetails = new JobCenterHistoryLoggerDetails(logHeader.HistoryId, "Email: Sent Successfully");
logHeader.EndTime = DateTime.Now;
logHeader.Message = "Report Completed";
logHeader.Status = JobCenterHistoryLogger.StatusEnum.Success;
logHeader.Update();
}
catch (Exception ex)
{
Log.write(string.Format("REPORT EXCEPTION: " + ex.Message));
logHeader.EndTime = DateTime.Now;
logHeader.Status = JobCenterHistoryLogger.StatusEnum.Error;
logHeader.Message = ex.Message;
logHeader.Update();
throw;
}
}
private async Task PopulateTemplate(DataTable listItems, string htmlList)
{
int iTotal = 0;
string tr = string.Empty;
string reportDate = DateTime.Today.ToShortDateString();
string strDetails = GetDetailsTemplate(ref htmlList);
string strThisDetails = strDetails;
string list1 = string.Empty;
string thisHtml = htmlList;
if (listItems != null && listItems.Rows.Count > 0)
{
Log.write("Records returned: " + listItems.Rows.Count.ToString());
int prevPatientId = 0;
string prevPONUmber = string.Empty;
DateTime prevServiceDate = DateTime.MinValue;
foreach (DataRow item in listItems.Rows)
{
iTotal++;
int thisPatientId = item.Field<int>("PATIENTID");
string FullName = item.Field<string>("FULLNAME");
string thisPONumber = item.Field<string>("INVOICENO");
DateTime thisAddedDate = item.Field<DateTime>("ADDEDONDATE");
DateTime thisServiceDate = item.Field<DateTime>("SVCDATE");
string thisEmail = item.Field<string>("EMAIL");
// Logic when there are multiple service dates for the same patient, invoice number, and added on date
string strServiceDates = thisServiceDate.ToShortDateString();
string strComma = ", ";
if (thisPatientId == prevPatientId)
{
if (thisPONumber != prevPONUmber)
{
if (thisServiceDate != prevServiceDate)
{
strServiceDates += strComma + prevServiceDate.ToShortDateString();
}
}
}
string emailTemplateHtml = Utils.GetTemplate("emailTemplate.html");
emailTemplateHtml = emailTemplateHtml.Replace("[PATIENT_NAME]", FullName);
emailTemplateHtml = emailTemplateHtml.Replace("[ADDED_DATE]", thisAddedDate.ToShortDateString());
emailTemplateHtml = emailTemplateHtml.Replace("[SERVICE_DATE]", strServiceDates);
if (!string.IsNullOrEmpty(thisEmail))
{
try
{
// Send confirmation email to customer
SendEmailToPatient(thisPatientId.ToString(), emailTemplateHtml, thisServiceDate.ToShortDateString(), thisEmail);
InsertContactNoteToPatient(thisPatientId.ToString(), strServiceDates, emailTemplateHtml);
}
catch
{
// Let the application continue
}
}
// Populate the main report
strThisDetails = strDetails;
strThisDetails = strThisDetails.Replace("[PATIENT_ID]", thisPatientId.ToString());
strThisDetails = strThisDetails.Replace("[PATIENT_NAME]", FullName);
strThisDetails = strThisDetails.Replace("[EMAIL]", thisEmail);
strThisDetails = strThisDetails.Replace("[INVOICE_NUMBER]", thisPONumber);
strThisDetails = strThisDetails.Replace("[ADDED_ON_DATE]", thisAddedDate.ToShortDateString());
strThisDetails = strThisDetails.Replace("[SERVICE_DATE]", strServiceDates);
tr += strThisDetails;
prevPatientId = thisPatientId;
prevPONUmber = thisPONumber;
prevServiceDate = thisServiceDate;
}
//
// Prepare report details
//
thisHtml = thisHtml.Replace("[TOTAL_PATIENTS]", iTotal.ToString());
thisHtml = thisHtml.Replace("[GRID_HERE]", tr);
thisHtml = thisHtml.Replace("[REPORT_DATE]", App.DateSpan);
thisHtml = thisHtml.Replace("[DETAILS1_HERE]", tr);
// only send report if there is data
if (iTotal > 0)
{
await Utils.SendEmailWithModernAuthentication("0000", thisHtml, "Confirmation Email to Patients Report", "");
Log.write("Report completed");
}
else
{
Log.write("Report not sent - NO DATA");
}
}
else
{
Log.write("No data found for the report that matched the criteria specified");
}
}
private string GetDetailsTemplate(ref string html)
{
int iFirstIndex = html.IndexOf("[DETAIL1_START]");
int iFirstIndexDetails = iFirstIndex + 15;
int iLastIndexDetails = html.IndexOf("[/DETAIL1_END]");
int iLastIndex = iLastIndexDetails + 14;
int iTotalLenght = html.Length;
string strTemplate = html.Substring(iFirstIndexDetails, iLastIndexDetails - iFirstIndexDetails);
html = html.Substring(0, iFirstIndex) + "[DETAILS1_HERE]" + html.Substring(iLastIndex, iTotalLenght - iLastIndex);
return strTemplate;
}
private async void SendEmailToPatient(string id, string html, string serviceDate, string emailTo)
{
await Utils.SendEmailWithModernAuthentication(id, html, "Acentus Order Confirmation", emailTo);
}
private void InsertContactNoteToPatient(string id, string serviceDate, string html)
{
string testMode = System.Configuration.ConfigurationManager.AppSettings["TestMode"];
if (testMode == "True")
{
// Do nothing.
}
else
{
string note = "ORDER CONFIRMATION EMAIL SENT CONFIRMING PATIENT’S AUTHORIZATION TO SHIP FOR NEXT DOS OF " + serviceDate;
db.InsertNote(id, note, html);
Log.write("Contact note added successfully to : " + id + " - " + id);
}
}
}
}