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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using LFSO83Lib;
using DocumentProcessor83;
using System.Data.SqlClient;

namespace BT_Statement_Merge
{
    public class Laserfiche
    {
        public LFApplication lfapp;
        public LFServer lfserv;
        public LFDatabase lfdb;
        public LFConnection conn;

        public string lfServer = ConfigurationManager.AppSettings["lfServer"];
        public string lfUser = ConfigurationManager.AppSettings["lfUser"];
        public string lfPass = ConfigurationManager.AppSettings["lfPass"];
        public string lfRepo = ConfigurationManager.AppSettings["lfRepo"];
        public string lfFolder = ConfigurationManager.AppSettings["lfFolder"];

        public Laserfiche()
        {
            lfapp = new LFApplication();
            lfserv = (LFServer)lfapp.GetServerByName(lfServer);
            lfdb = (LFDatabase)lfserv.GetDatabaseByName(lfRepo);
            conn = new LFConnection();
            conn.UserName = lfUser;
            conn.Password = lfPass;
            conn.Create(lfdb);
        }

        public void LaserficheActivate()
        {
            try
            {
                conn.Connect(lfdb);
            }
            catch (Exception ex)
            {
                Reporting.Error(ex.Message, ex.StackTrace);
            }
        }

        public void LaserficheDeactivate()
        {
            try
            {
                conn.Terminate();
            }
            catch {}
        }

        public ArrayList GetStatements()
        {
            ArrayList statements = new ArrayList();
            string mergeStatus = ConfigurationManager.AppSettings["Merge Status"];

            ILFFolder ParentFolder = (ILFFolder)lfdb.GetEntryByPath(lfFolder);
            ILFCollection allDocs = (ILFCollection)ParentFolder.GetChildren();

            foreach (ILFEntry entry in allDocs)
            {
                if (entry.EntryType != Entry_Type.ENTRY_TYPE_DOCUMENT)
                    continue;
                LFDocument doc = entry as LFDocument;
                LFFieldData fields = doc.FieldData;
                if (((string)fields.Field[ConfigurationManager.AppSettings["Document Type Field"]]).Equals(ConfigurationManager.AppSettings["Statement DocType"]) &&
                    ((string)fields.Field[ConfigurationManager.AppSettings["Status Field"]]).Equals(mergeStatus))
                {
                    statements.Add(entry);
                }
            }

            Console.Write(statements.Count + " statements found in Waiting state\n");

            return statements;
        }

        public ArrayList GetStaticDocumentIDs()
        {
            string staticDocFolder = ConfigurationManager.AppSettings["Static Page Folder"];
            ArrayList documents = new ArrayList();

            ILFFolder ParentFolder = (ILFFolder)lfdb.GetEntryByPath(staticDocFolder);
            ILFCollection allDocs = (ILFCollection)ParentFolder.GetChildren();

            foreach (ILFEntry entry in allDocs)
            {
                if (entry.EntryType != Entry_Type.ENTRY_TYPE_DOCUMENT)
                    continue;
                LFDocument doc = entry as LFDocument;
                documents.Add(doc.ID);
            }

            return documents;
        }

        public LFDocument GetDocument(int id)
        {
            return lfdb.GetEntryByID(id) as LFDocument;
        }

        public ArrayList GetOpenInvoiceIDs(int CustomerNumber, ArrayList InvoiceNumbers, int StoreNumber = -1)
        {
            ArrayList result = new ArrayList();
            ArrayList found = new ArrayList();
            ArrayList notFound = new ArrayList();

            if (InvoiceNumbers.Count == 0)
            {
                result.Add(found);
                result.Add(notFound);
                return result;
            }

            SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["MSSQL"].ConnectionString);
            string cust_num_col = ConfigurationManager.AppSettings["Customer Number Field"];
            string inv_num_col = ConfigurationManager.AppSettings["Invoice Number Field"];
            string store_num_col = ConfigurationManager.AppSettings["Store Number Field"];
            sql.Open();
            string lfsql = ConfigurationManager.AppSettings["LFSQL DB"];
            string query = "";

            if (StoreNumber != -1)
            {
                query = "SELECT doc.tocid as id, propval1.num_val as custnum, propval2.num_val as invoicenum, propval3.num_val as storenum " +
                "FROM " + lfsql + ".dbo.doc as doc " +
                "JOIN " + lfsql + ".dbo.propval as propval1 ON doc.tocid = propval1.tocid " +
                "JOIN " + lfsql + ".dbo.propdef as propdef1 ON propval1.prop_id = propdef1.prop_id " +
                "JOIN " + lfsql + ".dbo.propval as propval2 ON doc.tocid = propval2.tocid " +
                "JOIN " + lfsql + ".dbo.propdef as propdef2 ON propval2.prop_id = propdef2.prop_id " +
                "JOIN " + lfsql + ".dbo.propval as propval3 ON doc.tocid = propval3.tocid " +
                "JOIN " + lfsql + ".dbo.propdef as propdef3 ON propval3.prop_id = propdef3.prop_id " +
                "WHERE doc.pagenum = 0 " +
                " AND propval1.num_val = @CUSTNUM " +
                " AND propdef1.prop_name = @CUSTNUMCOL " +
                " AND propdef3.prop_name = @STORENUMCOL AND propval3.num_val <> "+StoreNumber+" ";
                Console.Out.WriteLine("Wholesale Statement");
            }
            else
            {
                query = "SELECT doc.tocid as id, propval1.num_val as custnum, propval2.num_val as invoicenum " +
                "FROM " + lfsql + ".dbo.doc as doc " +
                "JOIN " + lfsql + ".dbo.propval as propval1 ON doc.tocid = propval1.tocid " +
                "JOIN " + lfsql + ".dbo.propdef as propdef1 ON propval1.prop_id = propdef1.prop_id " +
                "JOIN " + lfsql + ".dbo.propval as propval2 ON doc.tocid = propval2.tocid " +
                "JOIN " + lfsql + ".dbo.propdef as propdef2 ON propval2.prop_id = propdef2.prop_id " +
                "WHERE doc.pagenum = 0 " +
                " AND propval1.num_val = @CUSTNUM " +
                " AND propdef1.prop_name = @CUSTNUMCOL ";
                Console.Out.WriteLine("Retail Statement");
            }

            query += "AND propdef2.prop_name = @INVOICENUMCOL AND ( propval2.num_val = ";

            if (InvoiceNumbers.Count == 1)
            {
                int n = Convert.ToInt32(InvoiceNumbers[0]);
                query += n + ")";
            }
            else
            {
                for (int i = 0; i < InvoiceNumbers.Count; i++)
                {
                    int n = Convert.ToInt32(InvoiceNumbers[i]);
                    string s = n.ToString();
                    
                    if (s.Length > 5)
                    {
                        query += n + " OR propval2.num_val = " + s.Substring(0, s.Length - 1);
                    }
                    else
                    {                    
                        query += n + " OR propval2.num_val = " + s;
                    }
                    if (i == InvoiceNumbers.Count - 1)
                    {
                        query += ")";
                    }
                    else
                    {
                        query += " OR propval2.num_val = ";
                    }
                }
            }
            SqlCommand cmd = new SqlCommand(query, sql);
            cmd.Parameters.AddWithValue("@CUSTNUM", CustomerNumber);
            cmd.Parameters.AddWithValue("@CUSTNUMCOL", cust_num_col);
            cmd.Parameters.AddWithValue("@INVOICENUMCOL", inv_num_col);
            cmd.Parameters.AddWithValue("@STORENUMCOL", store_num_col);

            SqlDataReader reader = null;
            try
            {
                reader = cmd.ExecuteReader();
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
                Console.Out.WriteLine(query);
                throw new Exception("Wholesale Error");
            }
            DataTable data = new DataTable();
            data.Load(reader);
            sql.Close();

            Console.Out.WriteLine("Query returned " + data.Rows.Count + " Rows");

            Hashtable invoices = new Hashtable();
            Hashtable excuses = new Hashtable();

            foreach (object o in InvoiceNumbers)
            {
                if (o == null) continue;
                string inv = o.ToString();
                bool f = false;
                foreach (DataRow r in data.Rows)
                {
                    int r0 = Convert.ToInt32(r[0]);
                    
                    string r2 = Convert.ToInt32(r[2]).ToString();
                    if (r2.Equals(inv)) //This row is a matching invoice
                    {
                        //Reporting.AppendLog("FOUND", r2);
                        try
                        {
                            LFDocument doc = lfdb.GetEntryByID(r0) as LFDocument;
                            if (!doc.FullPath.Contains(@"AR\Customers"))
                            {
                                doc.Dispose();
                                excuses[CustomerNumber] = doc.FullPath + @" does not contain AR\Customers";
                                continue;
                            }
                            doc.Dispose();
                        }
                        catch (Exception ex)
                        {
                            Reporting.AppendLog("LASERFICHE", ex.Message);
                            continue;
                        }
                        Console.Out.WriteLine("Found Invoice");
                        try
                        {
                            if ((invoices.ContainsKey(inv) && Convert.ToInt32(invoices[inv]) < r0) || !invoices.ContainsKey(inv))
                            {
                                Console.Out.WriteLine("   Adding Invoice for Merge");
                                invoices[inv] = r0;
                            }
                            else
                            {
                                Console.Out.WriteLine("  Not Adding Invoice for Merge");
                            }
                        }
                        catch
                        {
                            throw new Exception("Invalid Cast in ContainsKey");
                        }
                        //found.Add(Convert.ToInt32(r[0]));
                        //found.Add(r0);
                        f = true;
                    }
                }
                if (!f)
                {
                    notFound.Add(inv);
                    if (!excuses.ContainsKey(CustomerNumber))
                    {
                        excuses[CustomerNumber] = "Invoice(s) not found in Laserfiche";
                    }
                }
            }
            //found.Clear();
            ArrayList values = new ArrayList(invoices.Values);
            foreach (object v in values)
            {
                if (v == null) continue;
                found.Add(Convert.ToInt32(v));
            }
            result.Add(found);
            result.Add(notFound);
            result.Add(excuses);
            return result;
        }
    }

    public class Statement
    {
        public int EntryID { get; set; }
        public ArrayList invoices { get; set; }
    }
}

Commits for ChrisCompleteCodeTrunk/BTStatementMerge/BT Statement Merge/Laserfiche.cs

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