我需要计算hr_holidays类中任何给定两天之间的天数,并且需要忽略来自该计算的hr_company_holidays类的下列天数。 就像在 hr_holiday 类中一样,这个方法要求完成计算....
def _get_number_of_days(self, date_from, date_to):
diffdays = end_date-startdate+hr_comapny_holidays.dates
return diffdays
怎么做?我需要知道上面函数的其他行
"的 hr_company_holidays "
class hr_company_holidays(osv.osv):
_name = "hr.company.holidays"
_description = "hr.company.holidays"
_columns = {
'name': fields.char('Holiday', size=256, required=True),
'date': fields.date('Day', size=256, required=True),
}
答案 0 :(得分:1)
导入datetime
模块。将日期转换为日期对象,然后减去它们。
from datetime import datetime
from openerp import tools
start_date = datetime.strptime("1995-03-13", tools.DEFAULT_SERVER_DATE_FORMAT)
end_date = datetime.strptime("1995-03-19", tools.DEFAULT_SERVER_DATE_FORMAT)
delta = d1 - d0
print(delta.days)
OpenERP / Odoo中的日期字段采用字符串格式YYYY-MM-DD
。您的方法可以接受日期对象或字符串(您应该使用strptime()
方法转换为日期对象)并在方法内部执行类似的操作。最后,由于您有一些要忽略的日期,只需测试是否hr_holiday_start_date < hr_company_holiday_date < hr_holiday_end_date
,计算通过此日期的日期并将其减去差异。
<强>更新强>
您的方法应该在新API中看起来像这样:
@api.one
def _get_number_of_days(self, start_date, end_date):
# start_date and end_date are date strings in YYYY-MM-DD format
# First, get the hr_company_holidays models
hr_company_holidays_model = self.env['hr_company_holidays']
# Then, get how many holidays that are within start_date and end_date
holiday_count = hr_company_holidays.search_count([('date', '>', start_date), ('date', '<', end_date)])
# Convert start_date and end_date to date objects
start_date_obj = datetime.strptime(start_date, tools.DEFAULT_SERVER_DATE_FORMAT)
end_date_obj = datetime.strptime(end_date, tools.DEFAULT_SERVER_DATE_FORMAT)
# Get the delta object
delta = end_date_obj - start_date_obj
# Lastly, get number of days, ignoring holidays
total_days = delta.days - holiday_count
# It's up to you what to do next with this total_days
希望这有帮助!