pos_order_line.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_pos_order_line():
  4. user_store = r.env.user.store_id.id
  5. company_currency_rate = r.env.user.company_id.currency_id.rate
  6. validate = '''
  7. SELECT EXISTS(
  8. SELECT table_name
  9. FROM information_schema.columns
  10. WHERE table_schema='public'
  11. AND table_name='pos_order')
  12. '''
  13. validate_brand = '''
  14. SELECT EXISTS(
  15. SELECT table_name
  16. FROM information_schema.columns
  17. WHERE table_schema='public'
  18. AND table_name='product_brand')
  19. '''
  20. query_with_brand = '''
  21. SELECT
  22. pos.id,
  23. line.id,
  24. pos.name,
  25. CASE
  26. WHEN product.default_code IS NOT NULL
  27. THEN ('[' || product.default_code || '] ' || product.name_template)
  28. ELSE product.name_template
  29. END AS display_name,
  30. line.price_unit,
  31. line.qty,
  32. line.price_subtotal,
  33. line.price_subtotal_incl - line.price_subtotal AS impuestos,
  34. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  35. history.product_template_id,
  36. journal.store_id,
  37. pos.date_order,
  38. pos.company_id,
  39. pos.sale_journal,
  40. template.categ_id,
  41. (array_agg(attr_rel.att_id)) AS attr_rel,
  42. (array_agg(attr_value.name)) AS attr_value,
  43. (array_agg(attr.id)) AS attr,
  44. template.product_brand_id,
  45. brand.name,
  46. product.id,
  47. customer.name,
  48. customer.id,
  49. line.discount,
  50. pos.user_id,
  51. user_partner.name
  52. FROM pos_order AS pos
  53. LEFT JOIN pos_order_line AS line
  54. ON pos.id = line.order_id
  55. LEFT JOIN res_store_journal_rel as journal
  56. ON journal.journal_id = pos.sale_journal
  57. LEFT JOIN product_product as product
  58. ON product.id = line.product_id
  59. LEFT JOIN product_template as template
  60. ON template.id = product.product_tmpl_id
  61. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  62. ON attr_rel.prod_id = product.id
  63. LEFT JOIN product_attribute_value AS attr_value
  64. ON attr_value.id = attr_rel.att_id
  65. LEFT JOIN product_attribute AS attr
  66. ON attr.id = attr_value.attribute_id
  67. LEFT JOIN product_price_history AS history
  68. ON history.product_template_id = product.product_tmpl_id
  69. LEFT JOIN product_brand AS brand
  70. ON brand.id = template.product_brand_id
  71. LEFT JOIN res_partner AS customer
  72. ON customer.id = pos.partner_id
  73. LEFT JOIN res_users AS users
  74. ON users.id = pos.user_id
  75. LEFT JOIN res_partner AS user_partner
  76. ON user_partner.id = users.partner_id
  77. WHERE pos.state NOT IN ('draft')
  78. GROUP BY
  79. pos.id,
  80. line.id,
  81. pos.name,
  82. product.name_template,
  83. line.price_unit,
  84. line.qty,
  85. line.price_subtotal,
  86. history.product_template_id,
  87. journal.store_id,
  88. pos.date_order,
  89. pos.company_id,
  90. pos.sale_journal,
  91. template.categ_id,
  92. product.default_code,
  93. template.product_brand_id,
  94. brand.name,
  95. product.id,
  96. customer.name,
  97. customer.id,
  98. user_partner.name
  99. '''
  100. query_without_brand = '''
  101. SELECT
  102. pos.id,
  103. line.id,
  104. pos.name,
  105. CASE
  106. WHEN product.default_code IS NOT NULL
  107. THEN ('[' || product.default_code || '] ' || product.name_template)
  108. ELSE product.name_template
  109. END AS display_name,
  110. line.price_unit,
  111. line.qty,
  112. line.price_subtotal,
  113. line.price_subtotal_incl - line.price_subtotal AS impuestos,
  114. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  115. history.product_template_id,
  116. journal.store_id,
  117. pos.date_order,
  118. pos.company_id,
  119. pos.sale_journal,
  120. template.categ_id,
  121. (array_agg(attr_rel.att_id)) AS attr_rel,
  122. (array_agg(attr_value.name)) AS attr_value,
  123. (array_agg(attr.id)) AS attr,
  124. product.id,
  125. customer.name,
  126. customer.id,
  127. line.discount,
  128. pos.user_id,
  129. user_partner.name
  130. FROM pos_order AS pos
  131. LEFT JOIN pos_order_line AS line
  132. ON pos.id = line.order_id
  133. LEFT JOIN res_store_journal_rel as journal
  134. ON journal.journal_id = pos.sale_journal
  135. LEFT JOIN product_product as product
  136. ON product.id = line.product_id
  137. LEFT JOIN product_template as template
  138. ON template.id = product.product_tmpl_id
  139. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  140. ON attr_rel.prod_id = product.id
  141. LEFT JOIN product_attribute_value AS attr_value
  142. ON attr_value.id = attr_rel.att_id
  143. LEFT JOIN product_attribute AS attr
  144. ON attr.id = attr_value.attribute_id
  145. LEFT JOIN product_price_history AS history
  146. ON history.product_template_id = product.product_tmpl_id
  147. LEFT JOIN res_partner AS customer
  148. ON customer.id = pos.partner_id
  149. LEFT JOIN res_users AS users
  150. ON users.id = pos.user_id
  151. LEFT JOIN res_partner AS user_partner
  152. ON user_partner.id = users.partner_id
  153. WHERE pos.state NOT IN ('draft')
  154. GROUP BY
  155. pos.id,
  156. line.id,
  157. pos.name,
  158. product.name_template,
  159. line.price_unit,
  160. line.qty,
  161. line.price_subtotal,
  162. history.product_template_id,
  163. journal.store_id,
  164. pos.date_order,
  165. pos.company_id,
  166. pos.sale_journal,
  167. template.categ_id,
  168. product.default_code,
  169. template.product_brand_id,
  170. product.id,
  171. customer.name,
  172. customer.id,
  173. user_partner.name
  174. '''
  175. r.cr.execute(validate)
  176. for j in r.cr.fetchall():
  177. band = j[0]
  178. if band == True:
  179. r.cr.execute(validate_brand)
  180. for j in r.cr.fetchall():
  181. brand = j[0]
  182. if brand == True:
  183. r.cr.execute(query_with_brand,(tuple([company_currency_rate,company_currency_rate])))
  184. return [
  185. {
  186. 'order_id': j[0],
  187. 'order_line_id': j[1],
  188. 'name': j[2],
  189. 'product_name':j[3],
  190. 'price_unit':j[4],
  191. 'quantity':j[5],
  192. 'subtotal':j[6],
  193. 'tax': j[7],
  194. 'cost': j[8],
  195. 'template_id': j[9],
  196. 'store_id': j[10],
  197. 'date': j[11],
  198. 'company_id': j[12],
  199. 'journal_id': j[13],
  200. 'categ_id': j[14],
  201. 'attribute_value_ids': j[15],
  202. 'attribute_values': j[16],
  203. 'attribute_ids': j[17],
  204. 'product_brand_id': j[18],
  205. 'brand_name': j[19],
  206. 'product_id': j[20],
  207. 'customer_name': j[21],
  208. 'customer_id': j[22],
  209. 'discount': j[23],
  210. 'user_id': j[24],
  211. 'user_name': j[25],
  212. } for j in r.cr.fetchall()
  213. ]
  214. else:
  215. r.cr.execute(query_without_brand,(tuple([company_currency_rate,company_currency_rate])))
  216. return [
  217. {
  218. 'order_id': j[0],
  219. 'order_line_id': j[1],
  220. 'name': j[2],
  221. 'product_name':j[3],
  222. 'price_unit':j[4],
  223. 'quantity':j[5],
  224. 'subtotal':j[6],
  225. 'tax': j[7],
  226. 'cost': j[8],
  227. 'template_id': j[9],
  228. 'store_id': j[10],
  229. 'date': j[11],
  230. 'company_id': j[12],
  231. 'journal_id': j[13],
  232. 'categ_id': j[14],
  233. 'attribute_value_ids': j[15],
  234. 'attribute_values': j[16],
  235. 'attribute_ids': j[17],
  236. 'product_id': j[18],
  237. 'customer_name': j[19],
  238. 'customer_id': j[20],
  239. 'discount': j[21],
  240. 'user_id': j[22],
  241. 'user_name': j[23],
  242. } for j in r.cr.fetchall()
  243. ]
  244. else:
  245. return []