基于日期和时间(浮点)字段创建的插槽

时间:2017-07-21 07:26:00

标签: python openerp odoo-9

enter image description here我有两个日期字段。

from_date 和 的 TO_DATE 在One2many订单项中,有三个浮点字段

from_time to_time 间隔

必须根据上述参数创建插槽。

示例:

from_date = '2017-07-21'
to_date = '2017-07-21'

持续时间是一天。 One2many订单项的值为

from_time = 9.0
to_time = 10.0
interval = 30(in minutes)

输出应该生成两个插槽

1. '2017-07-21 09:00:00' '2017-07-21 09:30:00'
2. '2017-07-21 09:30:00' '2017-07-21 10:00:00'

它应该生成两个订单项。

如果持续时间是一周。 它应该生成2 * 7 = 14个插槽。

我使用了生成一天的代码。

@api.one
def generate(self):
    cr = self.env.cr
    uid = self.env.uid
    context = self.env.context
    event = self.pool.get('calendar.event')
    slot = self.pool.get('slot.booking')
    old_data_id = slot.search(cr, uid, [('slot_id', '=',self.id)], context=context)
    slot.unlink(cr, uid ,old_data_id)
    for each in self.shift_line:
        if each.interval > 60 or each.interval == 0:
            raise osv.except_osv(_('Attention!'), _('Please enter interval timings in minutes range like (10-60) '))
        interval = each.interval
        fmt = "%Y-%m-%d"
        start_date = datetime.strptime(self.from_date, fmt)
        end_date = datetime.strptime(self.to_date, fmt)
        days = []
        date = start_date
        pdb.set_trace()
        str_start_time = '%s %s' % (self.from_date,'{0:02.0f}:{1:02.0f}'.format(*divmod(each.from_time * 60, 60)))+':00'
        str_end_time = '%s %s' % (self.from_date,'{0:02.0f}:{1:02.0f}'.format(*divmod(each.to_time * 60, 60)))+':00'
        time = datetime.strptime(str_start_time, '%Y-%m-%d %H:%M:%S')
        end = datetime.strptime(str_end_time, '%Y-%m-%d %H:%M:%S')
        while date <= end_date:
            hours = []
            while time <= end:
                hours.append(time.strftime("%Y-%m-%d %H:%M:%S"))
                time += timedelta(minutes=interval)
            date += timedelta(days=1)
            time += timedelta(days=1)
            end += timedelta(days=1)
            days.append(hours)
        print "\n\n\n\n\nn\+++++++++++++++++++++days",days
        for hours in days[0][:-1]:
            val = datetime.strptime(hours, '%Y-%m-%d %H:%M:%S')
            val = val + timedelta(minutes=interval)
            values = {
                'name' : 'Slot for ' + self.employee_id.name,
                'start_datetime' : hours,
                'stop_datetime' : str(val),
                'slot_id' : self.id,
                'shift_lines_id' : each.id,
                'partner_id': self.employee_id.id,
                'duration' : each.interval,
                }
            print "\n\n\n\n\n\n\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++values",values
            slot.create(cr, uid, values, context=context)

感谢多天的任何帮助。

1 个答案:

答案 0 :(得分:0)

ist_timedelta = timedelta(seconds=((time in seconds)-10800))

添加这个将给出确切的答案,这里10800是因为UTC与KSA的3小时差异。