Skip to content

Commit

Permalink
[4233][ADD] mrp_unbuild_owner (#68)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Yoshi Tashiro <tashiro@quartile.co>
  • Loading branch information
AungKoKoLin1997 and yostashiro authored Apr 4, 2024
1 parent 7ce817c commit 39eed82
Show file tree
Hide file tree
Showing 11 changed files with 586 additions and 0 deletions.
56 changes: 56 additions & 0 deletions mrp_unbuild_owner/README.rst
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/qrtl/axls-custom/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 <https://github.com/qrtl/axls-custom/issues/new?body=module:%20mrp_unbuild_owner%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

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 <https://github.com/qrtl/axls-custom/tree/16.0/mrp_unbuild_owner>`_ project on GitHub.

You are welcome to contribute.
1 change: 1 addition & 0 deletions mrp_unbuild_owner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
12 changes: 12 additions & 0 deletions mrp_unbuild_owner/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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,
}
2 changes: 2 additions & 0 deletions mrp_unbuild_owner/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import mrp_unbuild
from . import stock_move
78 changes: 78 additions & 0 deletions mrp_unbuild_owner/models/mrp_unbuild.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions mrp_unbuild_owner/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions mrp_unbuild_owner/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module restores the original attribute value of the stock quantity for the product
when the manufacturing order is unbuilt.
Loading

0 comments on commit 39eed82

Please sign in to comment.