You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrackingDataImport.vbs 8.3KB

7 months ago
7 months ago
7 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. Option Explicit
  2. Dim dev:dev = True
  3. Dim outFile
  4. Dim WaitTime:WaitTime = 15000
  5. Dim DataDirectory
  6. Dim WshShell:Set WshShell = WScript.CreateObject("Wscript.Shell")
  7. Dim oConn:Set oConn = WScript.CreateObject("ADODB.Connection")
  8. Dim ConnectionString
  9. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  10. Dim glob:set glob = CreateObject("Chilkat_9_5_0.Global")
  11. Dim success:success = glob.UnlockBundle("KENTCM.CB1022025_RGzBPM5J655e")
  12. If (success <> 1) Then
  13. WriteLine(glob.LastErrorText)
  14. WScript.Quit
  15. End If
  16. Dim objCSV:Set objCsv = CreateObject("Chilkat_9_5_0.Csv")
  17. Dim WorkingDirectory:WorkingDirectory = Replace(WScript.ScriptFullName,WScript.ScriptName,"")
  18. If dev Then
  19. DataDirectory = WorkingDirectory & "test"
  20. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=F:\Development\Tracking_Kits\Data\webdata - Copy.mdb;"
  21. set outFile = objFSO.CreateTextFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\temp.csv",True)
  22. Else
  23. DataDirectory = "\\kci-syn-cl01\PC Transfer\TrackingDataImport"
  24. ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Engine Type=5;Data Source=C:\inetpub\Data\webdata - Copy.mdb;"
  25. set outFile = objFSO.CreateTextFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\temp.csv",True)
  26. End If
  27. CheckForFiles
  28. Dim KitID:KitID = CheckForJobsToCass()
  29. If KitID > 0 Then
  30. ExportMMCsv(KitID)
  31. RunMailManager
  32. ImportCass
  33. End If
  34. Function CheckForFiles()
  35. If objFSO.FolderExists(DataDirectory) Then
  36. Dim objFolder:Set objFolder = objFSO.GetFolder(DataDirectory)
  37. If objFolder.Files.Count > 0 Then
  38. 'WScript.Echo "Files found in directory: " & DataDirectory
  39. Dim objFile
  40. For Each objFile In objFolder.Files
  41. Dim CsvString:CsvString = ConvertCsvToString(objFile.Path)
  42. If ValidImportCSV(CsvString) Then
  43. SetupKit CsvString,objFile.Name
  44. objFSO.MoveFile objFile.Path, DataDirectory & "\import\" & objFile.Name
  45. End If
  46. Next
  47. Else
  48. 'WScript.Echo "No files found in directory: " & DataDirectory
  49. End If
  50. End If
  51. End Function
  52. Function ValidJcode(jcode)
  53. Dim oConn
  54. Set oConn = WScript.CreateObject("ADODB.Connection")
  55. oConn.ConnectionString = ConnectionString
  56. oConn.Open
  57. Dim oRs
  58. set oRs = oConn.Execute("Select * from Jurisdiction Where [JCode] = '" & jcode & "';")
  59. If oRs.EOF Then
  60. ValidJcode = 0
  61. Else
  62. ValidJcode = 1
  63. End If
  64. oRs.Close
  65. oConn.Close
  66. End Function
  67. Function SetupKit(CsvString,FileName)
  68. Dim JobNumber:JobNumber = Mid(FileName,9,6)
  69. Dim JCode:JCode = Left(Filename,5)
  70. If IsNumeric(JobNumber) Then
  71. If ValidJcode(JCode) Then
  72. WScript.Echo FileName & " Is a Valid CSV for Importing"
  73. Dim oConn:Set oConn = WScript.CreateObject("ADODB.Connection")
  74. oConn.ConnectionString = ConnectionString
  75. oConn.Open
  76. oConn.Execute("Insert Into Kit ([JobNumber], [Jcode], [CreatedOn], [JobType],[Filename],[Status]) VALUES ('" & JobNumber & "','" & JCode & "',#" & Now() & "#,'Purple Envelopes','" & FileName & "','Importing');")
  77. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit ORDER BY ID DESC")
  78. Dim kitId : kitId = rs("ID").value
  79. rs.close
  80. Dim i
  81. For i = 1 To objCSV.NumRows -1
  82. oConn.Execute("Insert Into [InkjetRecords] (KitID,VOTERID,LASTNAME,FIRSTNAME,MIDDLENAME" & _
  83. ",SUFFIX,PRECINCT,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,APPSENT,APPRETURNED,BALSENT,BALRETURNED" & _
  84. ",CountingBoard,UOCAVASTATUS,EMAILADDRESS,PHONENUMBER,BALLOT_NUMBER) VALUES (" & kitId & _
  85. ",'" & Replace(objCSV.GetCell(i,0),"'","''") & _
  86. "','" & objCsv.GetCell(i,1) & _
  87. "','" & Replace(objCSV.GetCell(i,2),"'","''") & _
  88. "','" & Replace(objCSV.GetCell(i,3),"'","''") & _
  89. "','" & Replace(objCSV.GetCell(i,4),"'","''") & _
  90. "','" & Replace(objCSV.GetCell(i,5),"'","''") & _
  91. "','" & Replace(objCSV.GetCell(i,6),"'","''") & _
  92. "','" & Replace(objCSV.GetCell(i,7),"'","''") & _
  93. "','" & Replace(objCSV.GetCell(i,8),"'","''") & _
  94. "','" & Replace(objCSV.GetCell(i,9),"'","''") & _
  95. "','" & Replace(objCSV.GetCell(i,10),"'","''") & _
  96. "','" & Replace(objCSV.GetCell(i,11),"'","''") & _
  97. "','" & Replace(objCSV.GetCell(i,12),"'","''") & _
  98. "','" & Replace(objCsv.GetCell(i,13),"'","''") & _
  99. "','" & objCsv.GetCell(i,14) & _
  100. "','" & objCsv.GetCell(i,15) & _
  101. "','" & objCsv.GetCell(i,16) & _
  102. "','" & objCsv.GetCell(i,17) & _
  103. "','" & objCsv.GetCell(i,18) & _
  104. "','" & objCsv.GetCell(i,19) & _
  105. "')")
  106. Next
  107. oConn.Execute("Update Kit SET [Status] = 'Ready to Cass' Where ID = " & kitId &";")
  108. 'oConn.Close
  109. End If
  110. End If
  111. End Function
  112. Function ConvertCsvToString(FilePath)
  113. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  114. Dim objCsvFile:set objCsvFile = objFSO.OpenTextFile(FilePath)
  115. Dim strContent:strContent = ""
  116. Dim intLineCount:intLineCount = 0
  117. Do Until objCsvFile.AtEndOfStream Or intLineCount >= 3
  118. objCsvFile.SkipLine
  119. intLineCount = intLineCount + 1
  120. Loop
  121. ' Read the remaining content into a string
  122. Do Until objCsvFile.AtEndOfStream
  123. Dim strLine:strLine = objCsvFile.ReadLine
  124. strContent = strContent & strLine & vbCrLf
  125. Loop
  126. ConvertCsvToString = strContent
  127. End Function
  128. Function ValidImportCSV(CsvFileAsString)
  129. objCSV.LoadFromString(CsvFileAsString)
  130. If objCSV.NumColumns = 20 Then
  131. debug.WriteLine objCsv.GetCell(1,1)
  132. ValidImportCSV = True
  133. Else
  134. ValidImportCSV = False
  135. End If
  136. End Function
  137. Sub ImportCass
  138. Dim currentRow
  139. objCsv.LoadFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\FROM_MM.CSV")
  140. For currentRow = 0 To objCsv.NumRows -1
  141. oConn.Execute("UPDATE InkJetRecords SET CassADDRESS1 = '" & Replace(objCsv.GetCell(currentRow,1),"'","''") & "', " &_
  142. "CassADDRESS2 = '" & Replace(objCSV.GetCell(currentRow,3),"'","''") & "', " &_
  143. "CassADDRESS3 = '" & Replace(objCSV.GetCell(currentRow,4),"'","''") & "', " &_
  144. "CassADDRESS4 = '" & Replace(objCSV.GetCell(currentRow,5),"'","''") & "', " &_
  145. "CassADDRESS5 = '" & Replace(objCsv.GetCell(currentRow,6) & ", " & objCsv.GetCell(currentRow,7) & " " & objCsv.GetCell(currentRow,8),"'","''") & "'" &_
  146. " WHERE ID = " & objCSV.GetCell(currentRow,0) & ";")
  147. Next
  148. oConn.Execute("UPDATE Kit SET Status ='Ready To Create Labels' WHERE ID =" & KitID & ";")
  149. End Sub
  150. Sub RunMailManager
  151. 'WshShell.Run "\\MM2012\APPS\BCC\MM2010\mailman.exe -p -j MMJOB.mjb -u DAN",1,True
  152. WshShell.Run "PsExec64.exe -i -e -u ntp\danielc -p SunBrightShine! \\MarkH2 ""\\MM2012\APPS\BCC\MM2010\mailman.exe"" -p -j MMJOB.mjb -u DAN",1,True
  153. End Sub
  154. Sub ExportMMCsv(KitId)
  155. Dim success
  156. objCsv.HasColumnNames = 1
  157. objCSV.EnableQuotes = 1
  158. success = objCsv.SetColumnName(0,"ID")
  159. success = objCsv.SetColumnName(1,"NAME")
  160. success = objCsv.SetColumnName(2,"ADDRESS1")
  161. success = objCsv.SetColumnName(3,"ADDRESS2")
  162. success = objCsv.SetColumnName(4,"ADDRESS3")
  163. success = objCsv.SetColumnName(5,"ADDRESS4")
  164. success = objCsv.SetColumnName(6,"ADDRESS5")
  165. Dim rs : Set rs = oConn.Execute("Select ID," & _
  166. "IIF(FIRSTNAME IS NULL,'',FIRSTNAME & ' ') & " & _
  167. "IIF(MIDDLENAME IS NULL,'',MIDDLENAME & ' ') & " & _
  168. "IIF(LASTNAME IS NULL,'',LASTNAME & ' ') & " & _
  169. "IIF(SUFFIX IS NULL,'',SUFFIX & ' ') " & _
  170. "AS NAME, ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5 FROM InkjetRecords WHERE KitID =" & KitID & " ORDER By ID;")
  171. Dim CurrentRow:CurrentRow = 0
  172. While Not rs.EOF
  173. objCSV.SetCell CurrentRow,0,rs("ID").value
  174. objCSV.SetCell CurrentRow,1,rs("NAME").value
  175. objCSV.SetCell CurrentRow,2,rs("ADDRESS1").value
  176. objCSV.SetCell CurrentRow,3,rs("ADDRESS2").value
  177. objCSV.SetCell CurrentRow,4,rs("ADDRESS3").value
  178. objCSV.SetCell CurrentRow,5,rs("ADDRESS4").value
  179. objCSV.SetCell CurrentRow,6,rs("ADDRESS5").value
  180. rs.MoveNext
  181. CurrentRow = CurrentRow +1
  182. Wend
  183. Dim CsvString:CsvString = objCSV.SaveToString()
  184. outFile.WriteLine CsvString
  185. outFile.Close
  186. End Sub
  187. Function CheckForJobsToCass()
  188. oConn.ConnectionString = ConnectionString
  189. oConn.Open
  190. 'oConn.Open
  191. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit Where Status ='Ready to Cass' ORDER BY ID DESC;")
  192. If Not rs.EOF Then
  193. Dim kitId : kitId = rs("ID").value
  194. rs.Close
  195. CheckForJobsToCass = KitID
  196. oConn.Execute("UPDATE Kit SET Status ='Cassing' WHERE ID =" & KitID & ";")
  197. Else
  198. CheckForJobsToCass = 0
  199. End If
  200. 'oConn.Close
  201. End Function

Powered by TurnKey Linux.