Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import datetime
- import sys
- conn = psycopg2.connect('dbname=cigarro host=10.40.140.25 user=postgres password=postgres')
- cur = conn.cursor()
- connmatriz = psycopg2.connect('dbname=matrizpitstop host=10.40.140.25 user=postgres password=postgres')
- curmatriz = connmatriz.cursor()
- cur.execute('select * from estoque')
- for rec in cur:
- print(rec)
- estoque = raw_input("estoque=")
- convertcache = {}
- cur.execute('SELECT base, target, factor FROM convert')
- for rec in cur:
- convertcache[rec[0]] = [rec[1], rec[2]]
- cigcache = {}
- contagemcache = {}
- try:
- while True:
- codigo = raw_input('Codigo=')
- factor = 1
- if convertcache.get(codigo, None) is not None:
- shite = convertcache[codigo]
- codigo = shite[0]
- factor = shite[1]
- if cigcache.get(codigo, None) is None:
- curmatriz.execute("SELECT nome FROM produto WHERE codigo_barra = '%s' " % codigo)
- buf = curmatriz.fetchone()
- if buf is None:
- # print('Cigarro nao encontrado')
- answr = raw_input('Cadastrar caixa?\n')
- if answr in ('S', 's'):
- codigo_un = raw_input('Codigo da unidade=')
- factor_caixa = raw_input('Quantidade na caixa=')
- try:
- cur.execute("insert into convert (base, target, factor) values ('%s', '%s', %d)"
- % (codigo, codigo_un, int(factor_caixa)))
- conn.commit()
- print('Cadastrado com sucesso')
- convertcache[codigo] = [codigo_un, int(factor_caixa)]
- factor = int(factor_caixa)
- codigo = codigo_un
- curmatriz.execute("SELECT nome FROM produto WHERE codigo_barra = '%s' " % codigo)
- buf = curmatriz.fetchone()
- cigcache[codigo] = buf[0]
- except:
- print('Cadastro falhou')
- else:
- cigcache[codigo] = 'Unknown'
- else:
- cigcache[codigo] = buf[0]
- if contagemcache.get(codigo, None) is None:
- contagemcache[codigo] = 0
- contagemcache[codigo] += factor
- index = 0
- for rec in contagemcache.items():
- print(str(index) + ' - ' + cigcache[rec[0]] + '=' + str(rec[1]))
- index += 1
- print('-----------')
- except:
- print(sys.exc_info())
- answ = raw_input('\n\nSalvar?\n')
- if answ in ('s', 'S', 'y', 'Y'):
- cur.execute('SELECT produto FROM saldos WHERE estoque = %d' % (int(estoque)))
- existingprod = []
- for rec in cur:
- existingprod.append(rec[0])
- for rec in contagemcache.items():
- # qrytext = "INSERT INTO mov (data, tipo, estoque_to, quantidade, produto) VALUES " \
- # "('%s', 'E', %d, %d, '%s')" % (
- # datetime.date.today(), int(estoque), int(rec[1]), rec[0])
- #
- # try:
- # cur.execute(qrytext)
- # except:
- # print('Nao foi possivel atualizar %s' + cigcache[rec[0]])
- if rec[0] in existingprod:
- qrytext = "UPDATE saldos SET quant = %d, last_modified = current_date WHERE " \
- " estoque = %d and produto = '%s'; " % (rec[1], int(estoque), rec[0])
- else:
- qrytext = "insert into saldos (estoque, produto, quant, last_modified) values " \
- " (%d, '%s', %d, current_date) " \
- % (int(estoque), rec[0], rec[1])
- try:
- print(qrytext)
- cur.execute(qrytext)
- conn.commit()
- print('OK\n----------------')
- except:
- print('Nao foi possivel atualizar ' + cigcache[rec[0]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement