Skip to content

Commit

Permalink
[MIG] maintenance_plan: Migration to 12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dalonsod committed Jul 9, 2019
1 parent 1f379df commit bf0465d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 54 deletions.
2 changes: 1 addition & 1 deletion maintenance_plan/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{'name': 'Maintenance Plan',
'summary': 'Extends preventive maintenance planning',
'version': '11.0.1.0.0',
'version': '12.0.1.0.0',
'author': 'Odoo Community Association (OCA), Camptocamp SA',
'license': 'AGPL-3',
'category': 'Maintenance',
Expand Down
2 changes: 1 addition & 1 deletion maintenance_plan/data/demo_maintenance_plan.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
</record>

</data>
</odoo>
</odoo>
54 changes: 21 additions & 33 deletions maintenance_plan/models/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MaintenanceKind(models.Model):
_name = 'maintenance.kind'
_description = 'Maintenance Kind'

name = fields.Char('Name', required=True, translate=True)
name = fields.Char(required=True, translate=True)
active = fields.Boolean('Active Kind', required=True, default=True)

_sql_constraints = [
Expand All @@ -33,10 +33,8 @@ class MaintenancePlan(models.Model):
comodel_name='maintenance.kind',
ondelete='restrict')

period = fields.Integer(string='Period',
help='Days between each maintenance')
duration = fields.Float(string='Duration',
help='Maintenance duration in hours')
period = fields.Integer(help='Days between each maintenance')
duration = fields.Float(help='Maintenance duration in hours')

next_maintenance_date = fields.Date('Next maintenance date',
compute='_compute_next_maintenance')
Expand All @@ -47,8 +45,7 @@ class MaintenancePlan(models.Model):
'equipment_id.maintenance_ids.maintenance_kind_id')
def _compute_next_maintenance(self):

date_now = fields.Date.context_today(self)
today_date = fields.Date.from_string(date_now)
today_date = fields.Date.context_today(self)

for plan in self.filtered(lambda x: x.period > 0):

Expand All @@ -68,48 +65,39 @@ def _compute_next_maintenance(self):
('close_date', '!=', False)], order="close_date desc", limit=1)
if next_maintenance_todo and last_maintenance_done:
next_date = next_maintenance_todo.request_date
date_gap = fields.Date.from_string(
next_maintenance_todo.request_date) - \
fields.Date.from_string(last_maintenance_done.close_date)
date_gap = next_maintenance_todo.request_date - \
last_maintenance_done.close_date
# If the gap between the last_maintenance_done and the
# next_maintenance_todo one is bigger than 2 times the period
# and next request is in the future
# We use 2 times the period to avoid creation too closed
# request from a manually one created
if date_gap > max(timedelta(0), period_timedelta * 2) \
and fields.Date.from_string(
next_maintenance_todo.request_date) > today_date:
and next_maintenance_todo.request_date > today_date:
# If the new date still in the past, we set it for today
if fields.Date.from_string(
last_maintenance_done.close_date) + \
if last_maintenance_done.close_date + \
period_timedelta < today_date:
next_date = date_now
next_date = today_date
else:
next_date = fields.Date.to_string(
fields.Date.from_string(
last_maintenance_done.close_date) +
period_timedelta)
next_date = last_maintenance_done.close_date + \
period_timedelta
elif next_maintenance_todo:
next_date = next_maintenance_todo.request_date
date_gap = fields.Date.from_string(
next_maintenance_todo.request_date) - today_date
date_gap = next_maintenance_todo.request_date - today_date
# If next maintenance to do is in the future, and in more than
# 2 times the period, we insert an new request
# We use 2 times the period to avoid creation too closed
# request from a manually one created
if date_gap > timedelta(0) and date_gap > period_timedelta * 2:
next_date = fields.Date.to_string(
today_date + period_timedelta)
next_date = today_date + period_timedelta
elif last_maintenance_done:
next_date = fields.Date.from_string(
last_maintenance_done.close_date) + period_timedelta
next_date = last_maintenance_done.close_date + period_timedelta
# If when we add the period to the last maintenance done and
# we still in past, we plan it for today
if next_date < today_date:
next_date = date_now
next_date = today_date
else:
next_date = fields.Date.to_string(
today_date + period_timedelta)
next_date = today_date + period_timedelta

plan.next_maintenance_date = next_date

Expand Down Expand Up @@ -156,9 +144,9 @@ def _compute_team_required(self):
equipment.maintenance_plan_ids) >= 1

def _prepare_request_from_plan(self, maintenance_plan):
team = self.maintenance_team_id
if not team:
team = self.env['maintenance.request']._get_default_team_id()
team_id = self.maintenance_team_id.id
if not team_id:
team_id = self.env['maintenance.request']._get_default_team_id()
return {
'name': _('Preventive Maintenance (%s) - %s') % (
maintenance_plan.maintenance_kind_id.name, self.name),
Expand All @@ -168,8 +156,8 @@ def _prepare_request_from_plan(self, maintenance_plan):
'equipment_id': self.id,
'maintenance_type': 'preventive',
'owner_user_id': self.owner_user_id.id or self.env.user.id,
'technician_user_id': self.technician_user_id.id,
'maintenance_team_id': team.id,
'user_id': self.technician_user_id.id,
'maintenance_team_id': team_id,
'maintenance_kind_id': maintenance_plan.maintenance_kind_id.id,
'duration': maintenance_plan.duration,
}
Expand Down
29 changes: 19 additions & 10 deletions maintenance_plan/tests/test_maintenance_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,38 @@ class TestMaintenancePlan(test_common.TransactionCase):
def setUp(self):
super().setUp()
self.printer1 = self.env.ref('maintenance.equipment_printer1')
self.monitor1 = self.env.ref('maintenance.equipment_monitor1')
self.cron = self.env.ref('maintenance.maintenance_requests_cron')

self.kind_quarterly = self.env['maintenance.kind'].create({
'name': 'Quarterly',
'active': True,
})
self.monitor1.maintenance_plan_ids = [(0, 0, {
'maintenance_kind_id': self.kind_quarterly.id,
'period': 90,
'duration': 3
})]

def test_next_maintenance_date(self):

today = fields.Date.today()
today_date = fields.Date.from_string(today)

for plan in self.printer1.maintenance_plan_ids:
plan_ids = self.printer1.maintenance_plan_ids + \
self.monitor1.maintenance_plan_ids
for plan in plan_ids:
self.assertEqual(plan.next_maintenance_date,
fields.Date.to_string(
today_date + timedelta(days=plan.period)))
today + timedelta(days=plan.period))

def test_generate_requests(self):

self.cron.method_direct_trigger()

generated_requests = self.env['maintenance.request'].search(
['|',
('maintenance_kind_id', '=', self.env.ref(
'maintenance_plan.maintenance_kind_monthly').id),
('maintenance_kind_id', '=', self.env.ref(
'maintenance_plan.maintenance_kind_weekly').id)
])
[('maintenance_kind_id', 'in', [
self.env.ref('maintenance_plan.maintenance_kind_monthly').id,
self.env.ref('maintenance_plan.maintenance_kind_weekly').id,
self.kind_quarterly.id])])

for req in generated_requests:
for plan in req.equipment_id.maintenance_plan_ids:
Expand Down
18 changes: 9 additions & 9 deletions maintenance_plan/views/maintenance.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="name">maintenance.kind.tree</field>
<field name="model">maintenance.kind</field>
<field name="arch" type="xml">
<tree string="Maintenance Kinds" editable="top">
<tree editable="top">
<field name="name"/>
<field name="active"/>
</tree>
Expand All @@ -15,15 +15,14 @@

<!-- maintenance.kind : actions -->
<record id="maintenance_kind_action" model="ir.actions.act_window">
<field name="name">Maintenance kinds</field>
<field name="name">Maintenance Kinds</field>
<field name="res_model">maintenance.kind</field>
<field name="view_mode">tree</field>
<field name="view_id" ref="maintenance_kind_view_tree"/>
</record>

<menuitem
id="menu_maintenance_kind_configuration"
name="Maintenance kinds"
parent="maintenance.menu_maintenance_configuration"
action="maintenance_kind_action"
sequence="4" />
Expand All @@ -34,7 +33,7 @@
<field name="name">maintenance.plan.form</field>
<field name="model">maintenance.plan</field>
<field name="arch" type="xml">
<form string="Maintenance Plan">
<form>
<group>
<field name="maintenance_kind_id"/>
<field name="period"/>
Expand All @@ -49,7 +48,7 @@
<field name="name">maintenance.plan.tree</field>
<field name="model">maintenance.plan</field>
<field name="arch" type="xml">
<tree string="Maintenance Plans">
<tree>
<field name="maintenance_kind_id"/>
<field name="period"/>
<field name="duration"/>
Expand All @@ -60,12 +59,14 @@

<!-- equipment : views inheritance -->
<record id="hr_equipment_view_form" model="ir.ui.view">
<field name="name">equipment.form.inherit</field>
<field name="inherit_id" ref="maintenance.hr_equipment_view_form" />
<field name="model">maintenance.equipment</field>
<field name="arch" type="xml">
<xpath expr="//group[@name='maintenance']" position="replace">
<group name="maintenance">
<xpath expr="//group[@name='maintenance']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//group[@name='maintenance']" position="after">
<group name="maintenance_plans">
<field name="maintenance_plan_ids" />
<field name="maintenance_team_required" invisible="1"/>
</group>
Expand All @@ -78,7 +79,6 @@

<!-- request : views inheritance -->
<record id="hr_equipment_request_view_form" model="ir.ui.view">
<field name="name">equipment.request.form.inherit</field>
<field name="model">maintenance.request</field>
<field name="inherit_id" ref="maintenance.hr_equipment_request_view_form" />
<field name="arch" type="xml">
Expand Down

0 comments on commit bf0465d

Please sign in to comment.