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: fixes and tests #1113

Open
wants to merge 4 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
4 changes: 2 additions & 2 deletions product_import/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def setUpClass(cls):
cls.wiz_model = cls.env["product.import"]
cls.supplier = cls.env["res.partner"].create({"name": "Catalogue Vendor"})

def _mock(self, method_name):
return mock.patch.object(type(self.wiz_model), method_name)
def _mock(self, method_name, **kw):
return mock.patch.object(type(self.wiz_model), method_name, **kw)

@property
def wiz_form(self):
Expand Down
176 changes: 172 additions & 4 deletions product_import/tests/test_product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"currency": {"iso": "EUR"},
"description": "Photo copy paper 80g A4, package of 500 sheets.",
"external_ref": "102",
"min_qty": 1.0,
"min_qty": 5.0,
"name": "Copy paper",
"price": 12.55,
"product_code": "MNTR011",
Expand All @@ -50,6 +50,32 @@
},
],
"ref": "1387",
"seller": {"name": "Catalogue Vendor"},
}
PARSED_CATALOG2 = {
"date": "2016-09-03",
"doc_type": "catalogue",
"products": [
{
"barcode": "1234567890114",
"code": "MNTR011",
"active": False,
"name": "Konnektor 1/4x1/4 CB4629",
"description": "Konnektor 1/4x1/4 CB4629",
"external_ref": "201459",
"uom": {"unece_code": "H87"},
"product_code": "201459",
"weight": 29.0,
"weight_uom": False,
"price": 0.0,
"currency": {"iso": "EUR"},
"min_qty": 9.0,
"manufacturer_ref": False,
"sale_delay": 11,
},
],
"ref": "214R",
"company": {"name": "Customer ABC"},
"seller": {
"contact": False,
"email": False,
Expand Down Expand Up @@ -88,10 +114,147 @@ def test_get_company_id(self):

def test_product_import(self):
# product.product
products = self.wiz_model._create_products(
self.parsed_catalog, seller=self.supplier
product_obj = self.env["product.product"].with_context(active_test=False)
existing = product_obj.search([], order="id")

wiz = self.wiz_model.create(
{"product_file": b"====", "product_filename": "test_import.xml"}
)
with self._mock("parse_product_catalogue", return_value=self.parsed_catalog):
wiz.import_button()
products = product_obj.search([], order="id") - existing
self.assertEqual(len(products), 3)
for product, parsed in zip(products, PARSED_CATALOG["products"]):

# Expected
expected = {
"code": parsed["code"],
"seller": PARSED_CATALOG["seller"]["name"],
"min_qty": parsed["min_qty"],
"price": parsed["price"],
"currency": parsed["currency"]["iso"],
"type": "product",
"uom_id": 1, # Units
"uom_po_id": 1,
"active": parsed.get("active", True),
}

# product.product "Product Variant"
[p_supplierinfo] = product.seller_ids
p_values = {
"code": product.default_code,
"seller": p_supplierinfo.name.name,
"min_qty": p_supplierinfo.min_qty,
"price": p_supplierinfo.price,
"currency": p_supplierinfo.currency_id.name,
"type": product.type,
"uom_id": product.uom_id.id,
"uom_po_id": product.uom_po_id.id,
"active": product.active,
}
for key in "name", "barcode", "description":
expected[key] = parsed[key]
p_values[key] = getattr(product, key)

# product.template "Product"
product_tmpl = product.product_tmpl_id
pt_values = {
**p_values,
"code": product_tmpl.default_code,
"uom_id": product_tmpl.uom_id.id,
"uom_po_id": product_tmpl.uom_po_id.id,
}
for key in "name", "barcode", "description", "type", "active":
pt_values[key] = getattr(product_tmpl, key)

self.assertEqual(p_values, expected)
self.assertEqual(pt_values, expected)
self.assertEqual(product.seller_ids, product_tmpl.seller_ids)
self.assertEqual(
product.seller_ids.mapped("delay")[0], parsed.get("sale_delay", 0)
)
# Pricelist is linked to the Product Template, not the Product Variant
self.assertEqual(
product.seller_ids.product_tmpl_id, product.product_tmpl_id
)
self.assertFalse(product.seller_ids.product_id)

def test_product_import_change(self):
# product.product
product_obj = self.env["product.product"].with_context(active_test=False)
existing = product_obj.search([], order="id")
parsed_catalog = {**PARSED_CATALOG2, "chatter_msg": []}

wiz = self.wiz_model.create(
{"product_file": b"====", "product_filename": "test_import.xml"}
)

# 1st import
with self._mock("parse_product_catalogue", return_value=self.parsed_catalog):
wiz.import_button()

# 2nd import
with self._mock("parse_product_catalogue", return_value=parsed_catalog):
wiz.import_button()
products = product_obj.search([], order="id") - existing
self.assertEqual(len(products), 3)
for product, parsed in zip(products, PARSED_CATALOG["products"]):
if product.default_code == "MNTR011":
[parsed] = PARSED_CATALOG2["products"]

# Expected
expected = {
"code": parsed["code"],
"seller": PARSED_CATALOG["seller"]["name"],
"min_qty": parsed["min_qty"],
"price": parsed["price"],
"currency": parsed["currency"]["iso"],
"type": "product",
"uom_id": 1, # Units
"uom_po_id": 1,
"active": parsed.get("active", True),
}

# product.product "Product Variant"
[p_supplierinfo] = product.seller_ids[:1]
p_values = {
"code": product.default_code,
"seller": p_supplierinfo.name.name,
"min_qty": p_supplierinfo.min_qty,
"price": p_supplierinfo.price,
"currency": p_supplierinfo.currency_id.name,
"type": product.type,
"uom_id": product.uom_id.id,
"uom_po_id": product.uom_po_id.id,
"active": product.active,
}
for key in "name", "barcode", "description":
expected[key] = parsed[key]
p_values[key] = getattr(product, key)

# product.template "Product"
product_tmpl = product.product_tmpl_id
pt_values = {
**p_values,
"code": product_tmpl.default_code,
"uom_id": product_tmpl.uom_id.id,
"uom_po_id": product_tmpl.uom_po_id.id,
}
for key in "name", "barcode", "description", "type", "active":
pt_values[key] = getattr(product_tmpl, key)

self.assertEqual(p_values, expected)
self.assertEqual(pt_values, expected)
self.assertEqual(product.seller_ids, product_tmpl.seller_ids)
self.assertEqual(
product.seller_ids.mapped("delay")[0], parsed.get("sale_delay", 0)
)

# 3rd import
with self._mock("parse_product_catalogue", return_value=self.parsed_catalog):
wiz.import_button()

self.assertEqual(product_obj.search([], order="id") - existing, products)
for product, parsed in zip(products, PARSED_CATALOG["products"]):

# Expected
Expand Down Expand Up @@ -140,7 +303,12 @@ def test_product_import(self):
self.assertEqual(product.seller_ids, product_tmpl.seller_ids)
self.assertEqual(
product.seller_ids.mapped("delay")[0], parsed.get("sale_delay", 0)
),
)
# Pricelist is linked to the Product Template, not the Product Variant
self.assertEqual(
product.seller_ids.product_tmpl_id, product.product_tmpl_id
)
self.assertFalse(product.seller_ids.product_id)

def test_import_button(self):
form = self.wiz_form
Expand Down
76 changes: 54 additions & 22 deletions product_import/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def _prepare_supplierinfo(self, seller_info, product):
and s_info.delay == seller_info["delay"]
):
seller_id = s_info.id
elif s_info.date_start == today:
# Overwrite if created the same day
seller_id = s_info.id
result.append((1, s_info.id, seller_info))
else:
result.append((1, s_info.id, {"date_end": yesterday}))
if not seller_id:
Expand All @@ -150,11 +154,10 @@ def _prepare_supplierinfo(self, seller_info, product):
return result

@api.model
def _prepare_product(self, parsed_product, chatter_msg, seller=None):
def _prepare_product(self, parsed_product, seller, company_id, chatter_msg):
# Important: barcode is unique key of product.template model
# So 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,)
Expand Down Expand Up @@ -187,7 +190,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": company_id,
"delay": parsed_product.get("sale_delay", 0),
}
product_vals["seller_ids"] = self._prepare_supplierinfo(seller_info, product)
Expand All @@ -197,14 +200,12 @@ def _prepare_product(self, parsed_product, chatter_msg, seller=None):
return product_vals

@api.model
def create_product(self, parsed_product, chatter_msg, seller=None):
product_vals = self._prepare_product(parsed_product, chatter_msg, seller=seller)
if not product_vals:
return False
def _save_product(self, product_vals, chatter_msg):
"""Create / Update a product."""
product = product_vals.pop("recordset", None)
if product:
product.write(product_vals)
logger.info("Product %d updated", product.id)
logger.debug("Product %s updated", product.default_code)
else:
product_active = product_vals.pop("active")
product = self.env["product.product"].create(product_vals)
Expand All @@ -213,33 +214,64 @@ def create_product(self, parsed_product, chatter_msg, seller=None):
# all characteristics into product.template
product.flush()
product.action_archive()
logger.info("Product %d created", product.id)
logger.debug("Product %s created", product.default_code)

# Archive product template, if product is archived
if product.active != product.product_tmpl_id.active:
product.product_tmpl_id.toggle_active()
return product

@api.model
def _create_products(self, catalogue, seller, filename=None):
products = self.env["product.product"].browse()
for product in catalogue.get("products"):
record = self.create_product(
product,
catalogue["chatter_msg"],
def _create_update_products(self, products, seller_id, company_id, chatter_msg):
"""Create / Update all products."""
seller = self.env["res.partner"].browse(seller_id)

for parsed_product in products:
product_vals = self._prepare_product(
parsed_product,
seller=seller,
company_id=company_id,
chatter_msg=chatter_msg,
)
if record:
products |= record
self._bdimport.post_create_or_update(catalogue, seller, doc_filename=filename)
logger.info("Products updated for vendor %d", seller.id)
return products
if product_vals:
product = self._save_product(product_vals, chatter_msg=chatter_msg)
chatter_msg.append(
f"Product created/updated {product.default_code} ({product.id})"
)
return True

@api.model
def create_update_products(self, products, seller_id, company_id, chatter_msg):
"""Create / Update a product.

This method can be overriden, for example to import asynchronously with queue_job.
"""
return self._create_update_products(
products, seller_id, company_id, chatter_msg=chatter_msg
)

def import_button(self):
self.ensure_one()
file_content = b64decode(self.product_file)
# 1st step: Parse the (UBL) document --> get a "catalogue" dictionary
catalogue = self.parse_product_catalogue(file_content, self.product_filename)
if not catalogue.get("products"):
raise UserError(_("This catalogue doesn't have any product!"))
company_id = self._get_company_id(catalogue)
seller = self._get_seller(catalogue)
self.with_context(product_company_id=company_id)._create_products(
catalogue, seller, filename=self.product_filename
# 2nd step: Prepare values and create the "product.product" records in Odoo
self.create_update_products(
catalogue["products"],
seller.id,
company_id,
chatter_msg=catalogue["chatter_msg"],
)
# Save imported file as attachment
self._bdimport.post_create_or_update(
catalogue, seller, doc_filename=self.product_filename
)
logger.info(
"Update for vendor %s: %d products", seller.name, len(catalogue["products"])
)

return {"type": "ir.actions.act_window_close"}
6 changes: 4 additions & 2 deletions product_import_ubl/wizard/product_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ def parse_ubl_catalogue_line(self, line, ns):
"price": float(ele_price.text or 0),
"currency": {"iso": currency} if currency else False,
"min_qty": min_qty,
"sale_delay": xline.text(
"cac:RequiredItemLocationQuantity/cbc:LeadTimeMeasure"
"sale_delay": int(
xline.text(
"cac:RequiredItemLocationQuantity/cbc:LeadTimeMeasure", 0
)
),
}
)
Expand Down
Loading