account_invoice_line.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_account_invoice_line_widget():
  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] AS cost
  20. FROM account_invoice AS invoice
  21. LEFT JOIN account_invoice_line AS line
  22. ON line.invoice_id = invoice.id
  23. LEFT JOIN product_product AS product
  24. ON line.product_id = product.id
  25. LEFT JOIN res_store_journal_rel AS journal
  26. ON journal.journal_id = invoice.journal_id
  27. LEFT JOIN product_price_history AS history
  28. ON history.product_template_id = product.product_tmpl_id
  29. LEFT JOIN res_currency_rate AS rate
  30. ON rate.currency_id = invoice.currency_id
  31. WHERE invoice.state NOT IN ('draft', 'cancel')
  32. AND TO_CHAR(invoice.date_invoice,'YYYY-MM') = TO_CHAR(current_date,'YYYY-MM')
  33. AND journal.store_id = ''' + str(user_store) + '''
  34. GROUP BY
  35. invoice.id,
  36. line.id,
  37. invoice.number,
  38. invoice.origin,
  39. invoice.date_invoice,
  40. product.name_template,
  41. line.price_unit,
  42. line.quantity,
  43. line.price_subtotal
  44. '''
  45. r.cr.execute(query,(tuple([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])))
  46. return [
  47. {
  48. 'invoice_id': j[0],
  49. 'invoice_line_id': j[1],
  50. 'number': j[2],
  51. 'origin': j[3],
  52. 'date': j[4],
  53. 'type': j[5],
  54. 'product_name':j[6],
  55. 'price_unit':j[7],
  56. 'quantity':j[8],
  57. 'subtotal':j[9],
  58. 'tax': j[10],
  59. 'cost': j[11],
  60. 'history_id': [12],
  61. } for j in r.cr.fetchall()
  62. ]