Advertisement
webmanix

Untitled

Sep 21st, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. import psycopg2
  2. import datetime
  3. import sys
  4.  
  5. conn = psycopg2.connect('dbname=cigarro host=10.40.140.25 user=postgres password=postgres')
  6. cur = conn.cursor()
  7.  
  8. connmatriz = psycopg2.connect('dbname=matrizpitstop host=10.40.140.25 user=postgres password=postgres')
  9. curmatriz = connmatriz.cursor()
  10.  
  11. cur.execute('select * from estoque')
  12.  
  13. for rec in cur:
  14. print(rec)
  15.  
  16. estoque = raw_input("estoque=")
  17.  
  18. convertcache = {}
  19.  
  20. cur.execute('SELECT base, target, factor FROM convert')
  21.  
  22. for rec in cur:
  23. convertcache[rec[0]] = [rec[1], rec[2]]
  24.  
  25. cigcache = {}
  26. contagemcache = {}
  27.  
  28. try:
  29. while True:
  30. codigo = raw_input('Codigo=')
  31. factor = 1
  32.  
  33. if convertcache.get(codigo, None) is not None:
  34. shite = convertcache[codigo]
  35. codigo = shite[0]
  36. factor = shite[1]
  37.  
  38. if cigcache.get(codigo, None) is None:
  39. curmatriz.execute("SELECT nome FROM produto WHERE codigo_barra = '%s' " % codigo)
  40.  
  41. buf = curmatriz.fetchone()
  42.  
  43. if buf is None:
  44. # print('Cigarro nao encontrado')
  45.  
  46. answr = raw_input('Cadastrar caixa?\n')
  47.  
  48. if answr in ('S', 's'):
  49. codigo_un = raw_input('Codigo da unidade=')
  50.  
  51. factor_caixa = raw_input('Quantidade na caixa=')
  52.  
  53. try:
  54. cur.execute("insert into convert (base, target, factor) values ('%s', '%s', %d)"
  55. % (codigo, codigo_un, int(factor_caixa)))
  56.  
  57. conn.commit()
  58. print('Cadastrado com sucesso')
  59.  
  60. convertcache[codigo] = [codigo_un, int(factor_caixa)]
  61. factor = int(factor_caixa)
  62. codigo = codigo_un
  63.  
  64. curmatriz.execute("SELECT nome FROM produto WHERE codigo_barra = '%s' " % codigo)
  65. buf = curmatriz.fetchone()
  66. cigcache[codigo] = buf[0]
  67.  
  68. except:
  69. print('Cadastro falhou')
  70. else:
  71. cigcache[codigo] = 'Unknown'
  72. else:
  73. cigcache[codigo] = buf[0]
  74.  
  75. if contagemcache.get(codigo, None) is None:
  76. contagemcache[codigo] = 0
  77.  
  78. contagemcache[codigo] += factor
  79. index = 0
  80.  
  81. for rec in contagemcache.items():
  82. print(str(index) + ' - ' + cigcache[rec[0]] + '=' + str(rec[1]))
  83. index += 1
  84.  
  85. print('-----------')
  86. except:
  87. print(sys.exc_info())
  88.  
  89. answ = raw_input('\n\nSalvar?\n')
  90.  
  91. if answ in ('s', 'S', 'y', 'Y'):
  92.  
  93. cur.execute('SELECT produto FROM saldos WHERE estoque = %d' % (int(estoque)))
  94.  
  95. existingprod = []
  96.  
  97. for rec in cur:
  98. existingprod.append(rec[0])
  99.  
  100. for rec in contagemcache.items():
  101. # qrytext = "INSERT INTO mov (data, tipo, estoque_to, quantidade, produto) VALUES " \
  102. # "('%s', 'E', %d, %d, '%s')" % (
  103. # datetime.date.today(), int(estoque), int(rec[1]), rec[0])
  104. #
  105. # try:
  106. # cur.execute(qrytext)
  107. # except:
  108. # print('Nao foi possivel atualizar %s' + cigcache[rec[0]])
  109.  
  110. if rec[0] in existingprod:
  111. qrytext = "UPDATE saldos SET quant = %d, last_modified = current_date WHERE " \
  112. " estoque = %d and produto = '%s'; " % (rec[1], int(estoque), rec[0])
  113. else:
  114. qrytext = "insert into saldos (estoque, produto, quant, last_modified) values " \
  115. " (%d, '%s', %d, current_date) " \
  116. % (int(estoque), rec[0], rec[1])
  117.  
  118. try:
  119. print(qrytext)
  120.  
  121. cur.execute(qrytext)
  122. conn.commit()
  123.  
  124. print('OK\n----------------')
  125. except:
  126. print('Nao foi possivel atualizar ' + cigcache[rec[0]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement