account_invoice_line.py 8.9 KB

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