account_invoice_line.py 10 KB

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