diff --git a/mrp_subcontracting_skip_no_negative/README.rst b/mrp_subcontracting_skip_no_negative/README.rst new file mode 100644 index 00000000000..b9a747bcfee --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/README.rst @@ -0,0 +1,111 @@ +=================================== +MRP Subcontracting Skip No Negative +=================================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:80dba345c55a955c43b1958c972e990820fa1ac1defcff4595750981a978919e + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/18.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-18-0/manufacture-18-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=18.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 + +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. + +.. |maintainer-yostashiro| image:: https://github.com/yostashiro.png?size=40px + :target: https://github.com/yostashiro + :alt: yostashiro +.. |maintainer-aungkokolin1997| image:: https://github.com/aungkokolin1997.png?size=40px + :target: https://github.com/aungkokolin1997 + :alt: aungkokolin1997 + +Current `maintainers `__: + +|maintainer-yostashiro| |maintainer-aungkokolin1997| + +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 00000000000..0650744f6bc --- /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 00000000000..abd22aad5ad --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/__manifest__.py @@ -0,0 +1,14 @@ +# Copyright 2023 Quartile (https://www.quartile.co) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) +{ + "name": "MRP Subcontracting Skip No Negative", + "version": "18.0.1.0.0", + "license": "AGPL-3", + "author": "Quartile, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/manufacture", + "category": "Manufacturing", + "depends": ["mrp_subcontracting", "stock_no_negative"], + "maintainers": ["yostashiro", "aungkokolin1997"], + "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 00000000000..1aa15554ed7 --- /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 00000000000..798424e1966 --- /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 00000000000..8e94f49b233 --- /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 17.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 00000000000..a33bde1e878 --- /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 00000000000..16853b89b0e --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/models/stock_move.py @@ -0,0 +1,21 @@ +# Copyright 2023 Quartile (https://www.quartile.co) +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import models + + +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 + ) + res = super(StockMove, moves_with_no_check)._action_done( + cancel_backorder=cancel_backorder + ) + res += super(StockMove, self - 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 00000000000..c4deea30511 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/models/stock_picking.py @@ -0,0 +1,44 @@ +# Copyright 2023 Quartile (https://www.quartile.co) +# 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() + + def _get_moves_to_backorder(self): + self.ensure_one() + moves = super()._get_moves_to_backorder() + if self.env.context.get("skip_negative_qty_check"): + return moves.filtered(lambda x: x.is_subcontract) + return moves + + def _create_backorder_picking(self): + self.ensure_one() + existing_backorder_picking = self.env["stock.picking"].search( + [("backorder_id", "=", self.id)] + ) + existing_subcontract_moves = existing_backorder_picking.move_ids.filtered( + lambda x: x.is_subcontract + ) + if ( + self.move_ids.filtered(lambda x: x.state == "done" and x.is_subcontract) + and existing_subcontract_moves + ): + return existing_backorder_picking + return super()._create_backorder_picking() diff --git a/mrp_subcontracting_skip_no_negative/pyproject.toml b/mrp_subcontracting_skip_no_negative/pyproject.toml new file mode 100644 index 00000000000..4231d0cccb3 --- /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 00000000000..fb5ab8eb00e --- /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 00000000000..6623e426402 --- /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 00000000000..3a0328b516c 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 00000000000..4aba1983cae --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/static/description/index.html @@ -0,0 +1,441 @@ + + + + + +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
  • +
+
+
+

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.

+

Current maintainers:

+

yostashiro aungkokolin1997

+

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 00000000000..7e62122f1f8 --- /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 00000000000..4092ea41d79 --- /dev/null +++ b/mrp_subcontracting_skip_no_negative/tests/test_mrp_subcontracting_skip_no_negative.py @@ -0,0 +1,154 @@ +# Copyright 2023 Quartile (https://www.quartile.co) +# Copyright 2024 Tecnativa - Víctor Martínez +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) + +from odoo import Command +from odoo.exceptions import ValidationError +from odoo.tests import Form +from odoo.tools import mute_logger + +from odoo.addons.base.tests.common import BaseCommon + + +class TestMrpSubcontractingSkipNoNegative(BaseCommon): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env = cls.env( + context=dict( + cls.env.context, + test_stock_no_negative=True, + ) + ) + warehouse = cls.env.ref("stock.warehouse0") + cls.stock_location = warehouse.lot_stock_id + cls.subcontractor_location = cls.env["stock.location"].create( + { + "name": "Subcontractor 1", + "usage": "internal", + "location_id": warehouse.view_location_id.id, + } + ) + cls.subcontractor_partner1 = cls.env["res.partner"].create( + { + "name": "Subcontractor 1", + "company_type": "company", + "property_stock_subcontractor": cls.subcontractor_location.id, + } + ) + cls.comp1 = cls.env["product.product"].create( + {"name": "Component1", "is_storable": True} + ) + cls.comp2 = cls.env["product.product"].create( + {"name": "Component2", "is_storable": True} + ) + cls.finished = cls.env["product.product"].create( + {"name": "Finished product", "is_storable": True} + ) + cls.bom = cls.env["mrp.bom"].create( + { + "product_tmpl_id": cls.finished.product_tmpl_id.id, + "product_id": cls.finished.id, + "product_uom_id": cls.finished.uom_id.id, + "type": "subcontract", + "subcontractor_ids": [ + Command.set([cls.subcontractor_partner1.id]), + ], + "bom_line_ids": [ + Command.create( + { + "product_id": cls.comp1.id, + "product_uom_id": cls.comp1.uom_id.id, + "product_qty": 1.0, + } + ), + Command.create( + { + "product_id": cls.comp2.id, + "product_uom_id": cls.comp2.uom_id.id, + "product_qty": 1.0, + } + ), + ], + } + ) + 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 + with picking_form.move_ids_without_package.new() as move: + move.product_id = cls.finished + move.product_uom_qty = 1.0 + cls.subcontracting_receipt = picking_form.save() + + def _create_stock_quant(self, product, qty): + self.env["stock.quant"].create( + { + "product_id": product.id, + "location_id": self.subcontractor_location.id, + "quantity": qty, + } + ) + + @mute_logger("odoo.models.unlink") + def test_mrp_subcontracting_no_stock_components(self): + """No stock at subcontractor: fail first on comp1, then on comp2.""" + self.subcontracting_receipt.action_confirm() + self.assertEqual(self.subcontracting_receipt.state, "assigned") + with self.assertRaises(ValidationError) as e1: + self.subcontracting_receipt.sudo().button_validate() + self.assertIn("Component1", str(e1.exception)) + self._create_stock_quant(self.comp1, 10) + with self.assertRaises(ValidationError) as e2: + self.subcontracting_receipt.sudo().button_validate() + self.assertIn("Component2", str(e2.exception)) + self._create_stock_quant(self.comp2, 10) + self.subcontracting_receipt.sudo().button_validate() + self.assertEqual(self.subcontracting_receipt.state, "done") + + def test_mrp_subcontracting_stock_components(self): + """Both components in stock -> subcontracting receipt OK.""" + 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") + self.subcontracting_receipt.sudo().button_validate() + self.assertEqual(self.subcontracting_receipt.state, "done") + + def test_mrp_subcontracting_allow_negative_stock(self): + """If subcontractor location allows negative stock, receipt is allowed.""" + self.subcontractor_location.allow_negative_stock = True + self.subcontracting_receipt.action_confirm() + self.assertEqual(self.subcontracting_receipt.state, "assigned") + self.subcontracting_receipt.sudo().button_validate() + self.assertEqual(self.subcontracting_receipt.state, "done") + + def test_mrp_subcontracting_with_normal_product(self): + """Extra normal product on the picking shouldn't break the flow.""" + another_product = self.env["product.product"].create( + { + "name": "Another Product", + "is_storable": True, + } + ) + self.env["stock.move"].create( + { + "picking_id": self.subcontracting_receipt.id, + "product_id": another_product.id, + "name": another_product.name, + "product_uom": another_product.uom_id.id, + "product_uom_qty": 1.0, + "location_id": self.subcontracting_receipt.location_id.id, + "location_dest_id": self.subcontracting_receipt.location_dest_id.id, + } + ) + 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") + self.subcontracting_receipt.sudo().button_validate() + self.assertEqual(self.subcontracting_receipt.state, "done") + products = self.subcontracting_receipt.move_ids.mapped("product_id") + self.assertIn(self.finished, products) + self.assertIn(another_product, products) + for move in self.subcontracting_receipt.move_ids: + self.assertEqual(move.quantity, 1.0)