From 4498c6d30c6e36e219c0103bc83d3817dcb9b2b5 Mon Sep 17 00:00:00 2001 From: David Date: Fri, 21 Apr 2023 17:22:29 +0200 Subject: [PATCH] [FIX] pos_order_return: make returns properly Before this PR the returns weren't functional when a stock picking was involved. TT42730 --- pos_order_return/README.rst | 7 ++- pos_order_return/models/pos_order.py | 39 +++++++----- pos_order_return/readme/ROADMAP.rst | 7 ++- .../static/description/index.html | 9 ++- .../tests/test_pos_order_return.py | 60 ++++++++++--------- 5 files changed, 69 insertions(+), 53 deletions(-) diff --git a/pos_order_return/README.rst b/pos_order_return/README.rst index a8f80849d1..539b9bc2ac 100644 --- a/pos_order_return/README.rst +++ b/pos_order_return/README.rst @@ -85,9 +85,10 @@ invoiced, a refund invoice will be made. Known issues / Roadmap ====================== -When migrating the module ``pos_order_return`` in version > 12.0 please merge -both modules ``pos_order_return`` and ``pos_order_return_traceability`` into a -single module. +* When migrating the module ``pos_order_return`` in version > 12.0 please merge + both modules ``pos_order_return`` and ``pos_order_return_traceability`` into a + single module. +* Stock update on session close is uncovered right now. Bug Tracker =========== diff --git a/pos_order_return/models/pos_order.py b/pos_order_return/models/pos_order.py index 37b7860bd2..f5e906cff1 100644 --- a/pos_order_return/models/pos_order.py +++ b/pos_order_return/models/pos_order.py @@ -2,10 +2,9 @@ # Copyright 2018 David Vidal # Copyright 2018 Lambda IS DOOEL # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). - - from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.tests import Form class PosOrder(models.Model): @@ -111,9 +110,13 @@ def action_pos_order_paid(self): def _create_picking_return(self): self.ensure_one() - picking = self.returned_order_id.picking_ids - ctx = dict(self.env.context, active_ids=picking.ids, active_id=picking.id) - wizard = self.env["stock.return.picking"].with_context(ctx).create({}) + return_form = Form( + self.env["stock.return.picking"].with_context( + active_id=self.returned_order_id.picking_ids.id, + active_model="stock.picking", + ) + ) + wizard = return_form.save() # Discard not returned lines wizard.product_return_moves.filtered( lambda x: x.product_id not in self.mapped("lines.product_id") @@ -127,20 +130,26 @@ def _create_picking_return(self): if to_return[move.product_id] < move.quantity: move.quantity = to_return[move.product_id] to_return[move.product_id] -= move.quantity - return wizard + picking = self.env["stock.picking"].browse(wizard.create_returns()["res_id"]) + for move in picking.move_lines: + move.quantity_done = move.product_uom_qty + picking._action_done() + picking.write( + { + "pos_session_id": self.session_id.id, + "pos_order_id": self.id, + "origin": self.name, + } + ) + return picking def _create_order_picking(self): """Odoo bases return picking if the quantities are negative, but it's not linked to the original one""" - orders = self.filtered( - lambda x: not x.returned_order_id or not x.returned_order_id.picking_ids - ) - res = super()._create_order_picking() - for order in self - orders: - wizard = order._create_picking_return() - res = wizard.create_returns() - order.write({"picking_ids": (6, 0, [res["res_id"]])}) - return res + self.ensure_one() + if not self.returned_order_id.picking_ids: + return super()._create_order_picking() + self.picking_ids = self._create_picking_return() class PosOrderLine(models.Model): diff --git a/pos_order_return/readme/ROADMAP.rst b/pos_order_return/readme/ROADMAP.rst index d5b189d5ac..b137a20ce6 100644 --- a/pos_order_return/readme/ROADMAP.rst +++ b/pos_order_return/readme/ROADMAP.rst @@ -1,3 +1,4 @@ -When migrating the module ``pos_order_return`` in version > 12.0 please merge -both modules ``pos_order_return`` and ``pos_order_return_traceability`` into a -single module. +* When migrating the module ``pos_order_return`` in version > 12.0 please merge + both modules ``pos_order_return`` and ``pos_order_return_traceability`` into a + single module. +* Stock update on session close is uncovered right now. diff --git a/pos_order_return/static/description/index.html b/pos_order_return/static/description/index.html index 5af0400270..5722a1eac5 100644 --- a/pos_order_return/static/description/index.html +++ b/pos_order_return/static/description/index.html @@ -3,7 +3,7 @@ - + Point of Sale Order Return