pos_order_line.py 8.0 KB

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