Skip to content

Commit

Permalink
[ADD] maintenance_equipment_stock
Browse files Browse the repository at this point in the history
  • Loading branch information
etobella committed Sep 20, 2023
1 parent afd6a89 commit f7781d6
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 0 deletions.
4 changes: 4 additions & 0 deletions maintenance_equipment_stock/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Maintenance Equipment Stock
===========================

Create Equipment from stock pickings in an easy way
1 change: 1 addition & 0 deletions maintenance_equipment_stock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions maintenance_equipment_stock/__manifest__.py
Original file line number Diff line number Diff line change
@@ -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": [],
}
5 changes: 5 additions & 0 deletions maintenance_equipment_stock/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions maintenance_equipment_stock/models/maintenance_equipment.py
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions maintenance_equipment_stock/models/product_template.py
Original file line number Diff line number Diff line change
@@ -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")
38 changes: 38 additions & 0 deletions maintenance_equipment_stock/models/stock_move_line.py
Original file line number Diff line number Diff line change
@@ -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,
}
29 changes: 29 additions & 0 deletions maintenance_equipment_stock/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions maintenance_equipment_stock/models/stock_production_lot.py
Original file line number Diff line number Diff line change
@@ -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")
1 change: 1 addition & 0 deletions maintenance_equipment_stock/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_equipment_from_purchase
48 changes: 48 additions & 0 deletions maintenance_equipment_stock/tests/test_equipment_from_purchase.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions maintenance_equipment_stock/views/maintenance_equipment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Dixmit
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="maintenance_equipment_form_view">
<field
name="name"
>maintenance.equipment.form (in maintenance_equipment_stock)</field>
<field name="model">maintenance.equipment</field>
<field name="inherit_id" ref="maintenance.hr_equipment_view_form" />
<field name="arch" type="xml">
<field name="serial_no" position="after">
<field name="lot_id" attrs="{'invisible': [('lot_id', '=', False)]}" />
<field
name="picking_id"
attrs="{'invisible': [('picking_id', '=', False)]}"
/>
<field
name="purchase_id"
attrs="{'invisible': [('purchase_id', '=', False)]}"
/>
</field>
</field>
</record>



</odoo>
38 changes: 38 additions & 0 deletions maintenance_equipment_stock/views/product_template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Dixmit
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="product_template_form_view">
<field
name="name"
>product.template.form (in maintenance_equipment_stock)</field>
<field name="model">product.template</field>
<field name="inherit_id" ref="stock.view_template_property_form" />
<field name="arch" type="xml">
<field name="tracking" position="after">
<field
name="maintenance_lot"
attrs="{'invisible': [('tracking', '!=', 'serial')]}"
/>
</field>
<notebook position="inside">
<page
name="maintenance"
string="Maintenance"
attrs="{'invisible': ['|','|', ('maintenance_lot', '=', False), ('type', '!=', 'product'), ('tracking', '!=', 'serial')]}"
>
<grpup>
<group>
<field name="maintenance_category_id" />
<field name="maintenance_team_id" />
</group>
</grpup>
</page>
</notebook>
</field>
</record>



</odoo>
33 changes: 33 additions & 0 deletions maintenance_equipment_stock/views/stock_picking.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Dixmit
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>

<record model="ir.ui.view" id="stock_picking_form_view">
<field name="name">stock.picking.form (in maintenance_equipment_stock)</field>
<field name="model">stock.picking</field>
<field name="inherit_id" ref="stock.view_picking_form" />
<field name="arch" type="xml">
<div name="button_box" position="inside">
<button
name="action_show_equipments"
class="oe_stat_button"
icon="fa-laptop"
type="object"
help="List view of equipments"
attrs="{'invisible': [('maintenance_equipment_count', '=', 0)]}"
>
<div class="o_form_field o_stat_info">
<span class="o_stat_value">
<field name="maintenance_equipment_count" />
</span>
<span class="o_stat_text">Equipment(s)</span>
</div>
</button>
</div>
</field>
</record>



</odoo>
16 changes: 16 additions & 0 deletions maintenance_equipment_stock/views/stock_production_lot.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2023 Dixmit
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->
<odoo>
<!--
<record model="ir.ui.view" id="stock_production_lot_form_view">
<field name="name">stock.production.lot.form (in maintenance_equipment_stock)</field>
<field name="model">stock.production.lot</field>
<field name="inherit_id" ref="stock.view_production_lot_form"/>
<field name="arch" type="xml">
</field>
</record>
-->


</odoo>
6 changes: 6 additions & 0 deletions setup/maintenance_equipment_stock/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit f7781d6

Please sign in to comment.