Skip to content

Commit

Permalink
[MIG] maintenance_equipment_sequence: Migration to 15.0
Browse files Browse the repository at this point in the history
  • Loading branch information
BernatPForgeFlow committed Oct 28, 2021
1 parent 53d29db commit 9e37e98
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 21 deletions.
4 changes: 2 additions & 2 deletions maintenance_equipment_sequence/__manifest__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Copyright 2020 ForgeFlow S.L. (https://forgeflow.com)
# Copyright 2021 ForgeFlow S.L. (https://forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Maintenance Equipment Sequence",
"summary": """
Adds sequence to maintenance equipment defined in the equipment's
category""",
"version": "14.0.1.0.0",
"version": "15.0.1.0.0",
"license": "AGPL-3",
"author": "ForgeFlow S.L.," "Odoo Community Association (OCA)",
"maintainers": ["AdriaGForgeFlow"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2021 ForgeFlow S.L. (https://forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


def migrate(cr, version):
"""
Column `code` of table `maintenance_equipment` has been modified to not have duplicates
and then has been copied to `serial_no` column because they have the same function and
`serial_no` is the one in the core.
Afterwards, column 'code' has been deleted.
"""

if not version:
return

cr.execute(
"""
UPDATE maintenance_equipment
SET code = serial_no
WHERE code IS NULL;"""
)

cr.execute(
"""
WITH cte AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY code ORDER BY code ) AS rno, code
FROM maintenance_equipment
)
UPDATE maintenance_equipment me
SET code = me.code || '_' || me.id
FROM cte
WHERE cte.rno>1 AND cte.code = me.code"""
)

cr.execute(
"""
UPDATE maintenance_equipment
SET serial_no = code
WHERE code IS NOT NULL;"""
)

cr.execute(
"""
ALTER TABLE maintenance_equipment
DROP COLUMN code;"""
)
17 changes: 7 additions & 10 deletions maintenance_equipment_sequence/models/maintenance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2020 ForgeFlow S.L. (https://forgeflow.com)
# Copyright 2021 ForgeFlow S.L. (https://forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
Expand All @@ -8,7 +8,6 @@ class MaintenanceEquipmentCategory(models.Model):
_inherit = "maintenance.equipment.category"

sequence_prefix = fields.Char(
string="Sequence Prefix",
help="The equipment's sequence will be created using this prefix.",
)
sequence_id = fields.Many2one(
Expand Down Expand Up @@ -101,31 +100,29 @@ def _compute_equipment_code(self):
[("category_id", "=", category.id)]
)
for equipment in category_equipments:
if not equipment.code and equipment.category_id.sequence_id:
equipment.code = equipment.category_id.sequence_id._next()
if not equipment.serial_no and equipment.category_id.sequence_id:
equipment.serial_no = equipment.category_id.sequence_id._next()


class MaintenanceEquipment(models.Model):
_inherit = "maintenance.equipment"

code = fields.Char(help="Equipment Code")

@api.model
def create(self, vals):
equipment = super(MaintenanceEquipment, self).create(vals)
if equipment.category_id and not equipment.code:
if equipment.category_id and not equipment.serial_no:
sequence_id = (
self.env["maintenance.equipment.category"]
.browse(vals["category_id"])
.sequence_id
)
if sequence_id:
equipment.code = sequence_id._next()
equipment.serial_no = sequence_id._next()
return equipment

def write(self, vals):
result = super(MaintenanceEquipment, self).write(vals)
for rec in self:
if rec.category_id and not rec.code and rec.category_id.sequence_id:
rec.code = rec.category_id.sequence_id._next()
if rec.category_id and not rec.serial_no and rec.category_id.sequence_id:
rec.serial_no = rec.category_id.sequence_id._next()
return result
1 change: 1 addition & 0 deletions maintenance_equipment_sequence/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* Adria Gil Sorribes <adria.gil@forgeflow.com>
* Núria Martín Xifré <nuria.martin@forgeflow.com>
* Bernat Puig Font <bernat.puig@forgeflow.com>
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def test_01_maintenance_equipment_sequence(self):
{"name": "Laptop 1", "category_id": categ_2.id}
)
categ_2._compute_equipment_code()
self.assertEqual(equipment_1.code, "TTC0001")
self.assertEqual(equipment_1.serial_no, "TTC0001")
# Set code manually
equipment_1.write({"code": "TTC0023"})
self.assertEqual(equipment_1.code, "TTC0023")
equipment_1.write({"serial_no": "TTC0023"})
self.assertEqual(equipment_1.serial_no, "TTC0023")
# Remove code and be automatically set to sequence next value
equipment_1.write({"code": False})
self.assertEqual(equipment_1.code, "TTC0002")
equipment_1.write({"serial_no": False})
self.assertEqual(equipment_1.serial_no, "TTC0002")
8 changes: 4 additions & 4 deletions maintenance_equipment_sequence/views/maintenance_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<xpath expr="//div[hasclass('oe_title')]" position="before">
<div class="row">
<div class="col-xs-2" style="padding-left:1.0em">
<label for="code" class="oe_edit_only" />
<label for="serial_no" class="oe_edit_only" string="Code" />
<h3>
<field name="code" />
<field name="serial_no" />
</h3>
</div>
</div>
Expand All @@ -46,7 +46,7 @@
<field name="inherit_id" ref="maintenance.hr_equipment_view_tree" />
<field name="arch" type="xml">
<field name="name" position="before">
<field name="code" />
<field name="serial_no" />
</field>
</field>
</record>
Expand All @@ -56,7 +56,7 @@
<field name="inherit_id" ref="maintenance.hr_equipment_view_search" />
<field name="arch" type="xml">
<field name="name" position="after">
<field name="code" />
<field name="serial_no" />
</field>
</field>
</record>
Expand Down

0 comments on commit 9e37e98

Please sign in to comment.