Reputation: 343
I'm attempting to convert hr_attendance records to payroll, however these datetimes need to be timezone sensitive to properly pay our employees. Is there a way to do this within the search domain?
Here is my current code
in hr.attendance
@api.onchange('check_in', 'check_out')
@api.constrains('check_in', 'check_out')
def _tz_check_in(self):
for attendance in self:
if attendance.location_id:
tz = pytz.timezone(attendance.location_id.location_timezone)
attendance.tz_check_in = pytz.timezone('UTC').localize(datetime.strptime(attendance.check_in, DEFAULT_SERVER_DATETIME_FORMAT)).astimezone(tz)
tz_check_in = fields.Datetime(string='check in w/ tz', compute=_tz_check_in, store=True)
and in hr.payslip
pay_period_attendances = self.env['hr.attendance'].search([
('employee_id', '=', employee.id),
('tz_check_in', '>=', pay_period['date_from'].strftime(DEFAULT_SERVER_DATETIME_FORMAT)),
('tz_check_in', '<=', pay_period['date_to'].strftime(DEFAULT_SERVER_DATETIME_FORMAT)),
('payslip_id', '=', False)])
however, the timezone sensitive date is not being stored.
Upvotes: 1
Views: 1083
Reputation: 343
To do this I created the following method
@api.constrains('check_in')
def _tz_check_in(self):
for attendance in self:
if attendance.location_id:
tz = pytz.timezone(attendance.location_id.location_timezone)
checkin = pytz.timezone('UTC').localize(datetime.strptime(attendance.check_in, DEFAULT_SERVER_DATETIME_FORMAT)).astimezone(tz).strftime(DEFAULT_SERVER_DATETIME_FORMAT)
if not attendance.tz_check_in or attendance.tz_check_in != checkin:
attendance.write({'tz_check_in': datetime.strptime(checkin, DEFAULT_SERVER_DATETIME_FORMAT)})
Which takes the check_in and converts it into a timezone sensitive string, then convert that string into a datetime object and enter that into the database. I am only using this field (tz_check_in) for reference purposes and all front end dates are the original dates.
Upvotes: 1