product_template.py 3.2 KB

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