product_template.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_products(type=None, location_ids=[]):
  4. product_obj = r.env['product.template']
  5. domain = [
  6. ('standard_price', '>=', 0),
  7. ('active', '=', True)
  8. ]
  9. if type == 'purchase':
  10. domain += [('purchase_ok', '=', True)]
  11. else:
  12. if product_obj.fields_get('hr_expense_ok'):
  13. domain += [('hr_expense_ok', '=', True)]
  14. else:
  15. return []
  16. in_locations = {}
  17. if len(location_ids) > 0:
  18. for q in r.env['stock.quant'].search([('location_id', 'in', location_ids)], order='product_id'):
  19. entry = in_locations.get(q.product_id.id, None)
  20. if entry:
  21. if q.location_id.id not in map(lambda x: x.get('id'), entry):
  22. entry.append({
  23. 'id': q.location_id.id,
  24. 'name': q.location_id.display_name
  25. })
  26. continue
  27. in_locations[q.product_id.id] = [
  28. {
  29. 'id': q.location_id.id,
  30. 'name': q.location_id.display_name
  31. }
  32. ]
  33. return [
  34. {
  35. 'id': p.id,
  36. 'name': p.name,
  37. 'displayName': p.display_name,
  38. 'ean13': p.ean13,
  39. 'image': p.image_medium,
  40. 'standardPrice': p.standard_price,
  41. 'variantCount': p.product_variant_count,
  42. 'quantity': 1,
  43. 'price': p.standard_price,
  44. 'minimumPrice': 0,
  45. 'maximumPrice': 0,
  46. 'variants': [{
  47. 'id': v.id,
  48. 'name': v.name,
  49. 'displayName': v.display_name,
  50. 'ean13': v.ean13,
  51. 'image': v.image_medium,
  52. 'standardPrice': v.standard_price,
  53. 'quantity': 1,
  54. 'price': v.standard_price,
  55. 'minimumPrice': 0,
  56. 'maximumPrice': 0,
  57. 'locations': in_locations.get(v.id, [])
  58. } for v in p.product_variant_ids if v.active]
  59. } for p in product_obj.search(domain)
  60. ]
  61. def create_product(values):
  62. p = r.env['product.template'].create({
  63. 'name': values.get('name'),
  64. 'standard_price': float(values.get('price')),
  65. 'ean13': values.get('ean13')
  66. })
  67. return {
  68. 'id': p.id,
  69. 'name': p.name,
  70. 'displayName': p.display_name,
  71. 'ean13': p.ean13,
  72. 'imageMedium': p.image_medium,
  73. 'standardPrice': p.standard_price,
  74. 'variantCount': p.product_variant_count,
  75. 'price': p.list_price,
  76. 'minimumPrice': p.minimum_price,
  77. 'maximumPrice': p.maximum_price,
  78. 'variants': [{
  79. 'id': v.id,
  80. 'name': v.name,
  81. 'displayName': v.display_name,
  82. 'ean13': v.ean13,
  83. 'imageMedium': v.image_medium,
  84. 'standardPrice': p.standard_price,
  85. 'price': v.standard_price,
  86. 'minimumPrice': p.minimum_price,
  87. 'maximumPrice': p.maximum_price
  88. } for v in p.product_variant_ids if v.active]
  89. }