product_template.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Pedro M. Baeza - Serv. Tecnol. Avanzados
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from openerp import api, models
  5. class ProductTemplate(models.Model):
  6. _inherit = 'product.template'
  7. @api.multi
  8. def create_variant_ids(self):
  9. """Write in the new created variants the current template cost price.
  10. """
  11. obj = self.with_context(bypass_down_write=True,
  12. bypass_template_history=True)
  13. res = super(ProductTemplate, obj).create_variant_ids()
  14. return res
  15. @api.multi
  16. def write(self, vals):
  17. """Propagate to the variants the template cost price (if modified)."""
  18. res = super(ProductTemplate, self).write(vals)
  19. if ('standard_price' in vals and
  20. not self.env.context.get('bypass_down_write')):
  21. self.mapped('product_variant_ids').with_context(
  22. bypass_template_history=True).write(
  23. {'standard_price': vals['standard_price']})
  24. if (vals.get('cost_method', False) and not self.env.context.get(
  25. 'force_not_load', False)):
  26. cost_method = vals.get('cost_method', False)
  27. products = self.filtered(
  28. lambda x: len(x.product_variant_ids) == 1 and
  29. x.product_variant_ids.cost_method != cost_method).mapped(
  30. 'product_variant_ids')
  31. products.with_context(force_not_load=True).write(
  32. {'cost_method': cost_method})
  33. return res
  34. class ProductPriceHistory(models.Model):
  35. _inherit = 'product.price.history'
  36. @api.model
  37. def create(self, values):
  38. if self.env.context.get('bypass_template_history'):
  39. return self
  40. return super(ProductPriceHistory, self).create(values)