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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DocuWare.Platform.ServerClient;
using DocuWare.WebIntegration;
using System.Configuration;
using M3Workflow.Data;
using M3Workflow.Data.Base;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;

namespace M3Workflow.Web
{
    /// <summary>
    /// Summary description for PostInvoice
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PostInvoice : System.Web.Services.WebService
    {

        [WebMethod]
        public string Complete(string cabinetId, int documentId)
        {
            Boolean autolog = (ConfigurationManager.AppSettings["M3_INVOICE_AUTOLOG"].ToString() == "1") ? true : false;

            string logDir = HttpContext.Current.Server.MapPath("~/");
            string logFile = Path.Combine(logDir, Guid.NewGuid().ToString() + ".txt");

            Guid cabinet;
            if (Guid.TryParse(cabinetId, out cabinet))
            {
                try
                {
                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Trying to connect to Docuware...\n");
                    }
                    var connection = ServiceConnection.Create(
                        new Uri(
                            String.Format(
                                "{0}://{1}/docuware/platform",
                                new object[] {
                            ConfigurationManager.AppSettings["DOCUWARE_SERVER_PROTOCOL"].ToString(),
                            ConfigurationManager.AppSettings["DOCUWARE_SERVER"].ToString()
                                }
                            )
                        ),
                        ConfigurationManager.AppSettings["DOCUWARE_ADMIN_USER"].ToString(),
                        ConfigurationManager.AppSettings["DOCUWARE_ADMIN_PASS"].ToString()

                    );

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Connected to Docuware...\n");
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile,
                            String.Format(
                                "Trying to get document from Docuware with documentId = {0} & cabinetId = {1}...\n",
                                new object[]
                                {
                                    documentId,
                                    cabinetId
                                }
                            )
                        );
                    }

                    var document = connection.GetFromDocumentForDocumentAsync(documentId, cabinetId).Result;

                    if (autolog)
                    {
                        if (document.IsSuccessStatusCode)
                        {
                            File.AppendAllText(logFile, "Got document from Docuware...\n");
                        }
                    }


                    if (autolog)
                    {
                        if (document.IsSuccessStatusCode)
                        {
                            File.AppendAllText(logFile, "Checking to see if document's field count is greater than zero...\n");
                        }
                    }

                    DocumentIndexFields fields = document.Content.GetDocumentIndexFieldsFromFieldsRelation();

                    if (autolog)
                    {
                        if (document.IsSuccessStatusCode)
                        {
                            File.AppendAllText(logFile, "Field count is " + fields.Field.Count.ToString() + "...\n");
                        }
                    }

                    if (autolog)
                    {
                            File.AppendAllText(logFile, "Trying to create json document connect to service with unencrypted credentials...\n");
                    }

                    AuthDataObject auth = JsonDataObject.FromJsonString<AuthDataObject>(
                        Encrypt.DecryptString(
                            fields["CREDS"].Item.ToString(),
                            ConfigurationManager.AppSettings["BBDS_ENCRYPT_PASSWORD"].ToString()
                        )
                    );

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Created auth object with unencrypted values (not yet attempted to connect)...\n");
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Building list of accounts...\n");
                    }

                    List<AccountPostDataObject> accounts = new List<AccountPostDataObject>();


                    if(autolog)
                    {
                        for(int i = 0; i != fields.Field.Count; i++)
                        {
                            Console.WriteLine();
                            string fieldName = fields.Field[0].FieldName;
                            string fieldValue = fields.Field[0].Item.ToString();
                        }
                    }


                    for (int i = 0; i <= 11; i++)
                    {
                        string fieldNameAccount = "ACCOUNT_" + (i + 1).ToString("000");
                        string fieldNameAmount = "AMOUNT_" + (i + 1).ToString("000");
                        if (fields[fieldNameAccount].Item.ToString() != String.Empty && fields[fieldNameAmount].Item.ToString() != "0.00")
                        {
                            string[] accountNum = fields[fieldNameAccount].Item.ToString().Split(' ');
                            accounts.Add(
                                new AccountPostDataObject(
                                    //fields[fieldNameAccount].Item.ToString(),
                                    accountNum[0],
                                    Decimal.Parse(fields[fieldNameAmount].Item.ToString()),
                                    false,
                                    i
                                )
                            );
                        }
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Found " + accounts.Count + " accounts...\n");
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Trying to connect to M3 service...\n");
                    }

                    UserDataObject udo = M3DataProvider.Authenticate(auth.UserName, auth.Password);

                    if (autolog) {
                        if(udo.Roles.Count > 0)
                        {
                            File.AppendAllText(logFile, "Auth successful got a token : " + udo.Token.ToString() + "...\n");
                        }
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Getting vendor and creating invoiceDataObject...\n");
                    }

                    var vendor = (
                        from v in M3DataProvider.GetVendors(
                            udo.Token,
                            Guid.Parse(fields["CUSTOMER_ID"].Item.ToString()),
                            Guid.Parse(fields["COMPANYID"].Item.ToString())
                            ).Vendors
                        where v.VendorName == fields["VENDORNAME"].Item.ToString()
                        select v
                    ).SingleOrDefault();

                    InvoiceDataObject invoiceDataObject = new InvoiceDataObject(
                        vendor.Id,
                        Guid.Parse(fields["COMPANYID"].Item.ToString()),
                        fields["INVOICENUMBER"].Item.ToString(),
                        Decimal.Parse(fields["INVOICE_AMOUNT"].Item.ToString()),
                        false,
                        false,
                        true,
                        accounts,
                        null,
                        Guid.Parse(fields["PROPERTYID"].Item.ToString()),
                        DateTime.Parse(fields["INVOICEDATE"].Item.ToString()),
                        DateTime.Parse(fields["DUE_DATE"].Item.ToString()),
                        (fields["PO_NUMBER"].Item.ToString() == String.Empty) ? String.Empty : fields["PO_NUMBER"].Item.ToString(),
                        fields["DESCRIPTION"].Item.ToString(),
                        DateTime.Now,
                        DateTime.Parse(fields["ACCOUNTING_PERIOD_DATE"].Item.ToString())
                    );

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Getting vendor and creating invoiceDataObject completed...\n");
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Attempting to post invoice...\n");
                    }

                    InvoicePostDataObject pdo = new InvoicePostDataObject(invoiceDataObject);

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Invoice posted...\n");
                    }

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Trying to parse result..\n");
                    }

                    InvoicePostResultDataObject iprdo = M3DataProvider.GetInvoicePostResult(
                        udo.Token,
                        Guid.Parse(fields["CUSTOMER_ID"].Item.ToString()),
                        pdo
                    );

                    if (autolog)
                    {
                        File.AppendAllText(logFile, "Got invoice number : " + iprdo.Invoice.InvoiceNumber.ToString() + "...\n");
                    }



                    if (autolog)
                    {

                        var json = Newtonsoft.Json.JsonConvert.SerializeObject(pdo).ToString();


                        SqlConnection cn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["IntelligentIndexingWorkflow"].ToString());
                        cn2.Open();
                        SqlCommand cmd = new SqlCommand("INSERT INTO [InvoiceTransactions] (Originator, Json) VALUES (@Originator, @Json)", cn2);
                        cmd.Parameters.AddWithValue("@Originator", "Service");
                        cmd.Parameters.AddWithValue("@Json", json);
                        cmd.ExecuteNonQuery();
                        cn2.Close();
                    }


                    if (iprdo.Invoice.Success)
                    {
                        return iprdo.Invoice.InvoiceId.ToString();
                    }
                    else
                    {
                        if (iprdo.Invoice.Reason != null)
                        {
                            return iprdo.Invoice.Reason[0].Description;
                        }
                        else
                        {
                            return "SERVICE EXCEPTION";
                        }
                    }
                }
                catch
                {
                    return "SERVICE EXCEPTION";
                }
            }
            else
            {
                return "SERVICE EXCEPTION";
            }
        }
    }
}

Commits for ChrisCompleteCodeTrunk/M3Workflow/M3Workflow.Web/PostInvoice.asmx.cs

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