account_invoice_line.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_account_invoice_line():
  4. user_store = r.env.user.store_id.id
  5. company_currency_rate = r.env.user.company_id.currency_id.rate
  6. query = '''
  7. SELECT
  8. invoice.id AS invoice_id,
  9. line.id AS invoice_line_id,
  10. invoice.number,
  11. invoice.origin,
  12. invoice.date_invoice,
  13. invoice.type,
  14. product.name_template,
  15. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  16. line.quantity AS cant,
  17. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  18. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  19. (array_agg(history.cost ORDER BY history.id DESC))[1] * line.quantity as cost,
  20. template.is_interest
  21. FROM account_invoice AS invoice
  22. LEFT JOIN account_invoice_line AS line
  23. ON line.invoice_id = invoice.id
  24. LEFT JOIN product_product AS product
  25. ON line.product_id = product.id
  26. LEFT JOIN product_template AS template
  27. ON template.id = product.product_tmpl_id
  28. LEFT JOIN res_store_journal_rel AS journal
  29. ON journal.journal_id = invoice.journal_id
  30. LEFT JOIN product_price_history AS history
  31. ON history.product_template_id = product.product_tmpl_id
  32. LEFT JOIN res_currency_rate AS rate
  33. ON rate.currency_id = invoice.currency_id
  34. WHERE invoice.state NOT IN ('draft', 'cancel')
  35. AND invoice.type = 'out_invoice'
  36. GROUP BY
  37. invoice.id,
  38. line.id,
  39. invoice.number,
  40. invoice.origin,
  41. invoice.date_invoice,
  42. product.name_template,
  43. line.price_unit,
  44. line.quantity,
  45. line.price_subtotal,
  46. template.is_interest
  47. '''
  48. r.cr.execute(query,(tuple([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])))
  49. return [
  50. {
  51. 'invoice_id': j[0],
  52. 'invoice_line_id': j[1],
  53. 'number': j[2],
  54. 'origin': j[3],
  55. 'date': j[4],
  56. 'type': j[5],
  57. 'product_name':j[6],
  58. 'price_unit':j[7],
  59. 'quantity':j[8],
  60. 'subtotal':j[9],
  61. 'tax': j[10],
  62. 'cost': j[11],
  63. 'is_interest': j[12],
  64. } for j in r.cr.fetchall()
  65. ]