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 29, 2021
1 parent 53d29db commit aacc1d3
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 28 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,93 @@
# Copyright 2021 ForgeFlow S.L. (https://forgeflow.com)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


def migrate(cr, version):
"""
Fields 'code' and 'serial_no' of model 'maintenance.equipment' fulfill the
same function, which is to have an internal identifier of an equipment.
Therefore, as a solution we will use only one column.
Field 'serial_no' is the one in odoo core and the one supposedly used for
identifying our equipment.
Based on this assumption, this migration will save on 'serial_no' column
all the original values plus all the 'code' values that are null in
'serial_no' original column. And after this, it will delete 'code' column.
POSSIBLE ISSUE:
Considering that this migration may not work correctly to everyone,
because it's giving priority to 'serial_no' before 'code', we will create
two new columns 'old_code' and 'old_serial_no' with the values of the
original columns.
ISSUE: If you identify your equipments with the 'code' field and you prefer to
save it's values rather than 'serial_no' values. We provide you the following
query:
SQL STATEMENT:
UPDATE maintenance_equipment
SET serial_no = old_code
WHERE old_code IS NOT NULL;
"""

if not version:
return

cr.execute(
"""
SELECT count(attname) FROM pg_attribute
WHERE attrelid = (SELECT oid FROM pg_class
WHERE relname = 'maintenance_equipment')
AND attname = 'code'"""
)

if not cr.fetchone()[0] == 1:
return

cr.execute(
"""
ALTER TABLE maintenance_equipment ADD COLUMN "old_serial_no" varchar;
UPDATE maintenance_equipment SET old_serial_no = serial_no;"""
)

cr.execute(
"""
ALTER TABLE maintenance_equipment ADD COLUMN "old_code" varchar;
UPDATE maintenance_equipment SET old_code = code"""
)

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

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

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")
22 changes: 11 additions & 11 deletions maintenance_equipment_sequence/views/maintenance_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
<field name="name">equipment.form</field>
<field name="model">maintenance.equipment</field>
<field name="inherit_id" ref="maintenance.hr_equipment_view_form" />
<field name="priority">99</field>
<field name="arch" type="xml">
<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" />
<h3>
<field name="code" />
</h3>
</div>
</div>
<field name="serial_no" position="attributes">
<attribute name="invisible">1</attribute>
</field>
<xpath expr="//div[hasclass('oe_title')]/*[1]" position="before">
<label for="serial_no" class="oe_edit_only" />
<h3>
<field name="serial_no" />
</h3>
</xpath>
</field>
</record>
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 aacc1d3

Please sign in to comment.