Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][IMP] product_import: optionally set company on product #1077

Open
wants to merge 2 commits into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
28 changes: 28 additions & 0 deletions product_import/views/res_config_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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='inter_company']" position="after">
<div
class="col-12 col-lg-6 o_setting_box"
title="Company-specific products"
id="product_import_company"
>
<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">
Assign company on imported products
</div>
</div>
</div>
</xpath>
</field>
</record>

</odoo>
47 changes: 33 additions & 14 deletions product_import/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,40 @@
result.append((0, 0, seller_info))
return result

def _search_product(self, domain, company_id):
if company_id:
domain = domain + [("company_id", "=", company_id)]

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

View check run for this annotation

Codecov / codecov/patch

product_import/wizard/product_import.py#L153

Added line #L153 was not covered by tests
return (
self.env["product.product"]
.with_context(active_test=False)
.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
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)
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)
# 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"
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"],
parsed_product["code"],
company_id=product_company_id,
)
uom = self._bdimport._match_uom(parsed_product["uom"], chatter_msg)
currency = self._bdimport._match_currency(
Expand All @@ -178,15 +197,15 @@
"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
Loading