pos_order_line.py 8.3 KB

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