odoo 11 / Python 3:如果排除某些工作日,如何查找订阅的到期日

odoo 11 / Python 3: How to find expiry date of subscription if some weekdays are excluded

我正在研究订阅管理系统。我在计算订阅到期日期时遇到问题。场景如下, 如果一个人订阅 1 个月,比如 1/11/2018 到 30/11/2018 [DD/MM/YYYY] 总共 30 天,但他想从每周 30 天中排除周五和周六。那么我应该如何计算到期日呢?

Logic is : Say End Date = Expiry Date then find Fri/Sat from 1/11/2018 to 30/11/2018 which comes out 5 Fri & 4 Sat = 9 days. Add to Expiry Date which will be 09/12/2018. Now search Fur & Sat between End Date & Expiry Date which comes out 1 Fri & 2 Sat = 3 days. Now End Date = Expiry Date and Expiry Date + 3 Days = 12/12/2018. Search between End Date & Expiry Date for Fri & Sat which is 0 so the Expiry Date is 12/12/2018 return value <<

以下代码执行此操作,但方法 returns 09/12/2018 而不是 12/12/2018。这有什么问题吗??

@api.one
def get_expiry_date(self, start, end, day_selection):
    print("i am in 2nd", start, end, day_selection)
    dayformat = '%Y-%m-%d'
    current_date = datetime.now().date()
    if start:
        start = datetime.strptime(str(start), dayformat).date()
    if end:    
        end = datetime.strptime(str(end), dayformat).date()        
    if day_selection:
        selected_days = day_selection        
    if start < current_date:
        start = datetime.strptime(str(start), dayformat).date()
    weekdays = self.weekday_count(start,end)
    print("days for start and end date",start,end, day_selection) 
    adddays = 0
    if weekdays:
        for i in range(len(day_selection)):                          
            for item in weekdays[0]:
                weekdays_dict = item
                print("dict", type(weekdays), type(weekdays[0]), weekdays_dict)
                print("compare", selected_days[i], weekdays_dict, selected_days[i] == weekdays_dict)
                if selected_days[i] == item:
                    adddays = adddays + weekdays[0].get(item)
        new_start = end
        end = datetime.strptime(str(end), dayformat).date() + timedelta(days=adddays)
        start = new_start
        print("New Expiry Date", start, end, adddays)
        if adddays > 0:               
            self.get_expiry_date(start, end, day_selection)
            print("type of end is ", type(end))
            print("selected days are", selected_days[i],weekdays[0], weekdays[0].get(item), adddays)  
            print("last returned values is",end)

return 结束

在您的问题中,没有 day_selectionweekday_count 的定义,因此很难看出发生了什么。问题可能出在递归上,这不是必需的。

如果 day_selection 定义为选择排除的天数([=15= 的列表],如 ['Mon', 'Tue']),则:

from datetime import datetime, timedelta

def get_expiry_date(start, end, day_selection):
    dayformat = '%Y-%m-%d'
    weekdays= {}
    start = datetime.strptime(str(start), dayformat).date()
    end = datetime.strptime(str(end), dayformat).date()
    # Count weekdays
    for i in range((end - start).days+1):
        weekday = (start + timedelta(i)).strftime('%a')
        weekdays[weekday] = 1 + weekdays.setdefault(weekday, 0)
    # Count subscription days to add
    sub_days_to_add = 0
    for selected in day_selection:
        sub_days_to_add += weekdays.setdefault(selected, 0)
    # Count calender days to add
    cal_days_extension = 0
    while sub_days_to_add > 0:
        if (end + timedelta(days=cal_days_extension + 1)).strftime('%a') not in day_selection:
            sub_days_to_add -= 1
        cal_days_extension += 1
    # Add to end day
    return end + timedelta(days=cal_days_extension)

测试:

print (get_expiry_date('2018-11-01', '2018-11-30', ['Fri', 'Sat']))
# --->  2018-12-12
print (get_expiry_date('2018-11-01', '2018-11-30', []))
# --->  2018-11-30

而且使用 from odoo.tools import DEFAULT_SERVER_DATE_FORMATdayformat = '%Y-%m-%d' 更安全。而对于 startend 这样的参数,如果它们是必填的,可以在 Odoo 中将这些字段设置为必填。

你想做的逻辑很难

据我了解你是运行一个订阅系统 基于天数而不是日期所以为了计算 结束日期你只需要这个:

  1. 开始日期(例如:01/11/2018)
  2. 天数(例如:30 天)
  3. 排除天数(例如:周五、周六)
# day are like this
#     Fri
#     Sat
#     Sun
#     Mon
#     Tue
#     Wed
#     Thu

# start day
def get_expiry_date(start_date, number_of_days, excluded_days = None):
    """ compute end date of subcription period """
    if excluded_days is None:
        excluded_days = []
    # check params
    start_date = str(start_date)
    if number_of_days < 1:
        raise exception.UserError(_('Number of days should be > 0!!'))  # import the translate "_"  method 
    if len(excluded_days) > 5:
        raise exception.UserError(_('To much excluded days!!'))  # import the translate "_"  method 

    date_format = '%Y-%m-%d'
    end_date = datetime.strptime(start_date, date_format)

    # compute end date
    # keeping adding one day until you finish your days
    add_one_day = timedelta(days=1)
    while number_of_days > 1:
        end_date += add_one_day
        if end_date.strftime('%a') not in excluded_days:
            # day is not excluded compute it
            number_of_days += -1

    return end_date.strftime(date_format)

这是一个您可以检查的测试值:

        print get_expiry_date('2018-11-01', 30, ['Fri', 'Sat'])  # 2018-12-12
        print get_expiry_date('2018-11-01', 30, ['Fri']) # 2018-12-05
        print get_expiry_date('2018-11-01', 30, []) # 2018-11-30
        print get_expiry_date('2018-11-01', 90, []) # 2019-01-29
        print get_expiry_date('2018-11-01', 30, ['Mon', 'Thu']) # 2018-12-11

如果您已经准备好一个 运行 系统,您可以使用这个:

def get_expiry_date(start_date, end_date, excluded_days = None):
    if excluded_days is None:
        excluded_days = []
    start_date = str(start_date)
    end_date = str(end_date)
    date_format = '%Y-%m-%d'
    # compute number of days
    number_of_days = (datetime.strptime(end_date, date_format) - datetime.strptime(start_date, date_format)).days
    expiry_date = datetime.strptime(start_date, date_format)

    if len(excluded_days) > 5:
        raise Exception('To much excluded days!!')

    # keeping adding one day until you finish your days
    one_day = timedelta(days=1)
    while number_of_days > 0:
        expiry_date += one_day
        # if day is not excluded reduce the number of left days
        if expiry_date.strftime('%a') not in excluded_days:
            number_of_days += -1

    return expiry_date.strftime(date_format)

print get_expiry_date('2018-11-01', '2018-11-30', ['Fri', 'Sat'])  # 2018-12-12