From d1505d370524604dfacb21d24e6dcd6be56b28ba Mon Sep 17 00:00:00 2001 From: "chafique.delli" Date: Fri, 28 Jun 2024 10:28:15 +0200 Subject: [PATCH] [16.0][ADD] rma_product_exchange --- rma/wizards/rma_make_picking.py | 11 +++--- rma_product_exchange/README.rst | 34 ++++++++++++++++++ rma_product_exchange/__init__.py | 2 ++ rma_product_exchange/__manifest__.py | 21 +++++++++++ rma_product_exchange/models/__init__.py | 3 ++ .../models/procurement_group.py | 20 +++++++++++ rma_product_exchange/models/rma_operation.py | 11 ++++++ rma_product_exchange/models/rma_order_line.py | 13 +++++++ rma_product_exchange/tests/__init__.py | 1 + .../tests/test_rma_product_exchange.py | 28 +++++++++++++++ .../views/rma_operation_view.xml | 24 +++++++++++++ .../views/rma_order_line_view.xml | 21 +++++++++++ rma_product_exchange/views/rma_order_view.xml | 19 ++++++++++ rma_product_exchange/wizards/__init__.py | 1 + .../wizards/rma_make_picking.py | 36 +++++++++++++++++++ .../odoo/addons/rma_product_exchange | 1 + setup/rma_product_exchange/setup.py | 6 ++++ 17 files changed, 248 insertions(+), 4 deletions(-) create mode 100644 rma_product_exchange/README.rst create mode 100644 rma_product_exchange/__init__.py create mode 100644 rma_product_exchange/__manifest__.py create mode 100644 rma_product_exchange/models/__init__.py create mode 100644 rma_product_exchange/models/procurement_group.py create mode 100644 rma_product_exchange/models/rma_operation.py create mode 100644 rma_product_exchange/models/rma_order_line.py create mode 100644 rma_product_exchange/tests/__init__.py create mode 100644 rma_product_exchange/tests/test_rma_product_exchange.py create mode 100644 rma_product_exchange/views/rma_operation_view.xml create mode 100644 rma_product_exchange/views/rma_order_line_view.xml create mode 100644 rma_product_exchange/views/rma_order_view.xml create mode 100644 rma_product_exchange/wizards/__init__.py create mode 100644 rma_product_exchange/wizards/rma_make_picking.py create mode 120000 setup/rma_product_exchange/odoo/addons/rma_product_exchange create mode 100644 setup/rma_product_exchange/setup.py diff --git a/rma/wizards/rma_make_picking.py b/rma/wizards/rma_make_picking.py index 637caea9b..bf0a3209c 100644 --- a/rma/wizards/rma_make_picking.py +++ b/rma/wizards/rma_make_picking.py @@ -137,6 +137,10 @@ def _get_procurement_data(self, item, group, qty, picking_type): } return procurement_data + @api.model + def _get_product(self, item): + return item.line_id.product_id + @api.model def _create_procurement(self, item, picking_type): errors = [] @@ -149,7 +153,7 @@ def _create_procurement(self, item, picking_type): else: qty = item.qty_to_deliver values = self._get_procurement_data(item, group, qty, picking_type) - product = item.line_id.product_id + product = self._get_product(item) if float_compare(qty, 0, product.uom_id.rounding) != 1: raise ValidationError( _( @@ -162,16 +166,15 @@ def _create_procurement(self, item, picking_type): procurements = [] try: procurement = group.Procurement( - item.line_id.product_id, + product, qty, - item.line_id.product_id.product_tmpl_id.uom_id, + product.product_tmpl_id.uom_id, values.get("location_id"), values.get("origin"), values.get("origin"), self.env.company, values, ) - procurements.append(procurement) # Trigger a route check with a mutable in the context that can be # cleared after the first rule selection diff --git a/rma_product_exchange/README.rst b/rma_product_exchange/README.rst new file mode 100644 index 000000000..79c63e777 --- /dev/null +++ b/rma_product_exchange/README.rst @@ -0,0 +1,34 @@ +.. image:: https://img.shields.io/badge/licence-LGPL--3-blue.svg + :alt: License LGPL-3 + +==================== +RMA Product Exchange +==================== + +This module allows you to: + +#. to exchange one product for another in an RMA. + +Usage +===== + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, please +check there if your issue has already been reported. If you spotted it first, +help us smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Contributors +------------ + +* Chafique DELLI + +Maintainer +---------- + +This module is maintained by Eficent diff --git a/rma_product_exchange/__init__.py b/rma_product_exchange/__init__.py new file mode 100644 index 000000000..aee8895e7 --- /dev/null +++ b/rma_product_exchange/__init__.py @@ -0,0 +1,2 @@ +from . import models +from . import wizards diff --git a/rma_product_exchange/__manifest__.py b/rma_product_exchange/__manifest__.py new file mode 100644 index 000000000..1b7f8da52 --- /dev/null +++ b/rma_product_exchange/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2024 Akretion +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +{ + "name": "RMA Product Exchange", + "version": "16.0.1.0.0", + "license": "LGPL-3", + "category": "RMA", + "summary": "Manage RMA for product exchange", + "author": "Akretion, ForgeFlow", + "website": "https://github.com/ForgeFlow/stock-rma", + "depends": [ + "rma", + ], + "data": [ + "views/rma_order_view.xml", + "views/rma_operation_view.xml", + "views/rma_order_line_view.xml", + ], + "installable": True, +} diff --git a/rma_product_exchange/models/__init__.py b/rma_product_exchange/models/__init__.py new file mode 100644 index 000000000..dd15faa65 --- /dev/null +++ b/rma_product_exchange/models/__init__.py @@ -0,0 +1,3 @@ +from . import rma_order_line +from . import rma_operation +from . import procurement_group diff --git a/rma_product_exchange/models/procurement_group.py b/rma_product_exchange/models/procurement_group.py new file mode 100644 index 000000000..ec85d2095 --- /dev/null +++ b/rma_product_exchange/models/procurement_group.py @@ -0,0 +1,20 @@ +# Copyright 2024 Akretion +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from odoo import api, models + + +class ProcurementGroup(models.Model): + _inherit = "procurement.group" + + @api.model + def run(self, procurements, raise_user_error=True): + if self.env.context.get("rma_item"): + new_procurements = [] + for procurement in procurements: + new_procurements.append( + procurement._replace( + product_id=self._context.get("rma_item").product_id + ) + ) + procurements = new_procurements + return super().run(procurements, raise_user_error=raise_user_error) diff --git a/rma_product_exchange/models/rma_operation.py b/rma_product_exchange/models/rma_operation.py new file mode 100644 index 000000000..398a8968f --- /dev/null +++ b/rma_product_exchange/models/rma_operation.py @@ -0,0 +1,11 @@ +# Copyright 2024 Akretion +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from odoo import fields, models + + +class RmaOperation(models.Model): + _inherit = "rma.operation" + + product_exchange = fields.Boolean( + help="Check if you wish to authorize product exchange.", default=False + ) diff --git a/rma_product_exchange/models/rma_order_line.py b/rma_product_exchange/models/rma_order_line.py new file mode 100644 index 000000000..486a3bb4b --- /dev/null +++ b/rma_product_exchange/models/rma_order_line.py @@ -0,0 +1,13 @@ +# Copyright 2024 Akretion +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) +from odoo import fields, models + + +class RmaOrderLine(models.Model): + _inherit = "rma.order.line" + + new_product_id = fields.Many2one( + comodel_name="product.product", + tracking=True, + ) + product_exchange = fields.Boolean(related="operation_id.product_exchange") diff --git a/rma_product_exchange/tests/__init__.py b/rma_product_exchange/tests/__init__.py new file mode 100644 index 000000000..5e38e638b --- /dev/null +++ b/rma_product_exchange/tests/__init__.py @@ -0,0 +1 @@ +from . import test_rma_product_exchange diff --git a/rma_product_exchange/tests/test_rma_product_exchange.py b/rma_product_exchange/tests/test_rma_product_exchange.py new file mode 100644 index 000000000..d123c09e4 --- /dev/null +++ b/rma_product_exchange/tests/test_rma_product_exchange.py @@ -0,0 +1,28 @@ +# Copyright 2024 Akretion +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo.addons.rma.tests.test_rma import TestRma + + +class TestRmaProductExchange(TestRma): + @classmethod + def setUpClass(cls): + super().setUpClass() + + def test_customer_rma_with_product_exchange(self): + self.rma_cust_replace_op_id.product_exchange = True + self.rma_customer_id.rma_line_ids.delivery_policy = "ordered" + self.rma_customer_id.rma_line_ids[0].new_product_id = self.product_id + self.rma_customer_id.rma_line_ids.action_rma_to_approve() + wizard = self.rma_make_picking.with_context( + **{ + "active_ids": self.rma_customer_id.rma_line_ids.ids, + "active_model": "rma.order.line", + "picking_type": "outgoing", + "active_id": 1, + } + ).create({}) + wizard._create_picking() + res = self.rma_customer_id.action_view_out_shipments() + picking = self.env["stock.picking"].browse(res["res_id"]) + self.assertEqual(picking.move_ids[0].product_id, self.product_id) diff --git a/rma_product_exchange/views/rma_operation_view.xml b/rma_product_exchange/views/rma_operation_view.xml new file mode 100644 index 000000000..cf3c12b16 --- /dev/null +++ b/rma_product_exchange/views/rma_operation_view.xml @@ -0,0 +1,24 @@ + + + + + rma.operation + + + + + + + + + + rma.operation + + + + + + + + + diff --git a/rma_product_exchange/views/rma_order_line_view.xml b/rma_product_exchange/views/rma_order_line_view.xml new file mode 100644 index 000000000..159910d27 --- /dev/null +++ b/rma_product_exchange/views/rma_order_line_view.xml @@ -0,0 +1,21 @@ + + + + + rma.order.line + + + + + + + + + + diff --git a/rma_product_exchange/views/rma_order_view.xml b/rma_product_exchange/views/rma_order_view.xml new file mode 100644 index 000000000..a5a14b51c --- /dev/null +++ b/rma_product_exchange/views/rma_order_view.xml @@ -0,0 +1,19 @@ + + + + rma.order + + + + + + + + + diff --git a/rma_product_exchange/wizards/__init__.py b/rma_product_exchange/wizards/__init__.py new file mode 100644 index 000000000..8a3b735f4 --- /dev/null +++ b/rma_product_exchange/wizards/__init__.py @@ -0,0 +1 @@ +from . import rma_make_picking diff --git a/rma_product_exchange/wizards/rma_make_picking.py b/rma_product_exchange/wizards/rma_make_picking.py new file mode 100644 index 000000000..ebc36a2c6 --- /dev/null +++ b/rma_product_exchange/wizards/rma_make_picking.py @@ -0,0 +1,36 @@ +# Copyright (C) 2024 Akretion +# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html) + +from odoo import api, models + + +class RmaMakePicking(models.TransientModel): + _inherit = "rma_make_picking.wizard" + + @api.returns("rma.order.line") + def _prepare_item(self, line): + values = super()._prepare_item(line) + if ( + self.env.context.get("picking_type") == "outgoing" + and line.product_exchange + and line.new_product_id + ): + values["product_id"] = line.new_product_id.id + return values + + @api.model + def _create_procurement(self, item, picking_type): + if self.env.context.get("picking_type") == "outgoing": + self = self.with_context(rma_item=item) + return super()._create_procurement(item, picking_type) + + @api.model + def _get_product(self, item): + product = super()._get_product(item) + if ( + self.env.context.get("picking_type") == "outgoing" + and item.line_id.product_exchange + and item.line_id.new_product_id + ): + product = item.line_id.new_product_id + return product diff --git a/setup/rma_product_exchange/odoo/addons/rma_product_exchange b/setup/rma_product_exchange/odoo/addons/rma_product_exchange new file mode 120000 index 000000000..c47713064 --- /dev/null +++ b/setup/rma_product_exchange/odoo/addons/rma_product_exchange @@ -0,0 +1 @@ +../../../../rma_product_exchange \ No newline at end of file diff --git a/setup/rma_product_exchange/setup.py b/setup/rma_product_exchange/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/rma_product_exchange/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)