Files
odoo_source/addons/project/models/res_partner.py
T
Walid 390807c494 [FIX] project: check company
Steps to reproduce:
- Install Stock and Project
- Create a new proect with current company as customer
(3 dots menu on project overview to the customer)
- Create a new warehouse for the current company
- Error pops

Bug:
since commit[1] check company is enforced on the project_id field of
partners in bothways
when creating a new warehouse CheckCompany fields are editted on the
partner if set (property_stock_customer/supplier) the issue is if
the partner's company_id is False while project belongs to company
"_check_company" fails

Fix:
Remove check company on project_ids from the partner's side and
implement it as python constraint

opw-3507952
[1]:https://github.com/odoo/odoo/pull/109464

closes odoo/odoo#143679

X-original-commit: 3741557dc4b5e84876247db851e9d2c862fcb93b
Signed-off-by: Xavier Bol (xbo) <xbo@odoo.com>
Signed-off-by: Walid Hanniche (waha) <waha@odoo.com>
2023-11-27 21:39:18 +00:00

64 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.tools import email_normalize
class ResPartner(models.Model):
""" Inherits partner and adds Tasks information in the partner form """
_inherit = 'res.partner'
project_ids = fields.One2many('project.project', 'partner_id', string='Projects')
task_ids = fields.One2many('project.task', 'partner_id', string='Tasks')
task_count = fields.Integer(compute='_compute_task_count', string='# Tasks')
@api.constrains('company_id', 'project_ids')
def _ensure_same_company_than_projects(self):
for partner in self:
if partner.company_id and partner.project_ids.company_id and partner.project_ids.company_id != partner.company_id:
raise UserError(_("Partner company cannot be different from its assigned projects' company"))
@api.constrains('company_id', 'task_ids')
def _ensure_same_company_than_tasks(self):
for partner in self:
if partner.company_id and partner.task_ids.company_id and partner.task_ids.company_id != partner.company_id:
raise UserError(_("Partner company cannot be different from its assigned tasks' company"))
def _compute_task_count(self):
# retrieve all children partners and prefetch 'parent_id' on them
all_partners = self.with_context(active_test=False).search_fetch(
[('id', 'child_of', self.ids)],
['parent_id'],
)
task_data = self.env['project.task']._read_group(
domain=[('partner_id', 'in', all_partners.ids)],
groupby=['partner_id'], aggregates=['__count']
)
self_ids = set(self._ids)
self.task_count = 0
for partner, count in task_data:
while partner:
if partner.id in self_ids:
partner.task_count += count
partner = partner.parent_id
# Deprecated: remove me in MASTER
def _create_portal_users(self):
partners_without_user = self.filtered(lambda partner: not partner.user_ids)
if not partners_without_user:
return self.env['res.users']
created_users = self.env['res.users']
for partner in partners_without_user:
created_users += self.env['res.users'].with_context(no_reset_password=True).sudo()._create_user_from_template({
'email': email_normalize(partner.email),
'login': email_normalize(partner.email),
'partner_id': partner.id,
'company_id': self.env.company.id,
'company_ids': [(6, 0, self.env.company.ids)],
'active': True,
})
return created_users