account_invoice_line.py 9.0 KB

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