product_template.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, _
  4. from odoo.exceptions import ValidationError
  5. class ProductTemplate(models.Model):
  6. _inherit = 'product.template'
  7. service_to_purchase = fields.Boolean(
  8. "Subcontract Service", company_dependent=True, copy=False,
  9. help="If ticked, each time you sell this product through a SO, a RfQ is automatically created to buy the product. Tip: don't forget to set a vendor on the product.")
  10. @api.constrains('service_to_purchase', 'seller_ids', 'type')
  11. def _check_service_to_purchase(self):
  12. for template in self:
  13. if template.service_to_purchase:
  14. if template.type != 'service':
  15. raise ValidationError(_("Product that is not a service can not create RFQ."))
  16. template._check_vendor_for_service_to_purchase(template.seller_ids)
  17. @api.model_create_multi
  18. def create(self, vals_list):
  19. for vals in vals_list:
  20. if vals.get('service_to_purchase'):
  21. self._check_vendor_for_service_to_purchase(vals.get('seller_ids'))
  22. return super().create(vals_list)
  23. def _check_vendor_for_service_to_purchase(self, sellers):
  24. if not sellers:
  25. raise ValidationError(_("Please define the vendor from whom you would like to purchase this service automatically."))
  26. @api.onchange('type', 'expense_policy')
  27. def _onchange_service_to_purchase(self):
  28. products_template = self.filtered(lambda p: p.type != 'service' or p.expense_policy != 'no')
  29. products_template.service_to_purchase = False