Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.75 KB | None | 0 0
  1. import psycopg2, sys, psycopg2.extras, time
  2.  
  3. order = 4419
  4.  
  5. try:
  6. con = psycopg2.connect(host='localhost', database='DB01', user='odoo', password='******')
  7. cur = con.cursor()
  8. po_lines = 'SELECT pos_order_line.id FROM public.pos_order_line, public.product_template '
  9. 'WHERE pos_order_line.product_id = product_template.id AND pos_order_line.order_id = %s '
  10. 'AND (product_template.pos_categ_id != 5 AND product_template.pos_categ_id != 6)'
  11. 'ORDER BY pos_order_line.id ASC'
  12. po_lines2 = 'SELECT pos_order_line.id, pos_order_line.order_id, product_template.name, pos_order_line.qty, product_template.pos_categ_id '
  13. 'FROM public.pos_order_line, public.product_template '
  14. 'WHERE pos_order_line.product_id = product_template.id AND pos_order_line.id = %s '
  15. 'ORDER BY pos_order_line.id ASC'
  16.  
  17. cur.execute(po_lines,[order]); fetch_lines = cur.fetchall()
  18. dish = ''; instr = []; kot = 0; dp = 0
  19. print fetch_lines
  20. for line in fetch_lines:
  21. cur.execute(po_lines2, [line]); pos_lines = cur.fetchone()
  22. if pos_lines[2].startswith('#'):
  23. instr.insert(1, pos_lines[2][2:]); kot = 1
  24. elif pos_lines[2].startswith('----'):
  25. dp = 1
  26. else:
  27. dish = pos_lines[2]
  28. kot = 0; instr = []
  29. if dp == 1:
  30. instr.insert(0, '!SERVIR DEPOIS!'); dp = 0
  31. if dish != pos_lines[2]:
  32. print 'Ordem: ', order, ' - Prato:', dish, ' - Instr:', instr, 'qt: ', pos_lines[3],'kot: ', kot, 'dp status:', dp
  33.  
  34. except psycopg2.DatabaseError, e:
  35. print 'Error %s' % e
  36. sys.exit(1)
  37.  
  38. finally:
  39. if con:
  40. con.close()
  41.  
  42. ID ORDER PRODUCT QTY CAT
  43. 12811 4419 "Crudo GR" 1.0 1
  44. 12812 4419 "Salame e Grana GR" 1.0 1
  45. 12813 4419 "---- servir depois ----" 1.0 7
  46. 12814 4419 "Nutella Ban GR" 1.0 3
  47. 12815 4419 "# Cortar em dois" 1.0 7
  48.  
  49. ID ORDER PRODUCT INSTR QTY
  50. 12811 4419 "Crudo GR" 1.0
  51. 12812 4419 "Salame e Grana GR" 1.0
  52. 12814 4419 "Nutella Ban GR" "!SERVIR DEPOIS! Cortar em dois" 1.0
  53.  
  54. po_lines = '''SELECT pos_order_line.id, pos_order_line.order_id, product_template.name, pos_order_line.qty, product_template.pos_categ_id
  55. FROM public.pos_order_line, public.product_template
  56. WHERE pos_order_line.product_id = product_template.id AND pos_order_line.order_id = %s
  57. AND (product_template.pos_categ_id != 5 AND product_template.pos_categ_id != 6)
  58. ORDER BY pos_order_line.id DESC'''
  59.  
  60. cur.execute(po_lines,[order]); fetch_lines = cur.fetchall()
  61. instr = ''; newTable = []
  62.  
  63. for i, line in enumerate(fetch_lines):
  64. if line[2].startswith('#') or line[2].startswith('----'):
  65. if line[2].startswith('#'):
  66. instr = instr + line[2][2:]
  67. if line[2].startswith('----'):
  68. line_in = fetch_lines[i-1]
  69. extract_line = tuple([item[3] for item in newTable if line_in[0] in item])
  70. newTable = [t for t in newTable if t[0] != line_in[0]]
  71. instr = '!SERVIR DEPOIS!/' + extract_line[0]
  72. newTable.append((line_in[0], line_in[1], line_in[2], instr))
  73. instr = ''
  74. else:
  75. newTable.append((line[0], line[1], line[2], instr))
  76. instr = ''
  77.  
  78. for i,l in enumerate(newTable[::-1]):
  79. print i,l
  80.  
  81. 0 (12811, 4419, 'Crudo GR', '')
  82. 1 (12812, 4419, 'Salame e Grana GR', '')
  83. 2 (12814, 4419, 'Nutella Ban GR', '!SERVIR DEPOIS!/Cortar em dois')
  84.  
  85. ID ORDER PRODUCT QTY CAT
  86. 12811 4419 "Crudo GR" 1.0 1
  87. 12812 4419 "Salame e Grana GR" 1.0 1
  88. 12813 4419 "---- servir depois ----" 1.0 7
  89. 12814 4419 "Nutella Ban GR" 1.0 3
  90. 12815 4419 "# Cortar em dois" 1.0 7
  91.  
  92. newTable = []
  93. Intr = ''
  94.  
  95. #Added something like
  96. #http://www.saltycrane.com/blog/2007/12/how-to-sort-table-by-columns-in-python/
  97. #import operator
  98. #fetch_lines = sorted(fetch_lines, key=operator.itemgetter(col))
  99.  
  100. #Sort table so looks like this
  101.  
  102. # ID ORDER PRODUCT QTY CAT
  103. # 12815 4419 "# Cortar em dois" 1.0 7
  104. # 12813 4419 "---- servir depois ----" 1.0 7
  105. # 12811 4419 "Crudo GR" 1.0 1
  106. # 12812 4419 "Salame e Grana GR" 1.0 1
  107. # 12814 4419 "Nutella Ban GR" 1.0 3
  108.  
  109.  
  110. for i,line in emnumerate(fetch_lines):
  111. if line[2].startswith('#') or line[2].startswith('----'):
  112. # Within this if statement you can make adjustment to text item
  113. if line[2].startswith('#')
  114. Intr = Intr + " Cortar em dois"
  115. if line[2].startswith('----')
  116. Intr = '!SERVIR DEPOIS!' + Intr
  117. elif i == len(fetch_lines) -1:
  118. newTable.append([line[0], ....., Intr , ...])
  119. elif i < len(fetch_lines)
  120. newTable.append([line[0], ....., '', ...])
  121. print table
  122.  
  123.  
  124. #Then sort by first column so table look right
  125. #table = sorted( newTable, key=operator.itemgetter(col))
  126.  
  127. #ID ORDER PRODUCT INSTR QTY
  128. #12811 4419 "Crudo GR" 1.0
  129. #12812 4419 "Salame e Grana GR" 1.0
  130. #12814 4419 "Nutella Ban GR" "!SERVIR DEPOIS! Cortar em dois" 1.0
  131.  
  132. po_lines = '''SELECT pos_order_line.id, pos_order_line.order_id, product_template.name, pos_order_line.qty, product_template.pos_categ_id
  133. FROM public.pos_order_line, public.product_template
  134. WHERE pos_order_line.product_id = product_template.id AND pos_order_line.order_id = %s
  135. AND (product_template.pos_categ_id != 5 AND product_template.pos_categ_id != 6)
  136. ORDER BY pos_order_line.id DESC'''
  137.  
  138. cur.execute(po_lines,[order]); fetch_lines = cur.fetchall()
  139. instr = ''; newTable = []
  140.  
  141. for i, line in enumerate(fetch_lines):
  142. if line[2].startswith('#') or line[2].startswith('----'):
  143. if line[2].startswith('#'):
  144. instr = instr + line[2][2:]
  145. if line[2].startswith('----'):
  146. line_in = fetch_lines[i-1]
  147. extract_line = tuple([item[3] for item in newTable if line_in[0] in item])
  148. newTable = [t for t in newTable if t[0] != line_in[0]]
  149. instr = '!SERVIR DEPOIS!/' + extract_line[0]
  150. newTable.append((line_in[0], line_in[1], line_in[2], instr))
  151. instr = ''
  152. else:
  153. newTable.append((line[0], line[1], line[2], instr))
  154. instr = ''
  155.  
  156. for i,l in enumerate(newTable[::-1]):
  157. print i,l
  158.  
  159. 0 (12811, 4419, 'Crudo GR', '')
  160. 1 (12812, 4419, 'Salame e Grana GR', '')
  161. 2 (12814, 4419, 'Nutella Ban GR', '!SERVIR DEPOIS!/Cortar em dois')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement