diff --git a/setup/stock_picking_return_restricted_qty/odoo/addons/stock_picking_return_restricted_qty b/setup/stock_picking_return_restricted_qty/odoo/addons/stock_picking_return_restricted_qty new file mode 120000 index 000000000000..4f7ad6659af9 --- /dev/null +++ b/setup/stock_picking_return_restricted_qty/odoo/addons/stock_picking_return_restricted_qty @@ -0,0 +1 @@ +../../../../stock_picking_return_restricted_qty \ No newline at end of file diff --git a/setup/stock_picking_return_restricted_qty/setup.py b/setup/stock_picking_return_restricted_qty/setup.py new file mode 100644 index 000000000000..28c57bb64031 --- /dev/null +++ b/setup/stock_picking_return_restricted_qty/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/stock_picking_return_restricted_qty/README.rst b/stock_picking_return_restricted_qty/README.rst new file mode 100644 index 000000000000..bc03d9af56d2 --- /dev/null +++ b/stock_picking_return_restricted_qty/README.rst @@ -0,0 +1,99 @@ +=================================== +Stock Picking Return Restricted Qty +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:53be3c3c8c4c46df50af7083ad7ad918a59add0e68c12d41718a261a2d727a70 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fstock--logistics--workflow-lightgray.png?logo=github + :target: https://github.com/OCA/stock-logistics-workflow/tree/16.0/stock_picking_return_restricted_qty + :alt: OCA/stock-logistics-workflow +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/stock-logistics-workflow-16-0/stock-logistics-workflow-16-0-stock_picking_return_restricted_qty + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png + :target: https://runboat.odoo-community.org/builds?repo=OCA/stock-logistics-workflow&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module doesn't allow you to return more quantity of products than the +delivered. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +To manage analytic account tags using this module, you need to: + +#. Navigate to **Inventory > Configuration > Operations Types**. +#. Select the Operation Type for which you want to enable the restriction. +#. Check the box "Restrict Return Quantity" and save. + +Usage +===== + +To use this module, you need to: + +#. Create an outgoing or an internal picking. +#. Include one or more products with enough stock. +#. Validate the picking. +#. Try to return more products than the delivered and see the warning message. + +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 to smash it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Tecnativa + +Contributors +~~~~~~~~~~~~ + +* `Tecnativa `_: + + * Carlos Roca + * Sergio Teruel + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/stock-logistics-workflow `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/stock_picking_return_restricted_qty/__init__.py b/stock_picking_return_restricted_qty/__init__.py new file mode 100644 index 000000000000..f7a5a9de0579 --- /dev/null +++ b/stock_picking_return_restricted_qty/__init__.py @@ -0,0 +1,4 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import wizard +from . import models diff --git a/stock_picking_return_restricted_qty/__manifest__.py b/stock_picking_return_restricted_qty/__manifest__.py new file mode 100644 index 000000000000..09262215abe5 --- /dev/null +++ b/stock_picking_return_restricted_qty/__manifest__.py @@ -0,0 +1,15 @@ +# Copyright 2020 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +{ + "name": "Stock Picking Return Restricted Qty", + "summary": "Restrict the return to delivered quantity", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "Tecnativa," "Odoo Community Association (OCA)", + "website": "https://github.com/OCA/stock-logistics-workflow", + "depends": ["stock"], + "data": [ + "views/stock_picking_type_views.xml", + ], +} diff --git a/stock_picking_return_restricted_qty/i18n/es.po b/stock_picking_return_restricted_qty/i18n/es.po new file mode 100644 index 000000000000..113d8e820ff4 --- /dev/null +++ b/stock_picking_return_restricted_qty/i18n/es.po @@ -0,0 +1,34 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_picking_return_restricted_qty +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0+e\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2020-03-09 10:45+0000\n" +"PO-Revision-Date: 2023-10-28 18:52+0000\n" +"Last-Translator: Ivorra78 \n" +"Language-Team: \n" +"Language: es\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking +msgid "Return Picking" +msgstr "Albarán de devolución" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking_line +msgid "Return Picking Line" +msgstr "Línea de Albarán de devolución" + +#. module: stock_picking_return_restricted_qty +#: code:addons/stock_picking_return_restricted_qty/wizard/stock_picking_return.py:0 +#, python-format +msgid "Return more quantities than delivered is not allowed." +msgstr "No está permitido devolver más cantidades de las entregadas." diff --git a/stock_picking_return_restricted_qty/i18n/it.po b/stock_picking_return_restricted_qty/i18n/it.po new file mode 100644 index 000000000000..11bb1de04a02 --- /dev/null +++ b/stock_picking_return_restricted_qty/i18n/it.po @@ -0,0 +1,34 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_picking_return_restricted_qty +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2023-11-24 10:33+0000\n" +"Last-Translator: mymage \n" +"Language-Team: none\n" +"Language: it\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=2; plural=n != 1;\n" +"X-Generator: Weblate 4.17\n" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking +msgid "Return Picking" +msgstr "Prelievo di reso" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking_line +msgid "Return Picking Line" +msgstr "Riga prelievo di reso" + +#. module: stock_picking_return_restricted_qty +#: code:addons/stock_picking_return_restricted_qty/wizard/stock_picking_return.py:0 +#: code:addons/stock_picking_return_restricted_qty/wizard/stock_picking_return.py:0 +#, python-format +msgid "Return more quantities than delivered is not allowed." +msgstr "Non è permesso rendere una quantità maggiore del consegnato." diff --git a/stock_picking_return_restricted_qty/i18n/stock_picking_return_restricted_qty.pot b/stock_picking_return_restricted_qty/i18n/stock_picking_return_restricted_qty.pot new file mode 100644 index 000000000000..d81cd38dd079 --- /dev/null +++ b/stock_picking_return_restricted_qty/i18n/stock_picking_return_restricted_qty.pot @@ -0,0 +1,31 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_picking_return_restricted_qty +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 15.0\n" +"Report-Msgid-Bugs-To: \n" +"Last-Translator: \n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking +msgid "Return Picking" +msgstr "" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking_line +msgid "Return Picking Line" +msgstr "" + +#. module: stock_picking_return_restricted_qty +#: code:addons/stock_picking_return_restricted_qty/wizard/stock_picking_return.py:0 +#: code:addons/stock_picking_return_restricted_qty/wizard/stock_picking_return.py:0 +#, python-format +msgid "Return more quantities than delivered is not allowed." +msgstr "" diff --git a/stock_picking_return_restricted_qty/i18n/zh_CN.po b/stock_picking_return_restricted_qty/i18n/zh_CN.po new file mode 100644 index 000000000000..9f8b46808b7c --- /dev/null +++ b/stock_picking_return_restricted_qty/i18n/zh_CN.po @@ -0,0 +1,33 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * stock_picking_return_restricted_qty +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 13.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2020-05-02 13:19+0000\n" +"Last-Translator: Dong \n" +"Language-Team: none\n" +"Language: zh_CN\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: nplurals=1; plural=0;\n" +"X-Generator: Weblate 3.10\n" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking +msgid "Return Picking" +msgstr "退货调拨单" + +#. module: stock_picking_return_restricted_qty +#: model:ir.model,name:stock_picking_return_restricted_qty.model_stock_return_picking_line +msgid "Return Picking Line" +msgstr "退货调拨明细" + +#. module: stock_picking_return_restricted_qty +#: code:addons/stock_picking_return_restricted_qty/wizard/stock_picking_return.py:0 +#, python-format +msgid "Return more quantities than delivered is not allowed." +msgstr "退货数量不允许超出送货数量。" diff --git a/stock_picking_return_restricted_qty/models/__init__.py b/stock_picking_return_restricted_qty/models/__init__.py new file mode 100644 index 000000000000..576c033a62ab --- /dev/null +++ b/stock_picking_return_restricted_qty/models/__init__.py @@ -0,0 +1 @@ +from . import stock_picking_type diff --git a/stock_picking_return_restricted_qty/models/stock_picking_type.py b/stock_picking_return_restricted_qty/models/stock_picking_type.py new file mode 100644 index 000000000000..355f2997c40b --- /dev/null +++ b/stock_picking_return_restricted_qty/models/stock_picking_type.py @@ -0,0 +1,16 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StockPickingType(models.Model): + _inherit = "stock.picking.type" + + restrict_return_qty = fields.Boolean( + string="Restrict Return Quantity", + help=( + "Enable this option to restrict returning more quantities " + "than delivered." + ), + default=False, + ) diff --git a/stock_picking_return_restricted_qty/readme/CONFIGURE.rst b/stock_picking_return_restricted_qty/readme/CONFIGURE.rst new file mode 100644 index 000000000000..a6e39015c30c --- /dev/null +++ b/stock_picking_return_restricted_qty/readme/CONFIGURE.rst @@ -0,0 +1,5 @@ +To manage analytic account tags using this module, you need to: + +#. Navigate to **Inventory > Configuration > Operations Types**. +#. Select the Operation Type for which you want to enable the restriction. +#. Check the box "Restrict Return Quantity" and save. diff --git a/stock_picking_return_restricted_qty/readme/CONTRIBUTORS.rst b/stock_picking_return_restricted_qty/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000000..88dc1504c1fd --- /dev/null +++ b/stock_picking_return_restricted_qty/readme/CONTRIBUTORS.rst @@ -0,0 +1,4 @@ +* `Tecnativa `_: + + * Carlos Roca + * Sergio Teruel diff --git a/stock_picking_return_restricted_qty/readme/DESCRIPTION.rst b/stock_picking_return_restricted_qty/readme/DESCRIPTION.rst new file mode 100644 index 000000000000..681ac9f94254 --- /dev/null +++ b/stock_picking_return_restricted_qty/readme/DESCRIPTION.rst @@ -0,0 +1,2 @@ +This module doesn't allow you to return more quantity of products than the +delivered. diff --git a/stock_picking_return_restricted_qty/readme/USAGE.rst b/stock_picking_return_restricted_qty/readme/USAGE.rst new file mode 100644 index 000000000000..adba34cc49eb --- /dev/null +++ b/stock_picking_return_restricted_qty/readme/USAGE.rst @@ -0,0 +1,6 @@ +To use this module, you need to: + +#. Create an outgoing or an internal picking. +#. Include one or more products with enough stock. +#. Validate the picking. +#. Try to return more products than the delivered and see the warning message. diff --git a/stock_picking_return_restricted_qty/static/description/icon.png b/stock_picking_return_restricted_qty/static/description/icon.png new file mode 100644 index 000000000000..3a0328b516c4 Binary files /dev/null and b/stock_picking_return_restricted_qty/static/description/icon.png differ diff --git a/stock_picking_return_restricted_qty/static/description/index.html b/stock_picking_return_restricted_qty/static/description/index.html new file mode 100644 index 000000000000..38140ae4de2e --- /dev/null +++ b/stock_picking_return_restricted_qty/static/description/index.html @@ -0,0 +1,446 @@ + + + + + +Stock Picking Return Restricted Qty + + + +
+

Stock Picking Return Restricted Qty

+ + +

Beta License: AGPL-3 OCA/stock-logistics-workflow Translate me on Weblate Try me on Runboat

+

This module doesn’t allow you to return more quantity of products than the +delivered.

+

Table of contents

+ +
+

Configuration

+

To manage analytic account tags using this module, you need to:

+
    +
  1. Navigate to Inventory > Configuration > Operations Types.
  2. +
  3. Select the Operation Type for which you want to enable the restriction.
  4. +
  5. Check the box “Restrict Return Quantity” and save.
  6. +
+
+
+

Usage

+

To use this module, you need to:

+
    +
  1. Create an outgoing or an internal picking.
  2. +
  3. Include one or more products with enough stock.
  4. +
  5. Validate the picking.
  6. +
  7. Try to return more products than the delivered and see the warning message.
  8. +
+
+
+

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 to smash it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Tecnativa
  • +
+
+
+

Contributors

+
    +
  • Tecnativa:
      +
    • Carlos Roca
    • +
    • Sergio Teruel
    • +
    +
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/stock-logistics-workflow project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/stock_picking_return_restricted_qty/tests/__init__.py b/stock_picking_return_restricted_qty/tests/__init__.py new file mode 100644 index 000000000000..bef8cf4a5532 --- /dev/null +++ b/stock_picking_return_restricted_qty/tests/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_stock_picking_return_restricted_qty diff --git a/stock_picking_return_restricted_qty/tests/test_stock_picking_return_restricted_qty.py b/stock_picking_return_restricted_qty/tests/test_stock_picking_return_restricted_qty.py new file mode 100644 index 000000000000..49980abbb8df --- /dev/null +++ b/stock_picking_return_restricted_qty/tests/test_stock_picking_return_restricted_qty.py @@ -0,0 +1,126 @@ +# Copyright 2020 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.exceptions import UserError +from odoo.tests import Form, common + + +class StockPickingReturnRestrictedQtyTest(common.TransactionCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + # Remove this variable in v16 and put instead: + # from odoo.addons.base.tests.common import DISABLED_MAIL_CONTEXT + DISABLED_MAIL_CONTEXT = { + "tracking_disable": True, + "mail_create_nolog": True, + "mail_create_nosubscribe": True, + "mail_notrack": True, + "no_reset_password": True, + } + cls.env = cls.env(context=dict(cls.env.context, **DISABLED_MAIL_CONTEXT)) + partner = cls.env["res.partner"].create({"name": "Test"}) + product = cls.env["product.product"].create( + {"name": "test_product", "type": "product"} + ) + picking_type_out = cls.env.ref("stock.picking_type_out") + stock_location = cls.env.ref("stock.stock_location_stock") + customer_location = cls.env.ref("stock.stock_location_customers") + cls.picking = cls.env["stock.picking"].create( + { + "partner_id": partner.id, + "picking_type_id": picking_type_out.id, + "location_id": stock_location.id, + "location_dest_id": customer_location.id, + "move_ids": [ + ( + 0, + 0, + { + "name": product.name, + "product_id": product.id, + "product_uom_qty": 20, + "product_uom": product.uom_id.id, + "location_id": stock_location.id, + "location_dest_id": customer_location.id, + }, + ) + ], + } + ) + cls.picking.action_confirm() + cls.picking.action_assign() + cls.picking.move_ids[:1].quantity_done = 20 + cls.picking.button_validate() + + def get_return_picking_wizard(self, picking): + stock_return_picking_form = Form( + self.env["stock.return.picking"].with_context( + active_ids=picking.ids, + active_id=picking.ids[0], + active_model="stock.picking", + ) + ) + return stock_return_picking_form.save() + + def test_return_not_allowed(self): + """On this test we create a return picking with more quantity + than the quantity that client have on his hand""" + self.picking.picking_type_id.restrict_return_qty = True + return_picking = self.get_return_picking_wizard(self.picking) + self.assertEqual(return_picking.product_return_moves.quantity, 20) + return_picking.product_return_moves.quantity = 30 + with self.assertRaises(UserError): + return_picking._create_returns() + + def test_return_without_restriction(self): + """On this test we create a return picking with more quantity + than the quantity that client have on his hand + without the quantity restriction""" + self.picking.picking_type_id.restrict_return_qty = False + return_picking = self.get_return_picking_wizard(self.picking) + self.assertEqual(return_picking.product_return_moves.quantity, 20) + return_picking.product_return_moves.quantity = 30 + return_picking._create_returns() + self.assertEqual(return_picking.product_return_moves.quantity, 30) + + def test_multiple_return(self): + """On this test we are going to follow a sequence that a client + can follow if he tries to return a product""" + self.picking.picking_type_id.restrict_return_qty = True + wiz = self.get_return_picking_wizard(self.picking) + wiz.product_return_moves.quantity = 10 + picking_returned_id = wiz._create_returns()[0] + picking_returned = self.env["stock.picking"].browse(picking_returned_id) + + wiz = self.get_return_picking_wizard(self.picking) + self.assertEqual(wiz.product_return_moves.quantity, 10) + + picking_returned._action_done() + wiz = self.get_return_picking_wizard(self.picking) + self.assertEqual(wiz.product_return_moves.quantity, 10) + + wiz.product_return_moves.quantity = 80 + with self.assertRaises(UserError): + wiz.product_return_moves._onchange_quantity() + + def test_multiple_return_without_restriction(self): + """On this test we are going to follow a sequence that a client + can follow if he tries to return a product + without the quantity restriction""" + self.picking.picking_type_id.restrict_return_qty = False + wiz = self.get_return_picking_wizard(self.picking) + wiz.product_return_moves.quantity = 10 + picking_returned_id = wiz._create_returns()[0] + picking_returned = self.env["stock.picking"].browse(picking_returned_id) + + wiz = self.get_return_picking_wizard(self.picking) + self.assertEqual(wiz.product_return_moves.quantity, 10) + + picking_returned._action_done() + wiz = self.get_return_picking_wizard(self.picking) + self.assertEqual(wiz.product_return_moves.quantity, 10) + + wiz.product_return_moves.quantity = 80 + wiz.product_return_moves._onchange_quantity() + self.assertEqual(wiz.product_return_moves.quantity, 80) diff --git a/stock_picking_return_restricted_qty/views/stock_picking_type_views.xml b/stock_picking_return_restricted_qty/views/stock_picking_type_views.xml new file mode 100644 index 000000000000..fb6c98c16b29 --- /dev/null +++ b/stock_picking_return_restricted_qty/views/stock_picking_type_views.xml @@ -0,0 +1,13 @@ + + + + stock.picking.type.restrict.return + stock.picking.type + + + + + + + + diff --git a/stock_picking_return_restricted_qty/wizard/__init__.py b/stock_picking_return_restricted_qty/wizard/__init__.py new file mode 100644 index 000000000000..f8de18c1e06f --- /dev/null +++ b/stock_picking_return_restricted_qty/wizard/__init__.py @@ -0,0 +1,3 @@ +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import stock_picking_return diff --git a/stock_picking_return_restricted_qty/wizard/stock_picking_return.py b/stock_picking_return_restricted_qty/wizard/stock_picking_return.py new file mode 100644 index 000000000000..f02412deb9bc --- /dev/null +++ b/stock_picking_return_restricted_qty/wizard/stock_picking_return.py @@ -0,0 +1,69 @@ +# Copyright 2020 Tecnativa - Carlos Roca +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo import _, api, models +from odoo.exceptions import UserError +from odoo.tools import float_compare + + +class ReturnPicking(models.TransientModel): + _inherit = "stock.return.picking" + + @api.model + def _prepare_stock_return_picking_line_vals_from_move(self, stock_move): + val = super()._prepare_stock_return_picking_line_vals_from_move(stock_move) + return_lines = self.env["stock.return.picking.line"] + val["quantity"] = return_lines.get_returned_restricted_quantity(stock_move) + return val + + def _create_returns(self): + restrict_return_qty = self.picking_id.picking_type_id.restrict_return_qty + + precision = self.env["decimal.precision"].precision_get( + "Product Unit of Measure" + ) + for return_line in self.product_return_moves: + quantity = return_line.get_returned_restricted_quantity(return_line.move_id) + + if restrict_return_qty and ( + float_compare( + return_line.quantity, quantity, precision_digits=precision + ) + > 0 + ): + raise UserError( + _("Return more quantities than delivered is not allowed.") + ) + return super()._create_returns() + + +class ReturnPickingLine(models.TransientModel): + _inherit = "stock.return.picking.line" + + @api.onchange("quantity") + def _onchange_quantity(self): + restrict_return_qty = ( + self.move_id.picking_id.picking_type_id.restrict_return_qty + ) + + qty = self.get_returned_restricted_quantity(self.move_id) + + if restrict_return_qty and self.quantity > qty: + raise UserError(_("Return more quantities than delivered is not allowed.")) + + def get_returned_restricted_quantity(self, stock_move): + """This function is created to know how many products + have the person who tries to create a return picking + on his hand.""" + qty = stock_move.product_qty + for line in stock_move.move_dest_ids.mapped("move_line_ids"): + if ( + line.move_id.origin_returned_move_id + and line.move_id.origin_returned_move_id != stock_move + ): + continue + if line.state in {"partially_available", "assigned"}: + qty -= line.reserved_qty + elif line.state == "done": + qty -= line.qty_done + return max(qty, 0.0)