def test_calendar_timezone(self): # user in timezone UTC-9 asks for work hours # Limits: between 2013-02-19 10:00:00 and 2013-02-26 15:30:00 (User TZ) # between 2013-02-19 19:00:00 and 2013-02-27 00:30:00 (UTC) # Leaves: between 2013-02-21 10:00:00 and 2013-02-26 12:00:00 (User TZ) # res: 19/02 (10-16 (beginning)) + 22/02 (0 (leave)) + 26/02 (12-15.30 (leave+ending)) self.env.user.tz = 'US/Alaska' (self.leave1 | self.leave2 | self.leave3).unlink() leave = self.env['resource.calendar.leaves'].create({ 'name': 'Timezoned Leaves', 'calendar_id': self.calendar.id, 'resource_id': self.resource1_id, 'date_from': to_naive_utc(Datetime.from_string('2013-02-21 10:00:00'), self.env.user), 'date_to': to_naive_utc(Datetime.from_string('2013-02-26 12:00:00'), self.env.user) }) res = self.calendar.get_work_hours_count( to_naive_utc(Datetime.from_string('2013-02-19 10:00:00'), self.env.user), to_naive_utc(Datetime.from_string('2013-02-26 15:30:00'), self.env.user), self.resource1_id, compute_leaves=True) self.assertEqual(res, 9.5)
def _interval_dates(self, frequency, company): """ Method used to compute the theoretical date from which account move lines should be fetched @param {string} frequency: a valid value of the selection field on the object (daily, monthly, annually) frequencies are literal (daily means 24 hours and so on) @param {recordset} company: the company for which the closing is done @return {dict} the theoretical date from which account move lines are fetched. date_stop date to which the move lines are fetched, always now() the dates are in their GECOERP Database string representation """ date_stop = datetime.utcnow() interval_from = None name_interval = '' if frequency == 'daily': interval_from = date_stop - timedelta(days=1) name_interval = _('Daily Closing') elif frequency == 'monthly': month_target = date_stop.month > 1 and date_stop.month - 1 or 12 year_target = month_target < 12 and date_stop.year or date_stop.year - 1 interval_from = date_stop.replace(year=year_target, month=month_target) name_interval = _('Monthly Closing') elif frequency == 'annually': year_target = date_stop.year - 1 interval_from = date_stop.replace(year=year_target) name_interval = _('Annual Closing') return { 'interval_from': FieldDateTime.to_string(interval_from), 'date_stop': FieldDateTime.to_string(date_stop), 'name_interval': name_interval }
def test_calendar_working_hours_of_date(self): # Test: day1, beginning at 10:30 -> work from 10:30 (arrival) until 16:00 wh = self.calendar.get_work_hours_count( Datetime.from_string('2013-02-19 10:30:00'), Datetime.from_string('2013-02-19 18:00:00'), self.resource1_id, compute_leaves=False) self.assertEqual(wh, 5.5)
def test_calendar_days_scheduling_timezone(self): self.env.user.tz = 'US/Alaska' res = self.calendar.plan_days( 5, to_naive_utc(Datetime.from_string('2013-02-12 09:08:07'), self.env.user)) self.assertEqual( to_naive_user_tz(res, self.env.user).date(), Datetime.from_string('2013-02-26 00:00:00').date())
def test_calendar_working_hours_leaves(self): # new API: resource and leaves # res: 2 weeks -> 40 hours - (3+4) leave hours res = self.calendar.get_work_hours_count( Datetime.from_string('2013-02-12 06:00:00'), Datetime.from_string('2013-02-22 23:00:00'), self.resource1_id, compute_leaves=True) self.assertEqual(res, 33.0)
def test_calendar_working_hours(self): # new API: resource without leaves # res: 2 weeks -> 40 hours res = self.calendar.get_work_hours_count( Datetime.from_string('2013-02-12 06:00:00'), Datetime.from_string('2013-02-22 23:00:00'), self.resource1_id, compute_leaves=False) self.assertEqual(res, 40.0)
def test_calendar_working_hours_count(self): calendar = self.env.ref('resource.resource_calendar_std_35h') res = calendar.get_work_hours_count( Datetime.from_string( '2017-05-03 14:03:00'), # Wednesday (8:00-12:00, 13:00-16:00) Datetime.from_string( '2017-05-04 11:03:00'), # Thursday (8:00-12:00, 13:00-16:00) resource_id=None, compute_leaves=False) self.assertEqual(res, 5.0)
def test_interval_and(self): self.assertEqual( self.env['resource.calendar']._interval_and( self.intervals[0], self.intervals[1]), self.calendar._interval_new( Datetime.from_string('2013-02-04 09:00:00'), Datetime.from_string('2013-02-04 11:00:00'))) self.assertEqual( self.env['resource.calendar']._interval_and( self.intervals[2], self.intervals[3]), None)
def test_interval_remove(self): working_interval = self.calendar._interval_new( Datetime.from_string('2013-02-04 08:00:00'), Datetime.from_string('2013-02-04 18:00:00')) result = self.env['resource.calendar']._interval_remove_leaves( working_interval, self.intervals) self.assertEqual(len(result), 1) # First interval: 04, 14-17 self.assertEqual(result[0][:2], (Datetime.from_string('2013-02-04 14:00:00'), Datetime.from_string('2013-02-04 17:00:00')))
def test_calendar_hours_scheduling_timezone(self): # user in timezone UTC-9 asks for work hours self.env.user.tz = 'US/Alaska' res = self.calendar.plan_hours( 42, to_naive_utc(Datetime.from_string('2013-02-12 09:25:00'), self.env.user)) self.assertEqual( res, to_naive_utc(Datetime.from_string('2013-02-26 11:25:00'), self.env.user))
def test_calendar_working_day_intervals_limited_attendances(self): """ Test attendances limited in time. """ attendance = self.env['resource.calendar.attendance'].search([ ('name', '=', 'Att3') ]) attendance.write({ 'date_from': self.date2 + relativedelta(days=7), 'date_to': False, }) intervals = self.calendar._get_day_work_intervals( self.date2.date(), start_time=self.date2.time()) self.assertEqual(len(intervals), 1) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))) attendance.write({ 'date_from': False, 'date_to': self.date2 - relativedelta(days=7), }) intervals = self.calendar._get_day_work_intervals( self.date2.date(), start_time=self.date2.time()) self.assertEqual(len(intervals), 1) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))) attendance.write({ 'date_from': self.date2 + relativedelta(days=7), 'date_to': self.date2 - relativedelta(days=7), }) intervals = self.calendar._get_day_work_intervals( self.date2.date(), start_time=self.date2.time()) self.assertEqual(len(intervals), 1) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))) attendance.write({ 'date_from': self.date2, 'date_to': self.date2, }) intervals = self.calendar._get_day_work_intervals( self.date2.date(), start_time=self.date2.time()) self.assertEqual(len(intervals), 2) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))) self.assertEqual(intervals[1][:2], (Datetime.from_string('2013-02-15 16:00:00'), Datetime.from_string('2013-02-15 23:00:00')))
def test_calendar_working_day_intervals_no_leaves(self): # Test: day0 without leaves: 1 interval intervals = self.calendar._get_day_work_intervals( Date.from_string('2013-02-12'), start_time=time(9, 8, 7)) self.assertEqual(len(intervals), 1) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-12 09:08:07'), Datetime.from_string('2013-02-12 16:00:00'))) self.assertEqual(intervals[0][2]['attendances'], self.att_1) # Test: day1, beginning at 10:30 -> work from 10:30 (arrival) until 16:00 intervals = self.calendar._get_day_work_intervals( Date.from_string('2013-02-19'), start_time=time(10, 30, 0)) self.assertEqual(len(intervals), 1) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-19 10:30:00'), Datetime.from_string('2013-02-19 16:00:00'))) self.assertEqual(intervals[0][2]['attendances'], self.att_1) # Test: day3 without leaves: 2 interval intervals = self.calendar._get_day_work_intervals( Date.from_string('2013-02-15'), start_time=time(10, 11, 12)) self.assertEqual(len(intervals), 2) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-15 10:11:12'), Datetime.from_string('2013-02-15 13:00:00'))) self.assertEqual(intervals[1][:2], (Datetime.from_string('2013-02-15 16:00:00'), Datetime.from_string('2013-02-15 23:00:00'))) self.assertEqual(intervals[0][2]['attendances'], self.att_2) self.assertEqual(intervals[1][2]['attendances'], self.att_3)
def test_interval_schedule_hours(self): cleaned_intervals = self.env['resource.calendar']._interval_merge( self.intervals) result = self.env['resource.calendar']._interval_schedule_hours( cleaned_intervals, 5.5) self.assertEqual(len(result), 2) # First interval: 03, 8-10 untouched self.assertEqual(result[0][:2], (Datetime.from_string('2013-02-03 08:00:00'), Datetime.from_string('2013-02-03 10:00:00'))) # First interval: 04, 08-11:30 self.assertEqual(result[1][:2], (Datetime.from_string('2013-02-04 08:00:00'), Datetime.from_string('2013-02-04 11:30:00')))
def ctx_tz(record, field): res_lang = None ctx = record._context tz_name = pytz.timezone(ctx.get('tz') or record.env.user.tz) timestamp = Datetime.from_string(record[field]) if ctx.get('lang'): res_lang = record.env['res.lang'].search([('code', '=', ctx['lang'])], limit=1) if res_lang: timestamp = pytz.utc.localize(timestamp, is_dst=False) return datetime.strftime( timestamp.astimezone(tz_name), res_lang.date_format + ' ' + res_lang.time_format) return Datetime.context_timestamp(record, timestamp)
def test_in_date_5(self): """ Receive the same lot at different times, once they're in the same location, the quants are merged and only the earliest incoming date is kept. """ stock_location = self.env.ref('stock.stock_location_stock') product1 = self.env['product.product'].create({ 'name': 'Product A', 'type': 'product', 'tracking': 'lot', }) lot1 = self.env['stock.production.lot'].create({ 'name': 'lot1', 'product_id': product1.id, }) in_date1 = datetime.now() self.env['stock.quant']._update_available_quantity(product1, stock_location, 1.0, lot_id=lot1, in_date=in_date1) quant = self.env['stock.quant'].search([ ('product_id', '=', product1.id), ('location_id', '=', stock_location.id), ]) self.assertEqual(len(quant), 1) self.assertEqual(quant.quantity, 1) self.assertEqual(quant.lot_id.id, lot1.id) from gecoerp.fields import Datetime self.assertEqual(quant.in_date, Datetime.to_string(in_date1)) in_date2 = datetime.now() - timedelta(days=5) self.env['stock.quant']._update_available_quantity(product1, stock_location, 1.0, lot_id=lot1, in_date=in_date2) quant = self.env['stock.quant'].search([ ('product_id', '=', product1.id), ('location_id', '=', stock_location.id), ]) self.assertEqual(len(quant), 1) self.assertEqual(quant.quantity, 2) self.assertEqual(quant.lot_id.id, lot1.id) self.assertEqual(quant.in_date, Datetime.to_string(in_date2))
def _check_session_timing(self): self.ensure_one() date_today = datetime.utcnow() session_start = Datetime.from_string(self.start_at) if not date_today - timedelta(hours=24) <= session_start: raise UserError( _("This session has been opened another day. To comply with the French law, you should close sessions on a daily basis. Please close session %s and open a new one." ) % self.name) return True
def test_calendar_working_hours_24(self): self.att_4 = self.env['resource.calendar.attendance'].create({ 'name': 'Att4', 'calendar_id': self.calendar.id, 'dayofweek': '2', 'hour_from': 0, 'hour_to': 24 }) res = self.calendar.get_work_hours_count( Datetime.from_string('2018-06-19 23:00:00'), Datetime.from_string('2018-06-21 01:00:00'), self.resource1_id, compute_leaves=True) self.assertAlmostEqual(res, 24.0)
def test_event_date_range(self): self.patcher = patch('gecoerp.addons.event.models.event.fields.Datetime', wraps=Datetime) self.mock_datetime = self.patcher.start() self.mock_datetime.now.return_value = Datetime.to_string(datetime.datetime(2015, 12, 31, 12, 0)) self.event_0.registration_ids.event_begin_date = datetime.datetime(2015, 12, 31, 18, 0) self.assertEqual(self.event_0.registration_ids.get_date_range_str(), u'today') self.event_0.registration_ids.event_begin_date = datetime.datetime(2016, 1, 1, 6, 0) self.assertEqual(self.event_0.registration_ids.get_date_range_str(), u'tomorrow') self.event_0.registration_ids.event_begin_date = datetime.datetime(2016, 1, 2, 6, 0) self.assertEqual(self.event_0.registration_ids.get_date_range_str(), u'in 2 days') self.mock_datetime.now.return_value = Datetime.to_string(datetime.datetime(2015, 12, 10, 12, 0)) self.event_0.registration_ids.event_begin_date = datetime.datetime(2016, 1, 25, 6, 0) self.assertEqual(self.event_0.registration_ids.get_date_range_str(), u'next month') self.patcher.stop()
def _get_car_atn(self, acquisition_date, car_value, fuel_type, co2): # Compute the correction coefficient from the age of the car now = Datetime.from_string(Datetime.now()) start = Datetime.from_string(acquisition_date) if start: number_of_month = ( now.year - start.year) * 12.0 + now.month - start.month + int( bool(now.day - start.day + 1)) if number_of_month <= 12: age_coefficient = 1.00 elif number_of_month <= 24: age_coefficient = 0.94 elif number_of_month <= 36: age_coefficient = 0.88 elif number_of_month <= 48: age_coefficient = 0.82 elif number_of_month <= 60: age_coefficient = 0.76 else: age_coefficient = 0.70 car_value = car_value * age_coefficient # Compute atn value from corrected car_value magic_coeff = 6.0 / 7.0 # Don't ask me why if fuel_type == 'electric': atn = 0.0 else: if fuel_type in ['diesel', 'hybrid']: reference = 87.0 else: reference = 105.0 if co2 <= reference: atn = car_value * max(0.04, (0.055 - 0.001 * (reference - co2))) * magic_coeff else: atn = car_value * min(0.18, (0.055 + 0.001 * (co2 - reference))) * magic_coeff return max(1280, atn) / 12.0
def test_calendar_working_day_intervals_leaves_generic(self): # Test: day0 with leaves outside range: 1 interval intervals = self.calendar._get_day_work_intervals( Date.from_string('2013-02-12'), start_time=time(7, 0, 0), compute_leaves=True) self.assertEqual(len(intervals), 1) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-12 08:00:00'), Datetime.from_string('2013-02-12 16:00:00'))) # Test: day0 with leaves: 2 intervals because of leave between 9 and 12, ending at 15:45:30 intervals = self.calendar._get_day_work_intervals( Date.from_string('2013-02-19'), start_time=time(8, 15, 0), end_time=time(15, 45, 30), compute_leaves=True) self.assertEqual(len(intervals), 2) self.assertEqual(intervals[0][:2], (Datetime.from_string('2013-02-19 08:15:00'), Datetime.from_string('2013-02-19 09:00:00'))) self.assertEqual(intervals[1][:2], (Datetime.from_string('2013-02-19 12:00:00'), Datetime.from_string('2013-02-19 15:45:30'))) self.assertEqual(intervals[0][2]['attendances'], self.att_1) self.assertEqual(intervals[0][2]['leaves'], self.leave1) self.assertEqual(intervals[1][2]['attendances'], self.att_1) self.assertEqual(intervals[0][2]['leaves'], self.leave1)
def test_work_days_count(self): # user in timezone UTC-9 asks for work hours self.env.user.tz = 'US/Alaska' res = self.test.get_work_days_count( to_naive_utc(Datetime.from_string('2013-02-12 06:00:00'), self.env.user), to_naive_utc(Datetime.from_string('2013-02-22 23:00:00'), self.env.user)) self.assertEqual(res, 3.75) # generic leaves, 3 hours res = self.test.get_work_days_count( to_naive_utc(Datetime.from_string('2013-02-12 06:00:00'), self.env.user), to_naive_utc(Datetime.from_string('2013-02-22 20:00:00'), self.env.user)) self.assertEqual(res, 3.5) # last day is truncated of 3 hours on 12) self.env['resource.calendar.leaves'].create({ 'name': 'Timezoned Leaves', 'calendar_id': self.test.resource_calendar_id.id, 'resource_id': self.test.resource_id.id, 'date_from': to_naive_utc(Datetime.from_string('2013-02-13 10:00:00'), self.env.user), 'date_to': to_naive_utc(Datetime.from_string('2013-02-17 12:00:00'), self.env.user) }) res = self.test.get_work_days_count( to_naive_utc(Datetime.from_string('2013-02-12 06:00:00'), self.env.user), to_naive_utc(Datetime.from_string('2013-02-22 20:00:00'), self.env.user)) self.assertEqual( res, 2.5 ) # one day is on leave and last day is truncated of 3 hours on 12)
def test_interval_merge(self): cleaned_intervals = self.env['resource.calendar']._interval_merge( self.intervals) self.assertEqual(len(cleaned_intervals), 3) # First interval: 03, unchanged self.assertEqual(cleaned_intervals[0][:2], (Datetime.from_string('2013-02-03 08:00:00'), Datetime.from_string('2013-02-03 10:00:00'))) # Second interval: 04, 08-14, combining 08-12 and 11-14, 09-11 being inside 08-12 self.assertEqual(cleaned_intervals[1][:2], (Datetime.from_string('2013-02-04 08:00:00'), Datetime.from_string('2013-02-04 14:00:00'))) # Third interval: 04, 17-21, 18-19 being inside 17-21 self.assertEqual(cleaned_intervals[2][:2], (Datetime.from_string('2013-02-04 17:00:00'), Datetime.from_string('2013-02-04 21:00:00')))
def test_calendar_hours_scheduling_timezone_2(self): # Call schedule_hours for a user in Autralia, Sydney (GMT+10) # Two cases: # - start at 2013-02-15 08:00:00 => 2013-02-14 21:00:00 in UTC # - start at 2013-02-15 11:00:00 => 2013-02-15 00:00:00 in UTC self.env.user.tz = 'Australia/Sydney' self.env['resource.calendar.attendance'].create({ 'name': 'Day3 - 1', 'dayofweek': '3', 'hour_from': 8, 'hour_to': 12, 'calendar_id': self.calendar.id, }) self.env['resource.calendar.attendance'].create({ 'name': 'Day3 - 2', 'dayofweek': '3', 'hour_from': 13, 'hour_to': 17, 'calendar_id': self.calendar.id, }) hours = 1.0 / 60.0 for test_date in ['2013-02-15 08:00:00', '2013-02-15 11:00:00']: start_dt = Datetime.from_string(test_date) start_dt_utc = to_naive_utc(start_dt, self.env.user) res = self.calendar._schedule_hours(hours, start_dt_utc) self.assertEqual( [(start_dt_utc, start_dt_utc.replace(minute=1))], res, 'resource_calendar: wrong schedule_hours computation')
def test_calendar_days_scheduling(self): res = self.calendar.plan_days( 5, Datetime.from_string('2013-02-12 09:08:07')) self.assertEqual(res.date(), Datetime.from_string('2013-02-26 00:00:00').date(), 'resource_calendar: wrong days scheduling') res = self.calendar.plan_days( -2, Datetime.from_string('2013-02-12 09:08:07')) self.assertEqual(res.date(), Datetime.from_string('2013-02-08 00:00:00').date(), 'resource_calendar: wrong days scheduling') res = self.calendar.plan_days( 5, Datetime.from_string('2013-02-12 09:08:07'), compute_leaves=True, resource_id=self.resource1_id) self.assertEqual(res.date(), Datetime.from_string('2013-03-01 00:00:00').date(), 'resource_calendar: wrong days scheduling')
def test_work_days_count_timezones_ultra(self): # user in timezone UTC+4 is attached to the resource and create leaves self.test.resource_id.write({ 'user_id': self.lost_user.id, }) reunion_leave = self.env['resource.calendar.leaves'].sudo( self.lost_user).create({ 'name': 'Timezoned Leaves', 'calendar_id': self.test.resource_calendar_id.id, 'resource_id': self.test.resource_id.id, 'date_from': to_naive_utc(Datetime.from_string('2013-02-12 10:00:00'), self.lost_user), 'date_to': to_naive_utc(Datetime.from_string('2013-02-12 12:00:00'), self.lost_user) }) self.assertEqual(reunion_leave.tz, 'Indian/Reunion') # user in timezone UTC-9 read and manipulate leaves self.env.user.tz = 'US/Alaska' res = self.test.get_work_days_data( to_naive_utc(Datetime.from_string('2013-02-12 06:00:00'), self.env.user), to_naive_utc(Datetime.from_string('2013-02-12 20:00:00'), self.env.user)) self.assertEqual(res['days'], 0.75) self.assertEqual(res['hours'], 6.0) # user in timezone UTC+4 read and manipulate leaves res = self.test.sudo(self.lost_user).get_work_days_data( to_naive_utc(Datetime.from_string('2013-02-12 06:00:00'), self.env.user), to_naive_utc(Datetime.from_string('2013-02-12 20:00:00'), self.env.user)) self.assertEqual(res['days'], 0.75) self.assertEqual(res['hours'], 6.0)
def test_09(self): """One day before""" date = Datetime.to_string(Datetime.from_string('2014-12-30 00:00:00')) self.mock_datetime.now.return_value = date self.assertAlmostEqual(self.line.theoritical_amount, -363)
def test_08(self): """After 308 days at noon""" date = Datetime.to_string(Datetime.from_string('2014-11-05 12:00:00')) # remember, remember self.mock_datetime.now.return_value = date self.assertAlmostEqual(self.line.theoritical_amount, -308.5)
def test_07(self): """After 182 days, exactly half of the budget line""" date = Datetime.to_string(Datetime.from_string('2014-07-02 00:00:00')) self.mock_datetime.now.return_value = date self.assertAlmostEqual(self.line.theoritical_amount, -182)
def test_06(self): """After 50 days""" date = Datetime.to_string(Datetime.from_string('2014-02-20 00:00:00')) self.mock_datetime.now.return_value = date self.assertAlmostEqual(self.line.theoritical_amount, -50)
def test_04(self): """After 48 hours""" date = Datetime.to_string(Datetime.from_string('2014-01-03 00:00:00')) self.mock_datetime.now.return_value = date self.assertAlmostEqual(self.line.theoritical_amount, -2)