diff --git a/product_import/__init__.py b/product_import/__init__.py index 4b25b97230..93aa2c1f84 100644 --- a/product_import/__init__.py +++ b/product_import/__init__.py @@ -1,3 +1,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import models from . import wizard diff --git a/product_import/__manifest__.py b/product_import/__manifest__.py index b9fa35f1b7..00716ddbf7 100644 --- a/product_import/__manifest__.py +++ b/product_import/__manifest__.py @@ -16,6 +16,7 @@ ], "data": [ "security/ir.model.access.csv", + "views/res_config_settings.xml", "wizard/product_import_view.xml", ], } diff --git a/product_import/models/__init__.py b/product_import/models/__init__.py new file mode 100644 index 0000000000..0b150f71c3 --- /dev/null +++ b/product_import/models/__init__.py @@ -0,0 +1,2 @@ +from . import res_company +from . import res_config_settings diff --git a/product_import/models/res_company.py b/product_import/models/res_company.py new file mode 100644 index 0000000000..32733eaf5f --- /dev/null +++ b/product_import/models/res_company.py @@ -0,0 +1,15 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResCompany(models.Model): + _inherit = "res.company" + + product_import_set_company = fields.Boolean( + string="Set company on imported product", + help="If active, then products are company-specific. " + "Beware that by default `barcode` is unique for all companies. " + "Install OCA add-on `product_barcode_constraint_per_company` " + "to circumvent this limitation.", + ) diff --git a/product_import/models/res_config_settings.py b/product_import/models/res_config_settings.py new file mode 100644 index 0000000000..87543120c1 --- /dev/null +++ b/product_import/models/res_config_settings.py @@ -0,0 +1,11 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ResConfigSettings(models.TransientModel): + _inherit = "res.config.settings" + + product_import_set_company = fields.Boolean( + related="company_id.product_import_set_company", readonly=False + ) diff --git a/product_import/views/res_config_settings.xml b/product_import/views/res_config_settings.xml new file mode 100644 index 0000000000..278d714bb8 --- /dev/null +++ b/product_import/views/res_config_settings.xml @@ -0,0 +1,28 @@ + + + + + res.config.settings + + + +
+
+ +
+
+
+
+
+
+
+ +
diff --git a/product_import/wizard/product_import.py b/product_import/wizard/product_import.py index 92a6950077..f86cafe7a4 100644 --- a/product_import/wizard/product_import.py +++ b/product_import/wizard/product_import.py @@ -148,21 +148,37 @@ def _prepare_supplierinfo(self, seller_info, product): result.append((0, 0, seller_info)) return result + def _existing_product(self, barcode, company_id): + product_domain = [("barcode", "=", barcode)] + if company_id: + product_domain += [("company_id", "=", company_id)] + return ( + self.env["product.product"] + .with_context(active_test=False) + .search(product_domain, limit=1) + ) + @api.model def _prepare_product(self, parsed_product, chatter_msg, seller=None): - # Important: barcode is unique key of product.template model - # So records product.product are created with company_id=False. + # By default records product.product are created with company_id=False. # Only the pricelist (product.supplierinfo) is company-specific. - product_company_id = self.env.context.get("product_company_id", False) + # Setting "product_import_set_company" change the behavior. + # Beware that barcode is unique key of product.template model + # Can be changed by OCA add-on "product_barcode_constraint_per_company" if not parsed_product["barcode"]: chatter_msg.append( _("Cannot import product without barcode: %s") % (parsed_product,) ) return False - product = ( - self.env["product.product"] - .with_context(active_test=False) - .search([("barcode", "=", parsed_product["barcode"])], limit=1) + import_company = self.env["res.company"].browse( + self.env.context.get("product_company_id") + ) + product_company_id = ( + import_company.id if import_company.product_import_set_company else False + ) + product = self._existing_product( + parsed_product["barcode"], + company_id=product_company_id, ) uom = self._bdimport._match_uom(parsed_product["uom"], chatter_msg) currency = self._bdimport._match_currency( @@ -178,7 +194,7 @@ def _prepare_product(self, parsed_product, chatter_msg, seller=None): "type": "product", "uom_id": uom.id, "uom_po_id": uom.id, - "company_id": False, + "company_id": product_company_id, } seller_info = { "name": seller and seller.id or False, @@ -186,7 +202,7 @@ def _prepare_product(self, parsed_product, chatter_msg, seller=None): "price": parsed_product["price"], "currency_id": currency.id, "min_qty": parsed_product["min_qty"], - "company_id": product_company_id, + "company_id": import_company.id, } product_vals["seller_ids"] = self._prepare_supplierinfo(seller_info, product) if product: