diff --git a/mrp_production_serial_matrix/README.rst b/mrp_production_serial_matrix/README.rst new file mode 100644 index 000000000..b00422bfa --- /dev/null +++ b/mrp_production_serial_matrix/README.rst @@ -0,0 +1,111 @@ +============================ +MRP Production Serial Matrix +============================ + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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/14.0/mrp_production_serial_matrix + :alt: OCA/manufacture +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/manufacture-14-0/manufacture-14-0-mrp_production_serial_matrix + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/129/14.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module aims to ease the processing of Manufacturing Orders with the +following characteristics: + +* The finished product is tracked by unique serial numbers. +* The quantity to be produced is more than one unit. +* Some of the components are tracked by serial numbers and/or lots. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +Go to a Manufacturing Order with the characteristics described above, then +proceed as follows: + +#. Click on the button "Serial Numbers Matrix". +#. Fill the *Finished Product Serials Numbers*. As you fill them you will see + how the matrix below is updated. +#. Fill the matrix. The matrix is composed of one row per final serial number + and one column per component serial number or lot needed to complete the + production. +#. Click on *Validate*. You will be redirected to all the MOs generated. + +Take into account the following: + +* You need to attend all the warnings at the bottom before validating the + matrix. They help you to identify error in the input (duplicated serial + numbers, not enough stock...). +* Rows with no final lot are ignored. You will have a backorder MO for the + remaining quantity. + +Known issues / Roadmap +====================== + +Currently, there are some limitations : + +* Components tracked by lots will only have one column. This means that all the + quantity needed of this component to manufacture a finished product unit ( + serial number) will be consuming a single lot. + +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 smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* ForgeFlow + +Contributors +~~~~~~~~~~~~ + +* `ForgeFlow `_: + + * Lois Rilo + +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_production_serial_matrix/__init__.py b/mrp_production_serial_matrix/__init__.py new file mode 100644 index 000000000..976591c99 --- /dev/null +++ b/mrp_production_serial_matrix/__init__.py @@ -0,0 +1,2 @@ +from . import wizards +from . import models diff --git a/mrp_production_serial_matrix/__manifest__.py b/mrp_production_serial_matrix/__manifest__.py new file mode 100644 index 000000000..4af7d9481 --- /dev/null +++ b/mrp_production_serial_matrix/__manifest__.py @@ -0,0 +1,21 @@ +# Copyright 2021 ForgeFlow S.L. (http://www.forgeflow.com) +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +{ + "name": "MRP Production Serial Matrix", + "version": "14.0.1.0.0", + "category": "Manufacturing", + "license": "AGPL-3", + "author": "ForgeFlow, Odoo Community Association (OCA)", + "website": "https://github.com/OCA/manufacture", + "depends": [ + "mrp", + "web_widget_x2many_2d_matrix", + ], + "data": [ + "security/ir.model.access.csv", + "wizards/mrp_production_serial_matrix_view.xml", + "views/mrp_production_views.xml", + ], + "installable": True, +} diff --git a/mrp_production_serial_matrix/models/__init__.py b/mrp_production_serial_matrix/models/__init__.py new file mode 100644 index 000000000..a9e5f13e4 --- /dev/null +++ b/mrp_production_serial_matrix/models/__init__.py @@ -0,0 +1 @@ +from . import mrp_production diff --git a/mrp_production_serial_matrix/models/mrp_production.py b/mrp_production_serial_matrix/models/mrp_production.py new file mode 100644 index 000000000..2a2d38cee --- /dev/null +++ b/mrp_production_serial_matrix/models/mrp_production.py @@ -0,0 +1,14 @@ +# Copyright 2021 ForgeFlow S.L. (http://www.forgeflow.com) +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from odoo import fields, models + + +class MrpProduction(models.Model): + _inherit = "mrp.production" + + show_serial_matrix = fields.Boolean(compute="_compute_show_serial_matrix") + + def _compute_show_serial_matrix(self): + for rec in self: + rec.show_serial_matrix = rec.product_id.tracking == "serial" diff --git a/mrp_production_serial_matrix/readme/CONTRIBUTORS.rst b/mrp_production_serial_matrix/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..1a927bdc4 --- /dev/null +++ b/mrp_production_serial_matrix/readme/CONTRIBUTORS.rst @@ -0,0 +1,3 @@ +* `ForgeFlow `_: + + * Lois Rilo diff --git a/mrp_production_serial_matrix/readme/DESCRIPTION.rst b/mrp_production_serial_matrix/readme/DESCRIPTION.rst new file mode 100644 index 000000000..4d223a1a6 --- /dev/null +++ b/mrp_production_serial_matrix/readme/DESCRIPTION.rst @@ -0,0 +1,6 @@ +This module aims to ease the processing of Manufacturing Orders with the +following characteristics: + +* The finished product is tracked by unique serial numbers. +* The quantity to be produced is more than one unit. +* Some of the components are tracked by serial numbers and/or lots. diff --git a/mrp_production_serial_matrix/readme/ROADMAP.rst b/mrp_production_serial_matrix/readme/ROADMAP.rst new file mode 100644 index 000000000..267a3befc --- /dev/null +++ b/mrp_production_serial_matrix/readme/ROADMAP.rst @@ -0,0 +1,5 @@ +Currently, there are some limitations : + +* Components tracked by lots will only have one column. This means that all the + quantity needed of this component to manufacture a finished product unit ( + serial number) will be consuming a single lot. diff --git a/mrp_production_serial_matrix/readme/USAGE.rst b/mrp_production_serial_matrix/readme/USAGE.rst new file mode 100644 index 000000000..9c1f5e853 --- /dev/null +++ b/mrp_production_serial_matrix/readme/USAGE.rst @@ -0,0 +1,18 @@ +Go to a Manufacturing Order with the characteristics described above, then +proceed as follows: + +#. Click on the button "Serial Numbers Matrix". +#. Fill the *Finished Product Serials Numbers*. As you fill them you will see + how the matrix below is updated. +#. Fill the matrix. The matrix is composed of one row per final serial number + and one column per component serial number or lot needed to complete the + production. +#. Click on *Validate*. You will be redirected to all the MOs generated. + +Take into account the following: + +* You need to attend all the warnings at the bottom before validating the + matrix. They help you to identify error in the input (duplicated serial + numbers, not enough stock...). +* Rows with no final lot are ignored. You will have a backorder MO for the + remaining quantity. diff --git a/mrp_production_serial_matrix/security/ir.model.access.csv b/mrp_production_serial_matrix/security/ir.model.access.csv new file mode 100644 index 000000000..267d70ea5 --- /dev/null +++ b/mrp_production_serial_matrix/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_mrp_production_serial_matrix,access mrp.production.serial.matrix,model_mrp_production_serial_matrix,base.group_user,1,1,1,1 +access_mrp_production_serial_matrix_line,access mrp.production.serial.matrix.line,model_mrp_production_serial_matrix_line,base.group_user,1,1,1,1 diff --git a/mrp_production_serial_matrix/static/description/icon.png b/mrp_production_serial_matrix/static/description/icon.png new file mode 100644 index 000000000..3a0328b51 Binary files /dev/null and b/mrp_production_serial_matrix/static/description/icon.png differ diff --git a/mrp_production_serial_matrix/static/description/index.html b/mrp_production_serial_matrix/static/description/index.html new file mode 100644 index 000000000..f7876d1e0 --- /dev/null +++ b/mrp_production_serial_matrix/static/description/index.html @@ -0,0 +1,461 @@ + + + + + + +MRP Production Serial Matrix + + + +
+

MRP Production Serial Matrix

+ + +

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

+

This module aims to ease the processing of Manufacturing Orders with the +following characteristics:

+
    +
  • The finished product is tracked by unique serial numbers.
  • +
  • The quantity to be produced is more than one unit.
  • +
  • Some of the components are tracked by serial numbers and/or lots.
  • +
+

Table of contents

+ +
+

Usage

+

Go to a Manufacturing Order with the characteristics described above, then +proceed as follows:

+
    +
  1. Click on the button “Serial Numbers Matrix”.
  2. +
  3. Fill the Finished Product Serials Numbers. As you fill them you will see +how the matrix below is updated.
  4. +
  5. Fill the matrix. The matrix is composed of one row per final serial number +and one column per component serial number or lot needed to complete the +production.
  6. +
  7. Click on Validate. You will be redirected to all the MOs generated.
  8. +
+

Take into account the following:

+
    +
  • You need to attend all the warnings at the bottom before validating the +matrix. They help you to identify error in the input (duplicated serial +numbers, not enough stock…).
  • +
  • Rows with no final lot are ignored. You will have a backorder MO for the +remaining quantity.
  • +
+
+
+

Known issues / Roadmap

+

Currently, there are some limitations :

+
    +
  • Components tracked by lots will only have one column. This means that all the +quantity needed of this component to manufacture a finished product unit ( +serial number) will be consuming a single lot.
  • +
+
+
+

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

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • ForgeFlow
  • +
+
+ +
+

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_production_serial_matrix/tests/__init__.py b/mrp_production_serial_matrix/tests/__init__.py new file mode 100644 index 000000000..50ad4dea5 --- /dev/null +++ b/mrp_production_serial_matrix/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mrp_production_serial_matrix diff --git a/mrp_production_serial_matrix/tests/test_mrp_production_serial_matrix.py b/mrp_production_serial_matrix/tests/test_mrp_production_serial_matrix.py new file mode 100644 index 000000000..89738b4dc --- /dev/null +++ b/mrp_production_serial_matrix/tests/test_mrp_production_serial_matrix.py @@ -0,0 +1,283 @@ +# Copyright 2021 ForgeFlow S.L. (http://www.forgeflow.com) +# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html + +from odoo.exceptions import UserError +from odoo.tests import Form +from odoo.tests.common import SavepointCase + + +class TestMrpProductionSerialMatrix(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.mo_obj = cls.env["mrp.production"] + cls.product_obj = cls.env["product.product"] + cls.lot_obj = cls.env["stock.production.lot"] + cls.quant_obj = cls.env["stock.quant"] + cls.bom_obj = cls.env["mrp.bom"] + cls.bom_line_obj = cls.env["mrp.bom.line"] + cls.move_line_obj = cls.env["stock.move.line"] + cls.wiz_obj = cls.env["mrp.production.serial.matrix"] + + cls.company = cls.env.ref("base.main_company") + cls.stock_loc = cls.env.ref("stock.stock_location_stock") + + # Products and lots: + cls.final_product = cls.product_obj.create( + { + "name": "Finished Product tracked by Serial Numbers", + "type": "product", + "tracking": "serial", + } + ) + cls.component_1_serial = cls.product_obj.create( + { + "name": "Component 1 tracked by Serial Numbers", + "type": "product", + "tracking": "serial", + } + ) + cls.serial_1_001 = cls._create_serial_number(cls.component_1_serial, "1-001") + cls.serial_1_002 = cls._create_serial_number(cls.component_1_serial, "1-002") + cls.serial_1_003 = cls._create_serial_number(cls.component_1_serial, "1-003") + cls.component_2_serial = cls.product_obj.create( + { + "name": "Component 2 tracked by Serial Numbers", + "type": "product", + "tracking": "serial", + } + ) + cls.serial_2_001 = cls._create_serial_number(cls.component_2_serial, "2-001") + cls.serial_2_002 = cls._create_serial_number(cls.component_2_serial, "2-002") + cls.serial_2_003 = cls._create_serial_number(cls.component_2_serial, "2-003") + cls.serial_2_004 = cls._create_serial_number(cls.component_2_serial, "2-004") + cls.serial_2_005 = cls._create_serial_number(cls.component_2_serial, "2-005") + cls.serial_2_006 = cls._create_serial_number(cls.component_2_serial, "2-006") + cls.component_3_lot = cls.product_obj.create( + { + "name": "Component 3 tracked by Lots", + "type": "product", + "tracking": "lot", + } + ) + cls.lot_3_001 = cls._create_serial_number( + cls.component_3_lot, "3-001", qty=10.0 + ) + cls.lot_3_002 = cls._create_serial_number(cls.component_3_lot, "3-002", qty=8.0) + cls.lot_3_003 = cls._create_serial_number( + cls.component_3_lot, "3-003", qty=12.0 + ) + cls.component_4_no_track = cls.product_obj.create( + { + "name": "Component 4 Not tracked", + "type": "product", + "tracking": "none", + } + ) + cls.quant_obj.create( + { + "product_id": cls.component_4_no_track.id, + "location_id": cls.stock_loc.id, + "quantity": 20.0, + } + ) + + # BoM + cls.bom_1 = cls.bom_obj.create( + { + "product_tmpl_id": cls.final_product.product_tmpl_id.id, + "product_id": cls.final_product.id, + "product_qty": 1.0, + } + ) + cls.bom_line_obj.create( + { + "bom_id": cls.bom_1.id, + "product_id": cls.component_1_serial.id, + "product_qty": 1.0, + } + ) + cls.bom_line_obj.create( + { + "bom_id": cls.bom_1.id, + "product_id": cls.component_2_serial.id, + "product_qty": 2.0, + } + ) + cls.bom_line_obj.create( + { + "bom_id": cls.bom_1.id, + "product_id": cls.component_3_lot.id, + "product_qty": 4.0, + } + ) + cls.bom_line_obj.create( + { + "bom_id": cls.bom_1.id, + "product_id": cls.component_4_no_track.id, + "product_qty": 1.0, + } + ) + + @classmethod + def _create_serial_number(cls, product, name, qty=1.0): + lot = cls.lot_obj.create( + { + "product_id": product.id, + "name": name, + "company_id": cls.company.id, + } + ) + if qty > 0: + cls.quant_obj.create( + { + "product_id": product.id, + "location_id": cls.stock_loc.id, + "quantity": qty, + "lot_id": lot.id, + } + ) + return lot + + @classmethod + def _create_mo(cls, qty): + production_form = Form(cls.mo_obj) + production_form.product_id = cls.final_product + production_form.bom_id = cls.bom_1 + production_form.product_qty = qty + production_form.product_uom_id = cls.final_product.uom_id + production_1 = production_form.save() + production_1.action_confirm() + production_1.action_assign() + return production_1 + + @classmethod + def _find_move_lines(cls, mo, component): + return cls.move_line_obj.search( + [ + ("move_id.raw_material_production_id", "=", mo.id), + ("product_id", "=", component.id), + ] + ) + + def test_01_process_mo_with_matrix(self): + """Extensive test including all the possibilities for components: + - 1 tracked by serials. + - 1 tracked by serials and needing more than one unit. + - 1 tracked by lots. + - 1 untracked. + """ + production_1 = self._create_mo(3.0) + self.assertEqual(production_1.state, "confirmed") + # Start matrix: + wizard_form = Form( + self.wiz_obj.with_context( + active_id=production_1.id, active_model="mrp.production" + ) + ) + expected = 3 * 4 + self.assertEqual(len(wizard_form.line_ids), expected) + self.assertEqual(wizard_form.lot_selection_warning_count, 0) + serial_fp_1 = self._create_serial_number(self.final_product, "ABC101", qty=0) + serial_fp_2 = self._create_serial_number(self.final_product, "ABC102", qty=0) + wizard_form.finished_lot_ids.add(serial_fp_1) + self.assertEqual(wizard_form.lot_selection_warning_count, 1) + wizard_form.finished_lot_ids.add(serial_fp_2) + self.assertEqual(wizard_form.lot_selection_warning_count, 2) + wizard = wizard_form.save() + lines = wizard.line_ids + # Fill first row: + cell_1_1 = lines.filtered( + lambda l: l.finished_lot_id == serial_fp_1 + and l.component_id == self.component_1_serial + ) + cell_1_1.component_lot_id = self.serial_1_001 + cell_1_2and3 = lines.filtered( + lambda l: l.finished_lot_id == serial_fp_1 + and l.component_id == self.component_2_serial + ) + self.assertEqual(len(cell_1_2and3), 2) + for n, cell in enumerate(cell_1_2and3): + if n == 0: + cell.component_lot_id = self.serial_2_001 + elif n == 1: + cell.component_lot_id = self.serial_2_002 + cell_1_4 = lines.filtered( + lambda l: l.finished_lot_id == serial_fp_1 + and l.component_id == self.component_3_lot + ) + cell_1_4.component_lot_id = self.lot_3_003 + + # Fill second row: + cell_2_1 = lines.filtered( + lambda l: l.finished_lot_id == serial_fp_2 + and l.component_id == self.component_1_serial + ) + # Simulate a mistake and select the same lot than before: + cell_2_1.component_lot_id = self.serial_1_001 + cell_2_2and3 = lines.filtered( + lambda l: l.finished_lot_id == serial_fp_2 + and l.component_id == self.component_2_serial + ) + self.assertEqual(len(cell_2_2and3), 2) + for n, cell in enumerate(cell_2_2and3): + if n == 0: + cell.component_lot_id = self.serial_2_005 + elif n == 1: + cell.component_lot_id = self.serial_2_004 + cell_2_4 = lines.filtered( + lambda l: l.finished_lot_id == serial_fp_2 + and l.component_id == self.component_3_lot + ) + cell_2_4.component_lot_id = self.lot_3_002 + + # There should be an error: + self.assertEqual(wizard.lot_selection_warning_count, 1) + with self.assertRaises(UserError): + wizard.button_validate() + + # Fix error: + cell_2_1.component_lot_id = self.serial_1_003 + + # Validate and check result: + wizard.button_validate() + mos = production_1.procurement_group_id.mrp_production_ids + self.assertEqual(len(mos), 3) + mo_1 = mos.filtered(lambda mo: mo.lot_producing_id == serial_fp_1) + self.assertEqual(mo_1.state, "done") + ml_c1 = self._find_move_lines(mo_1, self.component_1_serial) + self.assertEqual(ml_c1.qty_done, 1.0) + self.assertEqual(ml_c1.lot_id, self.serial_1_001) + ml_c2 = self._find_move_lines(mo_1, self.component_2_serial) + self.assertEqual(len(ml_c2), 2) + for ml in ml_c2: + self.assertEqual(ml.qty_done, 1.0) + self.assertEqual(ml_c2.mapped("lot_id"), self.serial_2_001 + self.serial_2_002) + ml_c3 = self._find_move_lines(mo_1, self.component_3_lot) + self.assertEqual(ml_c3.qty_done, 4.0) + self.assertEqual(ml_c3.lot_id, self.lot_3_003) + ml_c4 = self._find_move_lines(mo_1, self.component_4_no_track) + self.assertEqual(ml_c4.qty_done, 1.0) + self.assertFalse(ml_c4.lot_id) + + mo_2 = mos.filtered(lambda mo: mo.lot_producing_id == serial_fp_2) + self.assertEqual(mo_2.state, "done") + ml_c1 = self._find_move_lines(mo_2, self.component_1_serial) + self.assertEqual(ml_c1.qty_done, 1.0) + self.assertEqual(ml_c1.lot_id, self.serial_1_003) + ml_c2 = self._find_move_lines(mo_2, self.component_2_serial) + self.assertEqual(len(ml_c2), 2) + for ml in ml_c2: + self.assertEqual(ml.qty_done, 1.0) + self.assertEqual(ml_c2.mapped("lot_id"), self.serial_2_005 + self.serial_2_004) + ml_c3 = self._find_move_lines(mo_2, self.component_3_lot) + self.assertEqual(ml_c3.qty_done, 4.0) + self.assertEqual(ml_c3.lot_id, self.lot_3_002) + ml_c4 = self._find_move_lines(mo_2, self.component_4_no_track) + self.assertEqual(ml_c4.qty_done, 1.0) + self.assertFalse(ml_c4.lot_id) + + # MO holding the remaining qty + mo_3 = mos.filtered(lambda mo: not mo.lot_producing_id) + self.assertEqual(mo_3.state, "confirmed") + self.assertEqual(mo_3.product_qty, 1.0) diff --git a/mrp_production_serial_matrix/views/mrp_production_views.xml b/mrp_production_serial_matrix/views/mrp_production_views.xml new file mode 100644 index 000000000..6e51c6a43 --- /dev/null +++ b/mrp_production_serial_matrix/views/mrp_production_views.xml @@ -0,0 +1,20 @@ + + + + mrp.production.form - mrp_production_serial_matrix + mrp.production + + + + + + diff --git a/mrp_production_serial_matrix/wizards/__init__.py b/mrp_production_serial_matrix/wizards/__init__.py new file mode 100644 index 000000000..24a607e4a --- /dev/null +++ b/mrp_production_serial_matrix/wizards/__init__.py @@ -0,0 +1,2 @@ +from . import mrp_production_serial_matrix +from . import mrp_production_serial_matrix_line diff --git a/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix.py b/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix.py new file mode 100644 index 000000000..7d1fb2e9b --- /dev/null +++ b/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix.py @@ -0,0 +1,344 @@ +# Copyright 2021 ForgeFlow S.L. (https://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import _, api, fields, models +from odoo.exceptions import UserError, ValidationError +from odoo.tools.float_utils import float_compare, float_is_zero + + +class MrpProductionSerialMatrix(models.TransientModel): + _name = "mrp.production.serial.matrix" + _description = "Mrp Production Serial Matrix" + + production_id = fields.Many2one( + comodel_name="mrp.production", + string="Manufacturing Order", + readonly=True, + ) + product_id = fields.Many2one( + related="production_id.product_id", + readonly=True, + ) + company_id = fields.Many2one( + related="production_id.company_id", + readonly=True, + ) + finished_lot_ids = fields.Many2many( + string="Finished Product Serial Numbers", + comodel_name="stock.production.lot", + domain="[('product_id', '=', product_id)]", + ) + line_ids = fields.One2many( + string="Matrix Cell", + comodel_name="mrp.production.serial.matrix.line", + inverse_name="wizard_id", + ) + lot_selection_warning_msg = fields.Char(compute="_compute_lot_selection_warning") + lot_selection_warning_ids = fields.Many2many( + comodel_name="stock.production.lot", compute="_compute_lot_selection_warning" + ) + lot_selection_warning_count = fields.Integer( + compute="_compute_lot_selection_warning" + ) + + @api.depends("line_ids", "line_ids.component_lot_id") + def _compute_lot_selection_warning(self): + for rec in self: + warning_lots = self.env["stock.production.lot"] + warning_msgs = [] + # Serials: + serial_lines = rec.line_ids.filtered( + lambda l: l.component_id.tracking == "serial" + ) + serial_counter = {} + for sl in serial_lines: + if not sl.component_lot_id: + continue + serial_counter.setdefault(sl.component_lot_id, 0) + serial_counter[sl.component_lot_id] += 1 + for lot, counter in serial_counter.items(): + if counter > 1: + warning_lots += lot + warning_msgs.append( + "Serial number %s selected several times" % lot.name + ) + # Lots + lot_lines = rec.line_ids.filtered( + lambda l: l.component_id.tracking == "lot" + ) + lot_consumption = {} + for ll in lot_lines: + if not ll.component_lot_id: + continue + lot_consumption.setdefault(ll.component_lot_id, 0) + available_quantity = self.env["stock.quant"]._get_available_quantity( + ll.component_id, + ll.production_id.location_src_id, + lot_id=ll.component_lot_id, + ) + if ( + available_quantity - lot_consumption[ll.component_lot_id] + < ll.lot_qty + ): + warning_lots += ll.component_lot_id + warning_msgs.append( + "Lot %s not available at the needed qty (%s/%s)" + % (ll.component_lot_id.name, available_quantity, ll.lot_qty) + ) + lot_consumption[ll.component_lot_id] += ll.lot_qty + + not_filled_lines = rec.line_ids.filtered( + lambda l: l.finished_lot_id and not l.component_lot_id + ) + if not_filled_lines: + not_filled_finshed_lots = not_filled_lines.mapped("finished_lot_id") + warning_lots += not_filled_finshed_lots + warning_msgs.append( + "Some cells are not filled for some finished serial number (%s)" + % ", ".join(not_filled_finshed_lots.mapped("name")) + ) + rec.lot_selection_warning_msg = ", ".join(warning_msgs) + rec.lot_selection_warning_ids = warning_lots + rec.lot_selection_warning_count = len(warning_lots) + + @api.model + def default_get(self, fields): + res = super().default_get(fields) + production_id = self.env.context["active_id"] + active_model = self.env.context["active_model"] + if not production_id: + return res + assert active_model == "mrp.production", "Bad context propagation" + production = self.env["mrp.production"].browse(production_id) + if not production.show_serial_matrix: + raise UserError( + _("The finished product of this MO is not tracked by serial numbers.") + ) + + finished_lots = self.env["stock.production.lot"] + if production.lot_producing_id: + finished_lots = production.lot_producing_id + + matrix_lines = self._get_matrix_lines(production, finished_lots) + + res.update( + { + "line_ids": [(0, 0, x) for x in matrix_lines], + "production_id": production_id, + "finished_lot_ids": [(4, lot_id, 0) for lot_id in finished_lots.ids], + } + ) + return res + + def _get_matrix_lines(self, production, finished_lots): + tracked_components = [] + for line in production.bom_id.bom_line_ids: + if line.product_id.tracking == "serial": + # TODO: factor if parent is not 1. + # TODO: uom. + for i in range(1, int(line.product_qty) + 1): + tracked_components.append((line.product_id, i, 1)) + elif line.product_id.tracking == "lot": + tracked_components.append((line.product_id, 0, line.product_qty)) + + matrix_lines = [] + current_lot = False + new_lot_number = 0 + for _i in range(int(production.product_qty)): + if finished_lots: + current_lot = finished_lots[0] + else: + new_lot_number += 1 + for component_tuple in tracked_components: + line = self._prepare_matrix_line( + component_tuple, finished_lot=current_lot, number=new_lot_number + ) + matrix_lines.append(line) + if current_lot: + finished_lots -= current_lot + current_lot = False + return matrix_lines + + def _prepare_matrix_line(self, component_tuple, finished_lot=None, number=None): + component, lot_no, lot_qty = component_tuple + column_name = component.display_name + if lot_no > 0: + column_name += " (%s)" % lot_no + res = { + "component_id": component.id, + "component_column_name": column_name, + "lot_qty": lot_qty, + } + if finished_lot: + if isinstance(finished_lot.id, models.NewId): + # NewId instances are not handled correctly later, this is a + # small workaround. In future versions it might not be needed. + lot_id = finished_lot.id.origin + else: + lot_id = finished_lot.id + res.update( + { + "finished_lot_id": lot_id, + "finished_lot_name": finished_lot.name, + } + ) + elif isinstance(number, int): + res.update( + { + "finished_lot_name": _("(New Lot %s)") % number, + } + ) + return res + + @api.onchange("finished_lot_ids") + def _onchange_finished_lot_ids(self): + for rec in self: + matrix_lines = self._get_matrix_lines( + rec.production_id, + rec.finished_lot_ids, + ) + rec.line_ids = False + rec.write({"line_ids": [(0, 0, x) for x in matrix_lines]}) + + def button_validate(self): + self.ensure_one() + if self.lot_selection_warning_count > 0: + raise UserError( + _("Some issues has been detected in your selection: %s") + % self.lot_selection_warning_msg + ) + mos = self.env["mrp.production"] + current_mo = self.production_id + for fp_lot in self.finished_lot_ids: + # Apply selected lots in matrix and set the qty producing + current_mo.lot_producing_id = fp_lot + current_mo.qty_producing = 1.0 + current_mo._set_qty_producing() + for move in current_mo.move_raw_ids: + if move.product_id.tracking in ["serial", "lot"]: + # We filter using the lot nane because the ORM sometimes + # is not storing correctly the finished_lot_id in the lines + # after passing through the `_onchange_finished_lot_ids` + # method. + matrix_lines = self.line_ids.filtered( + lambda l: ( + l.finished_lot_id == fp_lot + or l.finished_lot_name == fp_lot.name + ) + and l.component_id == move.product_id + ) + if matrix_lines: + self._amend_reservations(move, matrix_lines) + self._consume_selected_lots(move, matrix_lines) + + # Complete MO and create backorder if needed. + mos += current_mo + res = current_mo.button_mark_done() + backorder_wizard = self.env["mrp.production.backorder"] + if isinstance(res, dict) and res.get("res_model") == backorder_wizard._name: + # create backorders... + lines = res.get("context", {}).get( + "default_mrp_production_backorder_line_ids" + ) + wizard = backorder_wizard.create( + { + "mrp_production_ids": current_mo.ids, + "mrp_production_backorder_line_ids": lines, + } + ) + wizard.action_backorder() + + backorder_ids = ( + current_mo.procurement_group_id.mrp_production_ids.filtered( + lambda mo: mo.state not in ["done", "cancel"] + ) + ) + current_mo = backorder_ids[0] if backorder_ids else False + if not current_mo: + break + else: + break + + # TODO: not specified lots: auto create lots? + if not mos: + mos = self.production_id + res = { + "domain": [("id", "in", mos.ids)], + "name": _("Manufacturing Orders"), + "src_model": "mrp.production.serial.matrix", + "view_type": "form", + "view_mode": "tree,form", + "view_id": False, + "views": False, + "res_model": "mrp.production", + "type": "ir.actions.act_window", + } + return res + + def _amend_reservations(self, move, matrix_lines): + lots_to_consume = matrix_lines.mapped("component_lot_id") + lots_in_move = move.move_line_ids.mapped("lot_id") + lots_to_reserve = lots_to_consume - lots_in_move + if lots_to_reserve: + to_unreserve_lots = lots_in_move - lots_to_consume + move.move_line_ids.filtered( + lambda l: l.lot_id in to_unreserve_lots + ).unlink() + for lot in lots_to_reserve: + if move.product_id.tracking == "lot": + qty = sum( + matrix_lines.filtered( + lambda l: l.component_lot_id == lot + ).mapped("lot_qty") + ) + qty = qty + else: + qty = 1.0 + self._reserve_lot_in_move(move, lot, qty=qty) + + return True + + def _consume_selected_lots(self, move, matrix_lines): + lots_to_consume = matrix_lines.mapped("component_lot_id") + precision_digits = self.env["decimal.precision"].precision_get( + "Product Unit of Measure" + ) + for ml in move.move_line_ids: + if ml.lot_id in lots_to_consume: + if move.product_id.tracking == "lot": + qty = sum( + matrix_lines.filtered( + lambda l: l.component_lot_id == ml.lot_id + ).mapped("lot_qty") + ) + ml.qty_done = qty + else: + ml.qty_done = ml.product_qty + elif float_is_zero(ml.product_qty, precision_digits=precision_digits): + ml.unlink() + else: + ml.qty_done = 0.0 + + def _reserve_lot_in_move(self, move, lot, qty): + precision_digits = self.env["decimal.precision"].precision_get( + "Product Unit of Measure" + ) + available_quantity = self.env["stock.quant"]._get_available_quantity( + move.product_id, + move.location_id, + lot_id=lot, + ) + if ( + float_compare(available_quantity, 0.0, precision_digits=precision_digits) + <= 0 + ): + raise ValidationError( + _("Serial/Lot number '%s' not available at source location.") % lot.name + ) + move._update_reserved_quantity( + qty, + available_quantity, + move.location_id, + lot_id=lot, + strict=True, + ) diff --git a/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix_line.py b/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix_line.py new file mode 100644 index 000000000..54cc15245 --- /dev/null +++ b/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix_line.py @@ -0,0 +1,38 @@ +# Copyright 2019 ForgeFlow S.L. (https://www.forgeflow.com) +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). + +from odoo import fields, models + + +class MrpProductionSerialMatrix(models.TransientModel): + _name = "mrp.production.serial.matrix.line" + _description = "Mrp Production Serial Matrix Line" + + wizard_id = fields.Many2one( + comodel_name="mrp.production.serial.matrix", + ) + production_id = fields.Many2one(related="wizard_id.production_id") + component_id = fields.Many2one(comodel_name="product.product") + component_column_name = fields.Char() + finished_lot_id = fields.Many2one(comodel_name="stock.production.lot") + finished_lot_name = fields.Char() + component_lot_id = fields.Many2one( + comodel_name="stock.production.lot", + domain="[('id', 'in', allowed_component_lot_ids)]", + ) + allowed_component_lot_ids = fields.Many2many( + comodel_name="stock.production.lot", + compute="_compute_allowed_component_lot_ids", + ) + lot_qty = fields.Float(digits="Product Unit of Measure") + + def _compute_allowed_component_lot_ids(self): + for rec in self: + available_quants = self.env["stock.quant"].search( + [ + ("location_id", "child_of", rec.production_id.location_src_id.id), + ("product_id", "=", rec.component_id.id), + ("quantity", ">", 0), + ] + ) + rec.allowed_component_lot_ids = available_quants.mapped("lot_id") diff --git a/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix_view.xml b/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix_view.xml new file mode 100644 index 000000000..313ac0dc7 --- /dev/null +++ b/mrp_production_serial_matrix/wizards/mrp_production_serial_matrix_view.xml @@ -0,0 +1,96 @@ + + + + mrp.production.serial.matrix.form + mrp.production.serial.matrix + +
+ + + + + + +

Select the Serial Numbers of the Finished Products that are going to be produced in this MO. You can create new ones on the fly if needed.

+
+ +
+ +

Fill each cell with the lot/serial number consumed to produce each finished product serial number

+
+ + + + + + + + + + + + +
+
+
+ +
+
+ + + Serial Numbers and Lot Consumption Matrix + mrp.production.serial.matrix + ir.actions.act_window + form + new + + +
diff --git a/setup/mrp_production_serial_matrix/odoo/addons/mrp_production_serial_matrix b/setup/mrp_production_serial_matrix/odoo/addons/mrp_production_serial_matrix new file mode 120000 index 000000000..e55bec17f --- /dev/null +++ b/setup/mrp_production_serial_matrix/odoo/addons/mrp_production_serial_matrix @@ -0,0 +1 @@ +../../../../mrp_production_serial_matrix \ No newline at end of file diff --git a/setup/mrp_production_serial_matrix/setup.py b/setup/mrp_production_serial_matrix/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/mrp_production_serial_matrix/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)