-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule1.vb
More file actions
342 lines (342 loc) · 15.6 KB
/
Module1.vb
File metadata and controls
342 lines (342 loc) · 15.6 KB
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
Imports System.IO
Imports System.Data.SqlClient
Imports System.Net.Mail
Imports System.Text.RegularExpressions
Module Module1
Public ITEMDBSTR As String = ""
Public ITEMMASTDB As String = ""
'Public ITEMDBSTR As String = "Data Source=ECOMDB3;Initial Catalog=RECBUYS;UID=sss;PWD=sssss"
'Public ITEMMASTDB As String = "Data Source=ecom-db1;Initial Catalog=ECOMLIVE;UID=sss;PWD=sssss"
Dim RunProduction As Boolean = False
Sub Main()
Dim body As String = "Job Started: " & Now.ToString & Environment.NewLine & Environment.NewLine
Dim ReportFiles() As String = {"RECMDLTW", "IAA1W", "IAC3W"}
Dim FilesProcessed As New List(Of String)
Dim FilesWithError As New List(Of String)
For Each StreamFile As String In ReportFiles
Dim args As String = ""
If RunProduction Then
args = "E:\ECOMETRY\LIVECODE\UserStr2\" & StreamFile & " JOBS 31000 E:\ECOMETRY\LISTENER\ECOMLIVE.ini"
Else
args = "E:\ECOMETRY\VERCODE\UserStr2\" & StreamFile & " JOBS 31040 E:\ECOMETRY\LISTENER\ECOMVER.ini"
ITEMDBSTR = "Data Source=ECOMDB3;Initial Catalog=RECBYSDEV;UID=sss;PWD=sssss"
ITEMMASTDB = "Data Source=ecom-db2;Initial Catalog=ECOMVER;UID=sss;PWD=sssss"
End If
Dim p As New Process
With p.StartInfo
.RedirectStandardOutput = True
.FileName = "E:\ECOMETRY\VERCODE\PRO\cronsched.exe"
.CreateNoWindow = False
.UseShellExecute = False
.Arguments = args
End With
p.Start()
p.WaitForExit()
Dim ReportFile As String = ""
Do While p.StandardOutput.Peek >= 0
Dim s As String = p.StandardOutput.ReadLine
If s.Contains("StdList") Then
Dim path As String = s.Substring(s.IndexOf("\\"), s.LastIndexOf("\") - s.IndexOf("\\") + 1)
Dim jobNo As String = s.Substring(s.LastIndexOf("."))
Dim SpoolDir As New DirectoryInfo(path)
For Each f As FileInfo In SpoolDir.GetFiles(StreamFile & "_*" & jobNo)
ReportFile = f.FullName
Select Case StreamFile
Case "IAA1W"
body &= "A1 Items" & Environment.NewLine
Case "IAC3W"
body &= "C3 Items" & Environment.NewLine
Case "RECMDLTW"
body &= "R1 Items" & Environment.NewLine
End Select
body &= f.FullName.Substring(2) & Environment.NewLine
Next
End If
Loop
If ReportFile.Length > 10 Then
body &= ProcessOneFile(ReportFile)
FilesProcessed.Add(ReportFile)
Else
FilesWithError.Add(StreamFile)
End If
Next
Dim anEmail As New MailMessage
With anEmail
.CC.Add("[email protected]")
If RunProduction Then
'.To.Add("[email protected]")
'.To.Add("[email protected]")
End If
.From = New MailAddress(My.Computer.Name & "@ecommerce.com")
Dim subject As String = "Import of R1, A1 and C3 items into Recommended Buy PO System executed"
For Each s As String In FilesProcessed
.Attachments.Add(New Attachment(s))
Next
If FilesWithError.Count > 0 Then
.Priority = MailPriority.High
For Each s As String In FilesWithError
Select Case s
Case "IAA1W"
body &= "Error processing the A1 items stream file IAA1W" & Environment.NewLine
Case "IAC3W"
body &= "Error processing the C3 items stream file IAC3W" & Environment.NewLine
Case "RECMDLTW"
body &= "Error processing the R1 items stream file RECMDLTW" & Environment.NewLine
End Select
Next
subject &= " with Error!"
End If
.Body = body & Environment.NewLine & "Job Ended: " & Now.ToString
.Subject = subject
End With
Dim aSMTPClient As New SmtpClient("172.16.92.10")
Try
aSMTPClient.Send(anEmail)
Catch ex As Exception
Console.WriteLine(ex.Message)
LogThis("EMAILERROR", ex.Message)
End Try
Console.WriteLine(vbCrLf & "done")
End Sub
Function ProcessOneFile(ByVal FilePath As String) As String
Dim tmpResult As String = ""
Dim CurrentVendor As String = ""
Dim rgx As New Regex("/[0-9\s][0-9\s]/", RegexOptions.IgnoreCase)
Using sr As New StreamReader(FilePath)
Dim counter As ULong = 0
Do While sr.Peek >= 0
Dim tmpline As String = sr.ReadLine
If tmpline.Contains("Selection: Vendor") Then
CurrentVendor = tmpline.Substring(19, 10).Trim
End If
Dim matches As MatchCollection = rgx.Matches(tmpline)
If matches.Count = 1 Then
If New FileInfo(FilePath).Name.ToUpper.StartsWith("RECM") Then
If tmpline.Trim.Length = 131 Then
Dim itemdesc As String = sr.ReadLine
Dim vendorline As String = sr.ReadLine
Dim vendordesc As String = sr.ReadLine
If CurrentVendor <> "PRID" Then
Dim tmpItem As New bikeItem(tmpline, itemdesc, vendorline, vendordesc, CurrentVendor, True)
tmpResult &= tmpItem.oItemNo & Environment.NewLine
Console.WriteLine(tmpItem.oEDPNO & vbTab & tmpItem.oItemNo & vbTab)
counter += 1
End If
End If
Else
If tmpline.Trim.Length = 132 Then
Dim itemdesc As String = sr.ReadLine
Dim vendorline As String = sr.ReadLine
Dim vendordesc As String = sr.ReadLine
If CurrentVendor <> "PRID" Then
Dim tmpItem As New bikeItem(tmpline, itemdesc, vendorline, vendordesc, CurrentVendor, False)
Console.WriteLine(tmpItem.oEDPNO & vbTab & tmpItem.oItemNo & vbTab)
tmpResult &= tmpItem.oItemNo & Environment.NewLine
counter += 1
End If
End If
End If
End If
Loop
tmpResult &= "Added " & counter & " items" & Environment.NewLine & Environment.NewLine
Console.WriteLine(vbCrLf & counter)
End Using
Return tmpResult
End Function
Public Sub LogThis(ByVal ShortText As String, ByVal LongText As String)
Dim entryDateTime As DateTime = Now
Dim logfile As String = My.Application.Info.DirectoryPath & "\" & My.Application.Info.ProductName & ".log"
My.Computer.FileSystem.WriteAllText(logfile, "[" & entryDateTime.ToString("yyyy-MM-dd HH:mm:ss") & "]" & " " & String.Format("{0,-15}{1}" & Environment.NewLine, ShortText, LongText), True)
End Sub
End Module
Class bikeItem
#Region "Class Private Variables"
'Private ITEMDBSTR As String = "Data Source=ECOMDB3;Initial Catalog=RECBYSDEV;UID=sss;PWD=sssss"
Public oItemNo As String = ""
Public oEDPNO As String = ""
Public oItemDesc As String = ""
Public oVendor As String = ""
Public oVendorNo As String = ""
Public oVendorDesc As String = ""
Public oStatus As String = ""
Public oPrice As String = ""
Public oMarginPercent As String = ""
Public oAvg52Wk As String = ""
Public oAvg26Wk As String = ""
Public oAvg13Wk As String = ""
Public oAvg8Wk As String = ""
Public oAvg4Wk As String = ""
Public oLastWk As String = ""
Public oPONum1 As String = ""
Public oPOExpDate1 As String = ""
Public oPOQty1 As String = ""
Public oPONum2 As String = ""
Public oPOExpDate2 As String = ""
Public oPOQty2 As String = ""
Public oPONum3 As String = ""
Public oPOExpDate3 As String = ""
Public oPOQty3 As String = ""
Public oPONum4 As String = ""
Public oPOExpDate4 As String = ""
Public oPOQty4 As String = ""
Public oTotalDue As String = ""
Public oOnHand As String = ""
Public oWeeksOfStock As String = ""
Public oReorderLevel As String = ""
Public oVendorPrice As String = ""
Public oLandedCost As String = ""
Public oMinQty As String = ""
Public oBOQty As String = ""
Public oRecmdBuy As String = ""
#End Region
Sub New(ByVal ItemLine As String, ByVal ItemDescriptionLine As String, ByVal VendorLine As String, ByVal VendorDescriptionLine As String, ByVal CurrentVendor As String, ByVal IsR1 As Boolean)
If IsR1 Then
oItemDesc = ItemDescriptionLine.Substring(3, 50).Trim
oAvg52Wk = ItemLine.Substring(15, 6).Trim
oAvg26Wk = ItemLine.Substring(21, 6).Trim
oAvg13Wk = ItemLine.Substring(28, 6).Trim
oAvg8Wk = ItemLine.Substring(35, 6).Trim
oAvg4Wk = ItemLine.Substring(42, 6).Trim
oLastWk = ItemLine.Substring(49, 4).Trim
oPONum1 = ItemLine.Substring(54, 9).Trim
oPOExpDate1 = ItemLine.Substring(64, 8).Trim
oPOQty1 = ItemLine.Substring(73, 5).Trim
oPONum2 = ItemDescriptionLine.Substring(54, 9).Trim
oPOExpDate2 = ItemDescriptionLine.Substring(64, 8).Trim
oPOQty2 = ItemDescriptionLine.Substring(73, 5).Trim
oPONum3 = VendorLine.Substring(54, 9).Trim
oPOExpDate3 = VendorLine.Substring(64, 8).Trim
oPOQty3 = VendorLine.Substring(73, 5).Trim
oPONum4 = VendorDescriptionLine.Substring(54, 9).Trim
oPOExpDate4 = VendorDescriptionLine.Substring(64, 8).Trim
oPOQty4 = VendorDescriptionLine.Substring(73, 5).Trim
oTotalDue = ItemLine.Substring(79, 5).Trim
oOnHand = ItemLine.Substring(85, 6).Trim
oWeeksOfStock = ItemLine.Substring(92, 6).Trim
oReorderLevel = ItemLine.Substring(99, 6).Trim
oVendorPrice = ItemLine.Substring(106, 7).Trim
oMinQty = ItemLine.Substring(114, 4).Trim
oBOQty = ItemLine.Substring(119, 5).Trim
oRecmdBuy = ItemLine.Substring(127, 5).Trim
oVendor = CurrentVendor
oVendorNo = VendorLine.Substring(1, 23).Trim
oStatus = VendorLine.Substring(25, 2)
oPrice = VendorLine.Substring(30, 8).Trim
oMarginPercent = VendorLine.Substring(40, 6).Trim
oVendorDesc = VendorDescriptionLine.Substring(3, 49).Trim
SetItemNoAndEDPNO(ItemLine.Substring(0, 14).Trim)
Else
oItemDesc = ItemDescriptionLine.Substring(3, 50).Trim
oAvg52Wk = ItemLine.Substring(17, 6).Trim
oAvg26Wk = ItemLine.Substring(23, 6).Trim
oAvg13Wk = ItemLine.Substring(30, 6).Trim
oAvg8Wk = ItemLine.Substring(37, 6).Trim
oAvg4Wk = ItemLine.Substring(44, 6).Trim
oLastWk = ItemLine.Substring(51, 4).Trim
oPONum1 = ItemLine.Substring(56, 9).Trim
oPOExpDate1 = ItemLine.Substring(66, 8).Trim
oPOQty1 = ItemLine.Substring(75, 5).Trim
oPONum2 = ItemDescriptionLine.Substring(56, 9).Trim
oPOExpDate2 = ItemDescriptionLine.Substring(66, 8).Trim
oPOQty2 = ItemDescriptionLine.Substring(75, 5).Trim
oPONum3 = VendorLine.Substring(56, 9).Trim
oPOExpDate3 = VendorLine.Substring(66, 8).Trim
oPOQty3 = VendorLine.Substring(75, 5).Trim
oPONum4 = VendorDescriptionLine.Substring(56, 9).Trim
oPOExpDate4 = VendorDescriptionLine.Substring(66, 8).Trim
oPOQty4 = VendorDescriptionLine.Substring(75, 5).Trim
oTotalDue = ItemLine.Substring(82, 5).Trim
oOnHand = ItemLine.Substring(87, 6).Trim
oWeeksOfStock = ItemLine.Substring(94, 6).Trim
oReorderLevel = ItemLine.Substring(101, 6).Trim
oVendorPrice = ItemLine.Substring(109, 9).Trim
oLandedCost = ItemLine.Substring(120, 9).Trim
oMinQty = ItemLine.Substring(129, 4).Trim
oBOQty = ItemDescriptionLine.Substring(129, 4).Trim
oVendor = CurrentVendor
oVendorNo = VendorLine.Substring(1, 23).Trim
oStatus = VendorLine.Substring(25, 2)
oPrice = VendorLine.Substring(30, 8).Trim
oMarginPercent = VendorLine.Substring(40, 6).Trim
oVendorDesc = VendorDescriptionLine.Substring(3, 49).Trim
SetItemNoAndEDPNO(ItemLine.Substring(0, 16).Trim)
End If
EnterIntoDB()
End Sub
Private Sub SetItemNoAndEDPNO(ByVal tmpItemNumber As String)
Dim queryString As String = "SELECT EDPNO,ITEMNO FROM ITEMMAST WHERE DESCRIPTION = '" & oItemDesc.Replace("'", "''") & "' AND ITEMNO LIKE '" & tmpItemNumber & "%'"
Using conn As New SqlConnection(ITEMMASTDB)
Dim cmd As New SqlCommand(queryString, conn)
Try
conn.Open()
Dim r As SqlDataReader = cmd.ExecuteReader
If r.HasRows Then
r.Read()
Dim tmp As String = r.Item(0)
oEDPNO = tmp.Trim
tmp = r.Item(1)
oItemNo = tmp.Trim
Else
Console.WriteLine(tmpItemNumber & " not found")
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
Private Sub EnterIntoDB()
Dim queryString As String = "INSERT INTO RECMDBUYITEMS" & _
"(RBI_NUMBER,RBI_EDPNO_NUMBER,RBI_DESCRIPTION,RBI_STATUS,RBI_PRICE,RBI_MARGIN,RBI_VENDOR,RBI_VENDOR_ITM_NUMBER,RBI_VENDOR_DESCRIPTION,RBI_VENDOR_PRICE,RBI_ORIGINAL_VENDOR_PRICE,RBI_LANDED_COST,RBI_AVG52WK" & _
",RBI_AVG26WK,RBI_AVG13WK,RBI_AVG8WK,RBI_AVG4WK,RBI_LASTWK,RBI_PO_NUMBER1,RBI_PO_EXPDATE1,RBI_PO_QTY1,RBI_PO_NUMBER2,RBI_PO_EXPDATE2,RBI_PO_QTY2,RBI_PO_NUMBER3" & _
",RBI_PO_EXPDATE3,RBI_PO_QTY3,RBI_PO_NUMBER4,RBI_PO_EXPDATE4,RBI_PO_QTY4,RBI_TOTAL_DUE,RBI_ONHAND,RBI_WEEKSOFSTOCK,RBI_REORDERLEVEL,RBI_MINQTY,RBI_BOQTY" & _
",RBI_RECMDBUY,RBI_ADDED_DATE) VALUES(" & _
"'" & oItemNo & "'," & _
"" & oEDPNO & "," & _
"'" & oItemDesc.Replace("'", "''") & "'," & _
"'" & oStatus & "'," & _
"'" & oPrice & "'," & _
"'" & oMarginPercent & "'," & _
"'" & oVendor & "'," & _
"'" & oVendorNo & "'," & _
"'" & oVendorDesc.Replace("'", "''") & "'," & _
"'" & oVendorPrice & "'," & _
"'" & oVendorPrice & "'," & _
"'" & oLandedCost & "'," & _
"'" & oAvg52Wk & "'," & _
"'" & oAvg26Wk & "'," & _
"'" & oAvg13Wk & "'," & _
"'" & oAvg8Wk & "'," & _
"'" & oAvg4Wk & "'," & _
"'" & oLastWk & "'," & _
"'" & oPONum1 & "'," & _
"'" & oPOExpDate1 & "'," & _
"'" & oPOQty1 & "'," & _
"'" & oPONum2 & "'," & _
"'" & oPOExpDate2 & "'," & _
"'" & oPOQty2 & "'," & _
"'" & oPONum3 & "'," & _
"'" & oPOExpDate3 & "'," & _
"'" & oPOQty3 & "'," & _
"'" & oPONum4 & "'," & _
"'" & oPOExpDate4 & "'," & _
"'" & oPOQty4 & "'," & _
"'" & oTotalDue & "'," & _
"'" & oOnHand & "'," & _
"'" & oWeeksOfStock & "'," & _
"'" & oReorderLevel & "'," & _
"'" & oMinQty & "'," & _
"'" & oBOQty & "'," & _
"'" & oRecmdBuy & "'," & _
"'" & Now.ToShortDateString & "')"
Using conn As New SqlConnection(ITEMDBSTR)
Dim cmd As New SqlCommand(queryString, conn)
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Console.WriteLine(queryString)
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
End Class