[FIX] sale_timesheet: prevent deleting service on timesheet

This commit prevents a user from deleting or archiving the default
"Service on Timesheet" product.

Before this commit, the users were receiving a constraint violation
error.

closes odoo/odoo#53655

Taskid: 2221046
X-original-commit: 761d28bcca1e4f7789d45eb4baa22d08f31717a4
Signed-off-by: Yannick Tivisse (yti) <yti@odoo.com>
This commit is contained in:
Simon Genin (ges)
2020-06-25 11:43:31 +00:00
committed by Yannick Tivisse
parent 76700a4a1e
commit 3806636d9b
+34 -1
View File
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
import threading
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
class ProductTemplate(models.Model):
@@ -67,6 +70,21 @@ class ProductTemplate(models.Model):
self.invoice_policy = 'order'
return res
def unlink(self):
time_product = self.env.ref('sale_timesheet.time_product')
if time_product.product_tmpl_id in self:
raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name)
return super(ProductTemplate, self).unlink()
def write(self, vals):
# timesheet product can't be archived
test_mode = getattr(threading.currentThread(), 'testing', False) or self.env.registry.in_test_mode()
if not test_mode and 'active' in vals and not vals['active']:
time_product = self.env.ref('sale_timesheet.time_product')
if time_product.product_tmpl_id in self:
raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name)
return super(ProductTemplate, self).write(vals)
class ProductProduct(models.Model):
_inherit = 'product.product'
@@ -75,3 +93,18 @@ class ProductProduct(models.Model):
""" Check if the product is a delivered timesheet """
self.ensure_one()
return self.type == 'service' and self.service_policy == 'delivered_timesheet'
def unlink(self):
time_product = self.env.ref('sale_timesheet.time_product')
if time_product in self:
raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name)
return super(ProductProduct, self).unlink()
def write(self, vals):
# timesheet product can't be archived
test_mode = getattr(threading.currentThread(), 'testing', False) or self.env.registry.in_test_mode()
if not test_mode and 'active' in vals and not vals['active']:
time_product = self.env.ref('sale_timesheet.time_product')
if time_product in self:
raise ValidationError(_('The product %s cannot be archived, nor deleted.') % time_product.name)
return super(ProductProduct, self).write(vals)