widget_stock_valuation.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_stock_valuation():
  4. user_store = r.env.user.store_id.id
  5. query = '''
  6. SELECT
  7. location.id,
  8. quant.id,
  9. quant.qty,
  10. quant.product_id,
  11. (array_agg(history.cost ORDER BY history.id DESC))[1] * quant.qty as cost
  12. FROM stock_location AS location
  13. LEFT JOIN stock_quant AS quant
  14. ON quant.location_id = location.id
  15. LEFT JOIN product_product AS product
  16. ON product.id = quant.product_id
  17. LEFT JOIN product_price_history AS history
  18. ON history.product_template_id = product.product_tmpl_id
  19. WHERE location.usage = 'internal'
  20. AND location.active = TRUE
  21. AND product.active = TRUE
  22. AND location.store_id = ''' + str(user_store) + '''
  23. GROUP BY
  24. location.id,
  25. quant.id,
  26. quant.qty,
  27. quant.product_id
  28. '''
  29. r.cr.execute(query)
  30. data = [
  31. {
  32. 'location_id': j[0],
  33. 'quant_id': j[1],
  34. 'qty': j[2],
  35. 'product_id': j[3],
  36. 'cost': j[4],
  37. } for j in r.cr.fetchall()
  38. ]
  39. values = []
  40. valuation = 0
  41. quantity = 0
  42. for item in data:
  43. valuation += item['cost']
  44. quantity += item['qty']
  45. values.append({
  46. 'valuation': valuation,
  47. 'quantity': quantity,
  48. })
  49. return values