diff --git a/mrp_subcontracting_skip_no_negative/README.rst b/mrp_subcontracting_skip_no_negative/README.rst new file mode 100644 index 0000000000..1519c736f2 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/README.rst @@ -0,0 +1,100 @@ +=================================== +MRP Subcontracting Skip No Negative +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:b4b59b86062f08db75f2e86b8b9fced861771da8483adf65c81e3a65c0ba1c79 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fmanufacture-lightgray.png?logo=github + :target: https://github.com/OCA/manufacture/tree/17.0/mrp_subcontracting_skip_no_negative + :alt: OCA/manufacture +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/manufacture-17-0/manufacture-17-0-mrp_subcontracting_skip_no_negative + :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/manufacture&target_branch=17.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module skips the negative quantity check, provided by +stock_no_negative, for subcontracting receipts. + +Background: +----------- + +Odoo processes subcontracting receipt in the order of: + +1. Transfer of the subcontracted product from the subcontractor location +to the internal location. 2. Production of the subcontracted product in +the subcontractor location. + +This sequence does not represent the reality where production is done +before transfer, and therefore the above Step 1 would fail with negative +stock in the subcontractor location, when stock_no_negative is +installed, unless the product/location is configured to allow negative +stock. + +ref. https://github.com/odoo/odoo/pull/75065 + +**Table of contents** + +.. contents:: + :local: + +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 +------- + +* Quartile Limited + +Contributors +------------ + +- `Quartile `__: + + - Aung Ko Ko Lin + +- `Tecnativa `__: + + - Víctor Martínez + +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/manufacture `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/mrp_subcontracting_skip_no_negative/__init__.py b/mrp_subcontracting_skip_no_negative/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mrp_subcontracting_skip_no_negative/__manifest__.py b/mrp_subcontracting_skip_no_negative/__manifest__.py new file mode 100644 index 0000000000..9d6bdc4887 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/__manifest__.py @@ -0,0 +1,13 @@ +# Copyright 2023 Quartile Limited +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) +{ + "name": "MRP Subcontracting Skip No Negative", + "version": "17.0.1.0.0", + "license": "AGPL-3", + "author": "Quartile Limited, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/manufacture", + "category": "Manufacturing", + "depends": ["mrp_subcontracting", "stock_no_negative"], + "installable": True, + "auto_install": True, +} diff --git a/mrp_subcontracting_skip_no_negative/i18n/es.po b/mrp_subcontracting_skip_no_negative/i18n/es.po new file mode 100644 index 0000000000..1aa15554ed --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/i18n/es.po @@ -0,0 +1,43 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mrp_subcontracting_skip_no_negative +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 14.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2024-11-05 07:53+0000\n" +"PO-Revision-Date: 2024-11-05 08:53+0100\n" +"Last-Translator: \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: \n" +"X-Generator: Poedit 3.0.1\n" + +#. module: mrp_subcontracting_skip_no_negative +#: model:ir.model,name:mrp_subcontracting_skip_no_negative.model_stock_move +msgid "Stock Move" +msgstr "Movimiento de existencias" + +#. module: mrp_subcontracting_skip_no_negative +#: model:ir.model,name:mrp_subcontracting_skip_no_negative.model_stock_picking +msgid "Transfer" +msgstr "Transferencia" + +#. module: mrp_subcontracting_skip_no_negative +#. odoo-python +#: code:addons/mrp_subcontracting_skip_no_negative/models/stock_move.py:0 +#, python-format +msgid "" +"You cannot validate this stock operation because the stock level of the " +"component product '{name}' would become negative ({qty}) on the stock " +"location '{location}' and negative stock is not allowed for this product and/" +"or location." +msgstr "" +"No se puede validar esta operación de stock porque el nivel de stock del " +"producto componente '{name}' se volvería negativo ({qty}) en la ubicación de " +"stock '{location}' y no se permite stock negativo para este producto y/o " +"ubicación." diff --git a/mrp_subcontracting_skip_no_negative/i18n/it.po b/mrp_subcontracting_skip_no_negative/i18n/it.po new file mode 100644 index 0000000000..798424e196 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/i18n/it.po @@ -0,0 +1,42 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mrp_subcontracting_skip_no_negative +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.0\n" +"Report-Msgid-Bugs-To: \n" +"PO-Revision-Date: 2024-11-08 09:35+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 5.6.2\n" + +#. module: mrp_subcontracting_skip_no_negative +#: model:ir.model,name:mrp_subcontracting_skip_no_negative.model_stock_move +msgid "Stock Move" +msgstr "Movimento di magazzino" + +#. module: mrp_subcontracting_skip_no_negative +#: model:ir.model,name:mrp_subcontracting_skip_no_negative.model_stock_picking +msgid "Transfer" +msgstr "Trasferimento" + +#. module: mrp_subcontracting_skip_no_negative +#. odoo-python +#: code:addons/mrp_subcontracting_skip_no_negative/models/stock_move.py:0 +#, python-format +msgid "" +"You cannot validate this stock operation because the stock level of the " +"component product '{name}' would become negative ({qty}) on the stock " +"location '{location}' and negative stock is not allowed for this product and/" +"or location." +msgstr "" +"Non è possibile convalidare questa operazione di magazzino perché il livello " +"di magazzino del prodotto componente '{name}' diventerebbe negativo ({qty}) " +"nell'ubicazione di magazzino '{location}' e per questo prodotto e/o " +"posizione non è consentito un magazzino negativo." diff --git a/mrp_subcontracting_skip_no_negative/i18n/mrp_subcontracting_skip_no_negative.pot b/mrp_subcontracting_skip_no_negative/i18n/mrp_subcontracting_skip_no_negative.pot new file mode 100644 index 0000000000..fbc852f62b --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/i18n/mrp_subcontracting_skip_no_negative.pot @@ -0,0 +1,35 @@ +# Translation of Odoo Server. +# This file contains the translation of the following modules: +# * mrp_subcontracting_skip_no_negative +# +msgid "" +msgstr "" +"Project-Id-Version: Odoo Server 16.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: mrp_subcontracting_skip_no_negative +#: model:ir.model,name:mrp_subcontracting_skip_no_negative.model_stock_move +msgid "Stock Move" +msgstr "" + +#. module: mrp_subcontracting_skip_no_negative +#: model:ir.model,name:mrp_subcontracting_skip_no_negative.model_stock_picking +msgid "Transfer" +msgstr "" + +#. module: mrp_subcontracting_skip_no_negative +#. odoo-python +#: code:addons/mrp_subcontracting_skip_no_negative/models/stock_move.py:0 +#, python-format +msgid "" +"You cannot validate this stock operation because the stock level of the " +"component product '{name}' would become negative ({qty}) on the stock " +"location '{location}' and negative stock is not allowed for this product " +"and/or location." +msgstr "" diff --git a/mrp_subcontracting_skip_no_negative/models/__init__.py b/mrp_subcontracting_skip_no_negative/models/__init__.py new file mode 100644 index 0000000000..a33bde1e87 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/models/__init__.py @@ -0,0 +1,2 @@ +from . import stock_move +from . import stock_picking diff --git a/mrp_subcontracting_skip_no_negative/models/stock_move.py b/mrp_subcontracting_skip_no_negative/models/stock_move.py new file mode 100644 index 0000000000..f3e98443e6 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/models/stock_move.py @@ -0,0 +1,78 @@ +# Copyright 2023 Quartile Limited +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import _, models +from odoo.exceptions import ValidationError +from odoo.tools import config, float_compare + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _action_done(self, cancel_backorder=False): + moves_with_no_check = self.filtered(lambda x: x.is_subcontract).with_context( + skip_negative_qty_check=True + ) + # For rather unlikely occassions where linked production is not in the right + # state. + for move in moves_with_no_check: + production_moves = self.search( + [ + ("move_dest_ids", "=", move.id), + ("state", "not in", ("done", "cancel")), + ] + ) + productions = production_moves.production_id + unassigned_productions = productions.filtered( + lambda p: p.reservation_state != "assigned" + ) + unassigned_productions.action_assign() + if all( + state == "assigned" + for state in unassigned_productions.mapped("reservation_state") + ): + continue + moves_with_no_check -= move + # If you have not been able to allocate previously it is because there is + # no stock, therefore it will leave the stock negative, we deduct the + # quantity checking the components and show the corresponding error. + test_condition = ( + config["test_enable"] and self.env.context.get("test_stock_no_negative") + ) or not config["test_enable"] + if not test_condition: + continue + qty_precision = self.env["decimal.precision"].precision_get( + "Product Unit of Measure" + ) + for p_move in unassigned_productions.move_raw_ids.filtered( + lambda x: x.state != "assigned" + and not x.product_id.allow_negative_stock + and not x.product_id.categ_id.allow_negative_stock + and not x.location_id.allow_negative_stock + ): + product = p_move.product_id.sudo() + location = p_move.location_id + location_qty = product.with_context(location=location.id).free_qty + new_qty = location_qty - p_move.product_uom_qty + if float_compare(new_qty, 0, precision_digits=qty_precision) == -1: + raise ValidationError( + _( + "You cannot validate this stock operation because the " + "stock level of the component product '{name}' would become" + " negative ({qty}) on the stock location '{location}' and " + "negative stock is not allowed for this product and/or " + "location." + ).format( + name=product.display_name, + qty=new_qty, + location=location.complete_name, + ) + ) + res = super(StockMove, self - moves_with_no_check)._action_done( + cancel_backorder=cancel_backorder + ) + res += super(StockMove, moves_with_no_check)._action_done( + cancel_backorder=cancel_backorder + ) + return res diff --git a/mrp_subcontracting_skip_no_negative/models/stock_picking.py b/mrp_subcontracting_skip_no_negative/models/stock_picking.py new file mode 100644 index 0000000000..015c8554ac --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/models/stock_picking.py @@ -0,0 +1,22 @@ +# Copyright 2023 Quartile Limited +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import models + + +class StockPicking(models.Model): + _inherit = "stock.picking" + + def _action_done(self): + res = super()._action_done() + self._check_negative_quants_after_process() + return res + + def _check_negative_quants_after_process(self): + product_ids = self.mapped("move_ids.product_id.id") + quants = self.env["stock.quant"].search( + [ + ("product_id", "in", product_ids), + ] + ) + quants.check_negative_qty() diff --git a/mrp_subcontracting_skip_no_negative/pyproject.toml b/mrp_subcontracting_skip_no_negative/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/mrp_subcontracting_skip_no_negative/readme/CONTRIBUTORS.md b/mrp_subcontracting_skip_no_negative/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..fb5ab8eb00 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/readme/CONTRIBUTORS.md @@ -0,0 +1,4 @@ +- [Quartile](https://www.quartile.co): + - Aung Ko Ko Lin +- [Tecnativa](https://www.tecnativa.com): + - Víctor Martínez diff --git a/mrp_subcontracting_skip_no_negative/readme/DESCRIPTION.md b/mrp_subcontracting_skip_no_negative/readme/DESCRIPTION.md new file mode 100644 index 0000000000..6623e42640 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/readme/DESCRIPTION.md @@ -0,0 +1,18 @@ +This module skips the negative quantity check, provided by +stock_no_negative, for subcontracting receipts. + +## Background: + +Odoo processes subcontracting receipt in the order of: + +1\. Transfer of the subcontracted product from the subcontractor +location to the internal location. 2. Production of the subcontracted +product in the subcontractor location. + +This sequence does not represent the reality where production is done +before transfer, and therefore the above Step 1 would fail with negative +stock in the subcontractor location, when stock_no_negative is +installed, unless the product/location is configured to allow negative +stock. + +ref. diff --git a/mrp_subcontracting_skip_no_negative/static/description/icon.png b/mrp_subcontracting_skip_no_negative/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/mrp_subcontracting_skip_no_negative/static/description/icon.png differ diff --git a/mrp_subcontracting_skip_no_negative/static/description/index.html b/mrp_subcontracting_skip_no_negative/static/description/index.html new file mode 100644 index 0000000000..96f7d1b63d --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/static/description/index.html @@ -0,0 +1,439 @@ + + + + + +MRP Subcontracting Skip No Negative + + + +
+

MRP Subcontracting Skip No Negative

+ + +

Beta License: AGPL-3 OCA/manufacture Translate me on Weblate Try me on Runboat

+

This module skips the negative quantity check, provided by +stock_no_negative, for subcontracting receipts.

+
+

Background:

+

Odoo processes subcontracting receipt in the order of:

+

1. Transfer of the subcontracted product from the subcontractor location +to the internal location. 2. Production of the subcontracted product in +the subcontractor location.

+

This sequence does not represent the reality where production is done +before transfer, and therefore the above Step 1 would fail with negative +stock in the subcontractor location, when stock_no_negative is +installed, unless the product/location is configured to allow negative +stock.

+

ref. https://github.com/odoo/odoo/pull/75065

+

Table of contents

+ +
+

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.

+
+ +
+
+

Authors

+
    +
  • Quartile Limited
  • +
+
+
+

Contributors

+ +
+
+

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/manufacture project on GitHub.

+

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

+
+
+ + diff --git a/mrp_subcontracting_skip_no_negative/tests/__init__.py b/mrp_subcontracting_skip_no_negative/tests/__init__.py new file mode 100644 index 0000000000..7e62122f1f --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mrp_subcontracting_skip_no_negative diff --git a/mrp_subcontracting_skip_no_negative/tests/test_mrp_subcontracting_skip_no_negative.py b/mrp_subcontracting_skip_no_negative/tests/test_mrp_subcontracting_skip_no_negative.py new file mode 100644 index 0000000000..e294969b67 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/tests/test_mrp_subcontracting_skip_no_negative.py @@ -0,0 +1,99 @@ +# Copyright 2023 Quartile Limited +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo.tests import Form +from odoo.tools import mute_logger + +from odoo.addons.mrp_subcontracting.tests.common import TestMrpSubcontractingCommon + + +class TestMrpSubcontractingSkipNoNegative(TestMrpSubcontractingCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env( + context=dict( + cls.env.context, + test_stock_no_negative=True, + ) + ) + picking_form = Form(cls.env["stock.picking"]) + picking_form.picking_type_id = cls.env.ref("stock.picking_type_in") + picking_form.partner_id = cls.subcontractor_partner1 + picking_form.picking_type_id.auto_show_reception_report = True + cls.env.user.groups_id += cls.env.ref("stock.group_reception_report") + with picking_form.move_ids_without_package.new() as move: + move.product_id = cls.finished + move.product_id.type = "product" + move.product_uom_qty = 1 + cls.subcontracting_receipt = picking_form.save() + type_id = picking_form.picking_type_id + cls.move = cls.env["stock.move"].create( + { + "name": "Test Move", + "product_id": cls.finished.id, + "product_uom_qty": 10, + "location_id": type_id.warehouse_id.view_location_id.id, + "location_dest_id": type_id.warehouse_id.view_location_id.id, + "move_orig_ids": False, + "product_uom": cls.env.ref("uom.product_uom_unit").id, + "state": "assigned", + } + ) + + def _create_stock_quant(self, product, qty): + partner1 = self.subcontractor_partner1 + self.env["stock.quant"].create( + { + "product_id": product.id, + "location_id": partner1.property_stock_subcontractor.id, + "quantity": qty, + } + ) + + @mute_logger("odoo.models.unlink") + def test_mrp_subcontracting_skip_no_negative_01(self): + self._create_stock_quant(self.comp1, 10) + self._create_stock_quant(self.comp2, 10) + self.subcontracting_receipt.action_confirm() + self.assertEqual(self.subcontracting_receipt.state, "assigned") + immediate_wizard = self.subcontracting_receipt.sudo().button_validate() + self.assertEqual( + immediate_wizard.get("res_model"), "report.stock.report_reception" + ) + immediate_wizard_form = self.env[immediate_wizard["res_model"]].with_context( + **immediate_wizard["context"] + ) + self.assertFalse(immediate_wizard_form) + self.assertEqual(self.subcontracting_receipt.state, "done") + + def test_mrp_subcontracting_skip_no_negative_03(self): + self._create_stock_quant(self.comp1, 10) + self._create_stock_quant(self.comp2, 10) + self.subcontracting_receipt.action_confirm() + self.assertEqual(self.subcontracting_receipt.state, "assigned") + immediate_wizard = self.subcontracting_receipt.sudo().button_validate() + self.assertEqual( + immediate_wizard.get("res_model"), "report.stock.report_reception" + ) + immediate_wizard_form = self.env[immediate_wizard["res_model"]].with_context( + **immediate_wizard["context"] + ) + self.assertFalse(immediate_wizard_form) + self.assertEqual(self.subcontracting_receipt.state, "done") + + def test_mrp_subcontracting_skip_no_negative_04(self): + partner1 = self.subcontractor_partner1 + partner1.property_stock_subcontractor.allow_negative_stock = True + self.subcontracting_receipt.action_confirm() + self.assertEqual(self.subcontracting_receipt.state, "assigned") + immediate_wizard = self.subcontracting_receipt.sudo().button_validate() + self.assertEqual( + immediate_wizard.get("res_model"), "report.stock.report_reception" + ) + immediate_wizard_form = self.env[immediate_wizard["res_model"]].with_context( + **immediate_wizard["context"] + ) + self.assertFalse(immediate_wizard_form) + self.assertEqual(self.subcontracting_receipt.state, "done")