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 8, 2019
1 parent 1f379df commit 5a06718
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 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
40 changes: 15 additions & 25 deletions maintenance_plan/models/maintenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,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 +67,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,7 +146,7 @@ def _compute_team_required(self):
equipment.maintenance_plan_ids) >= 1

def _prepare_request_from_plan(self, maintenance_plan):
team = self.maintenance_team_id
team = self.maintenance_team_id.id
if not team:
team = self.env['maintenance.request']._get_default_team_id()
return {
Expand All @@ -169,7 +159,7 @@ def _prepare_request_from_plan(self, maintenance_plan):
'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,
'maintenance_team_id': team,
'maintenance_kind_id': maintenance_plan.maintenance_kind_id.id,
'duration': maintenance_plan.duration,
}
Expand Down
4 changes: 1 addition & 3 deletions maintenance_plan/tests/test_maintenance_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ def setUp(self):
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:
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):

Expand Down

0 comments on commit 5a06718

Please sign in to comment.