diff --git a/mrp_unbuild_owner/README.rst b/mrp_unbuild_owner/README.rst new file mode 100644 index 00000000..502dc413 --- /dev/null +++ b/mrp_unbuild_owner/README.rst @@ -0,0 +1,56 @@ +================= +MRP Unbuild Owner +================= + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:179c57523b0c20cd660fa3c5ca7898d3877b54a7f5eb5d06950dc241947d6ecc + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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-qrtl%2Faxls--custom-lightgray.png?logo=github + :target: https://github.com/qrtl/axls-custom/tree/16.0/mrp_unbuild_owner + :alt: qrtl/axls-custom + +|badge1| |badge2| |badge3| + +This module restores the original attribute value of the stock quantity +for the product when the manufacturing order is unbuilt. + +**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 + +Maintainers +----------- + +This module is part of the `qrtl/axls-custom `_ project on GitHub. + +You are welcome to contribute. diff --git a/mrp_unbuild_owner/__init__.py b/mrp_unbuild_owner/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/mrp_unbuild_owner/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/mrp_unbuild_owner/__manifest__.py b/mrp_unbuild_owner/__manifest__.py new file mode 100644 index 00000000..1bcc0fb7 --- /dev/null +++ b/mrp_unbuild_owner/__manifest__.py @@ -0,0 +1,12 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "MRP Unbuild Owner", + "category": "MRP", + "license": "AGPL-3", + "author": "Quartile Limited", + "website": "https://www.quartile.co", + "version": "16.0.1.0.0", + "depends": ["mrp", "mrp_stock_owner_restriction"], + "installable": True, +} diff --git a/mrp_unbuild_owner/models/__init__.py b/mrp_unbuild_owner/models/__init__.py new file mode 100644 index 00000000..68415991 --- /dev/null +++ b/mrp_unbuild_owner/models/__init__.py @@ -0,0 +1,2 @@ +from . import mrp_unbuild +from . import stock_move diff --git a/mrp_unbuild_owner/models/mrp_unbuild.py b/mrp_unbuild_owner/models/mrp_unbuild.py new file mode 100644 index 00000000..f2457a71 --- /dev/null +++ b/mrp_unbuild_owner/models/mrp_unbuild.py @@ -0,0 +1,78 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class MrpUnbuild(models.Model): + _inherit = "mrp.unbuild" + + def action_validate(self): + owner = self.mo_id.owner_id + if owner: + self = self.with_context(force_restricted_owner_id=owner) + return super().action_validate() + + def _prepare_move_line_vals(self, move, origin_move_line, taken_quantity): + vals = super()._prepare_move_line_vals(move, origin_move_line, taken_quantity) + vals["owner_id"] = origin_move_line.owner_id.id + return vals + + def action_unbuild(self): + self.ensure_one() + if self.mo_id: + self = self.with_context(exact_unbuild=True) + return super().action_unbuild() + + def _get_move_line_vals(self, move, move_line): + return { + "move_id": move.id, + "owner_id": move_line.owner_id.id, + "qty_done": min(move.product_uom_qty, move_line.qty_done), + "product_id": move.product_id.id, + "product_uom_id": move.product_uom.id, + "location_id": move.location_id.id, + "location_dest_id": move.location_dest_id.id, + } + + def _generate_produce_moves(self): + """This logic is a bit hard to understand but necessary due to how the following + steps are written in the standard code: + https://github.com/OCA/OCB/blob/52bec03/addons/mrp/models/mrp_unbuild.py#L189-L207 + In short, we want to prepare stock.move.line records in advance with the + "correct" content before the standard code generates them with incorrectly + (without owner). + """ + if not self.env.context.get("exact_unbuild"): + return super()._generate_produce_moves() + # i.e. There is production order for the unbuild + # We need to remove the force_restrict_owner_id assignment to respect the owner + # of the original move line. + self = self.with_context(default_lot_id=False, force_restricted_owner_id=False) + moves = self.env["stock.move"] + for unbuild in self: + raw_moves = unbuild.mo_id.move_raw_ids.filtered( + lambda move: move.state == "done" + ) + factor = ( + unbuild.product_qty + / unbuild.mo_id.product_uom_id._compute_quantity( + unbuild.mo_id.product_qty, unbuild.product_uom_id + ) + ) + for raw_move in raw_moves: + move = unbuild._generate_move_from_existing_move( + raw_move, + factor, + raw_move.location_dest_id, + self.location_dest_id, + ) + if move.has_tracking == "none": + vals_list = [] + for move_line in raw_move.move_line_ids: + vals = self._get_move_line_vals(move, move_line) + vals_list.append(vals) + self.env["stock.move.line"].create(vals_list) + move.write({"state": "confirmed"}) + moves += move + return moves.with_context(produce_moves=True) diff --git a/mrp_unbuild_owner/models/stock_move.py b/mrp_unbuild_owner/models/stock_move.py new file mode 100644 index 00000000..c377792f --- /dev/null +++ b/mrp_unbuild_owner/models/stock_move.py @@ -0,0 +1,17 @@ +# Copyright 2024 Quartile Limited +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class StockMove(models.Model): + _inherit = "stock.move" + + def _action_confirm(self, merge=True, merge_into=False): + # We particularly want to skip + # https://github.com/OCA/OCB/blob/53e1941/addons/mrp/models/mrp_unbuild.py#L148 + # for component receipts to avoid generation of stock.move.line records with + # the standard logic. + if self._context.get("exact_unbuild") and self._context.get("produce_moves"): + return self + return super()._action_confirm(merge, merge_into) diff --git a/mrp_unbuild_owner/readme/DESCRIPTION.md b/mrp_unbuild_owner/readme/DESCRIPTION.md new file mode 100644 index 00000000..5b86c6b7 --- /dev/null +++ b/mrp_unbuild_owner/readme/DESCRIPTION.md @@ -0,0 +1,2 @@ +This module restores the original attribute value of the stock quantity for the product +when the manufacturing order is unbuilt. diff --git a/mrp_unbuild_owner/static/description/index.html b/mrp_unbuild_owner/static/description/index.html new file mode 100644 index 00000000..9874f3b5 --- /dev/null +++ b/mrp_unbuild_owner/static/description/index.html @@ -0,0 +1,410 @@ + + + + + + +MRP Unbuild Owner + + + +
+

MRP Unbuild Owner

+ + +

Beta License: AGPL-3 qrtl/axls-custom

+

This module restores the original attribute value of the stock quantity +for the product when the manufacturing order is unbuilt.

+

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.

+
+
+

Credits

+
+

Authors

+
    +
  • Quartile Limited
  • +
+
+
+

Maintainers

+

This module is part of the qrtl/axls-custom project on GitHub.

+

You are welcome to contribute.

+
+
+
+ + diff --git a/setup/mrp_unbuild_owner/odoo/addons/mrp_unbuild_owner b/setup/mrp_unbuild_owner/odoo/addons/mrp_unbuild_owner new file mode 120000 index 00000000..5d5bace2 --- /dev/null +++ b/setup/mrp_unbuild_owner/odoo/addons/mrp_unbuild_owner @@ -0,0 +1 @@ +../../../../mrp_unbuild_owner \ No newline at end of file diff --git a/setup/mrp_unbuild_owner/setup.py b/setup/mrp_unbuild_owner/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/mrp_unbuild_owner/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/test-requirements.txt b/test-requirements.txt index 35d0b815..92d374e5 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -10,3 +10,4 @@ git+https://github.com/qrtl/axls-oca@16.0#subdirectory=setup/web_ir_actions_act_ git+https://github.com/qrtl/axls-oca@16.0#subdirectory=setup/purchase_order_owner git+https://github.com/qrtl/axls-oca@16.0#subdirectory=setup/purchase_deposit git+https://github.com/qrtl/axls-oca@16.0#subdirectory=setup/purchase_analytic +odoo-addon-mrp-stock-owner-restriction @ git+https://github.com/qrtl/axls-oca.git@refs/pull/105/head#subdirectory=setup/mrp_stock_owner_restriction