From e532d4df0d3767c7a6dfb0d0e336f332bbdca8b7 Mon Sep 17 00:00:00 2001 From: Hetashree Chauhan Date: Mon, 1 Jul 2019 09:52:45 +0000 Subject: [PATCH] [IMP] purchase: make products company consistence. Purpose of the commit is to let user select only those products which are being shared among companies or belongs to own company. On purchase order user will only able to select those products which are belongs to same company as purchase order or products which are sharable by default. task-2025168 closes: #34342 Signed-off-by: Damien Bouvy (dbo) --- addons/purchase/models/purchase.py | 10 +++++++++- addons/purchase/views/purchase_views.xml | 8 ++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/addons/purchase/models/purchase.py b/addons/purchase/models/purchase.py index fc0bfea51b0..afd2ffe09c0 100644 --- a/addons/purchase/models/purchase.py +++ b/addons/purchase/models/purchase.py @@ -8,7 +8,7 @@ from odoo import api, fields, models, SUPERUSER_ID, _ from odoo.osv import expression from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT from odoo.tools.float_utils import float_compare -from odoo.exceptions import UserError, AccessError +from odoo.exceptions import AccessError, UserError, ValidationError from odoo.tools.misc import formatLang @@ -124,6 +124,14 @@ class PurchaseOrder(models.Model): company_id = fields.Many2one('res.company', 'Company', required=True, index=True, states=READONLY_STATES, default=lambda self: self.env.company.id) currency_rate = fields.Float("Currency Rate", compute='_compute_currency_rate', compute_sudo=True, store=True, readonly=True, help='Ratio between the purchase order currency and the company currency') + @api.constrains('company_id', 'order_line') + def _check_order_line_company_id(self): + for order in self: + companies = order.order_line.product_id.company_id + if companies and companies != order.company_id: + bad_products = order.order_line.product_id.filtered(lambda p: p.company_id and p.company_id != order.company_id) + raise ValidationError((_("Your quotation contains products from company %s whereas your quotation belongs to company %s. \n Please change the company of your quotation or remove the products from other companies (%s).") % (', '.join(companies.mapped('display_name')), order.company_id.display_name, ', '.join(bad_products.mapped('display_name'))))) + def _compute_access_url(self): super(PurchaseOrder, self)._compute_access_url() for order in self: diff --git a/addons/purchase/views/purchase_views.xml b/addons/purchase/views/purchase_views.xml index 27f53bc8f9d..eba32f0b995 100644 --- a/addons/purchase/views/purchase_views.xml +++ b/addons/purchase/views/purchase_views.xml @@ -204,7 +204,7 @@ 'required': [('display_type', '=', False)], }" context="{'partner_id':parent.partner_id, 'quantity':product_qty,'uom':product_uom, 'company_id': parent.company_id}" - force_save="1"/> + force_save="1" domain="[('purchase_ok', '=', True), '|', ('company_id', '=', False), ('company_id', '=', parent.company_id)]"/> @@ -232,9 +232,9 @@