hooks.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Ainara Galdona - AvanzOSC
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from openerp import api, SUPERUSER_ID
  5. def load_cost_price_on_variant(cr, registry):
  6. with api.Environment.manage():
  7. env = api.Environment(cr, SUPERUSER_ID, {})
  8. for field_name in ('standard_price', 'cost_method'):
  9. template_field = env['ir.model.fields'].search(
  10. [('model', '=', 'product.template'),
  11. ('name', '=', field_name)])
  12. product_field = env['ir.model.fields'].search(
  13. [('model', '=', 'product.product'),
  14. ('name', '=', field_name)])
  15. cr.execute("""
  16. SELECT
  17. NULLIF(substring(ir_property.res_id from 17), '')::
  18. integer
  19. FROM
  20. ir_property
  21. WHERE
  22. ir_property.fields_id = %s
  23. """, (product_field.id, ))
  24. existing_ids = [x[0] for x in cr.fetchall()]
  25. sql = """
  26. INSERT INTO ir_property
  27. (name, type, value_text, value_float, value_integer,
  28. company_id, res_id, fields_id)
  29. SELECT
  30. %s,
  31. ir_property.type,
  32. ir_property.value_text,
  33. ir_property.value_float,
  34. ir_property.value_integer,
  35. ir_property.company_id,
  36. 'product.product,' || product_product.id::text,
  37. %s
  38. FROM
  39. ir_property,
  40. product_product
  41. WHERE
  42. ir_property.fields_id = %s
  43. AND
  44. NULLIF(substring(ir_property.res_id from 18), '')::
  45. integer = product_product.product_tmpl_id
  46. """
  47. if existing_ids:
  48. cr.execute(
  49. sql + "AND product_product.id NOT IN %s",
  50. (field_name, product_field.id, template_field.id,
  51. tuple(existing_ids)))
  52. else:
  53. cr.execute(
  54. sql, (field_name, product_field.id, template_field.id))