Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import os
- import sys
- import datetime
- import time
- conn = psycopg2.connect('dbname=cigarro host=10.40.140.25 user=postgres password=postgres')
- cur = conn.cursor()
- shadowconn = psycopg2.connect('dbname=cigarro host=10.40.140.25 user=postgres password=postgres')
- shadowcur = shadowconn.cursor()
- connmatriz = psycopg2.connect('dbname=matrizpitstop host=10.40.140.25 user=postgres password=postgres')
- curmatriz = connmatriz.cursor()
- def proximo_turno():
- cur_time = datetime.datetime.now().time()
- if cur_time < datetime.time(6):
- return datetime.time(6)
- else:
- if cur_time < datetime.time(14):
- return datetime.time(14)
- else:
- if cur_time < datetime.time(18):
- return datetime.time(18)
- else:
- return datetime.time(23, 59)
- while True:
- try:
- next_turn = proximo_turno()
- print('Proximo turno %s' % next_turn)
- while datetime.datetime.now().time() < next_turn:
- time.sleep(2)
- cur.execute("SELECT produto, (select empresa from estoque where id = estoque), "
- " quant, last_turn, (select nome from estoque where id = estoque), "
- " estoque FROM saldos WHERE estoque > 99")
- filebuffer = '(%s) Contagem de cigarro.txt' % datetime.datetime.now().strftime("%Y-%m-%d %H-%M-%S")
- relbuffer = open(filebuffer, 'w')
- for rec in cur:
- qry = "SELECT sum(quantidade), sum(valor), (select nome from produto where grid = produto) FROM lancto " \
- " WHERE empresa = %d and produto = " \
- " (select grid from produto where codigo_barra = '%s') " \
- " and hora >= '%s' " \
- " and operacao in ('C', 'V', 'DF') " \
- " GROUP BY produto" % \
- (rec[1], rec[0], rec[3])
- curmatriz.execute(qry)
- info = curmatriz.fetchone()
- quantidade = rec[2]
- saldo = quantidade
- venda = 0
- if info is not None:
- venda = info[0]
- saldo = quantidade - venda
- buf = "[(%s) %s] %s\tSaldo anterior = %d\tVenda = %d\tSaldo = %d\n" % (rec[4], rec[0], info[2].ljust(60), quantidade, venda, saldo)
- relbuffer.write(buf)
- print(buf)
- else:
- curmatriz.execute("select nome from produto where codigo_barra = '%s' " % rec[0])
- zbuf = curmatriz.fetchone()
- buf = "[(%s) %s] %s\tSaldo anterior = %d\tVenda = %d\tSaldo = %d\n" % (rec[4], rec[0], zbuf[0].ljust(60), quantidade, venda, saldo)
- relbuffer.write(buf)
- print(buf)
- shadowcur.execute("UPDATE saldos SET last_turn = current_timestamp, "
- " last_modified = current_timestamp, "
- " quant = %d "
- "WHERE produto = '%s' and estoque = %d " % (saldo, rec[0], rec[5]))
- shadowconn.commit()
- relbuffer.flush()
- os.startfile(filebuffer, "print")
- except KeyboardInterrupt:
- break
- except:
- print(sys.exc_info()[0])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement