[REF] stock_calendar: remove overrides of resource methods

As standard code of resource module now includes attendances and leaves
in computed intervals, there is no need to keep hackish overrides of some
resource methods. Everything should work from base module.
This commit is contained in:
Thibault Delavallée
2017-02-27 14:04:48 +01:00
parent 9944209ef7
commit 583b9d0088
2 changed files with 9 additions and 122 deletions
+1 -107
View File
@@ -2,7 +2,7 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
from odoo import fields, models
class ResourceCalendarAttendance(models.Model):
@@ -15,109 +15,3 @@ class ResourceCalendarLeaves(models.Model):
_inherit = "resource.calendar.leaves"
group_id = fields.Many2one('procurement.group', string="Procurement Group")
class ResourceCalendar(models.Model):
_inherit = "resource.calendar"
# Keep as it takes into account times
@api.multi
def _get_leave_intervals(self, resource_id=None,
start_datetime=None, end_datetime=None):
"""Get the leaves of the calendar. Leaves can be filtered on the resource,
the start datetime or the end datetime.
:param int resource_id: the id of the resource to take into account when
computing the leaves. If not set, only general
leaves are computed. If set, generic and
specific leaves are computed.
:param datetime start_datetime: if provided, do not take into account leaves
ending before this date.
:param datetime end_datetime: if provided, do not take into account leaves
beginning after this date.
:return list leaves: list of tuples (start_datetime, end_datetime) of
leave intervals
"""
# TDE FIXME: waw, overriding completely a method + changing its result
# and its behavior, very nice. Please FIXME.
self.ensure_one()
leaves = []
for leave in self.leave_ids:
if leave.resource_id and not resource_id == leave.resource_id.id:
continue
date_from = fields.Datetime.from_string(leave.date_from)
if end_datetime and date_from > end_datetime:
continue
date_to = fields.Datetime.from_string(leave.date_to)
if start_datetime and date_to < start_datetime:
continue
leaves.append((date_from, date_to, leave.group_id.id))
return leaves
# --------------------------------------------------
# Utility methods
# --------------------------------------------------
@api.model
def _interval_remove_leaves(self, interval, leave_intervals):
""" Utility method that remove leave intervals from a base interval:
- clean the leave intervals, to have an ordered list of not-overlapping
intervals
- initiate the current interval to be the base interval
- for each leave interval:
- finishing before the current interval: skip, go to next
- beginning after the current interval: skip and get out of the loop
because we are outside range (leaves are ordered)
- beginning within the current interval: close the current interval
and begin a new current interval that begins at the end of the leave
interval
- ending within the current interval: update the current interval begin
to match the leave interval ending
- take into account the procurement group when needed
:param tuple interval: a tuple (beginning datetime, ending datetime) that
is the base interval from which the leave intervals
will be removed
:param list leave_intervals: a list of tuples (beginning datetime, ending datetime)
that are intervals to remove from the base interval
:return list intervals: a list of tuples (begin datetime, end datetime)
that are the remaining valid intervals """
# TDE FIXME: waw, overriding completely a method + changing its result
# and its behavior, very nice. Please FIXME.
if not interval:
return interval
if leave_intervals is None:
leave_intervals = []
intervals = []
#leave_intervals = self._interval_merge(leave_intervals) NOT NECESSARY TO CLEAN HERE AS IT WOULD REMOVE GROUP INFO
current_interval = list(interval)
for leave in leave_intervals:
if len(leave) > 2:
current_group = False
Att = self.env["resource.calendar.attendance"]
if leave[2]:
if len(current_interval) > 2:
current_group = current_interval[2] and Att.browse(current_interval[2]).group_id.id or False
if leave[2] != current_group:
continue
if leave[1] <= current_interval[0]:
continue
if leave[0] >= current_interval[1]:
break
if current_interval[0] < leave[0] < current_interval[1]:
current_interval[1] = leave[0]
intervals.append((current_interval[0], current_interval[1]))
current_interval = [leave[1], interval[1]]
# if current_interval[0] <= leave[1] <= current_interval[1]:
if current_interval[0] <= leave[1]:
current_interval[0] = leave[1]
if current_interval and current_interval[0] < interval[1]: # remove intervals moved outside base interval due to leaves
if len(interval) > 2:
intervals.append((current_interval[0], current_interval[1], interval[2]))
else:
intervals.append((current_interval[0], current_interval[1],))
return intervals
+8 -15
View File
@@ -32,10 +32,7 @@ class StockWarehouseOrderpoint(models.Model):
If a purchase calendar is defined, it should give the
:return [(date, group)]
"""
# TDE FIXME: unused context key 'no_round_hours' removed
# Check if orderpoint has last execution date and calculate if we need to calculate again already
Calendar = self.env["resource.calendar"]
Attendance = self.env["resource.calendar.attendance"]
group = False
date = False
now_date = datetime.utcnow()
@@ -45,13 +42,12 @@ class StockWarehouseOrderpoint(models.Model):
new_date = datetime.strptime(self.last_execution_date, DEFAULT_SERVER_DATETIME_FORMAT)
else:
new_date = datetime.utcnow()
# TDE note: I bet accessing interval[2] will crash, no ? this code seems very louche
intervals = Calendar._schedule_days(self.purchase_calendar_id.id, 1, new_date, compute_leaves=True)
intervals = self.purchase_calendar_id._schedule_days(1, new_date, compute_leaves=True)
for interval in intervals:
# If last execution date, interval should start after it in order not to execute the same orderpoint twice
# TODO: Make the interval a little bigger
if (self.last_execution_date and (interval[0] > new_date and interval[0] < now_date)) or (not self.last_execution_date and interval[0] < now_date and interval[1] > now_date):
group = Attendance.browse(interval[2]).group_id.id
group = interval[2] and interval[2]['attendances'].group_id.id
date = interval[1]
res_intervals += [(date, group), ]
else:
@@ -62,15 +58,13 @@ class StockWarehouseOrderpoint(models.Model):
"""
Date should be given in utc
"""
# TDE FIXME: unused context key 'no_round_hours' removed
Attendance = self.env["resource.calendar.attendance"]
# First check if the orderpoint has a Calendar as it should be delivered at this calendar date
purchase_date = False
delivery_date = start_date
if self.calendar_id and self.calendar_id.attendance_ids:
res = self.calendar_id._schedule_days(-1, start_date, compute_leaves=True)
if res and res[0][0] < start_date:
group_to_find = res[0][2] and Attendance.browse(res[0][2]).group_id.id
group_to_find = res[0][2] and res[0][2]['attendances'].group_id.id
delivery_date = res[0][0]
found_date = delivery_date
if self.purchase_calendar_id and self.purchase_calendar_id.attendance_ids:
@@ -78,7 +72,7 @@ class StockWarehouseOrderpoint(models.Model):
found_date = found_date + relativedelta(days=-1) # won't allow to deliver within the day
res = self.purchase_calendar_id._schedule_days(-1, found_date, compute_leaves=True)
for re in res:
group = re[2] and Attendance.browse(re[2]).group_id.id
group = re[2] and re[2]['attendances'].group_id.id
found_date = re[0]
if not purchase_date and (group_to_find and group_to_find == group or (not group_to_find)):
purchase_date = re[0]
@@ -87,15 +81,14 @@ class StockWarehouseOrderpoint(models.Model):
return purchase_date, delivery_date
def _get_next_dates(self, new_date=False, group=False):
# TDE FIXME: unused context key 'no_round_hours' removed
Attendance = self.env["resource.calendar.attendance"]
if not new_date:
new_date = datetime.utcnow()
now_date = datetime.utcnow()
# Search first calendar day (without group)
res = self.calendar_id._schedule_days(1, new_date, compute_leaves=True)
att_group = res and res[0][2] and Attendance.browse(res[0][2]).group_id.id
att_group = False
att_group = res and res[0][2] and res[0][2]['attendances'].group_id.id
# If hours are smaller than the current date, search a day further
if res and res[0][0] < now_date:
new_date = res[0][1] + relativedelta(days=1)
@@ -103,7 +96,7 @@ class StockWarehouseOrderpoint(models.Model):
for re in res:
att_group = False
if re[2]:
att_group = Attendance.browse(re[2]).group_id.id
att_group = re[2]['attendances'].group_id.id
if att_group == group:
break
@@ -116,7 +109,7 @@ class StockWarehouseOrderpoint(models.Model):
att_group = False
for re in res:
if re[2]:
att_group = Attendance.browse(re[2]).group_id.id
att_group = re[2]['attendances'].group_id.id
if att_group == group:
break
# number as safety pall for endless loops