Guest User

Untitled

a guest
Mar 7th, 2018
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.32 KB | None | 0 0
  1. #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
  2. ; #Warn ; Enable warnings to assist with detecting common errors.
  3. SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
  4. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
  5.  
  6. /*
  7. ###
  8. ### IndexVFSC written by
  9. ### Update notes:
  10. ### 17/02/16: Replaced FileAppend with FileOpen https://www.reddit.com/r/AutoHotkey/comments/462rpb/best_practice_for_handling_fileappend_errors_when/
  11. ### 16/02/16: Added A_USERNAME as a field. However, it appears that the FSC database overrides this field. Must write a script in EDMS to handle the RKYV_USER Field to go into Created By field before anything else.
  12. ### 19/02/16: Change WriteLine to Write to avoid extra lines being generated in .csv format.
  13. ### 08/11/16: update to EDMS
  14. ### 01/08/17: change directory from CSV1 to CSV11_1 (db 11 CSV 1)
  15. ### 30/10/17: changed funding type for STANDING ORDER to "STANDING ORDER" from "LOCAL AUTHORITY"
  16. ### 08/01/18: changed all three selections for STANDING ORDER to STANDING ORDER.
  17. ###
  18. */
  19.  
  20. ;below is copyright notice for the RANDOM function used in this program.
  21.  
  22. /*
  23. MERSENNE TWISTER ALGORITHIM
  24. Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.
  25.  
  26. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  27.  
  28. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  29. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  30. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.
  31.  
  32. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  33. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  34. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37.  
  38. Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.
  39. */
  40.  
  41. connection_string := ""
  42.  
  43. PayTypesFull := ADOSQL( connection_string ";coldelim= `t", "
  44. (
  45. SELECT PICKLISTVALUE
  46. FROM PICKLIST_CONDITIONAL
  47. WHERE PARENTID = 0
  48. )")
  49.  
  50. Loop, parse, PayTypesFull, `n
  51. {
  52. if (A_LoopField = "Divisional" or A_LoopField = "LA" or A_LoopField = "Borough" or A_LoopField = "Schemes")
  53. {
  54. continue
  55. }
  56. PayTypes = %PayTypes%|%A_LoopField%
  57. }
  58. StringReplace, PayTypes, PayTypes, |PICKLISTVALUE|
  59.  
  60. SchemesFull := ADOSQL( connection_string ";coldelim= `t", "
  61. (
  62. SELECT PICKLISTVALUE
  63. FROM PICKLIST_CONDITIONAL
  64. WHERE PARENTID = 11
  65. )")
  66.  
  67. Loop, parse, SchemesFull, `n
  68. {
  69. Schemes = %Schemes%|%A_LoopField%
  70. }
  71. StringReplace, Schemes, Schemes, |PICKLISTVALUE|
  72.  
  73. BorsFull := ADOSQL( connection_string ";coldelim= `t", "
  74. (
  75. SELECT PICKLISTVALUE
  76. FROM PICKLIST_CONDITIONAL
  77. WHERE PARENTID = 9
  78. )")
  79.  
  80. Loop, parse, BorsFull, `n
  81. {
  82. Bors = %Bors%|%A_LoopField%
  83. }
  84. StringReplace, Bors, Bors, |PICKLISTVALUE|
  85.  
  86. DivsFull := ADOSQL( connection_string ";coldelim= `t", "
  87. (
  88. SELECT PICKLISTVALUE
  89. FROM PICKLIST_CONDITIONAL
  90. WHERE PARENTID = 1640
  91. )")
  92.  
  93. Loop, parse,DivsFull, `n
  94. {
  95. Divs = %Divs%|%A_LoopField%
  96. }
  97. StringReplace, Divs, Divs, |PICKLISTVALUE|
  98.  
  99.  
  100. FundLocsFull := ADOSQL( connection_string ";coldelim= `t", "
  101. (
  102. SELECT PICKLISTVALUE
  103. FROM PICKLIST_CONDITIONAL
  104. WHERE PARENTID = 1682
  105. )")
  106.  
  107. Loop, parse,FundLocsFull, `n
  108. {
  109. FundLocs = %FundLocs%|%A_LoopField%
  110. }
  111.  
  112. connection_string := ""
  113.  
  114. DistribFull := ADOSQL( connection_string ";coldelim= `t", "
  115. (
  116. SELECT FULLNAME
  117. FROM USERS
  118. )")
  119.  
  120. DistribFull := RegExreplace(DistribFull, " Date: [0-9]{14}")
  121. DistribList = %A_SPACE%|
  122.  
  123. Loop, parse, DistribFull, `n
  124. {
  125. DistribList = %DistribList%|%A_LoopField%
  126. }
  127.  
  128. selectedfile = %1%
  129. StringLen, LenSF, selectedfile
  130. d = \
  131. StringGetPos, LastSlash, selectedfile, %d% , R
  132. LastSlash += 2
  133. StringMid, selectedfilename, selectedfile, %LastSlash%
  134. selectedtruename = %selectedfilename%
  135.  
  136. PaymentInv:
  137. InvoiceTypeSelect = 1
  138. gui, destroy
  139. gui, add, text, Right w125, Reference No:
  140. gui, add, text, Right w125, Payment Type:
  141. gui, add, text, Right w125, Funding Type:
  142. gui, add, text, Right w125, Funding Location:
  143. gui, add, text, Right w125, Doc Date:
  144. gui, add, text, Right w125, Academy Batch Number:
  145. gui, add, text, Right w125, Total Value of Document:
  146. gui, add, text, Right w125, Notes:
  147. gui, add, Edit, ym vRefNo
  148. gui, add, DropDownList, vPayType gPayTyping Sort w150, %PayTypes%
  149. gui, add, Edit, vFundType
  150. gui, add, DropDownList, vFundLoc Sort w200
  151. gui, add, DateTime, vDocDate
  152. gui, add, Edit, vAcadBatch
  153. gui, add, Edit, vValofDoc
  154. gui, add, Edit, vPayNotes w200 h50
  155. Gui, add, Button, default gNextBit, Index
  156. gui, add, button, gAcadInv, Change to Academy Invoice
  157. gui, show
  158. return
  159.  
  160. AcadInv:
  161. InvoiceTypeSelect = 2
  162. gui, Destroy
  163. gui, add, text, Right w125, Repair No
  164. gui, add, text, Right w125, Invoice Date
  165. gui, add, text, Right w125, Division
  166. gui, add, text, Right w125, Invoice Value £
  167. gui, add, text, Right w125, Distribution
  168. gui, add, text, Right w125, Notes:
  169. gui, add, Edit, ym vRepNo
  170. gui, add, DateTime, vADocDate
  171. gui, add, DropDownList, vDivis Sort w200,
  172. gui, add, Edit, vInvValue
  173. gui, add, DropDownList, vDistrib Sort w200, %DistribList%
  174. gui, add, Edit, vPayNotes w200 h50
  175. Gui, add, Button, gNextBit, Index
  176. gui, add, button, gPaymentInv, Change to Income Document
  177. gui, show
  178. return
  179.  
  180. GuiEscape:
  181. GuiClose:
  182. ExitApp
  183.  
  184. ReturnLoop:
  185. gui, show
  186. return
  187.  
  188. PayTyping:
  189. GuiControlGet, PayType
  190. GuiControl,, FundLoc, |
  191. If PayType = Universal Credit
  192. {
  193. GuiControl,, FundType, Universal Credit
  194. GuiControl,, FundLoc, Universal Credit
  195. GuiControl, ChooseString , FundLoc, Universal Credit
  196. }
  197. If (PayType = "Care Line Payments" or PayType = "Direct Debits" or PayType = "Giro" or PayType = "Housing Benefit" or PayType = "Rechargeable Repairs" or PayType = "DHP")
  198. {
  199. GuiControl,, FundType, Local Authority
  200. GuiControl,, FundLoc, %FundLocs%
  201. }
  202. If PayType = Standing Orders
  203. {
  204. GuiControl,, FundType, Standing Order
  205. GuiControl,, FundLoc, Standing Order
  206. GuiControl, ChooseString, FundLoc, Standing Order
  207. }
  208. If (PayType = "Cash Sheet - DIVISIONAL")
  209. {
  210. GuiControl,, FundType, Divisional
  211. GuiControl,, FundLoc, %Divs%
  212. }
  213. If (PayType = "DSS")
  214. {
  215. GuiControl,, FundType, Department Working Pensions
  216. GuiControl,, FundLoc, DWP
  217. GuiControl, ChooseString, FundLoc, DWP
  218. }
  219. If (PayType = "Supporting People")
  220. {
  221. GuiControl,, FundType, Borough
  222. GuiControl,, FundLoc, %Bors%
  223. }
  224. If (PayType = "Cash Sheet - SCHEME")
  225. {
  226. GuiControl,, FundType, Schemes
  227. GuiControl,, FundLoc, %Schemes%
  228. }
  229. If (PayType = "OTC")
  230. {
  231. GuiControl,, FundType, OTC
  232. GuiControl,, FundLoc, OTC
  233. GuiControl, ChooseString, FundLoc, OTC
  234. }
  235. If (PayType = "Social Services")
  236. {
  237. GuiControl,, FundType, Social Services
  238. GuiControl,, FundLoc, Social Services
  239. GuiControl, ChooseString, FundLoc, Social Services
  240. }
  241. If (PayType = "Court Costs")
  242. {
  243. GuiControl,, FundType, Court Costs
  244. GuiControl,, FundLoc, Court Costs
  245. GuiControl, ChooseString, FundLoc, Court Costs
  246. }
  247. return
  248.  
  249. NextBit:
  250. If InvoiceTypeSelect = 1
  251. {
  252. Gui, Submit, NoHide
  253. If RefNo =
  254. {
  255. MsgBox, Enter a Reference Number
  256. goto ReturnLoop
  257. }
  258. If PayType =
  259. {
  260. MsgBox, Enter a Payment Type
  261. goto ReturnLoop
  262. }
  263. If FundType =
  264. {
  265. MsgBox, Enter a Funding Type
  266. goto ReturnLoop
  267. }
  268. If FundLoc =
  269. {
  270. MsgBox, Enter a Funding Location
  271. goto ReturnLoop
  272. }
  273. If AcadBatch =
  274. {
  275. MsgBox, Enter a Academy Batch Number
  276. goto ReturnLoop
  277. }
  278. If ValofDoc =
  279. {
  280. MsgBox, Enter a Value of Documents
  281. goto ReturnLoop
  282. }
  283. Gui, Destroy
  284.  
  285. StringMid, DocDate, DocDate, 1, 8
  286. DocDate = %DocDate%000000
  287.  
  288. CopyRetries = 0
  289.  
  290. CopyLoop:
  291. EndFileName = %A_NowUTC%_%selectedfilename%
  292. FileCopy, %selectedfile%, ...\%EndFileName%, 0
  293.  
  294. If ErrorLevel > 0
  295. {
  296. Goto ErrorHandleFileCopy
  297. }
  298.  
  299. ReCopy:
  300. fileObj := FileOpen("...DUMP.csv", "a-d")
  301. load\CSV11_1\%selectedfilename%"`,%RefNo%`,%PayType%`,%FundType%`,%FundLoc%`,%DocDate%`,%AcadBatch%`,%ValofDoc%`,%PayNotes%`,%A_USERNAME%, ...DUMP.csv
  302. fileObj.Write("`r`n""E:\Cherry\database\11\dataload\CSV11_1\" . EndFileName . """`," . RefNo "`," . PayType . "`," . FundType . "`," . FundLoc . "`," . DocDate . "`," AcadBatch . "`," . ValofDoc . "`," . PayNotes . "`," . A_USERNAME)
  303. If ErrorLevel > 0
  304. {
  305. If CopyRetries > 9
  306. {
  307. Goto ErrorHandleCopy
  308. }
  309. Random, SleepTime, 100, 300
  310. Sleep %SleepTime%
  311. CopyRetries += 1
  312. Goto ReCopy
  313. }
  314. MsgBox, %selectedtruename% indexed to FSC database
  315.  
  316. ExitApp
  317. }
  318.  
  319.  
  320. Gui, Submit, NoHide
  321. If RepNo =
  322. {
  323. MsgBox, Enter a Repair Number
  324. goto ReturnLoop
  325. }
  326. If ADocDate =
  327. {
  328. MsgBox, Select Document Date
  329. goto ReturnLoop
  330. }
  331. If Divis =
  332. {
  333. MsgBox, Enter Division
  334. goto ReturnLoop
  335. }
  336. If InvValue =
  337. {
  338. MsgBox, Enter Invoice Value
  339. goto ReturnLoop
  340. }
  341.  
  342. Gui, Destroy
  343. StringMid, ADocDate, ADocDate, 1, 8
  344. DocDate = %ADocDate%000000
  345.  
  346. CopyRetries = 0
  347.  
  348. ACopyLoop:
  349. EndFileName = %A_NowUTC%_%selectedfilename%
  350. FileCopy, %selectedfile%, ...%EndFileName%, 0
  351. If ErrorLevel > 0
  352. {
  353. Goto AErrorHandleFileCopy
  354. }
  355.  
  356. CopyRetries = 0
  357.  
  358. AReCopy:
  359. fileObj := FileOpen("...", "a-d")
  360. fileObj.Write("`r`n""..." . EndFileName . """`," . RepNo "`," . ADocDate . "`," . Divis . "`," . InvValue . "`," . Distrib . "`," PayNotes . "`," . A_USERNAME)
  361. If ErrorLevel > 0
  362. {
  363. If CopyRetries > 9
  364. {
  365. Goto AErrorHandleCopy
  366. }
  367. Random, SleepTime, 100, 300
  368. Sleep %SleepTime%
  369. CopyRetries += 1
  370. Goto AReCopy
  371. }
  372. MsgBox, %selectedtruename% indexed to FSC database
  373.  
  374. ExitApp
  375.  
  376.  
  377. ErrorHandleCopy:
  378. MsgBox, Error, could not write to ..., please contact for assistance.`nError Code: %A_LastError%
  379. ExitApp
  380.  
  381. ErrorHandleFileCopy:
  382. MsgBox, Error, could not write file to ... - please contact for assistance.`nError Code: %A_LastError%
  383. ExitApp
  384.  
  385. AErrorHandleCopy:
  386. MsgBox, Error, could not write to ..., please contact for assistance.`nError Code: %A_LastError%
  387. ExitApp
  388.  
  389. AErrorHandleFileCopy:
  390. MsgBox, Error, could not write file to ... - please contact for assistance.`nError Code: %A_LastError%
  391. ExitApp
  392.  
  393. /*
  394. ###############################################################################################################
  395. ###### ADOSQL v5.04L ######
  396. ###############################################################################################################
  397.  
  398. Wraps the utility of ADODB to connect to a database, submit a query, and read the resulting recordset.
  399. Returns the result as a new object (or array of objects, if the query has multiple statements).
  400. To instead have this function return a string, include a delimiter option in the connection string.
  401. */
  402.  
  403. Global ADOSQL_LastError, ADOSQL_LastQuery ; These super-globals are for debugging your SQL queries.
  404.  
  405. ADOSQL( Connection_String, Query_Statement ) {
  406. ; Uses an ADODB object to connect to a database, submit a query and read the resulting recordset.
  407. ; By default, this function returns an object. If the query generates exactly one result set, the object is
  408. ; a 2-dimensional array containing that result (the first row contains the column names). Otherwise, the
  409. ; returned object is an array of all the results. To instead have this function return a string, append either
  410. ; ";RowDelim=`n" or ";ColDelim=`t" to the connection string (substitute your preferences for "`n" and "`t").
  411. ; If there is more than one table in the output string, they are separated by 3 consecutive row-delimiters.
  412. ; ErrorLevel is set to "Error" if ADODB is not available, or the COM error code if a COM error is encountered.
  413. ; Otherwise ErrorLevel is set to zero.
  414.  
  415. coer := "", txtout := 0, rd := "`n", cd := "CSV", str := Connection_String ; 'str' is shorter.
  416.  
  417. ; Examine the connection string for output formatting options.
  418. If ( 9 < oTbl := 9 + InStr( ";" str, ";RowDelim=" ) )
  419. {
  420. rd := SubStr( str, oTbl, 0 - oTbl + oRow := InStr( str ";", ";", 0, oTbl ) )
  421. str := SubStr( str, 1, oTbl - 11 ) SubStr( str, oRow )
  422. txtout := 1
  423. }
  424. If ( 9 < oTbl := 9 + InStr( ";" str, ";ColDelim=" ) )
  425. {
  426. cd := SubStr( str, oTbl, 0 - oTbl + oRow := InStr( str ";", ";", 0, oTbl ) )
  427. str := SubStr( str, 1, oTbl - 11 ) SubStr( str, oRow )
  428. txtout := 1
  429. }
  430.  
  431. ComObjError( 0 ) ; We'll manage COM errors manually.
  432.  
  433. ; Create a connection object. > http://www.w3schools.com/ado/ado_ref_connection.asp
  434. ; If something goes wrong here, return blank and set the error message.
  435. If !( oCon := ComObjCreate( "ADODB.Connection" ) )
  436. Return "", ComObjError( 1 ), ErrorLevel := "Error"
  437. , ADOSQL_LastError := "Fatal Error: ADODB is not available."
  438.  
  439.  
  440. oCon.ConnectionTimeout := 3 ; Allow 3 seconds to connect to the server.
  441. oCon.CursorLocation := 3 ; Use a client-side cursor server.
  442. oCon.CommandTimeout := 900 ; A generous 15 minute timeout on the actual SQL statement.
  443. oCon.Open( str ) ; open the connection.
  444.  
  445. ; Execute the query statement and get the recordset. > http://www.w3schools.com/ado/ado_ref_recordset.asp
  446. If !( coer := A_LastError )
  447. oRec := oCon.execute( ADOSQL_LastQuery := Query_Statement )
  448.  
  449. If !( coer := A_LastError ) ; The query executed OK, so examine the recordsets.
  450. {
  451. o3DA := [] ; This is a 3-dimensional array.
  452. While IsObject( oRec )
  453. If !oRec.State ; Recordset.State is zero if the recordset is closed, so we skip it.
  454. oRec := oRec.NextRecordset()
  455. Else ; A row-returning operation returns an open recordset
  456. {
  457. oFld := oRec.Fields
  458. o3DA.Insert( oTbl := [] )
  459. oTbl.Insert( oRow := [] )
  460.  
  461. Loop % cols := oFld.Count ; Put the column names in the first row.
  462. oRow[ A_Index ] := oFld.Item( A_Index - 1 ).Name
  463.  
  464. While !oRec.EOF ; While the record pointer is not at the end of the recordset...
  465. {
  466. oTbl.Insert( oRow := [] )
  467. oRow.SetCapacity( cols ) ; Might improve performance on huge tables??
  468. Loop % cols
  469. oRow[ A_Index ] := oFld.Item( A_Index - 1 ).Value
  470. oRec.MoveNext() ; move the record pointer to the next row of values
  471. }
  472.  
  473. oRec := oRec.NextRecordset() ; Get the next recordset.
  474. }
  475.  
  476. If (txtout) ; If the user wants plaintext output, copy the results into a string
  477. {
  478. Query_Statement := "x"
  479. Loop % o3DA.MaxIndex()
  480. {
  481. Query_Statement .= rd rd
  482. oTbl := o3DA[ A_Index ]
  483. Loop % oTbl.MaxIndex()
  484. {
  485. oRow := oTbl[ A_Index ]
  486. Loop % oRow.MaxIndex()
  487. If ( cd = "CSV" )
  488. {
  489. str := oRow[ A_Index ]
  490. StringReplace, str, str, ", "", A
  491. If !ErrorLevel || InStr( str, "," ) || InStr( str, rd )
  492. str := """" str """"
  493. Query_Statement .= ( A_Index = 1 ? rd : "," ) str
  494. }
  495. Else
  496. Query_Statement .= ( A_Index = 1 ? rd : cd ) oRow[ A_Index ]
  497. }
  498. }
  499. Query_Statement := SubStr( Query_Statement, 2 + 3 * StrLen( rd ) )
  500. }
  501. }
  502. Else ; Oh NOES!! Put a description of each error in 'ADOSQL_LastError'.
  503. {
  504. oErr := oCon.Errors ; > http://www.w3schools.com/ado/ado_ref_error.asp
  505. Query_Statement := "x"
  506. Loop % oErr.Count
  507. {
  508. oFld := oErr.Item( A_Index - 1 )
  509. str := oFld.Description
  510. Query_Statement .= "`n`n" SubStr( str, 1 + InStr( str, "]", 0, 2 + InStr( str, "][", 0, 0 ) ) )
  511. . "`n Number: " oFld.Number
  512. . ", NativeError: " oFld.NativeError
  513. . ", Source: " oFld.Source
  514. . ", SQLState: " oFld.SQLState
  515. }
  516. ADOSQL_LastError := SubStr( Query_Statement, 4 )
  517. Query_Statement := ""
  518. txtout := 1
  519. }
  520.  
  521. ; Close the connection and return the result. Local objects are cleaned up as the function returns.
  522. oCon.Close()
  523. ComObjError( 1 )
  524. ErrorLevel := coer
  525. Return txtout ? Query_Statement : o3DA.MaxIndex() = 1 ? o3DA[1] : o3DA
  526. } ; END - ADOSQL( Connection_String, Query_Statement )
Add Comment
Please, Sign In to add comment