From f7781d6b49820b93d5852d04be784af7d5ec9144 Mon Sep 17 00:00:00 2001 From: Enric Tobella Date: Mon, 18 Sep 2023 20:41:41 +0200 Subject: [PATCH] [ADD] maintenance_equipment_stock --- maintenance_equipment_stock/README.rst | 4 ++ maintenance_equipment_stock/__init__.py | 1 + maintenance_equipment_stock/__manifest__.py | 23 +++++++++ .../models/__init__.py | 5 ++ .../models/maintenance_equipment.py | 13 +++++ .../models/product_template.py | 12 +++++ .../models/stock_move_line.py | 38 +++++++++++++++ .../models/stock_picking.py | 29 +++++++++++ .../models/stock_production_lot.py | 10 ++++ maintenance_equipment_stock/tests/__init__.py | 1 + .../tests/test_equipment_from_purchase.py | 48 +++++++++++++++++++ .../views/maintenance_equipment.xml | 29 +++++++++++ .../views/product_template.xml | 38 +++++++++++++++ .../views/stock_picking.xml | 33 +++++++++++++ .../views/stock_production_lot.xml | 16 +++++++ .../odoo/addons/maintenance_equipment_stock | 1 + setup/maintenance_equipment_stock/setup.py | 6 +++ 17 files changed, 307 insertions(+) create mode 100644 maintenance_equipment_stock/README.rst create mode 100644 maintenance_equipment_stock/__init__.py create mode 100644 maintenance_equipment_stock/__manifest__.py create mode 100644 maintenance_equipment_stock/models/__init__.py create mode 100644 maintenance_equipment_stock/models/maintenance_equipment.py create mode 100644 maintenance_equipment_stock/models/product_template.py create mode 100644 maintenance_equipment_stock/models/stock_move_line.py create mode 100644 maintenance_equipment_stock/models/stock_picking.py create mode 100644 maintenance_equipment_stock/models/stock_production_lot.py create mode 100644 maintenance_equipment_stock/tests/__init__.py create mode 100644 maintenance_equipment_stock/tests/test_equipment_from_purchase.py create mode 100644 maintenance_equipment_stock/views/maintenance_equipment.xml create mode 100644 maintenance_equipment_stock/views/product_template.xml create mode 100644 maintenance_equipment_stock/views/stock_picking.xml create mode 100644 maintenance_equipment_stock/views/stock_production_lot.xml create mode 120000 setup/maintenance_equipment_stock/odoo/addons/maintenance_equipment_stock create mode 100644 setup/maintenance_equipment_stock/setup.py diff --git a/maintenance_equipment_stock/README.rst b/maintenance_equipment_stock/README.rst new file mode 100644 index 00000000..e3ce39bf --- /dev/null +++ b/maintenance_equipment_stock/README.rst @@ -0,0 +1,4 @@ +Maintenance Equipment Stock +=========================== + +Create Equipment from stock pickings in an easy way diff --git a/maintenance_equipment_stock/__init__.py b/maintenance_equipment_stock/__init__.py new file mode 100644 index 00000000..0650744f --- /dev/null +++ b/maintenance_equipment_stock/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/maintenance_equipment_stock/__manifest__.py b/maintenance_equipment_stock/__manifest__.py new file mode 100644 index 00000000..b284e39a --- /dev/null +++ b/maintenance_equipment_stock/__manifest__.py @@ -0,0 +1,23 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Maintenance Equipment Stock", + "summary": """ + Create equipments from stocks""", + "version": "14.0.1.0.0", + "license": "AGPL-3", + "author": "Dixmit", + "website": "https://github.com/oxigensalud/odoo-addons", + "depends": [ + "maintenance", + "purchase_stock", + ], + "data": [ + "views/stock_picking.xml", + "views/stock_production_lot.xml", + "views/maintenance_equipment.xml", + "views/product_template.xml", + ], + "demo": [], +} diff --git a/maintenance_equipment_stock/models/__init__.py b/maintenance_equipment_stock/models/__init__.py new file mode 100644 index 00000000..827c9a6b --- /dev/null +++ b/maintenance_equipment_stock/models/__init__.py @@ -0,0 +1,5 @@ +from . import product_template +from . import maintenance_equipment +from . import stock_production_lot +from . import stock_move_line +from . import stock_picking diff --git a/maintenance_equipment_stock/models/maintenance_equipment.py b/maintenance_equipment_stock/models/maintenance_equipment.py new file mode 100644 index 00000000..ae85c9de --- /dev/null +++ b/maintenance_equipment_stock/models/maintenance_equipment.py @@ -0,0 +1,13 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class MaintenanceEquipment(models.Model): + _inherit = "maintenance.equipment" + + lot_id = fields.Many2one("stock.production.lot", readonly=True) + picking_id = fields.Many2one("stock.picking", readonly=True) + stock_move_line_id = fields.Many2one("stock.move.line", readonly=True) + purchase_id = fields.Many2one("purchase.order", readonly=True) diff --git a/maintenance_equipment_stock/models/product_template.py b/maintenance_equipment_stock/models/product_template.py new file mode 100644 index 00000000..0de6ae2b --- /dev/null +++ b/maintenance_equipment_stock/models/product_template.py @@ -0,0 +1,12 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + maintenance_lot = fields.Boolean() + maintenance_category_id = fields.Many2one("maintenance.equipment.category") + maintenance_team_id = fields.Many2one("maintenance.team") diff --git a/maintenance_equipment_stock/models/stock_move_line.py b/maintenance_equipment_stock/models/stock_move_line.py new file mode 100644 index 00000000..c0abe2df --- /dev/null +++ b/maintenance_equipment_stock/models/stock_move_line.py @@ -0,0 +1,38 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class StockMoveLine(models.Model): + + _inherit = "stock.move.line" + + def _action_done(self): + result = super()._action_done() + for ml in self.filtered(lambda m: m.exists()): + if ( + ml.product_id.maintenance_lot + and ml.product_id.type == "product" + and ml.product_id.tracking == "serial" + ): + ml._create_maintenance_equipment() + return result + + def _create_maintenance_equipment(self): + return self.env["maintenance.equipment"].create( + self._create_maintenance_equipment_vals() + ) + + def _create_maintenance_equipment_vals(self): + return { + "lot_id": self.lot_id.id, + "picking_id": self.move_id.picking_id.id, + "stock_move_line_id": self.id, + "name": self.product_id.display_name, + "company_id": self.company_id.id, + "partner_id": self.move_id.picking_id.partner_id.id, + "category_id": self.product_id.maintenance_category_id.id, + "maintenance_team_id": self.product_id.maintenance_team_id.id, + "purchase_id": self.move_id.picking_id.purchase_id.id, + } diff --git a/maintenance_equipment_stock/models/stock_picking.py b/maintenance_equipment_stock/models/stock_picking.py new file mode 100644 index 00000000..8505955d --- /dev/null +++ b/maintenance_equipment_stock/models/stock_picking.py @@ -0,0 +1,29 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StockPicking(models.Model): + + _inherit = "stock.picking" + + maintenance_equipment_ids = fields.One2many( + "maintenance.equipment", inverse_name="picking_id" + ) + maintenance_equipment_count = fields.Integer( + compute="_compute_maintenance_equipment_count" + ) + + def _compute_maintenance_equipment_count(self): + for record in self: + record.maintenance_equipment_count = len(record.maintenance_equipment_ids) + + def action_show_equipments(self): + self.ensure_one() + action = self.env["ir.actions.act_window"]._for_xml_id( + "maintenance.hr_equipment_action" + ) + action["context"] = {} + action["domain"] = [("id", "in", self.maintenance_equipment_ids.ids)] + return action diff --git a/maintenance_equipment_stock/models/stock_production_lot.py b/maintenance_equipment_stock/models/stock_production_lot.py new file mode 100644 index 00000000..587aea80 --- /dev/null +++ b/maintenance_equipment_stock/models/stock_production_lot.py @@ -0,0 +1,10 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class StockProductionLot(models.Model): + _inherit = "stock.production.lot" + + equipment_ids = fields.One2many("maintenance.equipment", inverse_name="lot_id") diff --git a/maintenance_equipment_stock/tests/__init__.py b/maintenance_equipment_stock/tests/__init__.py new file mode 100644 index 00000000..22d344a0 --- /dev/null +++ b/maintenance_equipment_stock/tests/__init__.py @@ -0,0 +1 @@ +from . import test_equipment_from_purchase diff --git a/maintenance_equipment_stock/tests/test_equipment_from_purchase.py b/maintenance_equipment_stock/tests/test_equipment_from_purchase.py new file mode 100644 index 00000000..2e616237 --- /dev/null +++ b/maintenance_equipment_stock/tests/test_equipment_from_purchase.py @@ -0,0 +1,48 @@ +# Copyright 2023 Dixmit +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo.tests.common import SavepointCase + + +class TestEquipmentFromPurchase(SavepointCase): + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.partner = cls.env["res.partner"].create({"name": "Demo Partner"}) + cls.product = cls.env["product.template"].create( + { + "name": "My Product", + "type": "product", + "tracking": "serial", + "maintenance_lot": True, + } + ) + cls.purchase = cls.env["purchase.order"].create( + { + "partner_id": cls.partner.id, + "order_line": [ + ( + 0, + 0, + { + "product_id": cls.product.product_variant_id.id, + "name": cls.product.name, + }, + ) + ], + } + ) + + def test_equipment_creation(self): + self.purchase.button_confirm() + picking = self.purchase.picking_ids + self.assertTrue(picking) + picking.move_ids_without_package.move_line_ids.write( + { + "lot_name": "1234", + "qty_done": 1, + } + ) + self.assertFalse(picking.maintenance_equipment_ids) + picking.button_validate() + self.assertTrue(picking.maintenance_equipment_ids) diff --git a/maintenance_equipment_stock/views/maintenance_equipment.xml b/maintenance_equipment_stock/views/maintenance_equipment.xml new file mode 100644 index 00000000..1feec6a0 --- /dev/null +++ b/maintenance_equipment_stock/views/maintenance_equipment.xml @@ -0,0 +1,29 @@ + + + + + + maintenance.equipment.form (in maintenance_equipment_stock) + maintenance.equipment + + + + + + + + + + + + + diff --git a/maintenance_equipment_stock/views/product_template.xml b/maintenance_equipment_stock/views/product_template.xml new file mode 100644 index 00000000..ddf41538 --- /dev/null +++ b/maintenance_equipment_stock/views/product_template.xml @@ -0,0 +1,38 @@ + + + + + + product.template.form (in maintenance_equipment_stock) + product.template + + + + + + + + + + + + + + + + + + + + + diff --git a/maintenance_equipment_stock/views/stock_picking.xml b/maintenance_equipment_stock/views/stock_picking.xml new file mode 100644 index 00000000..97148d9d --- /dev/null +++ b/maintenance_equipment_stock/views/stock_picking.xml @@ -0,0 +1,33 @@ + + + + + + stock.picking.form (in maintenance_equipment_stock) + stock.picking + + +
+ +
+
+
+ + + +
diff --git a/maintenance_equipment_stock/views/stock_production_lot.xml b/maintenance_equipment_stock/views/stock_production_lot.xml new file mode 100644 index 00000000..cd6e6a69 --- /dev/null +++ b/maintenance_equipment_stock/views/stock_production_lot.xml @@ -0,0 +1,16 @@ + + + + + + + diff --git a/setup/maintenance_equipment_stock/odoo/addons/maintenance_equipment_stock b/setup/maintenance_equipment_stock/odoo/addons/maintenance_equipment_stock new file mode 120000 index 00000000..2d951bf5 --- /dev/null +++ b/setup/maintenance_equipment_stock/odoo/addons/maintenance_equipment_stock @@ -0,0 +1 @@ +../../../../maintenance_equipment_stock \ No newline at end of file diff --git a/setup/maintenance_equipment_stock/setup.py b/setup/maintenance_equipment_stock/setup.py new file mode 100644 index 00000000..28c57bb6 --- /dev/null +++ b/setup/maintenance_equipment_stock/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)