Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.68 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2. Imports EASendMail
  3. Imports System.IO
  4. Imports System.Text
  5.  
  6. Public Class App
  7.  
  8. #Region "connectors"
  9.  
  10. Public axCZKEM1 As New zkemkeeper.CZKEM
  11. Dim sqlconn As New MySqlConnection
  12. Dim sqlComm As New MySqlCommand
  13. Dim sqlReader As MySqlDataReader
  14.  
  15. #End Region
  16.  
  17. #Region "global variables"
  18.  
  19. Dim appName As String = "Automated Fingerprint Scanner Email Notifier"
  20.  
  21. Dim myConnectionString As String = "Server=localhost;Database=time_attendance;Uid=root;Pwd=;"
  22.  
  23. Dim timestring As String
  24. Dim acmShift As String
  25. Dim acmFlag As Integer
  26. Dim sdwEnrollNumber As String = ""
  27. Dim idwInOutMode As Integer
  28.  
  29. Dim _mailFrom As String = "opc-ta@g-able.com"
  30. Dim _mailTo As String = ""
  31. Dim _mailHead As String = "parawee.t@g-able.com , pornlert.w@g-able.com , anek.r@g-able.com , phorntip.m@g-able.com"
  32. Dim tmpString As String = ""
  33.  
  34. Dim _shift_out As String
  35. Dim shift_out_A As String = "16.00 กะเช้า"
  36. Dim shift_out_B As String = "24.00 กะบ่าย"
  37. Dim shift_out_C As String = "8.00 กะดึก"
  38.  
  39. Dim _report_time As String
  40. Dim reportInA As String = "7.15 กะเช้า"
  41. Dim reportInB As String = "12.15 กะบ่าย"
  42. Dim reportInC As String = "23.15 กะดึก"
  43.  
  44. Dim reportOutA As String = "16.20 กะเช้า"
  45. Dim reportOutB As String = "24.20 กะบ่าย"
  46. Dim reportOutC As String = "8.20 กะดึก"
  47.  
  48. Dim _report_status As String
  49. Dim _count As String
  50.  
  51. Dim pri As Integer
  52.  
  53. Dim _userid As String
  54. Dim _username As String
  55.  
  56. Dim csvFile As String = ""
  57.  
  58. '------------------------------------------------------------------
  59.  
  60. 'shiftA at 7.00-16.00
  61.  
  62. 'earlyA at 4.00
  63. 'lateA at 8.00
  64. Dim earlyA As DateTime
  65. Dim lateA As DateTime
  66.  
  67. 'earlyOutA at 15:30
  68. 'lateOutA at 16.20
  69. Dim earlyOutA As DateTime
  70. Dim lateOutA As DateTime
  71.  
  72. '------------------------------------------------------------------
  73.  
  74. 'shiftB at 15.00-24.00
  75.  
  76. 'earlyB at 12.00
  77. 'lateB at 16.00
  78. Dim earlyB As DateTime
  79. Dim lateB As DateTime
  80.  
  81. 'earlyOutB at 23.30
  82. 'lateOutB at 24.20
  83. Dim earlyOutB As DateTime
  84. Dim lateOutB As DateTime
  85.  
  86. '------------------------------------------------------------------
  87.  
  88. 'shiftC at 23.00-8.00
  89.  
  90. 'earlyC at 20.00
  91. 'lateC at 24.00
  92. Dim earlyC As DateTime
  93. Dim lateC As DateTime
  94.  
  95. 'earlyOutC at 7:30
  96. 'lateOutC at 8:20
  97. Dim earlyOutC As DateTime
  98. Dim lateOutC As DateTime
  99.  
  100. '------------------------------------------------------------------
  101.  
  102. #End Region
  103.  
  104. #Region "Communicate with device"
  105.  
  106. Private bIsConnected = False 'the boolean value identifies whether the device is connected
  107. Private iMachineNumber As Integer 'the serial number of the device.After connecting the device ,this value will be changed.
  108.  
  109. 'If your device supports the TCP/IP communications, you can refer to this.
  110. 'when you are using the tcp/ip communication,you can distinguish different devices by their IP address.
  111.  
  112. Private Sub btnConnect_Click(sender As System.Object, e As System.EventArgs) Handles btnConnect.Click
  113. If txtIP.Text.Trim() = "" Or txtPort.Text.Trim() = "" Then
  114. MsgBox("IP and Port cannot be null", MsgBoxStyle.Exclamation, "Error")
  115. Return
  116. End If
  117. Cursor = Cursors.WaitCursor
  118. If MachineOnlineIcon.Visible = True Then
  119. axCZKEM1.Disconnect()
  120. bIsConnected = False
  121. btnConnect.Text = "Connect"
  122. status_MachineOffline()
  123. Cursor = Cursors.Default
  124. Return
  125. End If
  126.  
  127. Dim idwErrorCode As Integer
  128. bIsConnected = axCZKEM1.Connect_Net(txtIP.Text.Trim(), Convert.ToInt32(txtPort.Text.Trim()))
  129. If bIsConnected = True Then
  130. btnConnect.Text = "Disconnect"
  131. btnConnect.Refresh()
  132. status_MachineOnline()
  133. iMachineNumber = 1 'In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1.
  134. axCZKEM1.RegEvent(iMachineNumber, 65535) 'Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all)
  135. Else
  136. axCZKEM1.GetLastError(idwErrorCode)
  137. lblstatus.Text = "Unable to connect the device,ErrorCode= '" & idwErrorCode & "' "
  138. End If
  139. Cursor = Cursors.Default
  140. End Sub
  141.  
  142. #End Region
  143.  
  144. #Region "Application"
  145.  
  146. 'Download the attendance records from the device, sync to database
  147. Private Sub btnSyncData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSyncData.Click
  148. Syncing()
  149. End Sub
  150.  
  151. 'Get the count of attendance records in from ternimal.
  152. Private Sub btnGetRecordCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetRecordCount.Click
  153. If bIsConnected = False Then
  154. MsgBox("Please connect the device first", MsgBoxStyle.Exclamation, "Error")
  155. Return
  156. End If
  157. Dim idwErrorCode As Integer
  158. Dim iValue = 0
  159.  
  160. axCZKEM1.EnableDevice(iMachineNumber, False) 'disable the device
  161. If axCZKEM1.GetDeviceStatus(iMachineNumber, 6, iValue) = True Then 'Here we use the function "GetDeviceStatus" to get the record's count.The parameter "Status" is 6.
  162. MsgBox("The count of the AttLogs in the device is " + iValue.ToString(), MsgBoxStyle.Information, "Success")
  163. Else
  164. axCZKEM1.GetLastError(idwErrorCode)
  165. MsgBox("Operation failed,ErrorCode=" & idwErrorCode, MsgBoxStyle.Exclamation, "Error")
  166. End If
  167.  
  168. axCZKEM1.EnableDevice(iMachineNumber, True) 'enable the device
  169. status_MachineOnline()
  170. End Sub
  171.  
  172. 'database connection check
  173. Public Sub connectMysqlCheck()
  174. If Not sqlconn Is Nothing Then
  175. sqlconn.ConnectionString = myConnectionString
  176. Try
  177. sqlconn.Open()
  178. MsgBox("Connected to MySQL Database.", 64, appName)
  179. lblstatus.Text = "Status : Connected to MySQL Database."
  180. sqlconn.Close()
  181. Catch
  182. MsgBox("Unable to connect to MySQL Database, returning...", 48, appName)
  183. lblstatus.Text = "Status : Unable to connect to MySQL Database ."
  184. Return
  185. End Try
  186. End If
  187. End Sub
  188. 'machine/device connection check
  189. Private Sub connectMachineCheck()
  190. If bIsConnected = False Then
  191. MsgBox("Please connect the device", MsgBoxStyle.Exclamation, "Error")
  192. status_MachineOffline()
  193. Return
  194. Else
  195. status_MachineOnline()
  196. End If
  197. End Sub
  198.  
  199. #End Region
  200.  
  201. #Region "Algs"
  202.  
  203. 'method for sync
  204. Private Sub Syncing()
  205. Cursor = Cursors.WaitCursor
  206. Timer1.Enabled = False
  207.  
  208. 'truncate(shift_a, shift_b, shift_c,raw)
  209. Try
  210. sqlconn.Open()
  211. sqlComm.Connection = sqlconn
  212.  
  213. 'Dim cmd As MySqlCommand = New MySqlCommand
  214. 'Dim mysqlBackup As MySqlBackup = New MySqlBackup(cmd)
  215. 'cmd.Connection = sqlconn
  216. 'My.Computer.FileSystem.CreateDirectory("D:\TimeAttendanceBackup\")
  217. 'Dim exportFile As String = "D:\TimeAttendanceBackup\backup.sql"
  218. 'exportFile = exportFile.Replace(".sql", "_" & System.DateTime.Now.ToString("yyyyMMddhhmmss") & ".sql")
  219. 'mysqlBackup.ExportToFile(exportFile)
  220.  
  221. Dim SQL As String
  222. SQL = "truncate shift_a; truncate shift_b; truncate shift_c; truncate raw"
  223. sqlComm.CommandText = SQL
  224. sqlComm.ExecuteNonQuery()
  225. sqlconn.Close()
  226. Catch
  227. lblstatus.Text = "Unable to connect to MySQL Database, returning..."
  228. Cursor = Cursors.Default
  229. Return
  230. End Try
  231.  
  232. 'sync
  233. pull_machine()
  234. Cursor = Cursors.Default
  235. Timer1.Enabled = True
  236. End Sub
  237. '[sub method for sync] pull data
  238. Private Sub pull_machine()
  239.  
  240. Dim iGLCount = 0
  241. pri = 0
  242.  
  243. Dim idwVerifyMode As Integer
  244. Dim idwYear As Integer
  245. Dim idwMonth As Integer
  246. Dim idwDay As Integer
  247. Dim idwHour As Integer
  248. Dim idwMinute As Integer
  249. Dim idwSecond As Integer
  250. Dim idwWorkcode As Integer
  251.  
  252. Dim idwErrorCode As Integer
  253. Dim lvItem As New ListViewItem("Items", 0)
  254.  
  255. axCZKEM1.EnableDevice(iMachineNumber, False) 'disable the device
  256. If axCZKEM1.ReadGeneralLogData(iMachineNumber) Then 'read all the attendance records to the memory
  257. 'get records from the memory
  258. While axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, sdwEnrollNumber, idwVerifyMode, idwInOutMode, idwYear, idwMonth, idwDay, idwHour, idwMinute, idwSecond, idwWorkcode)
  259. iGLCount += 1
  260. pri += 1
  261. 'moved variable
  262. timestring = idwYear.ToString("0000") & "/" & idwMonth.ToString("00") & "/" & idwDay.ToString("00") & " " & idwHour.ToString("00") & ":" & idwMinute.ToString("00") & ":" & idwSecond.ToString("00")
  263.  
  264. Dim dateToCheck As New DateTime(idwYear, idwMonth, idwDay) 'any date you want to check here
  265. Dim Check As DateTime = dateToCheck.AddDays(1)
  266.  
  267. 'earlyA at 5.00
  268. 'lateA at 9.00
  269. earlyA = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "05:00:00"
  270. lateA = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "09:00:00"
  271.  
  272. 'earlyB at 13.00
  273. 'lateB at 17.00
  274. earlyB = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "13:00:00"
  275. lateB = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "17:00:00"
  276.  
  277. 'earlyC at 21.00
  278. 'lateC at 1.00
  279. earlyC = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "21:00:00"
  280. If dateToCheck.Month = Check.Month Then
  281. lateC = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() + 1 & " " & "01:00:00"
  282. Else
  283. lateC = idwYear.ToString() & "/" & idwMonth.ToString() + 1 & "/" & "01" & " " & "01:00:00"
  284. End If
  285.  
  286. 'earlyOutA at 15:30
  287. 'lateOutA at 18.00
  288. earlyOutA = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "15:30:00"
  289. lateOutA = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "18:00:00"
  290.  
  291. 'earlyOutB at 23.30
  292. 'lateOutB at 02.00
  293. earlyOutB = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "23:30:00"
  294. If dateToCheck.Month = Check.Month Then
  295. lateOutB = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() + 1 & " " & "02:00:00"
  296. Else
  297. lateOutB = idwYear.ToString() & "/" & idwMonth.ToString() + 1 & "/" & "1" & " " & "02:00:00"
  298. End If
  299.  
  300. 'earlyOutC at 7:30
  301. 'lateOutC at 8:30
  302. earlyOutC = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "07:30:00"
  303. lateOutC = idwYear.ToString() & "/" & idwMonth.ToString() & "/" & idwDay.ToString() & " " & "08:30:00"
  304.  
  305. shift_Manage()
  306.  
  307. '---------------------------------------------------------------------------
  308. 'push to mysql
  309. sqlconn.Open()
  310. sqlComm.Connection = sqlconn
  311. Dim SQL As String
  312. SQL = "insert into raw (iGLCount,iMachineNumber,sdwEnrollNumber,idwVerifyMode,idwInOutMode,datetime,idwWorkcode,acmShift) " & _
  313. "values ('" & iGLCount & "','" & iMachineNumber.ToString & "', '" & sdwEnrollNumber.ToString & "', '" & idwVerifyMode.ToString & "', '" & idwInOutMode.ToString & "', '" & timestring & "', '" & idwWorkcode.ToString & "', '" & acmShift & "')"
  314. sqlComm.CommandText = SQL
  315. sqlComm.ExecuteNonQuery()
  316. sqlconn.Close()
  317.  
  318. End While
  319. Else
  320. axCZKEM1.GetLastError(idwErrorCode)
  321. If idwErrorCode <> 0 Then
  322. MsgBox("Reading data from terminal failed,ErrorCode: " & idwErrorCode, MsgBoxStyle.Exclamation, "Error")
  323. Else
  324. MsgBox("No data from terminal returns!", MsgBoxStyle.Exclamation, "Error")
  325. End If
  326. End If
  327. axCZKEM1.EnableDevice(iMachineNumber, True) 'enable the device
  328. End Sub
  329. '[sub method for sync] shift manipulate
  330. Private Sub shift_Manage()
  331.  
  332. Dim timestringparse As DateTime = DateTime.ParseExact(timestring, "yyyy/MM/dd HH:mm:ss", Nothing)
  333.  
  334. '------------------------------------------------------------------
  335. 'login
  336. If idwInOutMode.ToString = 0 Then
  337. sqlconn.Open()
  338. acmFlag = "0"
  339. If earlyA < timestringparse And timestringparse < lateA Then
  340. acmShift = "A"
  341. sqlComm.Connection = sqlconn
  342. Dim SQL As String
  343. SQL = "insert into shift_a (pri,id_om,datetime,io_mode,flag)" & _
  344. "values ('" & pri & "','" & sdwEnrollNumber.ToString & "', '" & timestring & "','" & idwInOutMode.ToString & "','" & acmFlag & "')"
  345. sqlComm.CommandText = SQL
  346. sqlComm.ExecuteNonQuery()
  347.  
  348. ElseIf earlyB < timestringparse And timestringparse < lateB Then
  349. acmShift = "B"
  350. sqlComm.Connection = sqlconn
  351. Dim SQL As String
  352. SQL = "insert into shift_b (pri,id_om,datetime,io_mode,flag)" & _
  353. "values ('" & pri & "','" & sdwEnrollNumber.ToString & "', '" & timestring & "','" & idwInOutMode.ToString & "','" & acmFlag & "')"
  354. sqlComm.CommandText = SQL
  355. sqlComm.ExecuteNonQuery()
  356.  
  357. ElseIf earlyC < timestringparse And timestringparse < lateC Then
  358. acmShift = "C"
  359. sqlComm.Connection = sqlconn
  360. Dim SQL As String
  361. SQL = "insert into shift_c (pri,id_om,datetime,io_mode,flag)" & _
  362. "values ('" & pri & "','" & sdwEnrollNumber.ToString & "', '" & timestring & "','" & idwInOutMode.ToString & "','" & acmFlag & "')"
  363. sqlComm.CommandText = SQL
  364. sqlComm.ExecuteNonQuery()
  365.  
  366. End If
  367. sqlconn.Close()
  368.  
  369. '------------------------------------------------------------------
  370.  
  371. 'logout
  372. ElseIf idwInOutMode.ToString = 1 Then
  373. sqlconn.Open()
  374. acmFlag = "1"
  375. If earlyOutA < timestringparse And timestringparse < lateOutA Then
  376. acmShift = "A"
  377. sqlComm.Connection = sqlconn
  378. Dim SQL As String
  379. SQL = "update shift_a set flag='" & acmFlag & "'" & _
  380. "where '" & sdwEnrollNumber.ToString & "'=id_om and Flag=0"
  381. sqlComm.CommandText = SQL
  382. sqlComm.ExecuteNonQuery()
  383.  
  384. ElseIf earlyOutB < timestringparse And timestringparse < lateOutB Then
  385. acmShift = "B"
  386. sqlComm.Connection = sqlconn
  387. Dim SQL As String
  388. SQL = "update shift_b set flag='" & acmFlag & "'" & _
  389. "where '" & sdwEnrollNumber.ToString & "'=id_om and Flag=0"
  390. sqlComm.CommandText = SQL
  391. sqlComm.ExecuteNonQuery()
  392.  
  393. ElseIf earlyOutC < timestringparse And timestringparse < lateOutC Then
  394. acmShift = "C"
  395. sqlComm.Connection = sqlconn
  396. Dim SQL As String
  397. SQL = "update shift_c set flag='" & acmFlag & "'" & _
  398. "where '" & sdwEnrollNumber.ToString & "'=id_om and Flag=0"
  399. sqlComm.CommandText = SQL
  400. sqlComm.ExecuteNonQuery()
  401. End If
  402. sqlconn.Close()
  403. End If
  404. End Sub
  405.  
  406. '---------------------------------------------------------------------------
  407. 'update csv button
  408. Private Sub btnCSV_Click(sender As System.Object, e As System.EventArgs) Handles btnCSV.Click
  409.  
  410. OpenFileDialog1.Title = "Please Select a File"
  411. OpenFileDialog1.FileName = ""
  412. OpenFileDialog1.InitialDirectory = ""
  413. OpenFileDialog1.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*"
  414. OpenFileDialog1.ShowDialog()
  415.  
  416. End Sub
  417. 'edit
  418. Private Sub OpenFileDialog1_FileOk(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  419.  
  420. Dim readfile As String = OpenFileDialog1.FileName
  421.  
  422. Dim tempdir As String = Path.GetDirectoryName(readfile)
  423. Dim tempfile As String = "tmp_file.csv"
  424. Dim destinationFile = Path.Combine(tempdir, tempfile)
  425.  
  426. Dim fileContent As String = File.ReadAllText(readfile, Encoding.Default)
  427. File.WriteAllText(destinationFile, fileContent, Encoding.UTF8)
  428.  
  429. csvFile = destinationFile.Replace("\", "/")
  430. Dim SQL As String
  431. SQL = "truncate user;" & _
  432. "LOAD DATA LOCAL INFILE '" & csvFile & "' " & _
  433. "INTO TABLE user " & _
  434. "FIELDS TERMINATED BY ',' IGNORE 2 LINES " & _
  435. "(no,id_om,id_user,firstname,lastname,nickname,mobile,email)"
  436. Try
  437. sqlconn.Open()
  438. sqlComm.Connection = sqlconn
  439. sqlComm.CommandText = SQL
  440. sqlComm.ExecuteNonQuery()
  441. File.Delete(destinationFile)
  442. Catch ex As MySqlException
  443. MsgBox(ex.ToString)
  444. File.Delete(destinationFile)
  445. sqlconn.Close()
  446. End Try
  447. sqlconn.Close()
  448.  
  449. lblFilename.Text = "Updated user with '" & OpenFileDialog1.FileName & "' "
  450. lblFilename.Visible = True
  451. End Sub
  452.  
  453. '---------------------------------------------------------------------------
  454.  
  455. '---------------------------------------------------------------------------
  456. 'for lights indicator
  457. Private Sub status_MachineOnline()
  458. lblMachineState.Text = "Machine State:Connected"
  459. MachineOnlineIcon.Visible = True
  460. MachineOfflineIcon.Visible = False
  461. End Sub
  462. Private Sub status_MachineOffline()
  463. lblMachineState.Text = "Machine State:Disconnected"
  464. MachineOnlineIcon.Visible = False
  465. MachineOfflineIcon.Visible = True
  466. End Sub
  467.  
  468. '---------------------------------------------------------------------------
  469.  
  470. 'loading
  471. Private Sub MyBase_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  472. Me.Text = appName
  473. Timer1.Enabled = True
  474. connectMysqlCheck()
  475. End Sub
  476. 'tick 1000ms
  477. Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
  478. lblClock.Text = Format(Now, "yyyy-MM-dd HH:mm:ss")
  479. If DateTime.Now.Minute = 0 Or DateTime.Now.Minute = 30 Then
  480. Syncing()
  481. End If
  482. time_Alert()
  483. End Sub
  484. #End Region
  485.  
  486. #Region "notifier"
  487.  
  488. 'WIP
  489. 'need polishing reportin/out
  490.  
  491. Private Sub time_Alert()
  492. Dim now As DateTime = DateTime.Now
  493. If (15 = now.Hour) And (35 = now.Minute) Then
  494. Syncing()
  495. Timer1.Enabled = False
  496. 'noti 1A
  497. _shift_out = shift_out_A
  498. sqlconn.Open()
  499. sqlComm.Connection = sqlconn
  500. Dim SQL As String
  501. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  502. "from shift_a s, user u " & _
  503. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  504. Dim lvItem As New ListViewItem("Items", 0)
  505. sqlComm.CommandText = SQL
  506. sqlReader = sqlComm.ExecuteReader()
  507. If sqlReader.HasRows Then
  508. Do While sqlReader.Read()
  509. 'need to do sth here
  510. _mailTo = sqlReader.Item("useremail")
  511. _userid = sqlReader.Item("userid")
  512. _username = sqlReader.Item("username")
  513. sendMailNoti()
  514. Loop
  515. End If
  516. sqlReader.Close()
  517. sqlconn.Close()
  518. Timer1.Enabled = True
  519.  
  520. ElseIf (15 = now.Hour) And (50 = now.Minute) Then
  521. Syncing()
  522. Timer1.Enabled = False
  523. 'noti 2A
  524. _shift_out = shift_out_A
  525. sqlconn.Open()
  526. sqlComm.Connection = sqlconn
  527. Dim SQL As String
  528. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  529. "from shift_a s, user u " & _
  530. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  531. Dim lvItem As New ListViewItem("Items", 0)
  532. sqlComm.CommandText = SQL
  533. sqlReader = sqlComm.ExecuteReader()
  534. If sqlReader.HasRows Then
  535. Do While sqlReader.Read()
  536. 'need to do sth here
  537. _mailTo = sqlReader.Item("useremail")
  538. _userid = sqlReader.Item("userid")
  539. _username = sqlReader.Item("username")
  540. sendMailNoti()
  541. Loop
  542. End If
  543. sqlReader.Close()
  544. sqlconn.Close()
  545. Timer1.Enabled = True
  546.  
  547. ElseIf (16 = now.Hour) And (10 = now.Minute) Then
  548. Syncing()
  549. Timer1.Enabled = False
  550. 'noti 3A
  551. _shift_out = shift_out_A
  552. sqlconn.Open()
  553. sqlComm.Connection = sqlconn
  554. Dim SQL As String
  555. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  556. "from shift_a s, user u " & _
  557. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  558. Dim lvItem As New ListViewItem("Items", 0)
  559. sqlComm.CommandText = SQL
  560. sqlReader = sqlComm.ExecuteReader()
  561. If sqlReader.HasRows Then
  562. Do While sqlReader.Read()
  563. 'need to do sth here
  564. _mailTo = sqlReader.Item("useremail")
  565. _userid = sqlReader.Item("userid")
  566. _username = sqlReader.Item("username")
  567. sendMailNoti()
  568. Loop
  569. End If
  570. sqlReader.Close()
  571. sqlconn.Close()
  572. Timer1.Enabled = True
  573. '--------------------------------------------------------
  574. ElseIf (23 = now.Hour) And (35 = now.Minute) Then
  575. Syncing()
  576. Timer1.Enabled = False
  577. 'noti 1B
  578. _shift_out = shift_out_B
  579. sqlconn.Open()
  580. sqlComm.Connection = sqlconn
  581. Dim SQL As String
  582. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  583. "from shift_b s, user u " & _
  584. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  585. Dim lvItem As New ListViewItem("Items", 0)
  586. sqlComm.CommandText = SQL
  587. sqlReader = sqlComm.ExecuteReader()
  588. If sqlReader.HasRows Then
  589. Do While sqlReader.Read()
  590. 'need to do sth here
  591. _mailTo = sqlReader.Item("useremail")
  592. _userid = sqlReader.Item("userid")
  593. _username = sqlReader.Item("username")
  594. sendMailNoti()
  595. Loop
  596. End If
  597. sqlReader.Close()
  598. sqlconn.Close()
  599. Timer1.Enabled = True
  600. ElseIf (23 = now.Hour) And (50 = now.Minute) Then
  601. Syncing()
  602. Timer1.Enabled = False
  603. 'noti 2B
  604. _shift_out = shift_out_B
  605. sqlconn.Open()
  606. sqlComm.Connection = sqlconn
  607. Dim SQL As String
  608. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  609. "from shift_b s, user u " & _
  610. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  611. Dim lvItem As New ListViewItem("Items", 0)
  612. sqlComm.CommandText = SQL
  613. sqlReader = sqlComm.ExecuteReader()
  614. If sqlReader.HasRows Then
  615. Do While sqlReader.Read()
  616. 'need to do sth here
  617. _mailTo = sqlReader.Item("useremail")
  618. _userid = sqlReader.Item("userid")
  619. _username = sqlReader.Item("username")
  620. sendMailNoti()
  621. Loop
  622. End If
  623. sqlReader.Close()
  624. sqlconn.Close()
  625. Timer1.Enabled = True
  626. ElseIf (0 = now.Hour) And (10 = now.Minute) Then
  627. Syncing()
  628. Timer1.Enabled = False
  629. 'noti 3B
  630. _shift_out = shift_out_B
  631. sqlconn.Open()
  632. sqlComm.Connection = sqlconn
  633. Dim SQL As String
  634. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  635. "from shift_b s, user u " & _
  636. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE()) OR DATE(`datetime`) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
  637. Dim lvItem As New ListViewItem("Items", 0)
  638. sqlComm.CommandText = SQL
  639. sqlReader = sqlComm.ExecuteReader()
  640. If sqlReader.HasRows Then
  641. Do While sqlReader.Read()
  642. 'need to do sth here
  643. _mailTo = sqlReader.Item("useremail")
  644. _userid = sqlReader.Item("userid")
  645. _username = sqlReader.Item("username")
  646. sendMailNoti()
  647. Loop
  648. End If
  649. sqlReader.Close()
  650. sqlconn.Close()
  651. Timer1.Enabled = True
  652. '--------------------------------------------------------
  653.  
  654. ElseIf (7 = now.Hour) And (35 = now.Minute) Then
  655. Syncing()
  656. Timer1.Enabled = False
  657. 'noti 1C
  658. _shift_out = shift_out_C
  659. sqlconn.Open()
  660. sqlComm.Connection = sqlconn
  661. Dim SQL As String
  662. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  663. "from shift_c s, user u " & _
  664. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE()) OR DATE(`datetime`) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
  665. Dim lvItem As New ListViewItem("Items", 0)
  666. sqlComm.CommandText = SQL
  667. sqlReader = sqlComm.ExecuteReader()
  668. If sqlReader.HasRows Then
  669. Do While sqlReader.Read()
  670. 'need to do sth here
  671. _mailTo = sqlReader.Item("useremail")
  672. _userid = sqlReader.Item("userid")
  673. _username = sqlReader.Item("username")
  674. sendMailNoti()
  675. Loop
  676. End If
  677. sqlReader.Close()
  678. sqlconn.Close()
  679. Timer1.Enabled = True
  680. ElseIf (7 = now.Hour) And (50 = now.Minute) Then
  681. Syncing()
  682. Timer1.Enabled = False
  683. 'noti 2C
  684. _shift_out = shift_out_C
  685. sqlconn.Open()
  686. sqlComm.Connection = sqlconn
  687. Dim SQL As String
  688. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  689. "from shift_c s, user u " & _
  690. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE()) OR DATE(`datetime`) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
  691. Dim lvItem As New ListViewItem("Items", 0)
  692. sqlComm.CommandText = SQL
  693. sqlReader = sqlComm.ExecuteReader()
  694. If sqlReader.HasRows Then
  695. Do While sqlReader.Read()
  696. 'need to do sth here
  697. _mailTo = sqlReader.Item("useremail")
  698. _userid = sqlReader.Item("userid")
  699. _username = sqlReader.Item("username")
  700. sendMailNoti()
  701. Loop
  702. End If
  703. sqlReader.Close()
  704. sqlconn.Close()
  705. Timer1.Enabled = True
  706. ElseIf (8 = now.Hour) And (10 = now.Minute) Then
  707. Syncing()
  708. Timer1.Enabled = False
  709. 'noti 3C
  710. _shift_out = shift_out_C
  711. sqlconn.Open()
  712. sqlComm.Connection = sqlconn
  713. Dim SQL As String
  714. SQL = "select u.email as useremail, u.id_user as userid , u.firstname as username " & _
  715. "from shift_c s, user u " & _
  716. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE()) OR DATE(`datetime`) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
  717. Dim lvItem As New ListViewItem("Items", 0)
  718. sqlComm.CommandText = SQL
  719. sqlReader = sqlComm.ExecuteReader()
  720. If sqlReader.HasRows Then
  721. Do While sqlReader.Read()
  722. 'need to do sth here
  723. _mailTo = sqlReader.Item("useremail")
  724. _userid = sqlReader.Item("userid")
  725. _username = sqlReader.Item("username")
  726. sendMailNoti()
  727. Loop
  728. End If
  729. sqlReader.Close()
  730. sqlconn.Close()
  731. Timer1.Enabled = True
  732. '--------------------------------------------------------
  733.  
  734. 'reportin; report to head who tap in
  735. ElseIf (7 = now.Hour) And (25 = now.Minute) Then
  736. Syncing()
  737. Timer1.Enabled = False
  738. 'reportin A
  739. 'select id from shift_a where io_mode=1
  740. sqlconn.Open()
  741. sqlComm.Connection = sqlconn
  742. Dim SQL As String
  743. SQL = "select u.firstname as username " & _
  744. "from shift_a s, user u " & _
  745. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  746. Dim lvItem As New ListViewItem("Items", 0)
  747. sqlComm.CommandText = SQL
  748. sqlReader = sqlComm.ExecuteReader()
  749. If sqlReader.HasRows Then
  750. Do While sqlReader.Read()
  751. 'need to do sth here
  752. tmpString += sqlReader.Item("username") & vbCrLf
  753. Loop
  754. End If
  755. _report_status = "แตะเครื่องเข้างาน"
  756. _report_time = reportInA
  757. sendMailReport()
  758. sqlReader.Close()
  759. sqlconn.Close()
  760. Timer1.Enabled = True
  761. ElseIf (12 = now.Hour) And (25 = now.Minute) Then
  762. Syncing()
  763. Timer1.Enabled = False
  764. 'reportin B
  765. 'select id from shift_b where io_mode=1
  766. sqlconn.Open()
  767. sqlComm.Connection = sqlconn
  768. Dim SQL As String
  769. SQL = "select u.firstname as username " & _
  770. "from shift_b, user u " & _
  771. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  772. Dim lvItem As New ListViewItem("Items", 0)
  773. sqlComm.CommandText = SQL
  774. sqlReader = sqlComm.ExecuteReader()
  775. If sqlReader.HasRows Then
  776. Do While sqlReader.Read()
  777. 'need to do sth here
  778. tmpString += sqlReader.Item("username") & vbCrLf
  779. Loop
  780. End If
  781. _report_status = "แตะเครื่องเข้างาน"
  782. _report_time = reportInB
  783. sendMailReport()
  784. sqlReader.Close()
  785. sqlconn.Close()
  786. Timer1.Enabled = True
  787. ElseIf (23 = now.Hour) And (25 = now.Minute) Then
  788. Syncing()
  789. Timer1.Enabled = False
  790. 'reportin C
  791. 'select id from shift_c where io_mode=1
  792. sqlconn.Open()
  793. sqlComm.Connection = sqlconn
  794. Dim SQL As String
  795. SQL = "select u.firstname as username " & _
  796. "from shift_c s, user u " & _
  797. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  798. Dim lvItem As New ListViewItem("Items", 0)
  799. sqlComm.CommandText = SQL
  800. sqlReader = sqlComm.ExecuteReader()
  801. If sqlReader.HasRows Then
  802. Do While sqlReader.Read()
  803. 'need to do sth here
  804. tmpString += sqlReader.Item("username") & vbCrLf
  805. Loop
  806. End If
  807. _report_status = "แตะเครื่องเข้างาน"
  808. _report_time = reportInC
  809. sendMailReport()
  810. sqlReader.Close()
  811. sqlconn.Close()
  812. Timer1.Enabled = True
  813. '--------------------------------------------------------
  814. 'reportout; report to head who didn't tap out
  815. ElseIf (16 = now.Hour) And (35 = now.Minute) Then
  816. Syncing()
  817. Timer1.Enabled = False
  818. 'reportout A
  819. 'select id from shift_a where flag=0
  820. sqlconn.Open()
  821. sqlComm.Connection = sqlconn
  822. Dim SQL As String
  823. SQL = "select u.firstname as username, u.mobile as mobile " & _
  824. "from shift_a s, user u " & _
  825. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE())"
  826. Dim lvItem As New ListViewItem("Items", 0)
  827. sqlComm.CommandText = SQL
  828. sqlReader = sqlComm.ExecuteReader()
  829. If sqlReader.HasRows Then
  830. Do While sqlReader.Read()
  831. 'need to do sth here
  832. tmpString += sqlReader.Item("username") & "เบอร์โทร " & sqlReader.Item("mobile") & vbCrLf
  833. Loop
  834. End If
  835. _report_status = "ยังไม่แตะเครื่องออกจากงาน"
  836. _report_time = reportOutA
  837. sendMailReport()
  838. sqlReader.Close()
  839. sqlconn.Close()
  840. Timer1.Enabled = True
  841. ElseIf (0 = now.Hour) And (35 = now.Minute) Then
  842. Syncing()
  843. Timer1.Enabled = False
  844. 'reportout B
  845. 'select id from shift_b where flag=0
  846. sqlconn.Open()
  847. sqlComm.Connection = sqlconn
  848. Dim SQL As String
  849. SQL = "select u.firstname as username " & _
  850. "from shift_b s, user u " & _
  851. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE()) OR DATE(`datetime`) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
  852. Dim lvItem As New ListViewItem("Items", 0)
  853. sqlComm.CommandText = SQL
  854. sqlReader = sqlComm.ExecuteReader()
  855. If sqlReader.HasRows Then
  856. Do While sqlReader.Read()
  857. 'need to do sth here
  858. tmpString += sqlReader.Item("username") & "เบอร์โทร " & sqlReader.Item("mobile") & vbCrLf
  859. Loop
  860. End If
  861. _report_status = "ยังไม่แตะเครื่องออกจากงาน"
  862. _report_time = reportOutB
  863. sendMailReport()
  864. sqlReader.Close()
  865. sqlconn.Close()
  866. Timer1.Enabled = True
  867.  
  868. ElseIf (8 = now.Hour) And (35 = now.Minute) Then
  869. Syncing()
  870. Timer1.Enabled = False
  871. 'reportout C
  872. 'select id from shift_c where flag=0
  873. sqlconn.Open()
  874. sqlComm.Connection = sqlconn
  875. Dim SQL As String
  876. SQL = "select u.firstname as username " & _
  877. "from shift_c s, user u " & _
  878. "where s.flag=0 and s.id_om=u.id_om and DATE(`datetime`) = DATE(CURDATE()) OR DATE(`datetime`) = DATE_ADD(CURDATE(), INTERVAL -1 DAY)"
  879. Dim lvItem As New ListViewItem("Items", 0)
  880. sqlComm.CommandText = SQL
  881. sqlReader = sqlComm.ExecuteReader()
  882. If sqlReader.HasRows Then
  883. Do While sqlReader.Read()
  884. 'need to do sth here
  885. tmpString += sqlReader.Item("username") & "เบอร์โทร " & sqlReader.Item("mobile") & vbCrLf
  886. Loop
  887. End If
  888. _report_status = "ยังไม่แตะเครื่องออกจากงาน"
  889. _report_time = reportOutC
  890. sendMailReport()
  891. sqlReader.Close()
  892. sqlconn.Close()
  893. Timer1.Enabled = True
  894. End If
  895. End Sub
  896. 'need polishing mail body
  897. Private Sub sendMailNoti()
  898. Dim oMail As New SmtpMail("TryIt")
  899. Dim oSmtp As New SmtpClient()
  900. ' Your office 365 email address
  901. oMail.From = "opc-ta@g-able.com"
  902. ' Set recipient email address, please change it to yours
  903. oMail.To = _mailTo
  904.  
  905. ' Set email subject
  906. oMail.Subject = "แจ้งเตือน : '" & _userid & "' '" & _username & "' กรุณาแตะลายนิ้วมือที่เครื่องเพื่อออกงาน เวลา '" & _shift_out & "' ."
  907.  
  908. ' Set email body
  909. oMail.TextBody = "แจ้งเตือน : '" & _userid & "' '" & _username & "' กรุณาแตะลายนิ้วมือที่เครื่องเพื่อออกงาน เวลา '" & _shift_out & "' ." & _
  910. " '" & vbCrLf & "' โปรแกรมแจ้งเตือนอัตโนมัติ"
  911.  
  912. ' Your SMTP server address,
  913. Dim oServer As New SmtpServer("mail.g-able.com")
  914.  
  915. oServer.Protocol = ServerProtocol.ExchangeEWS
  916.  
  917. ' user authentication should use your
  918. ' email address as the user name.
  919. oServer.User = "opc-ta@g-able.com"
  920. oServer.Password = "opc-t@99"
  921.  
  922. ' detect SSL/TLS connection automatically
  923. oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
  924. Try
  925. lblstatus.Text = "start to send email over SSL ..."
  926. oSmtp.SendMail(oServer, oMail)
  927. lblstatus.Text = "email was sent successfully!"
  928. Catch ep As Exception
  929. MsgBox("failed to send email with the following error:")
  930. MsgBox(ep.Message)
  931. End Try
  932.  
  933. End Sub
  934. Private Sub sendMailReport()
  935. Dim oMail As New SmtpMail("TryIt")
  936. Dim oSmtp As New SmtpClient()
  937. ' Your office 365 email address
  938. oMail.From = "opc-ta@g-able.com"
  939. ' Set recipient email address, please change it to yours
  940. oMail.To = ""
  941. oMail.To.Add("parawee.t@g-able.com")
  942. oMail.To.Add("pornlert.w@g-able.com")
  943. oMail.To.Add("anek.r@g-able.com")
  944. oMail.To.Add("phorntip.m@g-able.com")
  945.  
  946. ' Set email subject
  947. oMail.Subject = "อีเมลรายงานสรุป : รายชื่อพนักงานที่ '" & _report_status & "' เวลา '" & _report_time & "' ."
  948.  
  949. ' Set email body
  950. oMail.TextBody = "รายชื่อ '" & vbCrLf & "' '" & tmpString & "' " & _
  951. "โปรแกรมแจ้งเตือนอัตโนมัติ"
  952.  
  953. ' Your SMTP server address,
  954. Dim oServer As New SmtpServer("mail.g-able.com")
  955.  
  956. oServer.Protocol = ServerProtocol.ExchangeEWS
  957.  
  958. ' user authentication should use your
  959. ' email address as the user name.
  960. oServer.User = "opc-ta@g-able.com"
  961. oServer.Password = "opc-t@99"
  962.  
  963. ' detect SSL/TLS connection automatically
  964. oServer.ConnectType = SmtpConnectType.ConnectSSLAuto
  965. Try
  966. lblstatus.Text = "start to send email over SSL ..."
  967. oSmtp.SendMail(oServer, oMail)
  968. lblstatus.Text = "email was sent successfully!"
  969. Catch ep As Exception
  970. MsgBox("failed to send email with the following error:")
  971. MsgBox(ep.Message)
  972. End Try
  973. tmpString = ""
  974. End Sub
  975.  
  976. #End Region
  977. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement