product_product.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding: utf-8 -*-
  2. # © 2015 Ainara Galdona - AvanzOSC
  3. # © 2015 Pedro M. Baeza - Serv. Tecnol. Avanzados
  4. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  5. from openerp import api, fields, models
  6. from openerp.addons import decimal_precision as dp
  7. class ProductProduct(models.Model):
  8. _inherit = 'product.product'
  9. @api.model
  10. def _get_selection_cost_method(self):
  11. return self.env['product.template'].fields_get(
  12. allfields=['cost_method'])['cost_method']['selection']
  13. @api.model
  14. def _set_standard_price(self, product, value):
  15. """Store the standard price change in order to be able to retrieve the
  16. cost of a product variant for a given date
  17. """
  18. # With this, we make sure everyone can record the history
  19. # independently from its ACL
  20. price_history_obj = self.env['product.price.history.product'].sudo()
  21. user_company = self.env.user.company_id.id
  22. company_id = self.env.context.get('force_company', user_company)
  23. price_history_obj.create({
  24. 'product_id': product.id,
  25. 'product_template_id': product.product_tmpl_id.id,
  26. 'cost': value,
  27. 'company_id': company_id,
  28. })
  29. # Save the value in the template if there's only one variant.
  30. template = product.product_tmpl_id
  31. if template.product_variant_count == 1:
  32. template.with_context(bypass_down_write=1).standard_price = value
  33. standard_price = fields.Float(
  34. string='Cost Price', digits=dp.get_precision('Product Price'),
  35. help="Cost price of the product variant used for standard "
  36. "stock valuation in accounting and used as a base price on purchase "
  37. "orders. Expressed in the default unit of measure of the product.",
  38. groups="base.group_user", company_dependent=True)
  39. cost_method = fields.Selection(
  40. string="Costing Method", selection='_get_selection_cost_method',
  41. help="Standard Price: The cost price is manually updated at the end "
  42. "of a specific period (usually every year).\n"
  43. "Average Price: The cost price is recomputed at each incoming "
  44. "shipment and used for the product valuation.\n"
  45. "Real Price: The cost price displayed is the price of the last "
  46. "outgoing product (will be use in case of inventory loss for "
  47. "example).",
  48. required=True, copy=True, company_dependent=True)
  49. @api.model
  50. def create(self, values):
  51. if 'product_tmpl_id' in values and 'standard_price' not in values:
  52. template = self.env['product.template'].browse(
  53. values.get('product_tmpl_id'))
  54. values.update({
  55. 'standard_price': template.standard_price,
  56. })
  57. product = super(ProductProduct, self).create(values)
  58. self._set_standard_price(product, values.get('standard_price', 0.0))
  59. return product
  60. @api.multi
  61. def write(self, values):
  62. if 'standard_price' in values:
  63. for product in self:
  64. product._set_standard_price(product, values['standard_price'])
  65. if (values.get('cost_method', False) and not
  66. self.env.context.get('force_not_load', False)):
  67. cost_method = values.get('cost_method', False)
  68. templates = self.mapped('product_tmpl_id').filtered(
  69. lambda x: len(x.product_variant_ids) == 1 and
  70. x.cost_method != cost_method)
  71. templates.with_context(force_not_load=True).write(
  72. {'cost_method': cost_method})
  73. return super(ProductProduct, self).write(values)
  74. class ProductPriceHistory(models.Model):
  75. _inherit = 'product.price.history'
  76. _name = 'product.price.history.product'
  77. product_id = fields.Many2one(
  78. comodel_name='product.product', string='Product', ondelete='cascade')