25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

311 satır
11KB

  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. Dim KitID
  28. ' CheckForFiles
  29. ' KitID = CheckForJobsToCass()
  30. ' If KitID > 0 Then
  31. ' ExportMMCsv(KitID)
  32. ' RunMailManager
  33. ' ImportCass
  34. ' End If
  35. oConn.Open(ConnectionString)
  36. KitID = CheckForReadyToLabel()
  37. If KitID > 0 Then
  38. createTrackingInfoForKit(KitID)
  39. End If
  40. WScript.Quit
  41. Function createTrackingInfoForKit(KitID)
  42. Dim KitRs:set KitRs = oConn.Execute("Select * from Kit Where ID =" & KitID & ";")
  43. Dim InkjetRs:Set InkjetRs = oConn.Execute("Select * from InkjetRecords Where KitID =" & KitID & ";")
  44. Dim SerialNumberStart:SerialNumberStart = GetSetting("SerialNumberStart")
  45. Dim serialOffset:serialOffset = GetSetting("SerialOffset")
  46. Dim serialStart:serialStart = CLng(GetSetting("SerialNumberStart")) + CLng(GetSetting("SerialOffset"))
  47. Dim Counter:Counter=0
  48. Dim KitLabelID
  49. Dim KitLabelRs
  50. Dim Step : If KitRs("InboundSTID") <> "" Then : Step = 2 : Else Step = 1
  51. serialStart = PadLeft(serialStart + CLng(Counter),9,"0")
  52. While Not InkjetRs.EOF
  53. If Step = 1 Then
  54. oConn.Execute("INSERT INTO KitLabels (KitID,OutboundSerial,OutboundIMBDigits) " &_
  55. "VALUES(" & KitID & ",'" & serialStart & "','" & KitRs("OutBoundSTID").Value & serialStart & "000000000" & "');")
  56. Set KitLabelRs = oConn.Execute("SELECT TOP 1 [ID] FROM KitLabels ORDER BY ID DESC")
  57. KitLabelID = KitLabelRs("ID").Value
  58. oConn.Execute("UPDATE InkjetRecords Set [KitLabelID]=" & KitLabelID & " WHERE ID=" & InkjetRs("ID") & ";")
  59. serialStart = serialStart + 1
  60. Else
  61. ' oConn.Execute("INSERT INTO KitLabels (KitID,OutboundSerial,InBoundSerial,OutbondIMBDigitds,InBoundIMBDigits) " &_
  62. ' "VALUES(" & KitID & ",' & ")
  63. End If
  64. InkjetRs.MoveNext
  65. Wend
  66. End Function
  67. Function GetSetting(settingName)
  68. Dim rs:Set rs = oConn.Execute("Select Value From [Settings] Where [Name] = '" & settingName & "';")
  69. If Not rs.EOF Then
  70. GetSetting = rs(0).value
  71. rs.Close
  72. Else
  73. SetSetting = 0
  74. rs.Close
  75. End If
  76. End Function
  77. Function CheckForReadyToLabel()
  78. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit Where Status ='Ready to Assign Labels' ORDER BY ID DESC;")
  79. If Not rs.EOF Then
  80. Dim kitId : kitId = rs("ID").value
  81. rs.Close
  82. CheckForReadyToLabel = KitID
  83. oConn.Execute("UPDATE Kit SET Status ='Applying Serial Numbers' WHERE ID =" & KitID & ";")
  84. Else
  85. CheckForReadyToLabel = 0
  86. End If
  87. End Function
  88. Function CheckForFiles()
  89. If objFSO.FolderExists(DataDirectory) Then
  90. Dim objFolder:Set objFolder = objFSO.GetFolder(DataDirectory)
  91. If objFolder.Files.Count > 0 Then
  92. 'WScript.Echo "Files found in directory: " & DataDirectory
  93. Dim objFile
  94. For Each objFile In objFolder.Files
  95. Dim CsvString:CsvString = ConvertCsvToString(objFile.Path)
  96. If ValidImportCSV(CsvString) Then
  97. SetupKit CsvString,objFile.Name
  98. objFSO.MoveFile objFile.Path, DataDirectory & "\import\" & objFile.Name
  99. End If
  100. Next
  101. Else
  102. 'WScript.Echo "No files found in directory: " & DataDirectory
  103. End If
  104. End If
  105. End Function
  106. Function ValidJcode(jcode)
  107. Dim oConn
  108. Set oConn = WScript.CreateObject("ADODB.Connection")
  109. oConn.ConnectionString = ConnectionString
  110. oConn.Open
  111. Dim oRs
  112. set oRs = oConn.Execute("Select * from Jurisdiction Where [JCode] = '" & jcode & "';")
  113. If oRs.EOF Then
  114. ValidJcode = 0
  115. Else
  116. ValidJcode = 1
  117. End If
  118. oRs.Close
  119. oConn.Close
  120. End Function
  121. Function SetupKit(CsvString,FileName)
  122. Dim JobNumber:JobNumber = Mid(FileName,9,6)
  123. Dim JCode:JCode = Left(Filename,5)
  124. If IsNumeric(JobNumber) Then
  125. If ValidJcode(JCode) Then
  126. WScript.Echo FileName & " Is a Valid CSV for Importing"
  127. Dim oConn:Set oConn = WScript.CreateObject("ADODB.Connection")
  128. oConn.ConnectionString = ConnectionString
  129. oConn.Open
  130. oConn.Execute("Insert Into Kit ([JobNumber], [Jcode], [CreatedOn], [JobType],[Filename],[Status]) VALUES ('" & JobNumber & "','" & JCode & "',#" & Now() & "#,'Purple Envelopes','" & FileName & "','Importing');")
  131. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit ORDER BY ID DESC")
  132. Dim kitId : kitId = rs("ID").value
  133. rs.close
  134. Dim i
  135. For i = 1 To objCSV.NumRows -1
  136. oConn.Execute("Insert Into [InkjetRecords] (KitID,VOTERID,LASTNAME,FIRSTNAME,MIDDLENAME" & _
  137. ",SUFFIX,PRECINCT,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5,APPSENT,APPRETURNED,BALSENT,BALRETURNED" & _
  138. ",CountingBoard,UOCAVASTATUS,EMAILADDRESS,PHONENUMBER,BALLOT_NUMBER) VALUES (" & kitId & _
  139. ",'" & Replace(objCSV.GetCell(i,0),"'","''") & _
  140. "','" & objCsv.GetCell(i,1) & _
  141. "','" & Replace(objCSV.GetCell(i,2),"'","''") & _
  142. "','" & Replace(objCSV.GetCell(i,3),"'","''") & _
  143. "','" & Replace(objCSV.GetCell(i,4),"'","''") & _
  144. "','" & Replace(objCSV.GetCell(i,5),"'","''") & _
  145. "','" & Replace(objCSV.GetCell(i,6),"'","''") & _
  146. "','" & Replace(objCSV.GetCell(i,7),"'","''") & _
  147. "','" & Replace(objCSV.GetCell(i,8),"'","''") & _
  148. "','" & Replace(objCSV.GetCell(i,9),"'","''") & _
  149. "','" & Replace(objCSV.GetCell(i,10),"'","''") & _
  150. "','" & Replace(objCSV.GetCell(i,11),"'","''") & _
  151. "','" & Replace(objCSV.GetCell(i,12),"'","''") & _
  152. "','" & Replace(objCsv.GetCell(i,13),"'","''") & _
  153. "','" & objCsv.GetCell(i,14) & _
  154. "','" & objCsv.GetCell(i,15) & _
  155. "','" & objCsv.GetCell(i,16) & _
  156. "','" & objCsv.GetCell(i,17) & _
  157. "','" & objCsv.GetCell(i,18) & _
  158. "','" & objCsv.GetCell(i,19) & _
  159. "')")
  160. Next
  161. oConn.Execute("Update Kit SET [Status] = 'Ready to Cass' Where ID = " & kitId &";")
  162. 'oConn.Close
  163. End If
  164. End If
  165. End Function
  166. Function ConvertCsvToString(FilePath)
  167. Dim objFSO:Set objFSO = CreateObject("Scripting.FileSystemObject")
  168. Dim objCsvFile:set objCsvFile = objFSO.OpenTextFile(FilePath)
  169. Dim strContent:strContent = ""
  170. Dim intLineCount:intLineCount = 0
  171. Do Until objCsvFile.AtEndOfStream Or intLineCount >= 3
  172. objCsvFile.SkipLine
  173. intLineCount = intLineCount + 1
  174. Loop
  175. ' Read the remaining content into a string
  176. Do Until objCsvFile.AtEndOfStream
  177. Dim strLine:strLine = objCsvFile.ReadLine
  178. strContent = strContent & strLine & vbCrLf
  179. Loop
  180. ConvertCsvToString = strContent
  181. End Function
  182. Function ValidImportCSV(CsvFileAsString)
  183. objCSV.LoadFromString(CsvFileAsString)
  184. If objCSV.NumColumns = 20 Then
  185. debug.WriteLine objCsv.GetCell(1,1)
  186. ValidImportCSV = True
  187. Else
  188. ValidImportCSV = False
  189. End If
  190. End Function
  191. Sub ImportCass
  192. Dim currentRow
  193. objCsv.LoadFile("\\kci-syn-cl01\PC Transfer\TrackingDataExport\FROM_MM.CSV")
  194. For currentRow = 0 To objCsv.NumRows -1
  195. oConn.Execute("UPDATE InkJetRecords SET CassADDRESS1 = '" & Replace(objCsv.GetCell(currentRow,1),"'","''") & "', " &_
  196. "CassADDRESS2 = '" & Replace(objCSV.GetCell(currentRow,3),"'","''") & "', " &_
  197. "CassADDRESS3 = '" & Replace(objCSV.GetCell(currentRow,4),"'","''") & "', " &_
  198. "CassADDRESS4 = '" & Replace(objCSV.GetCell(currentRow,5),"'","''") & "', " &_
  199. "CassADDRESS5 = '" & Replace(objCsv.GetCell(currentRow,6) & ", " & objCsv.GetCell(currentRow,7) & " " & objCsv.GetCell(currentRow,8),"'","''") & "'" &_
  200. " WHERE ID = " & objCSV.GetCell(currentRow,0) & ";")
  201. Next
  202. oConn.Execute("UPDATE Kit SET Status ='Ready To Assign STIDS' WHERE ID =" & KitID & ";")
  203. oConn.Execute("UPDATE Kit SET [Cass] = 1 WHERE ID =" & KitID & ";")
  204. End Sub
  205. Sub RunMailManager
  206. 'WshShell.Run "\\MM2012\APPS\BCC\MM2010\mailman.exe -p -j MMJOB.mjb -u DAN",1,True
  207. 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
  208. End Sub
  209. Sub ExportMMCsv(KitId)
  210. Dim success
  211. objCsv.HasColumnNames = 1
  212. objCSV.EnableQuotes = 1
  213. success = objCsv.SetColumnName(0,"ID")
  214. success = objCsv.SetColumnName(1,"NAME")
  215. success = objCsv.SetColumnName(2,"ADDRESS1")
  216. success = objCsv.SetColumnName(3,"ADDRESS2")
  217. success = objCsv.SetColumnName(4,"ADDRESS3")
  218. success = objCsv.SetColumnName(5,"ADDRESS4")
  219. success = objCsv.SetColumnName(6,"ADDRESS5")
  220. Dim rs : Set rs = oConn.Execute("Select ID," & _
  221. "IIF(FIRSTNAME IS NULL,'',FIRSTNAME & ' ') & " & _
  222. "IIF(MIDDLENAME IS NULL,'',MIDDLENAME & ' ') & " & _
  223. "IIF(LASTNAME IS NULL,'',LASTNAME & ' ') & " & _
  224. "IIF(SUFFIX IS NULL,'',SUFFIX & ' ') " & _
  225. "AS NAME, ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS4,ADDRESS5 FROM InkjetRecords WHERE KitID =" & KitID & " ORDER By ID;")
  226. Dim CurrentRow:CurrentRow = 0
  227. While Not rs.EOF
  228. objCSV.SetCell CurrentRow,0,rs("ID").value
  229. objCSV.SetCell CurrentRow,1,rs("NAME").value
  230. objCSV.SetCell CurrentRow,2,rs("ADDRESS1").value
  231. objCSV.SetCell CurrentRow,3,rs("ADDRESS2").value
  232. objCSV.SetCell CurrentRow,4,rs("ADDRESS3").value
  233. objCSV.SetCell CurrentRow,5,rs("ADDRESS4").value
  234. objCSV.SetCell CurrentRow,6,rs("ADDRESS5").value
  235. rs.MoveNext
  236. CurrentRow = CurrentRow +1
  237. Wend
  238. Dim CsvString:CsvString = objCSV.SaveToString()
  239. outFile.WriteLine CsvString
  240. outFile.Close
  241. End Sub
  242. Function CheckForJobsToCass()
  243. oConn.ConnectionString = ConnectionString
  244. oConn.Open
  245. 'oConn.Open
  246. Dim rs : set rs = oConn.Execute("Select TOP 1 ID from Kit Where Status ='Ready to Cass' ORDER BY ID DESC;")
  247. If Not rs.EOF Then
  248. Dim kitId : kitId = rs("ID").value
  249. rs.Close
  250. CheckForJobsToCass = KitID
  251. oConn.Execute("UPDATE Kit SET Status ='Cassing' WHERE ID =" & KitID & ";")
  252. Else
  253. CheckForJobsToCass = 0
  254. End If
  255. 'oConn.Close
  256. End Function
  257. Public Function PadLeft(originalString,desiredLength,Char)
  258. Dim padLength
  259. padLength = desiredLength - Len(originalString)
  260. If padLength > 0 Then
  261. ' Left pad the string with zeros
  262. PadLeft = String(padLength, Char) & originalString
  263. Else
  264. ' If the original string is already longer or equal to the desired length, no padding is needed
  265. PadLeft = originalString
  266. End If
  267. End Function

Powered by TurnKey Linux.