diff --git a/connector_woocommerce/__manifest__.py b/connector_woocommerce/__manifest__.py index 1e38167ff..6623c2f83 100644 --- a/connector_woocommerce/__manifest__.py +++ b/connector_woocommerce/__manifest__.py @@ -16,7 +16,7 @@ }, "depends": [ "connector_extension_woocommerce", - "website_sale", + "website_sale_product_document", "connector_wordpress", "sale_stock", ], diff --git a/connector_woocommerce/models/common/product_attachment.py b/connector_woocommerce/models/common/product_attachment.py index ecf6fcecd..2958b857a 100644 --- a/connector_woocommerce/models/common/product_attachment.py +++ b/connector_woocommerce/models/common/product_attachment.py @@ -4,7 +4,7 @@ from odoo import fields, models -# This model is used to store in order attachments from product images +# This model is used to store in order attachments from product images - documents class ProductAttachment(models.TransientModel): _name = "product.attachment" _order = "sequence" diff --git a/connector_woocommerce/models/product_product/export_mapper.py b/connector_woocommerce/models/product_product/export_mapper.py index 6fdb7e120..a4eb638c2 100644 --- a/connector_woocommerce/models/product_product/export_mapper.py +++ b/connector_woocommerce/models/product_product/export_mapper.py @@ -88,12 +88,48 @@ def stock(self, record): @mapping def description(self, record): - return { - "description": record.with_context( - lang=self.backend_record.language_id.code - ).variant_public_description - or None - } + description = record.with_context( + lang=self.backend_record.language_id.code + ).variant_public_description + document_description = "" + if record.variant_document_ids: + if self.collection.wordpress_backend_id: + with self.collection.wordpress_backend_id.work_on( + "wordpress.ir.attachment" + ) as work: + exporter = work.component(self._usage) + binder = exporter.binder_for("wordpress.ir.attachment") + for data in record.variant_document_ids: + external_id = binder.get_external_dict_ids( + data.attachment_id, check_external_id=False + ) + if external_id: + variant_document = record.variant_document_ids.filtered( + lambda x: x.datas == data.attachment_id.datas + ) + document_description += "
" % ( + external_id["source_url"], + variant_document.with_context( + lang=self.backend_record.language_id.code + ).description, + ) + else: + if ( + self.backend_record.wordpress_backend_id + and not self.backend_record.wordpress_backend_id.test_database + ): + assert external_id, ( + "Unexpected error on %s:" + "The backend id cannot be obtained." + "At this stage, the backend record should " + "have been already linked via " + "._export_dependencies. " % record._name + ) + if description: + description = description + "\n" + document_description + else: + description = document_description + return {"description": description or None} @mapping def parent_id(self, record): @@ -104,27 +140,24 @@ def parent_id(self, record): @mapping def image(self, record): # WooCommerce only allows one image per variant product - if record.product_attachment_ids and self.collection.wordpress_backend_id: + if record.product_image_attachment_ids and self.collection.wordpress_backend_id: with self.collection.wordpress_backend_id.work_on( "wordpress.ir.attachment" ) as work: exporter = work.component(self._usage) binder = exporter.binder_for("wordpress.ir.attachment") - if record.product_attachment_ids: - image = record.product_attachment_ids[0].attachment_id - values = binder.get_external_dict_ids( - image, check_external_id=False - ) - if ( - not values - and self.backend_record.wordpress_backend_id.test_database - ): - return - return { - "image": { - "id": values["id"], - } + image = record.product_image_attachment_ids[0].attachment_id + values = binder.get_external_dict_ids(image, check_external_id=False) + if ( + not values + and self.backend_record.wordpress_backend_id.test_database + ): + return + return { + "image": { + "id": values["id"], } + } @mapping def attributes(self, record): diff --git a/connector_woocommerce/models/product_product/exporter.py b/connector_woocommerce/models/product_product/exporter.py index 82612f8be..de07032f6 100644 --- a/connector_woocommerce/models/product_product/exporter.py +++ b/connector_woocommerce/models/product_product/exporter.py @@ -62,7 +62,7 @@ def _export_dependencies(self, relation): attribute_line.attribute_id, "woocommerce.product.attribute" ) if ( - relation.product_attachment_ids + relation.product_image_attachment_ids and len(relation.product_tmpl_id.product_variant_ids) > 1 ): if self.collection.wordpress_backend_id: @@ -71,6 +71,17 @@ def _export_dependencies(self, relation): ) as work: exporter = work.component(self._usage) exporter._export_dependency( - relation.product_attachment_ids[0].attachment_id, + relation.product_image_attachment_ids[0].attachment_id, "wordpress.ir.attachment", ) + if relation.product_document_attachment_ids: + if self.collection.wordpress_backend_id: + with self.collection.wordpress_backend_id.work_on( + "wordpress.ir.attachment" + ) as work: + exporter = work.component(self._usage) + for attachment in relation.product_document_attachment_ids: + exporter._export_dependency( + attachment.attachment_id, + "wordpress.ir.attachment", + ) diff --git a/connector_woocommerce/models/product_product/product.py b/connector_woocommerce/models/product_product/product.py index d3cc74d5d..f7a8cec91 100644 --- a/connector_woocommerce/models/product_product/product.py +++ b/connector_woocommerce/models/product_product/product.py @@ -31,6 +31,7 @@ class ProductProduct(models.Model): "alternative_product_ids", "accessory_product_ids", "variant_inventory_availability", + "variant_document_ids", "product_tmpl_id", "product_tmpl_id.has_attributes", "product_tmpl_id.woocommerce_enabled", @@ -83,12 +84,12 @@ def _compute_variant_is_published(self): for rec in self: rec.variant_is_published = rec.product_tmpl_id.is_published - product_attachment_ids = fields.Many2many( + product_image_attachment_ids = fields.Many2many( comodel_name="product.attachment", - compute="_compute_product_attachment_ids", + compute="_compute_product_image_attachment_ids", ) - def _compute_product_attachment_ids(self): + def _compute_product_image_attachment_ids(self): for rec in self: attachment = self.env["ir.attachment"].search( [ @@ -98,7 +99,7 @@ def _compute_product_attachment_ids(self): ] ) if attachment: - rec.product_attachment_ids = [ + rec.product_image_attachment_ids = [ ( 0, 0, @@ -122,7 +123,7 @@ def _compute_product_attachment_ids(self): ("res_field", "=", "image_1920"), ] ) - rec.product_attachment_ids = [ + rec.product_image_attachment_ids = [ ( 0, 0, @@ -132,5 +133,28 @@ def _compute_product_attachment_ids(self): }, ) ] - if not rec.product_attachment_ids: - rec.product_attachment_ids = self.env["product.attachment"] + if not rec.product_image_attachment_ids: + rec.product_image_attachment_ids = self.env["product.attachment"] + + product_document_attachment_ids = fields.Many2many( + comodel_name="product.attachment", + compute="_compute_product_document_attachment_ids", + ) + + def _compute_product_document_attachment_ids(self): + for rec in self: + sequence = 0 + for attachment in rec.variant_document_ids.mapped("attachment_id"): + rec.product_document_attachment_ids = [ + ( + 0, + 0, + { + "attachment_id": attachment.id, + "sequence": sequence, + }, + ) + ] + sequence += 1 + if not rec.product_document_attachment_ids: + rec.product_document_attachment_ids = self.env["product.attachment"] diff --git a/connector_woocommerce/models/product_template/export_mapper.py b/connector_woocommerce/models/product_template/export_mapper.py index 1f786b587..a642fcb31 100644 --- a/connector_woocommerce/models/product_template/export_mapper.py +++ b/connector_woocommerce/models/product_template/export_mapper.py @@ -111,6 +111,43 @@ def description(self, record): ).variant_public_description return {"description": description if description else ""} + @mapping + def short_description(self, record): + if self.collection.wordpress_backend_id: + with self.collection.wordpress_backend_id.work_on( + "wordpress.ir.attachment" + ) as work: + exporter = work.component(self._usage) + binder = exporter.binder_for("wordpress.ir.attachment") + short_description = "" + for data in record.template_document_ids: + external_id = binder.get_external_dict_ids( + data.attachment_id, check_external_id=False + ) + if external_id: + template_document = record.template_document_ids.filtered( + lambda x: x.datas == data.attachment_id.datas + ) + short_description += "\n" % ( + external_id["source_url"], + template_document.description, + ) + else: + if ( + self.backend_record.wordpress_backend_id + and not self.backend_record.wordpress_backend_id.test_database + ): + assert external_id, ( + "Unexpected error on %s:" + "The backend id cannot be obtained." + "At this stage, the backend record should " + "have been already linked via " + "._export_dependencies. " % record._name + ) + return { + "short_description": short_description if short_description else "" + } + @mapping def product_type(self, record): return {"type": "variable" if record.has_attributes else "simple"} @@ -198,8 +235,10 @@ def images(self, record): exporter = work.component(self._usage) binder = exporter.binder_for("wordpress.ir.attachment") img_list = [] - if record.product_attachment_ids: - for image in record.product_attachment_ids.mapped("attachment_id"): + if record.product_image_attachment_ids: + for image in record.product_image_attachment_ids.mapped( + "attachment_id" + ): external_id = binder.get_external_dict_ids( image, check_external_id=False ) diff --git a/connector_woocommerce/models/product_template/exporter.py b/connector_woocommerce/models/product_template/exporter.py index 3aa9848ad..4ea261084 100644 --- a/connector_woocommerce/models/product_template/exporter.py +++ b/connector_woocommerce/models/product_template/exporter.py @@ -66,8 +66,13 @@ def _export_dependencies(self, relation): "wordpress.ir.attachment" ) as work: exporter = work.component(self._usage) - for attachment in relation.product_attachment_ids: + for image_attachment in relation.product_image_attachment_ids: exporter._export_dependency( - attachment.attachment_id, + image_attachment.attachment_id, + "wordpress.ir.attachment", + ) + for document_attachment in relation.product_document_attachment_ids: + exporter._export_dependency( + document_attachment.attachment_id, "wordpress.ir.attachment", ) diff --git a/connector_woocommerce/models/product_template/product_template.py b/connector_woocommerce/models/product_template/product_template.py index e0acf9eb1..cd9daeadb 100644 --- a/connector_woocommerce/models/product_template/product_template.py +++ b/connector_woocommerce/models/product_template/product_template.py @@ -42,6 +42,7 @@ def _compute_has_attributes(self): "public_description", "inventory_availability", "has_attributes", + "template_document_ids", "woocommerce_enabled", ) def _compute_woocommerce_write_date(self): @@ -112,12 +113,12 @@ def _inverse_inventory_availability(self): rec.inventory_availability ) - product_attachment_ids = fields.Many2many( + product_image_attachment_ids = fields.Many2many( comodel_name="product.attachment", - compute="_compute_product_attachment_ids", + compute="_compute_product_image_attachment_ids", ) - def _compute_product_attachment_ids(self): + def _compute_product_image_attachment_ids(self): for rec in self: attachment = self.env["ir.attachment"].search( [ @@ -127,7 +128,7 @@ def _compute_product_attachment_ids(self): ] ) if attachment: - rec.product_attachment_ids = [ + rec.product_image_attachment_ids = [ ( 0, 0, @@ -151,7 +152,7 @@ def _compute_product_attachment_ids(self): ("res_field", "=", "image_1920"), ] ) - rec.product_attachment_ids = [ + rec.product_image_attachment_ids = [ ( 0, 0, @@ -161,8 +162,28 @@ def _compute_product_attachment_ids(self): }, ) ] - if not rec.product_attachment_ids: - rec.product_attachment_ids = self.env["product.attachment"] + if not rec.product_image_attachment_ids: + rec.product_image_attachment_ids = self.env["product.attachment"] + + product_document_attachment_ids = fields.Many2many( + comodel_name="product.attachment", + compute="_compute_product_document_attachment_ids", + ) + + def _compute_product_document_attachment_ids(self): + for rec in self: + sequence = 0 + for attachment in rec.template_document_ids.mapped("attachment_id"): + rec.product_document_attachment_ids = [ + ( + 0, + 0, + {"attachment_id": attachment.id, "sequence": sequence}, + ) + ] + sequence += 1 + if not rec.product_document_attachment_ids: + rec.product_document_attachment_ids = self.env["product.attachment"] def write(self, vals): res = super().write(vals) diff --git a/connector_woocommerce/models/sale_order_line/sale_order_line.py b/connector_woocommerce/models/sale_order_line/sale_order_line.py index 6b6b083ee..66dd092d9 100644 --- a/connector_woocommerce/models/sale_order_line/sale_order_line.py +++ b/connector_woocommerce/models/sale_order_line/sale_order_line.py @@ -8,9 +8,6 @@ class SaleOrderLine(models.Model): _inherit = "sale.order.line" - # TODO: check lotes de facturacion -- no se como deberia verse, - # en las que he hecho sale un error de que no hay factura - # This field is created with digits=False to force # the creation with type numeric, like discount. woocommerce_discount = fields.Float(