Skip to content

Commit

Permalink
[IMP] product_import: optionally set company on product
Browse files Browse the repository at this point in the history
  • Loading branch information
florentx committed Jul 10, 2023
1 parent 6d35e8d commit 21db770
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 9 deletions.
1 change: 1 addition & 0 deletions product_import/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

from . import models
from . import wizard
1 change: 1 addition & 0 deletions product_import/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"data": [
"security/ir.model.access.csv",
"views/res_config_settings.xml",
"wizard/product_import_view.xml",
],
}
2 changes: 2 additions & 0 deletions product_import/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import res_company
from . import res_config_settings
15 changes: 15 additions & 0 deletions product_import/models/res_company.py
Original file line number Diff line number Diff line change
@@ -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.",
)
11 changes: 11 additions & 0 deletions product_import/models/res_config_settings.py
Original file line number Diff line number Diff line change
@@ -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
)
22 changes: 22 additions & 0 deletions product_import/views/res_config_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="view_config_settings" model="ir.ui.view">
<field name="model">res.config.settings</field>
<field name="inherit_id" ref="product.res_config_settings_view_form"/>
<field name="arch" type="xml">
<xpath expr="//div[@id='product_general_settings']" position="inside">
<div class="o_setting_left_pane">
<field name="product_import_set_company"/>
</div>
<div class="o_setting_right_pane">
<label for="product_import_set_company"/>
<div class="text-muted">
Set company on imported products.
</div>
</div>
</xpath>
</field>
</record>

</odoo>
36 changes: 27 additions & 9 deletions product_import/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,39 @@ 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)]

Check warning on line 154 in product_import/wizard/product_import.py

View check run for this annotation

Codecov / codecov/patch

product_import/wizard/product_import.py#L154

Added line #L154 was not covered by tests
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(
Expand All @@ -178,15 +196,15 @@ 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,
"product_code": parsed_product["product_code"],
"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:
Expand Down

0 comments on commit 21db770

Please sign in to comment.