| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 3 |
|
| 4 |
from odoo import api, fields, models, _ |
| 5 |
from odoo.exceptions import ValidationError |
| 6 |
|
| 7 |
|
| 8 |
class ProductTemplate(models.Model): |
| 9 |
_inherit = 'product.template' |
| 10 |
|
| 11 |
service_to_purchase = fields.Boolean( |
| 12 |
"Subcontract Service", company_dependent=True, copy=False, |
| 13 |
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.") |
| 14 |
|
| 15 |
@api.constrains('service_to_purchase', 'seller_ids', 'type') |
| 16 |
def _check_service_to_purchase(self): |
| 17 |
for template in self: |
| 18 |
if template.service_to_purchase: |
| 19 |
if template.type != 'service': |
| 20 |
raise ValidationError(_("Product that is not a service can not create RFQ.")) |
| 21 |
template._check_vendor_for_service_to_purchase(template.seller_ids) |
| 22 |
|
| 23 |
@api.model_create_multi |
| 24 |
def create(self, vals_list): |
| 25 |
for vals in vals_list: |
| 26 |
if vals.get('service_to_purchase'): |
| 27 |
self._check_vendor_for_service_to_purchase(vals.get('seller_ids')) |
| 28 |
return super().create(vals_list) |
| 29 |
|
| 30 |
def _check_vendor_for_service_to_purchase(self, sellers): |
| 31 |
if not sellers: |
| 32 |
raise ValidationError(_("Please define the vendor from whom you would like to purchase this service automatically.")) |
| 33 |
|
| 34 |
@api.onchange('type', 'expense_policy') |
| 35 |
def _onchange_service_to_purchase(self): |
| 36 |
products_template = self.filtered(lambda p: p.type != 'service' or p.expense_policy != 'no') |
| 37 |
products_template.service_to_purchase = False |