diff --git a/product_import/wizard/product_import.py b/product_import/wizard/product_import.py index 236f3fc1e7..4e241e5408 100644 --- a/product_import/wizard/product_import.py +++ b/product_import/wizard/product_import.py @@ -149,14 +149,21 @@ 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)] + def _search_product(self, domain, company_id): if company_id: - product_domain += [("company_id", "=", company_id)] + domain = domain + [("company_id", "=", company_id)] return ( self.env["product.product"] .with_context(active_test=False) - .search(product_domain, limit=1) + .search(domain, order="active DESC", limit=1) + ) + + def _existing_product(self, barcode, code, company_id): + product = None + if barcode: + product = self._search_product([("barcode", "=", barcode)], company_id) + return product or self._search_product( + [("default_code", "=", code)], company_id ) @api.model @@ -166,17 +173,13 @@ def _prepare_product(self, parsed_product, seller, company_id, chatter_msg): # 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 import_company = self.env["res.company"].browse(company_id) product_company_id = ( import_company.id if import_company.product_import_set_company else False ) product = self._existing_product( parsed_product["barcode"], + parsed_product["code"], company_id=product_company_id, ) uom = self._bdimport._match_uom(parsed_product["uom"], chatter_msg)