Advertisement
webmanix

Untitled

Sep 25th, 2017
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import psycopg2
  2. import os
  3. import sys
  4. import datetime
  5. import time
  6.  
  7. conn = psycopg2.connect('dbname=cigarro host=10.40.140.25 user=postgres password=postgres')
  8. cur = conn.cursor()
  9.  
  10. shadowconn = psycopg2.connect('dbname=cigarro host=10.40.140.25 user=postgres password=postgres')
  11. shadowcur = shadowconn.cursor()
  12.  
  13. connmatriz = psycopg2.connect('dbname=matrizpitstop host=10.40.140.25 user=postgres password=postgres')
  14. curmatriz = connmatriz.cursor()
  15.  
  16. def proximo_turno():
  17. cur_time = datetime.datetime.now().time()
  18.  
  19. if cur_time < datetime.time(6):
  20. return datetime.time(6)
  21. else:
  22. if cur_time < datetime.time(14):
  23. return datetime.time(14)
  24. else:
  25. if cur_time < datetime.time(18):
  26. return datetime.time(18)
  27. else:
  28. return datetime.time(23, 59)
  29.  
  30. while True:
  31. try:
  32. next_turn = proximo_turno()
  33. print('Proximo turno %s' % next_turn)
  34.  
  35. while datetime.datetime.now().time() < next_turn:
  36. time.sleep(2)
  37.  
  38. cur.execute("SELECT produto, (select empresa from estoque where id = estoque), "
  39. " quant, last_turn, (select nome from estoque where id = estoque), "
  40. " estoque FROM saldos WHERE estoque > 99")
  41.  
  42.  
  43. filebuffer = '(%s) Contagem de cigarro.txt' % datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
  44. relbuffer = open(filebuffer, 'w')
  45.  
  46. for rec in cur:
  47.  
  48. qry = "SELECT sum(quantidade), sum(valor), (select nome from produto where grid = produto) FROM lancto " \
  49. " WHERE empresa = %d and produto = " \
  50. " (select grid from produto where codigo_barra = '%s') " \
  51. " and hora >= '%s' " \
  52. " and operacao in ('C', 'V', 'DF') " \
  53. " GROUP BY produto" % \
  54. (rec[1], rec[0], rec[3])
  55.  
  56. curmatriz.execute(qry)
  57. info = curmatriz.fetchone()
  58.  
  59. quantidade = rec[2]
  60. saldo = quantidade
  61. venda = 0
  62.  
  63. if info is not None:
  64. venda = info[0]
  65. saldo = quantidade - venda
  66. buf = "[(%s) %s] %s\tSaldo anterior = %d\tVenda = %d\tSaldo = %d\n" % (rec[4], rec[0], info[2].ljust(60), quantidade, venda, saldo)
  67. relbuffer.write(buf)
  68. print(buf)
  69. else:
  70. curmatriz.execute("select nome from produto where codigo_barra = '%s' " % rec[0])
  71. zbuf = curmatriz.fetchone()
  72. buf = "[(%s) %s] %s\tSaldo anterior = %d\tVenda = %d\tSaldo = %d\n" % (rec[4], rec[0], zbuf[0].ljust(60), quantidade, venda, saldo)
  73. relbuffer.write(buf)
  74. print(buf)
  75.  
  76. shadowcur.execute("UPDATE saldos SET last_turn = current_timestamp, "
  77. " last_modified = current_timestamp, "
  78. " quant = %d "
  79. "WHERE produto = '%s' and estoque = %d " % (saldo, rec[0], rec[5]))
  80.  
  81. shadowconn.commit()
  82.  
  83. relbuffer.flush()
  84. os.startfile(filebuffer, "print")
  85. except KeyboardInterrupt:
  86. break
  87. except:
  88. print(sys.exc_info()[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement