diff --git a/addons/sale_timesheet/models/product.py b/addons/sale_timesheet/models/product.py index ce79b7278f1..63183f0a3b0 100644 --- a/addons/sale_timesheet/models/product.py +++ b/addons/sale_timesheet/models/product.py @@ -19,8 +19,8 @@ class ProductTemplate(models.Model): ('timesheet', 'Timesheets on project (one fare per SO/Project)'), ], ondelete={'timesheet': 'set default'}) # override domain - project_id = fields.Many2one(domain="[('company_id', '=', current_company_id), ('allow_billable', '=', True), ('pricing_type', '=', 'task_rate'), ('allow_timesheets', 'in', [service_policy == 'delivered_timesheet' or '', True])]") - project_template_id = fields.Many2one(domain="[('company_id', '=', current_company_id), ('allow_billable', '=', True), ('pricing_type', '!=', 'task_rate'), ('allow_timesheets', 'in', [service_policy == 'delivered_timesheet' or '', True])]") + project_id = fields.Many2one(domain="[('company_id', '=', current_company_id), ('allow_billable', '=', True), ('pricing_type', '=', 'task_rate'), ('allow_timesheets', 'in', [service_policy == 'delivered_timesheet', True])]") + project_template_id = fields.Many2one(domain="[('company_id', '=', current_company_id), ('allow_billable', '=', True), ('pricing_type', '!=', 'task_rate'), ('allow_timesheets', 'in', [service_policy == 'delivered_timesheet', True])]") service_upsell_warning = fields.Boolean('Upsell Warning', help="The salesperson in charge will be assigned an activity informing him of an upselling opportunity once the selected threshold is reached.") service_upsell_threshold = fields.Float('Threshold', help="Percentage of time delivered compared to the prepaid amount that must be reached for the upselling opportunity activity to be triggered.") @@ -70,6 +70,25 @@ class ProductTemplate(models.Model): self.invoice_policy = 'order' return res + @api.model + def _get_onchange_service_policy_updates(self, service_tracking, service_policy, project_id, project_template_id): + vals = {} + if service_tracking != 'no' and service_policy == 'delivered_timesheet': + if project_id and not project_id.allow_timesheets: + vals['project_id'] = False + elif project_template_id and not project_template_id.allow_timesheets: + vals['project_template_id'] = False + return vals + + @api.onchange('service_policy') + def _onchange_service_policy(self): + vals = self._get_onchange_service_policy_updates(self.service_tracking, + self.service_policy, + self.project_id, + self.project_template_id) + if vals: + self.update(vals) + @api.ondelete(at_uninstall=False) def _unlink_except_master_data(self): time_product = self.env.ref('sale_timesheet.time_product') @@ -94,6 +113,15 @@ class ProductProduct(models.Model): self.ensure_one() return self.type == 'service' and self.service_policy == 'delivered_timesheet' + @api.onchange('service_policy') + def _onchange_service_policy(self): + vals = self.product_tmpl_id._get_onchange_service_policy_updates(self.service_tracking, + self.service_policy, + self.project_id, + self.project_template_id) + if vals: + self.update(vals) + @api.ondelete(at_uninstall=False) def _unlink_except_master_data(self): time_product = self.env.ref('sale_timesheet.time_product')