Subversion Repository Public Repository

ChrisCompleteCodeTrunk

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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;
//using DocumentProcessor80;
//using LFSO80Lib;
//using LFSO83Lib;
//using DocumentProcessor83;
//using PdfExporter83;
using LFSO83Lib;
using DocumentProcessor83;
using PdfExporter83;
using System.Net.Mail;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace EmailAndFaxWithStmt
{
    public sealed class EventLogLevel
    {
        private EventLogLevel()
        {
        }

        public const int Information = 2;
        public const int Warning = 1;
        public const int Error = 0;
    }

    public class EmailFaxLib
    {
        // Sleep in seconds
        private static int ThreadSleep = int.Parse(ConfigurationManager.AppSettings["ThreadSleep"]);
        private static string lfUser = ConfigurationManager.AppSettings["lfUser"];
        private static string lfPass = ConfigurationManager.AppSettings["lfPass"];
        private static string lfRepo = ConfigurationManager.AppSettings["lfRepo"];
        private static string lfServ = ConfigurationManager.AppSettings["lfServ"];
        private static string lfParent = ConfigurationManager.AppSettings["lfParent"];
        private static string SMTPServerName = ConfigurationManager.AppSettings["SMTPServerName"];
        private static int Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
        private static string FromEmailAddress = ConfigurationManager.AppSettings["FromEmailAddress"];
        private static string BCC = ConfigurationManager.AppSettings["BCC"];
        private static string Body = ConfigurationManager.AppSettings["Body"];
        private static string SubjectInvoice = ConfigurationManager.AppSettings["SubjectInvoice"];
        private static string SubjectStmt = ConfigurationManager.AppSettings["SubjectStmt"];
        private static string FaxKey = ConfigurationManager.AppSettings["FaxKey"];
        private static string AdminEmailAddress = ConfigurationManager.AppSettings["AdminEmailAddress"];
        private static string AdminSubject = ConfigurationManager.AppSettings["AdminSubject"];
        private static string AdminBodyEmail = ConfigurationManager.AppSettings["AdminBodyEmail"];
        private static string AdminBodyEmailGnrl = ConfigurationManager.AppSettings["AdminBodyEmailGnrl"];
        private static string AdminBodyFax = ConfigurationManager.AppSettings["AdminBodyFax"];

        private static string lfStmtFolder = ConfigurationManager.AppSettings["lfStmtFolder"];
        private static string lfInvFolder = ConfigurationManager.AppSettings["lfInvFolder"];

        private static bool IslfInvFolderChecked = Convert.ToBoolean(ConfigurationManager.AppSettings["IslfInvFolderChecked"]);
        private static bool IslfStmtFolderChecked = Convert.ToBoolean(ConfigurationManager.AppSettings["IslfStmtFolderChecked"]);

        private static string lfStmtFolderpath = ConfigurationManager.AppSettings["lfStmtFolderpath"];
        private static string lfInvFolderPath = ConfigurationManager.AppSettings["lfInvFolderPath"];

        private static string PdfFolderPath = ConfigurationManager.AppSettings["PdfFolderPath"];

        private static string TemplateName = ConfigurationManager.AppSettings["TemplateName"];

        private static bool TestMode = ConfigurationManager.AppSettings["Test Mode"].Equals("True");

        static ArrayList stmtAttachments = new ArrayList();
        static ArrayList invAttachments = new ArrayList();

        static ArrayList stmtEmailAttachments = new ArrayList();
        static ArrayList invFaxAttachments = new ArrayList();

        static ArrayList stmtAttachmentNames = new ArrayList();
        static ArrayList invAttachmentNames = new ArrayList();

        static ArrayList stmtFaxAttachmentNames = new ArrayList();
        static ArrayList invFaxAttachmentNames = new ArrayList();

        static List<int> listMarkPages = new List<int>();

        static LFConnection connection = null;
        static LFDatabase db = null;
        static LFFolder parentFolder = null;

        private static string docName = string.Empty;
        private static int docID;

        private static string invDocName = string.Empty;
        private static int invDocID;

        private static string stmtDocName = string.Empty;
        private static int stmtDocID;


        private EmailFaxLib()
        {
        }

        public static void ProcessNewDocuments()
        {
            if (!Directory.Exists(PdfFolderPath))
                Directory.CreateDirectory(PdfFolderPath);

            invAttachments.Clear();
            invAttachmentNames.Clear();
            stmtAttachments.Clear();
            stmtAttachmentNames.Clear();

            //Delete all files
            try
            {
                string[] deletepaths = Directory.GetFiles(PdfFolderPath);
                foreach (string deletepath in deletepaths)
                    File.Delete(deletepath);
            }
            catch
            {

            }
            string ErrorInfo = string.Empty;
            try
            {
                if (Configuration.LogLevel >= EventLogLevel.Information)
                    EventLog.WriteEntry(Configuration.LogSource, "Folder: " + lfParent, EventLogEntryType.Information);

                LFApplication app = new LFApplication();
                LFServer serv = app.GetServerByName(lfServ);
                db = serv.GetDatabaseByName(lfRepo);

                CreateConnection(lfParent, db);

                if (connection != null && !connection.IsTerminated)
                {

                }

                ILFFolder ParentFolder = db.GetEntryByPath(lfParent);
                ILFCollection allDocs = ParentFolder.GetChildren();
                LFTemplate MyTemp = (LFTemplate)db.GetTemplateByName(TemplateName);

                SmtpClient objMailer = new SmtpClient(SMTPServerName, Port);

                int doNotSentStampID = 0;
                try
                {
                    if (db.GetPublicStampByName("DO NOT SEND") != null)
                        doNotSentStampID = Convert.ToInt32(db.GetPublicStampByName("DO NOT SEND").ID);
                }
                catch { }

                //// Look for Invoice attachment or statement folder for Attachements
                //if (IslfInvFolderChecked == true )
                //    ProcessInvoice(db.GetEntryByPath(lfInvFolder));

                //if (IslfStmtFolderChecked == true)
                //    ProcessStatement(db.GetEntryByPath(lfStmtFolder));

                // Look into Parent folder
                foreach (ILFEntry entry in allDocs)
                {
                    if (entry.EntryType == Entry_Type.ENTRY_TYPE_DOCUMENT)
                    {
                        // Clear Previous entries
                        if (invAttachmentNames.Count > 0)
                            invAttachments.Clear();
                        if (invAttachmentNames.Count > 0)
                            invAttachmentNames.Clear();
                        if (stmtAttachments.Count > 0)
                            stmtAttachments.Clear();
                        if (stmtAttachmentNames.Count > 0)
                            stmtAttachmentNames.Clear();

                        LFDocument doc = (LFDocument)entry;
                        string timeStamp = DateTime.Now.ToString("MMddyyyyHHmmss");

                        try
                        {
                            if (doc.Template != null && ((LFTemplate)doc.Template).Name == TemplateName)
                            {
                                LFTemplate template;
                                LFFieldData fields;
                                template = doc.Template;
                                fields = doc.FieldData;

                                //doc.LockObject(Lock_Type.LOCK_TYPE_WRITE);

                                // LOCK the Doc before getting data
                                fields.LockObject(Lock_Type.LOCK_TYPE_WRITE);

                                string LfDocType = fields.Field["Document Type"];
                                string LfHasBeenEmailed = fields.Field["Has been emailed"];
                                string LfInvoiceVia = fields.Field["Invoice Via"];

                                docName = doc.Name;
                                docID = doc.ID;

                                // Checking Temp Fields "Has been emailed" for "YES" and "Has been emailed" for "YES"
                                if (LfHasBeenEmailed != "Yes" && LfHasBeenEmailed != "PROBLEM")
                                {
                                    if (LfDocType == "Statements" || LfDocType == "Invoice" || LfDocType == "Credit")
                                    {
                                        // Chris Landress : 07/02/2017 - Check that none of the required values are null before continuing.
                                        //                               If so must be put into Hold* folder with a send via value of "PROBLEM". 
                                        if (LfInvoiceVia.Equals("E") || LfInvoiceVia.Equals("F") || LfInvoiceVia.Equals("A") || LfInvoiceVia.Equals("B") || LfInvoiceVia.Equals("C"))
                                        {
                                            Attachment Attachment = null;
                                            MailMessage Message = new MailMessage();

                                            string Email = string.Empty;
                                            string invnum = string.Empty;
                                            string statement = string.Empty;
                                            string statementSubject = string.Empty;

                                            //Get all field Values
                                            string LfEmailAddress = fields.Field["Email Address"];
                                            string LfEmailAddress1 = fields.Field["Email Address #2"];
                                            string LfFaxNumber = fields.Field["Fax Number"];
                                            string LfFaxNumber1 = fields.Field["Fax Number #2"];
                                            string LfInvoiceNo = fields.Field["Invoice Number"];

                                            //if (LfHasBeenEmailed != "Yes" && LfHasBeenEmailed != "PROBLEM")
                                            //{
                                            MailProcessor.connection = connection;

                                            if (LfInvoiceVia != null && (LfInvoiceVia.Equals("F") || LfInvoiceVia.Equals("C") || LfInvoiceVia.Equals("A")))
                                            {
                                                MailProcessor.FaxDocument(doc, db);
                                            }

                                            if (LfInvoiceVia != null && (LfInvoiceVia.Equals("E") || LfInvoiceVia.Equals("B") || LfInvoiceVia.Equals("A")))
                                            {
                                                MailProcessor.EmailDocument(doc, db);
                                            }
                                        }
                                        else
                                        {
                                            if (!fields.IsLocked)
                                            {
                                                fields.LockObject(Lock_Type.LOCK_TYPE_WRITE);
                                            }
                                            fields.Field["Has been emailed"] = "PROBLEM";
                                            fields.Update();
                                            fields.UnlockObject();
                                            EventLog.WriteEntry(Configuration.LogSource, string.Format("Marked as PROBLEM for the docID: {0} and docName: {1}. The Invoice Via field is empty or incorrect Must be: A, B, C, E or F.", docID, docName), EventLogEntryType.Warning);
                                        }
                                    }
                                    else
                                    {
                                        if (!fields.IsLocked)
                                        {
                                            fields.LockObject(Lock_Type.LOCK_TYPE_WRITE);
                                        }
                                        fields.Field["Has been emailed"] = "PROBLEM";
                                        fields.Update();
                                        fields.UnlockObject();
                                        EventLog.WriteEntry(Configuration.LogSource, string.Format("Marked as PROBLEM for the docID: {0} and docName: {1}. The Doctype field is {2}", docID, docName, LfDocType), EventLogEntryType.Warning);
                                    }
                                }

                                continue;
                            }
                        }
                        catch (System.Runtime.InteropServices.COMException ex)
                        {
                            if (ex.ErrorCode == -1073470679 && ex.Message == "Entry not found.")
                            {
                                string errorMessage = string.Format("{0} for docID:{1} and docName = {2} and LF COM errorCode = {3}", ex.Message, docID, docName, ex.ErrorCode);
                                EventLog.WriteEntry(Configuration.LogSource, errorMessage, EventLogEntryType.Error);
                            }

                            if (ex.ErrorCode == -1073470666 && ex.Message == "Entry locked.")
                            {
                                string errorMessage = string.Format("{0} for docID:{1} and docName = {2} and LF COM errorCode = {3}", ex.Message, docID, docName, ex.ErrorCode);
                                EventLog.WriteEntry(Configuration.LogSource, errorMessage, EventLogEntryType.Error);
                            }

                            if (ex.ErrorCode == -2147220671 && ex.Message == "Page does not contain image data.")
                            {
                                string errorMessage = string.Format("{0} for docID:{1} and docName = {2} and LF COM errorCode = {3}", ex.Message, docID, docName, ex.ErrorCode);
                                EventLog.WriteEntry(Configuration.LogSource, errorMessage, EventLogEntryType.Error);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (Configuration.LogLevel >= EventLogLevel.Error)
                    EventLog.WriteEntry(Configuration.LogSource, ex.ToString() + Environment.NewLine + Environment.NewLine +
                                     "[Error Info]" + Environment.NewLine + ErrorInfo + Environment.NewLine + Environment.NewLine +
                                     "[Stack Trace]" + Environment.NewLine + new System.Diagnostics.StackTrace().ToString(), EventLogEntryType.Error);
                //Debug.Assert(false, ex.Message);
            }
            finally
            {
                CloseConnection();
            }
        }

        public static LFConnection CreateConnection(string folderpath, LFDatabase database)
        {
            try
            {
                if (connection == null || connection.IsTerminated)
                {
                    connection = new LFConnection();
                    connection.UserName = lfUser;
                    connection.Password = lfPass;
                    connection.Create(database);

                    parentFolder = database.GetEntryByPath(folderpath);
                    EventLog.WriteEntry(Configuration.LogSource, "LF Connection to the database successfully created", EventLogEntryType.Information);
                }
                //}
            }
            catch (Exception ex)
            {
                CloseConnection();
                throw ex;
            }

            return connection;
        }

        public static void CloseConnection()
        {
            try
            {
                if (connection != null && !connection.IsTerminated)
                {
                    try
                    {
                        connection.Terminate();
                        EventLog.WriteEntry(Configuration.LogSource, " LF Connection Terminated Successfully", EventLogEntryType.Information);
                    }
                    catch
                    {
                    }
                    connection = null;
                }
            }

            catch (System.Runtime.InteropServices.COMException ex)
            {
                string errorMessage = string.Format("{0} and LF COM errorCode = {1}", ex.Message, ex.ErrorCode);
                EventLog.WriteEntry(Configuration.LogSource, errorMessage, EventLogEntryType.Error);
            }

            catch (Exception ex)
            {
                throw ex;
            }
            connection = null;
        }
    }
}

Commits for ChrisCompleteCodeTrunk/ATC_EmailandFaxGateway/EmailAndFaxWithStmt/EmailFaxLib.cs

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000