From e8967d25569e31f3a5b847dd7ba92ec509a411d1 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Fri, 10 Nov 2023 12:09:56 +0100 Subject: [PATCH 1/3] [REF] pos_order_to_sale_order : add sale order line sequence in the prepare function --- pos_order_to_sale_order/models/sale_order.py | 4 ++-- pos_order_to_sale_order/models/sale_order_line.py | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/pos_order_to_sale_order/models/sale_order.py b/pos_order_to_sale_order/models/sale_order.py index be32ace84d..a6c674d033 100644 --- a/pos_order_to_sale_order/models/sale_order.py +++ b/pos_order_to_sale_order/models/sale_order.py @@ -14,8 +14,8 @@ def _prepare_from_pos(self, order_data): session = PosSession.browse(order_data["pos_session_id"]) SaleOrderLine = self.env["sale.order.line"] order_lines = [ - Command.create(SaleOrderLine._prepare_from_pos(line[2])) - for line in order_data["lines"] + Command.create(SaleOrderLine._prepare_from_pos(i + 1, line_data[2])) + for (i, line_data) in enumerate(order_data["lines"]) ] return { "partner_id": order_data["partner_id"], diff --git a/pos_order_to_sale_order/models/sale_order_line.py b/pos_order_to_sale_order/models/sale_order_line.py index 157f2badc0..edcd73cc8c 100644 --- a/pos_order_to_sale_order/models/sale_order_line.py +++ b/pos_order_to_sale_order/models/sale_order_line.py @@ -9,13 +9,14 @@ class SaleOrderLine(models.Model): _inherit = "sale.order.line" @api.model - def _prepare_from_pos(self, order_line_data): + def _prepare_from_pos(self, sequence, order_line_data): ProductProduct = self.env["product.product"] product = ProductProduct.browse(order_line_data["product_id"]) product_name = product.name if order_line_data.get("customer_note"): product_name += "\n" + order_line_data["customer_note"] return { + "sequence": sequence, "product_id": order_line_data["product_id"], "name": product_name, "product_uom_qty": order_line_data["qty"], From 2b18895e87b589ea93a3f5e9328c6347ea4fddf7 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Fri, 10 Nov 2023 12:34:26 +0100 Subject: [PATCH 2/3] [IMP] pos_order_to_sale_order : use customer_note to prepare sale order line name fields --- pos_order_to_sale_order/models/sale_order.py | 11 +++++++++++ pos_order_to_sale_order/models/sale_order_line.py | 6 ------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/pos_order_to_sale_order/models/sale_order.py b/pos_order_to_sale_order/models/sale_order.py index a6c674d033..08a7677e87 100644 --- a/pos_order_to_sale_order/models/sale_order.py +++ b/pos_order_to_sale_order/models/sale_order.py @@ -33,6 +33,17 @@ def create_order_from_pos(self, order_data, action): order_vals = self._prepare_from_pos(order_data) sale_order = self.create(order_vals) + for (i, line_data) in enumerate(order_data["lines"]): + if line_data[2].get("customer_note", False): + order_line = sale_order.order_line.filtered( + lambda x: x.sequence == i + 1 + ) + order_line.write( + { + "name": f"{order_line.name}\n{line_data[2].get('customer_note', False)}" + } + ) + # Confirm Sale Order if action in ["confirmed", "delivered", "invoiced"]: sale_order.action_confirm() diff --git a/pos_order_to_sale_order/models/sale_order_line.py b/pos_order_to_sale_order/models/sale_order_line.py index edcd73cc8c..2810733f83 100644 --- a/pos_order_to_sale_order/models/sale_order_line.py +++ b/pos_order_to_sale_order/models/sale_order_line.py @@ -10,15 +10,9 @@ class SaleOrderLine(models.Model): @api.model def _prepare_from_pos(self, sequence, order_line_data): - ProductProduct = self.env["product.product"] - product = ProductProduct.browse(order_line_data["product_id"]) - product_name = product.name - if order_line_data.get("customer_note"): - product_name += "\n" + order_line_data["customer_note"] return { "sequence": sequence, "product_id": order_line_data["product_id"], - "name": product_name, "product_uom_qty": order_line_data["qty"], "discount": order_line_data["discount"], "price_unit": order_line_data["price_unit"], From 85cd9139b20b08d2ae243f86e081c35457c3a3dc Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Fri, 10 Nov 2023 12:48:57 +0100 Subject: [PATCH 3/3] [IMP] pos_order_to_sale_order : performance : directly overload _get_sale_order_line_multiline_description_sale to avoid to write on sale.order.line after having creating the sale.order.line --- pos_order_to_sale_order/models/sale_order.py | 15 +++------------ pos_order_to_sale_order/models/sale_order_line.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pos_order_to_sale_order/models/sale_order.py b/pos_order_to_sale_order/models/sale_order.py index 08a7677e87..54768e6ab5 100644 --- a/pos_order_to_sale_order/models/sale_order.py +++ b/pos_order_to_sale_order/models/sale_order.py @@ -31,18 +31,9 @@ def _prepare_from_pos(self, order_data): def create_order_from_pos(self, order_data, action): # Create Draft Sale order order_vals = self._prepare_from_pos(order_data) - sale_order = self.create(order_vals) - - for (i, line_data) in enumerate(order_data["lines"]): - if line_data[2].get("customer_note", False): - order_line = sale_order.order_line.filtered( - lambda x: x.sequence == i + 1 - ) - order_line.write( - { - "name": f"{order_line.name}\n{line_data[2].get('customer_note', False)}" - } - ) + sale_order = self.with_context( + pos_order_lines_data=[x[2] for x in order_data.get("lines", [])] + ).create(order_vals) # Confirm Sale Order if action in ["confirmed", "delivered", "invoiced"]: diff --git a/pos_order_to_sale_order/models/sale_order_line.py b/pos_order_to_sale_order/models/sale_order_line.py index 2810733f83..3a4ec24fc9 100644 --- a/pos_order_to_sale_order/models/sale_order_line.py +++ b/pos_order_to_sale_order/models/sale_order_line.py @@ -18,3 +18,14 @@ def _prepare_from_pos(self, sequence, order_line_data): "price_unit": order_line_data["price_unit"], "tax_id": order_line_data["tax_ids"], } + + def _get_sale_order_line_multiline_description_sale(self): + res = super()._get_sale_order_line_multiline_description_sale() + + for (i, line_data) in enumerate( + self.env.context.get("pos_order_lines_data", []) + ): + if line_data.get("customer_note", False) and self.sequence == i + 1: + res += "\n" + line_data.get("customer_note") + + return res