product_template.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_products(type=None):
  4. product_obj = r.env['product.template']
  5. domain = [('standard_price', '>=', 0), ('active', '=', True)]
  6. if type == 'purchase':
  7. domain.append(('purchase_ok', '=', True))
  8. else:
  9. if product_obj.fields_get('hr_expense_ok'):
  10. domain.append(('hr_expense_ok', '=', True))
  11. else:
  12. return []
  13. return [
  14. {
  15. 'id': p.id,
  16. 'name': p.name,
  17. 'displayName': p.display_name,
  18. 'ean13': p.ean13,
  19. 'imageMedium': p.image_medium,
  20. 'standardPrice': p.standard_price,
  21. 'variantCount': p.product_variant_count,
  22. 'quantity': 1,
  23. 'price': p.standard_price,
  24. 'minimumPrice': p.minimum_price,
  25. 'maximumPrice': p.maximum_price,
  26. 'variants': [{
  27. 'id': v.id,
  28. 'name': v.name,
  29. 'displayName': v.display_name,
  30. 'ean13': v.ean13,
  31. 'imageMedium': v.image_medium,
  32. 'standardPrice': v.standard_price,
  33. 'quantity': 1,
  34. 'price': v.standard_price,
  35. 'minimumPrice': p.minimum_price,
  36. 'maximumPrice': p.maximum_price
  37. } for v in p.product_variant_ids if v.active]
  38. } for p in product_obj.search(domain)
  39. ]
  40. def create_product(self, values):
  41. p = r.env['product.template'].create({
  42. 'name': values.get('name'),
  43. 'standard_price': float(values.get('price')),
  44. 'ean13': values.get('ean13')
  45. })
  46. return {
  47. 'id': p.id,
  48. 'name': p.name,
  49. 'displayName': p.display_name,
  50. 'ean13': p.ean13,
  51. 'imageMedium': p.image_medium,
  52. 'standardPrice': p.standard_price,
  53. 'variantCount': p.product_variant_count,
  54. 'price': p.list_price,
  55. 'minimumPrice': p.minimum_price,
  56. 'maximumPrice': p.maximum_price,
  57. 'variants': [{
  58. 'id': v.id,
  59. 'name': v.name,
  60. 'displayName': v.display_name,
  61. 'ean13': v.ean13,
  62. 'imageMedium': v.image_medium,
  63. 'standardPrice': p.standard_price,
  64. 'price': v.standard_price,
  65. 'minimumPrice': p.minimum_price,
  66. 'maximumPrice': p.maximum_price
  67. } for v in p.product_variant_ids if v.active]
  68. }