product_template.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 'discount': 0,
  40. 'categoryId': p.categ_id.id or None,
  41. 'variants': [{
  42. 'id': v.id,
  43. 'name': v.display_name,
  44. 'ean13': v.ean13,
  45. 'defaultCode': p.default_code,
  46. 'image': v.image_small if image_type == 'small' else v.image_medium,
  47. 'listPrice': v.list_price,
  48. 'standardPrice': v.standard_price,
  49. 'quantity': 1,
  50. 'price': v.list_price,
  51. 'discount': 0,
  52. 'pricelistId': v.pricelist_id.id or None,
  53. 'locations': in_locations.get(v.id, []),
  54. 'categoryId': v.categ_id.id or None,
  55. 'tmplId': p.id or None,
  56. 'tmplCategoryId': p.categ_id.id or None
  57. } for v in p.product_variant_ids]
  58. } for p in r.env[_MODEL].search(domain)
  59. ]
  60. def create_product(**kw):
  61. product = r.env[_MODEL].create({
  62. 'name': kw.get('name'),
  63. 'listPrice': float(kw.get('price')),
  64. 'ean13': kw.get('ean13')
  65. })
  66. return {
  67. 'id': product.id,
  68. 'name': product.display_name,
  69. 'ean13': product.ean13,
  70. 'image': product.image_small,
  71. 'listPrice': product.list_price,
  72. 'variantCount': product.product_variant_count,
  73. 'quantity': 1,
  74. 'price': product.list_price,
  75. 'discount': 0,
  76. 'variants': [{
  77. 'id': variant.id,
  78. 'name': variant.display_name,
  79. 'ean13': variant.ean13,
  80. 'image': variant.image_small,
  81. 'listPrice': variant.list_price,
  82. 'quantity': 1,
  83. 'price': variant.list_price,
  84. 'discount': 0,
  85. } for variant in product.product_variant_ids if variant.active]
  86. }