def test_03_EveryHour(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every Hour' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime() date = addToDate(now, day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityHourFrequency(1) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEquals(alarm.getAlarmDate(), date) LOG(message + ' now :',0,now) now = addToDate(now,day=2) LOG(message + ' now :',0,now) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,hour=1) self.assertEquals(alarm.getAlarmDate(),next_date) now = addToDate(now,hour=1,minute=5) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,hour=1) self.assertEquals(alarm.getAlarmDate(),next_date) # check if manual invoking does not break getAlarmDate() result. alarm.activeSense() self.assertEquals(alarm.getAlarmDate(),next_date)
def getAccountingDate(accounting_date): accounting_day = 25 if accounting_date.day() <= accounting_day: accounting_date = addToDate(accounting_date, dict(month=-1)) diff = accounting_day - accounting_date.day() accounting_date = addToDate(accounting_date, dict(day=diff)) return accounting_date
def getNextPeriodicalDate(self, current_date, next_start_date=None): """ Get the next date where this periodic event should start. XXX It completely reimplements the PeriodictyMixin method because the minimal duration between dates is day, and not minute Better way would be to improve the API of getNextPeriodicalDate, and optimize addToDate method. """ # XXX Copy/Paste from PeriodicityMixin if next_start_date is None: next_start_date = current_date if next_start_date > current_date: return else: # Make sure the old date is not too far away day_count = int(current_date-next_start_date) next_start_date = next_start_date + day_count next_start_date = addToDate(next_start_date, day=1) while 1: if (self._validateDay(next_start_date)) and \ (self._validateWeek(next_start_date)) and \ (self._validateMonth(next_start_date)): break else: next_start_date = addToDate(next_start_date, day=1) return next_start_date
def test_03_EveryHour(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every Hour' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime() date = addToDate(now, day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityHourFrequency(1) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEqual(alarm.getAlarmDate(), date) LOG(message + ' now :',0,now) now = addToDate(now,day=2) LOG(message + ' now :',0,now) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,hour=1) self.assertEqual(alarm.getAlarmDate(),next_date) now = addToDate(now,hour=1,minute=5) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,hour=1) self.assertEqual(alarm.getAlarmDate(),next_date) # check if manual invoking does not break getAlarmDate() result. alarm.activeSense() self.assertEqual(alarm.getAlarmDate(),next_date)
def _getDatePeriodDataList(self): """ Get all periods between periodicity start date and periodicity stop date """ result = [] exception_value_list = self.getCalendarPeriodExceptionValueList() exception_date_list = [x.getExceptionDate() \ for x in exception_value_list] exception_date_list = [x for x in exception_date_list if x is not None] exception_date_list.sort() if len(exception_date_list) != 0: current_exception_date = exception_date_list.pop(0).Date() else: current_exception_date = None start_date = self.getStartDate() if start_date is not None: stop_date = self.getStopDate(start_date) periodicity_stop_date = self.getPeriodicityStopDate( start_date) second_duration = int(stop_date) - int(start_date) if second_duration > 0: # First date has to respect the periodicity config next_start_date = self.getNextPeriodicalDate(addToDate(start_date, day=-1)) while (next_start_date is not None) and \ (next_start_date <= periodicity_stop_date): # Check that next_start_date is not an exception if (current_exception_date is not None) and \ (current_exception_date == next_start_date.Date()): # We match an exception date # So, don't return this value # Update the next exception date if len(exception_date_list) != 0: current_exception_date = exception_date_list.pop(0).Date() else: current_exception_date = None elif (current_exception_date is not None) and \ (current_exception_date < next_start_date.Date()): # SQL method don't like iterator # yield (next_start_date, next_start_date+duration) result.append({'start_date': next_start_date, 'stop_date': addToDate(next_start_date, second=second_duration), 'quantity': self.getQuantity()}) # Update the next exception date if len(exception_date_list) != 0: current_exception_date = exception_date_list.pop(0).Date() else: current_exception_date = None else: # SQL method don't like iterator # yield (next_start_date, next_start_date+duration) result.append({'start_date': next_start_date, 'stop_date': addToDate(next_start_date, second=second_duration), 'quantity': self.getQuantity()}) next_start_date = self.getNextPeriodicalDate(next_start_date) return result
def _getDatePeriodDataList(self): """ Get all periods between periodicity start date and periodicity stop date """ result = [] exception_value_list = self.getCalendarPeriodExceptionValueList() exception_date_list = [x.getExceptionDate() \ for x in exception_value_list] exception_date_list = [x for x in exception_date_list if x is not None] exception_date_list.sort() if len(exception_date_list) != 0: current_exception_date = exception_date_list.pop(0).Date() else: current_exception_date = None start_date = self.getStartDate() if start_date is not None: stop_date = self.getStopDate(start_date) periodicity_stop_date = self.getPeriodicityStopDate( start_date) second_duration = int(stop_date) - int(start_date) if second_duration > 0: # First date has to respect the periodicity config next_start_date = self.getNextPeriodicalDate(start_date-1) while (next_start_date is not None) and \ (next_start_date <= periodicity_stop_date): # Check that next_start_date is not an exception if (current_exception_date is not None) and \ (current_exception_date == next_start_date.Date()): # We match an exception date # So, don't return this value # Update the next exception date if len(exception_date_list) != 0: current_exception_date = exception_date_list.pop(0).Date() else: current_exception_date = None elif (current_exception_date is not None) and \ (current_exception_date < next_start_date.Date()): # SQL method don't like iterator # yield (next_start_date, next_start_date+duration) result.append([next_start_date, addToDate(next_start_date, second=second_duration)]) # Update the next exception date if len(exception_date_list) != 0: current_exception_date = exception_date_list.pop(0).Date() else: current_exception_date = None else: # SQL method don't like iterator # yield (next_start_date, next_start_date+duration) result.append({'start_date': next_start_date, 'stop_date': addToDate(next_start_date, second=second_duration), 'quantity': self.getQuantity()}) next_start_date = self.getNextPeriodicalDate(next_start_date) return result
def getNextPeriodicalDate(current_date): next_start_date = current_date next_start_date = addToDate(next_start_date, day=1) while 1: if (validateDay(next_start_date)) and \ (validateWeek(next_start_date)) and \ (validateMonth(next_start_date)): break else: next_start_date = addToDate(next_start_date, day=1) return next_start_date
def getNextPeriodicalDate(current_date): next_start_date = current_date previous_date = next_start_date next_start_date = addToDate(next_start_date, day=1) while 1: if (validateDay(next_start_date)) and \ (validateWeek(next_start_date)) and \ (validateMonth(next_start_date)): break else: next_start_date = addToDate(next_start_date, day=1) return next_start_date
def expandOpenOrderRule(self, applied_rule, **kw): """ Expand tries to find all applicable supply path and all applicable transformations. """ number_of_months_in_year = 12 # get career list that use this employment contract : career_list = self.getAggregateRelatedValueList() current_date = DateTime() for career in career_list: employee = career.getParentRelativeUrl() employer = career.getSubordinationValue( ) is not None and career.getSubordinationValue().getRelativeUrl( ) or None start_date = career.getStartDate() stop_date = career.getStopDate() for year_count in range(stop_date.year() - start_date.year() + 1): for month_count in range(stop_date.month() - start_date.month() + 1): # for first movement, we use the start_date day movement_start_date = addToDate(start_date, year=year_count, month=month_count) if month_count != 0 or year_count != 0: # if there is more than one movement in the period, start date is the begining of the month movement_start_date = DateTime( movement_start_date.strftime('%Y/%m/01 00:00:00')) movement_stop_date = atTheEndOfPeriod( movement_start_date, 'month') - 1 # create only one year in the future if movement_start_date > addToDate(current_date, year=1): break # if the stop_date is in not at the end of the month, use it if stop_date < movement_stop_date: movement_stop_date = stop_date if not self.assertMovementExists(applied_rule, movement_start_date) and\ movement_stop_date.month() <= number_of_months_in_year: property_dict = dict() simulation_movement = applied_rule.newContent( id='movement_%s_%s' % (movement_start_date.year(), movement_stop_date.month()), start_date=movement_start_date, stop_date=movement_stop_date, source=employee, destination=employer, source_section=employee, destination_section=employer, quantity=self.getQuantity(), quantity_unit=self.getQuantityUnit(), resource=self.getResource())
def test_17_tic(self): """ Make sure that the tic method on alarm is working """ alarm = self.newAlarm() alarm.setEnabled(True) self.tic() sense_method_id = 'Alarm_testSenseMethodForTic' skin_folder_id = 'custom' skin_folder = self.getPortal().portal_skins[skin_folder_id] skin_folder.manage_addProduct['PythonScripts']\ .manage_addPythonScript(id=sense_method_id) # Make the sense method fail skin_folder[sense_method_id].ZPythonScript_edit('*args,**kw', 'context.setDescription("a")') del skin_folder alarm.setActiveSenseMethodId(sense_method_id) self.tic() alarm_tool = self.getPortal().portal_alarms # Nothing should happens yet alarm_tool.tic() self.tic() self.assertTrue(alarm.getDescription() in (None, '')) now = DateTime() date = addToDate(now, day=-1) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityMinuteFrequency(1) self.tic() alarm_tool.tic() self.tic() self.assertEqual(alarm.getDescription(), 'a')
def test_16_uncatalog(self): """ Check that deleting an alarm uncatalogs it. """ alarm = self.newAlarm(enabled=True) self.tic() now = DateTime() date = addToDate(now, day=1) alarm.setPeriodicityStartDate(date) self.tic() self.assertEqual(alarm.getAlarmDate(), date) # This should not do change the alarm date alarm.setNextAlarmDate(current_date=now) self.tic() self.assertEqual(alarm.getAlarmDate(), date) # Delete the alarm a_tool = self.getAlarmTool() alarm_uid = alarm.getUid() a_tool.manage_delObjects(uids=[alarm_uid]) self.tic() # Check that related entry was removed sql_connection = self.getSQLConnection() sql = 'select * from alarm where uid=%s' % alarm_uid result = sql_connection.manage_test(sql) self.assertEqual(0, len(result))
def test_16_uncatalog(self, quiet=0, run=run_all_test): """ Check that deleting an alarm uncatalogs it. """ if not run: return if not quiet: message = 'Test Uncatalog' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ', 0, message) alarm = self.newAlarm(enabled=True) self.tic() now = DateTime() date = addToDate(now, day=1) alarm.setPeriodicityStartDate(date) self.tic() self.assertEqual(alarm.getAlarmDate(), date) # This should not do change the alarm date alarm.setNextAlarmDate(current_date=now) self.tic() self.assertEqual(alarm.getAlarmDate(), date) # Delete the alarm a_tool = self.getAlarmTool() alarm_uid = alarm.getUid() a_tool.manage_delObjects(uids=[alarm_uid]) self.tic() # Check that related entry was removed sql_connection = self.getSQLConnection() sql = 'select * from alarm where uid=%s' % alarm_uid result = sql_connection.manage_test(sql) self.assertEqual(0, len(result))
def test_16_uncatalog(self, quiet=0, run=run_all_test): """ Check that deleting an alarm uncatalogs it. """ if not run: return if not quiet: message = 'Test Uncatalog' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ', 0, message) alarm = self.newAlarm(enabled=True) self.tic() now = DateTime() date = addToDate(now, day=1) alarm.setPeriodicityStartDate(date) self.tic() self.assertEquals(alarm.getAlarmDate(), date) # This should not do change the alarm date alarm.setNextAlarmDate(current_date=now) self.tic() self.assertEquals(alarm.getAlarmDate(), date) # Delete the alarm a_tool = self.getAlarmTool() alarm_uid = alarm.getUid() a_tool.manage_delObjects(uids=[alarm_uid]) self.tic() # Check that related entry was removed sql_connection = self.getSQLConnection() sql = 'select * from alarm where uid=%s' % alarm_uid result = sql_connection.manage_test(sql) self.assertEquals(0, len(result))
def _getDatePeriodDataList(self): """ Get all periods between periodicity start date and periodicity stop date """ result = [] exception_value_list = self.getCalendarPeriodExceptionValueList() exception_date_list = [x.getExceptionDate() \ for x in exception_value_list] exception_date_set = set( [x.Date() for x in exception_date_list if x is not None] ) start_date = self.getStartDate() if start_date is not None: stop_date = self.getStopDate(start_date) periodicity_stop_date = self.getPeriodicityStopDate( start_date) duration = stop_date - start_date if duration > 0: # First date has to respect the periodicity config next_start_date = self.getNextPeriodicalDate(addToDate(start_date, day=-1)) while (next_start_date is not None) and \ (next_start_date <= periodicity_stop_date): if next_start_date.Date() not in exception_date_set: result.append({'start_date': next_start_date, 'stop_date': next_start_date + duration, 'quantity': self.getQuantity()}) next_start_date = self.getNextPeriodicalDate(next_start_date) return result
def expandOpenOrderRule(self, applied_rule, **kw): """ Expand tries to find all applicable supply path and all applicable transformations. """ number_of_months_in_year = 12 # get career list that use this employment contract : career_list = self.getAggregateRelatedValueList() current_date = DateTime() for career in career_list: employee = career.getParentRelativeUrl() employer = career.getSubordinationValue() is not None and career.getSubordinationValue().getRelativeUrl() or None start_date = career.getStartDate() stop_date = career.getStopDate() for year_count in range(stop_date.year() - start_date.year() + 1): for month_count in range(stop_date.month() - start_date.month()+1): # for first movement, we use the start_date day movement_start_date = addToDate(start_date, year=year_count, month=month_count) if month_count != 0 or year_count != 0: # if there is more than one movement in the period, start date is the begining of the month movement_start_date = DateTime(movement_start_date.strftime('%Y/%m/01 00:00:00')) movement_stop_date = atTheEndOfPeriod(movement_start_date, 'month')-1 # create only one year in the future if movement_start_date > addToDate(current_date, year=1): break # if the stop_date is in not at the end of the month, use it if stop_date < movement_stop_date: movement_stop_date = stop_date if not self.assertMovementExists(applied_rule, movement_start_date) and\ movement_stop_date.month() <= number_of_months_in_year: property_dict = dict() simulation_movement = applied_rule.newContent( id = 'movement_%s_%s' % (movement_start_date.year(), movement_stop_date.month()), start_date = movement_start_date, stop_date = movement_stop_date, source = employee, destination = employer, source_section = employee, destination_section = employer, quantity = self.getQuantity(), quantity_unit = self.getQuantityUnit(), resource = self.getResource() )
def test_13_EveryMinute(self): alarm = self.newAlarm(enabled=True) now = DateTime() date = addToDate(now,hour=2) alarm.setPeriodicityStartDate(now) alarm.setPeriodicityMinuteFrequency(1) self.tic() alarm.setNextAlarmDate(current_date=date) self.assertEqual(alarm.getAlarmDate(),date)
def getLeaveBlocAsDict(leave_period, leave_category): bloc = {} bloc['S21.G00.60.001'] = leave_category.getCodification() bloc['S21.G00.60.002'] = formatDate(leave_period.getStartDate()) bloc['S21.G00.60.003'] = formatDate(leave_period.getStopDate()) bloc['S21.G00.60.004'] = '01' # we do subrogation first_subrogation_day = addToDate(leave_period.getStartDate(), day=3) bloc['S21.G00.60.005'] = formatDate(first_subrogation_day) # 3 months of subrogation, as defined in the collective agreement bloc['S21.G00.60.006'] = formatDate( addToDate(first_subrogation_day, month=3, days=-1)) bloc['S21.G00.60.007'] = bank_account.getIban() bloc['S21.G00.60.008'] = bank_account.getBicCode() # employee restarted work during this period if getattr(leave_period, 'expiration_date', None): bloc['S21.G00.60.010'] = formatDate(leave_period.getExpirationDate()) bloc['S21.G00.60.011'] = '01' # Restart normally return bloc
def test_03_EveryHour(self): alarm = self.newAlarm(enabled=True) now = DateTime() date = addToDate(now, day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityHourFrequency(1) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEqual(alarm.getAlarmDate(), date) now = addToDate(now,day=2) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,hour=1) self.assertEqual(alarm.getAlarmDate(),next_date) now = addToDate(now,hour=1,minute=5) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,hour=1) self.assertEqual(alarm.getAlarmDate(),next_date) # check if manual invoking does not break getAlarmDate() result. alarm.activeSense() self.assertEqual(alarm.getAlarmDate(),next_date)
def render_date_range(date_range): m = re.match(r'(\d+)([dwmy])', date_range) if m is not None: period_dict = { 'd':'day', 'w':'week', 'm':'month', 'y':'year', } num, period = m.groups() period = period_dict[period.lower()] return addToDate(DateTime(), **{period:-int(num)})
def test_12_Every5Minutes(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every 5 Minutes' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime() minute_to_remove = now.minute() % 5 now = addToDate(now,minute=-minute_to_remove) date = addToDate(now,day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityMinuteFrequency(5) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEquals(alarm.getAlarmDate(),date) LOG(message + ' now :',0,now) now = addToDate(now,day=2) LOG(message + ' now :',0,now) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,minute=5) self.assertEquals(alarm.getAlarmDate(),next_date) now = addToDate(now,minute=5,second=14) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,minute=5) self.assertEquals(alarm.getAlarmDate(),next_date)
def test_04_Every3Hours(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every 3 Hours' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime().toZone('UTC') hour_to_remove = now.hour() % 3 now = addToDate(now,hour=-hour_to_remove) date = addToDate(now,day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityHourFrequency(3) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEqual(alarm.getAlarmDate(),date) LOG(message + ' now :',0,now) now = addToDate(now,day=2) LOG(message + ' now :',0,now) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,hour=3) self.assertEqual(alarm.getAlarmDate(),next_date) now = addToDate(now,hour=3,minute=7,second=4) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,hour=3) self.assertEqual(alarm.getAlarmDate(),next_date)
def test_12_Every5Minutes(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every 5 Minutes' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime() minute_to_remove = now.minute() % 5 now = addToDate(now,minute=-minute_to_remove) date = addToDate(now,day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityMinuteFrequency(5) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEqual(alarm.getAlarmDate(),date) LOG(message + ' now :',0,now) now = addToDate(now,day=2) LOG(message + ' now :',0,now) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,minute=5) self.assertEqual(alarm.getAlarmDate(),next_date) now = addToDate(now,minute=5,second=14) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,minute=5) self.assertEqual(alarm.getAlarmDate(),next_date)
def test_04_Every3Hours(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every 3 Hours' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime().toZone('UTC') hour_to_remove = now.hour() % 3 now = addToDate(now,hour=-hour_to_remove) date = addToDate(now,day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityHourFrequency(3) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEquals(alarm.getAlarmDate(),date) LOG(message + ' now :',0,now) now = addToDate(now,day=2) LOG(message + ' now :',0,now) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,hour=3) self.assertEquals(alarm.getAlarmDate(),next_date) now = addToDate(now,hour=3,minute=7,second=4) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,hour=3) self.assertEquals(alarm.getAlarmDate(),next_date)
def getLeaveBlocAsDict(leave_period): bloc = {} bloc['S21.G00.60.001'] = leave_period.getResourceValue().getCodification() bloc['S21.G00.60.002'] = formatDate(leave_period.getStartDate()) bloc['S21.G00.60.003'] = formatDate(leave_period.getStopDate()) # employee left during this period if from_date < leave_period.getStartDate() < effective_date: bloc['S21.G00.60.004'] = '01' # we do subrogation first_subrogation_day = addToDate(leave_period.getStartDate(), day=3) bloc['S21.G00.60.005'] = formatDate(first_subrogation_day) # 3 months of subrogation, as defined in the collective agreement bloc['S21.G00.60.006'] = formatDate(addToDate(first_subrogation_day, month=3, days=-1)) bank_account = payment_transaction.getSourcePayment() bloc['S21.G00.60.007'] = bank_account.getIban() bloc['S21.G00.60.008'] = bank_account.getBicCode() else: bloc['S21.G00.60.004'] = '02' # we don't do subrogation # employee restarted work during this period if getattr(leave_period, 'expiration_date', None): bloc['S21.G00.60.010'] = formatDate(leave_period.getExpirationDate()) bloc['S21.G00.60.011'] = '01' # Restart normally return bloc
def test_02_Initialization(self): """ Test some basic things right after the creation """ alarm = self.newAlarm() self.tic() now = DateTime() date = addToDate(now,day=1) alarm.setPeriodicityStartDate(date) self.assertEqual(alarm.getAlarmDate(), None) alarm.setEnabled(True) self.assertEqual(alarm.getAlarmDate(), date) alarm.setNextAlarmDate(current_date=now) # This should not do change the alarm date self.assertEqual(alarm.getAlarmDate(),date)
def test_13_EveryMinute(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every Minute' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime() date = addToDate(now,hour=2) alarm.setPeriodicityStartDate(now) alarm.setPeriodicityMinuteFrequency(1) self.tic() alarm.setNextAlarmDate(current_date=date) self.assertEquals(alarm.getAlarmDate(),date)
def setUpPeriodicity(hosting_subscription): from Products.ERP5Type.DateUtils import addToDate, getClosestDate start_date = hosting_subscription.getCreationDate() start_date = getClosestDate(target_date=start_date, precision='day') while start_date.day() >= 29: start_date = addToDate(start_date, to_add={'day': -1}) periodicity_month_day_list = [start_date.day()] periodicity_hour_list=[0] periodicity_minute_list=[0] hosting_subscription.edit( periodicity_month_day_list=periodicity_month_day_list, periodicity_hour_list=periodicity_hour_list, periodicity_minute_list=periodicity_minute_list )
def getLeaveBlocAsDict(leave_period): bloc = {} bloc['S21.G00.60.001'] = leave_period.getResourceValue().getCodification() bloc['S21.G00.60.002'] = formatDate(leave_period.getStartDate()) bloc['S21.G00.60.003'] = formatDate(leave_period.getStopDate()) # employee left during this period if from_date < leave_period.getStartDate() < effective_date: bloc['S21.G00.60.004'] = '01' # we do subrogation first_subrogation_day = addToDate(leave_period.getStartDate(), day=3) bloc['S21.G00.60.005'] = formatDate(first_subrogation_day) # 3 months of subrogation, as defined in the collective agreement bloc['S21.G00.60.006'] = formatDate( addToDate(first_subrogation_day, month=3, days=-1)) bank_account = payment_transaction.getSourcePayment() bloc['S21.G00.60.007'] = bank_account.getIban() bloc['S21.G00.60.008'] = bank_account.getBicCode() else: bloc['S21.G00.60.004'] = '02' # we don't do subrogation # employee restarted work during this period if getattr(leave_period, 'expiration_date', None): bloc['S21.G00.60.010'] = formatDate(leave_period.getExpirationDate()) bloc['S21.G00.60.011'] = '01' # Restart normally return bloc
def test_13_EveryMinute(self, quiet=0, run=run_all_test): if not run: return if not quiet: message = 'Test Every Minute' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm(enabled=True) now = DateTime() date = addToDate(now,hour=2) alarm.setPeriodicityStartDate(now) alarm.setPeriodicityMinuteFrequency(1) self.tic() alarm.setNextAlarmDate(current_date=date) self.assertEqual(alarm.getAlarmDate(),date)
def calculateOpenOrderLineStopDate(open_order_line, hosting_subscription): end_date = hosting_subscription.HostingSubscription_calculateSubscriptionStopDate() if end_date is None: # Be sure that start date is different from stop date next_stop_date = hosting_subscription.getNextPeriodicalDate(hosting_subscription.HostingSubscription_calculateSubscriptionStartDate()) current_stop_date = next_stop_date while next_stop_date < now: # Return result should be < now, it order to provide stability in simulation (destruction if it happen should be >= now) current_stop_date = next_stop_date next_stop_date = \ hosting_subscription.getNextPeriodicalDate(current_stop_date) return addToDate(current_stop_date, to_add={'second': -1}) else: stop_date = end_date return stop_date
def test_02_Initialization(self, quiet=0, run=run_all_test): """ Test some basic things right after the creation """ if not run: return if not quiet: message = 'Test Initialization' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm() self.tic() now = DateTime() date = addToDate(now,day=1) alarm.setPeriodicityStartDate(date) self.assertEqual(alarm.getAlarmDate(), None) alarm.setEnabled(True) self.assertEqual(alarm.getAlarmDate(), date) alarm.setNextAlarmDate(current_date=now) # This should not do change the alarm date self.assertEqual(alarm.getAlarmDate(),date)
def test_02_Initialization(self, quiet=0, run=run_all_test): """ Test some basic things right after the creation """ if not run: return if not quiet: message = 'Test Initialization' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ',0,message) alarm = self.newAlarm() self.tic() now = DateTime() date = addToDate(now,day=1) alarm.setPeriodicityStartDate(date) self.assertEquals(alarm.getAlarmDate(), None) alarm.setEnabled(True) self.assertEquals(alarm.getAlarmDate(), date) alarm.setNextAlarmDate(current_date=now) # This should not do change the alarm date self.assertEquals(alarm.getAlarmDate(),date)
def test_integer_add_to_date(self): date = DateTime('2000/01/01 %s' % self.timezone) self.assertEqual(DateTime('2000/01/01 00:01:30 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, second=90).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/01/01 01:30:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, minute=90).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/01/04 18:00:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, hour=90).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/03/31 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, day=90).toZone('UTC').ISO()) self.assertEqual(DateTime('2007/07/01 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, month=90).toZone('UTC').ISO()) self.assertEqual(DateTime('2090/01/01 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, year=90).toZone('UTC').ISO())
def test_negative_add_to_date(self): date = DateTime('2000/01/01 %s' % self.timezone) self.assertEqual(DateTime('1999/12/31 23:59:59 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, second=-1).toZone('UTC').ISO()) self.assertEqual(DateTime('1999/12/31 23:59:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, minute=-1).toZone('UTC').ISO()) self.assertEqual(DateTime('1999/12/31 23:00:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, hour=-1).toZone('UTC').ISO()) self.assertEqual(DateTime('1999/12/31 00:00:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, day=-1).toZone('UTC').ISO()) self.assertEqual(DateTime('1999/12/01 00:00:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, month=-1).toZone('UTC').ISO()) self.assertEqual(DateTime('1999/01/01 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, year=-1).toZone('UTC').ISO())
def test_float_add_to_date(self): date = DateTime('2000/01/01 %s' % self.timezone) self.assertEqual(DateTime('2000/01/01 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, second=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/01/01 00:00:30 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, minute=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/01/01 00:30:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, hour=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/01/01 12:00:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, day=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/01/16 12:00:00 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, month=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2000/07/01 %s' % self.timezone).toZone('UTC').ISO(), addToDate(date, year=0.5).toZone('UTC').ISO())
def test_complex_float_add_to_date(self): complex_date = DateTime('2004/03/16 01:23:54 %s' % self.timezone) self.assertEqual(DateTime('2004/03/16 01:23:54 %s' % self.timezone).toZone('UTC').ISO(), addToDate(complex_date, second=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2004/03/16 01:24:24 %s' % self.timezone).toZone('UTC').ISO(), addToDate(complex_date, minute=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2004/03/16 01:53:54 %s' % self.timezone).toZone('UTC').ISO(), addToDate(complex_date, hour=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2004/03/16 13:23:54 %s' % self.timezone).toZone('UTC').ISO(), addToDate(complex_date, day=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2004/03/31 13:23:54 %s' % self.timezone).toZone('UTC').ISO(), addToDate(complex_date, month=0.5).toZone('UTC').ISO()) self.assertEqual(DateTime('2004/09/16 01:23:54 %s' % self.timezone).toZone('UTC').ISO(), addToDate(complex_date, year=0.5).toZone('UTC').ISO())
def test_17_tic(self, quiet=0, run=run_all_test): """ Make sure that the tic method on alarm is working """ if not run: return if not quiet: message = 'Test AlarmTool Tic' ZopeTestCase._print('\n%s ' % message) LOG('Testing... ', 0, message) alarm = self.newAlarm() alarm.setEnabled(True) transaction.commit() self.tic() sense_method_id = 'Alarm_testSenseMethodForTic' skin_folder_id = 'custom' skin_folder = self.getPortal().portal_skins[skin_folder_id] skin_folder.manage_addProduct['PythonScripts']\ .manage_addPythonScript(id=sense_method_id) # Make the sense method fail skin_folder[sense_method_id].ZPythonScript_edit('*args,**kw', 'context.setDescription("a")') del skin_folder alarm.setActiveSenseMethodId(sense_method_id) transaction.commit() self.tic() alarm_tool = self.getPortal().portal_alarms # Nothing should happens yet alarm_tool.tic() transaction.commit() self.tic() self.assertTrue(alarm.getDescription() in (None, '')) now = DateTime() date = addToDate(now, day=-1) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityMinuteFrequency(1) transaction.commit() self.tic() alarm_tool.tic() transaction.commit() self.tic() self.assertEquals(alarm.getDescription(), 'a')
def test_automatic_solve(self): alarm = self.newAlarm() alarm.setEnabled(True) alarm.setAutomaticSolve(True) alarm.setPeriodicityStartDate(addToDate(DateTime(), day=-1)) alarm.setPeriodicityMinuteFrequency(1) sense_method_id = 'Alarm_testSenseMethodWithAutomaticSolve' skin_folder_id = 'custom' skin_folder = self.getPortal().portal_skins[skin_folder_id] skin_folder.manage_addProduct['PythonScripts']\ .manage_addPythonScript(id=sense_method_id) skin_folder[sense_method_id].ZPythonScript_edit('fixit=0, *args,**kw', 'if fixit: context.setDescription("fixed")') alarm.setActiveSenseMethodId(sense_method_id) self.tic() self.portal.portal_alarms.tic() self.tic() self.assertEqual(alarm.getDescription(), 'fixed')
def test_12_Every5Minutes(self): alarm = self.newAlarm(enabled=True) now = DateTime() minute_to_remove = now.minute() % 5 now = addToDate(now,minute=-minute_to_remove) date = addToDate(now,day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityMinuteFrequency(5) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEqual(alarm.getAlarmDate(),date) now = addToDate(now,day=2) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,minute=5) self.assertEqual(alarm.getAlarmDate(),next_date) now = addToDate(now,minute=5,second=14) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,minute=5) self.assertEqual(alarm.getAlarmDate(),next_date)
def test_04_Every3Hours(self): alarm = self.newAlarm(enabled=True) now = DateTime().toZone('UTC') hour_to_remove = now.hour() % 3 now = addToDate(now,hour=-hour_to_remove) date = addToDate(now,day=2) alarm.setPeriodicityStartDate(date) alarm.setPeriodicityHourFrequency(3) self.tic() alarm.setNextAlarmDate(current_date=now) self.assertEqual(alarm.getAlarmDate(),date) now = addToDate(now,day=2) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(date,hour=3) self.assertEqual(alarm.getAlarmDate(),next_date) now = addToDate(now,hour=3,minute=7,second=4) alarm.setNextAlarmDate(current_date=now) next_date = addToDate(next_date,hour=3) self.assertEqual(alarm.getAlarmDate(),next_date)
def test_simulation(self): self._prepare() applied_rule_list = self.portal.portal_catalog(portal_type='Applied Rule', causality_uid=self.subscription.getUid()) self.assertEqual(1, len(applied_rule_list)) applied_rule = applied_rule_list[0].getObject() rule = applied_rule.getSpecialiseValue() self.assertEqual('Subscription Item Root Simulation Rule', rule.getPortalType()) self.assertEqual('default_subscription_item_rule', rule.getReference()) simulation_movement_list = self.portal.portal_catalog( portal_type='Simulation Movement', parent_uid=applied_rule.getUid(), sort_on=(('movement.start_date', 'ASC'),) ) # There are 2 movements, for February and March self.assertEqual(2, len(simulation_movement_list)) # Check the list of expected simulation idx = 0 for simulation_movement in simulation_movement_list: simulation_movement = simulation_movement.getObject() movement_start_date = addToDate(self.initial_date, to_add=dict(month=idx)) movement_stop_date = addToDate(self.initial_date, to_add=dict(month=idx+1)) # Check simulation movement property self.assertEqual(movement_start_date, simulation_movement.getStartDate()) self.assertEqual(movement_stop_date, simulation_movement.getStopDate()) self.assertEqual(self.open_order_line.getQuantity(), simulation_movement.getQuantity()) self.assertEqual(self.open_order_line.getQuantityUnit(), simulation_movement.getQuantityUnit()) self.assertEqual(self.open_order_line.getPrice(), simulation_movement.getPrice()) self.assertEqual(self.open_order_line.getPriceCurrency(), simulation_movement.getPriceCurrency()) self.assertEqual(self.open_order_line.getSource(), simulation_movement.getSource()) self.assertEqual(self.open_order_line.getSourceSection(), simulation_movement.getSourceSection()) self.assertEqual(self.open_order_line.getDestination(), simulation_movement.getDestination()) self.assertEqual(self.open_order_line.getDestinationSection(), simulation_movement.getDestinationSection()) self.assertEqual(self.open_order_line.getSpecialise(), simulation_movement.getSpecialise()) self.assertEqual(self.open_order_line.getResource(), simulation_movement.getResource()) self.assertEqual(applied_rule.getSpecialiseValue().getTradePhaseList(), simulation_movement.getTradePhaseList()) self.assertSameSet(self.open_order_line.getAggregateList(), simulation_movement.getAggregateList()) self.assertEqual('planned', simulation_movement.getSimulationState()) self.assertEqual(None, simulation_movement.getDelivery()) applied_rule_list_level_2 = [q.getSpecialiseReference() for q in simulation_movement.contentValues(portal_type='Applied Rule')] self.assertEqual(0, len(applied_rule_list_level_2)) SimulationMovement.original_getSimulationState = SimulationMovement\ .getSimulationState try: def getSimulationState(self): return 'delivered' SimulationMovement.getSimulationState = getSimulationState simulation_movement.expand(expand_policy='immediate') applied_rule_list_level_2 = simulation_movement.contentValues( portal_type='Applied Rule') self.assertSameSet([], applied_rule_list_level_2) finally: SimulationMovement.getSimulationState = SimulationMovement\ .original_getSimulationState transaction.abort() # check next simulation movement idx += 1
if context.getPortalType() != 'Person': raise TypeError('Person object is required') from DateTime import DateTime from Products.ERP5Type.DateUtils import addToDate key = context.Base_getBearerTokenKey() if not key: raise ValueError('Bearer Key Token is not defined') token = { 'expiration_timestamp': addToDate(DateTime(), to_add={ 'hour': 1 }).timeTime(), 'reference': context.getReference(), 'user-agent': context.REQUEST.getHeader('User-Agent'), 'remote-addr': context.REQUEST.get('REMOTE_ADDR') } hmac = context.Base_getHMAC(key, str(token)) context.Base_setBearerToken(hmac, token) return hmac, token['expiration_timestamp']
from Products.ERP5Type.DateUtils import addToDate from Products.ZSQLCatalog.SQLCatalog import Query portal = context.getPortalObject() portal_categories = portal.portal_categories now = DateTime() effective_date = context.getEffectiveDate() previous_pay_day = addToDate(effective_date, month=-1) # Get period dates result = portal.portal_catalog(portal_type="DSN Monthly Report", simulation_state="validated", sort_on=[("creation_date", "descending")]) from_date = DateTime(effective_date.year(), effective_date.month(), 1) # We report leave periods which are not over yet ... result = portal.portal_catalog(portal_type='Leave Request Period', query=Query(expiration_date=None)) leave_period_list = [period.getObject() for period in result] # ... And leave periods which ended during last period result = portal.portal_catalog(portal_type='Leave Request Period', expiration_date=">=%s" % from_date.strftime("%Y/%m")) # We need to filter results, because we can't search in a date interval with # a smaller grain than a month. leave_period_list.extend([ period.getObject() for period in result if period.getExpirationDate() > from_date
if ctp_code not in aggregated_social_contribution_dict: aggregated_social_contribution_dict[ctp_code] = employee_ctp[ctp_code].copy() else: for summing_parameter in ('base', 'quantity'): aggregated_social_contribution_dict[ctp_code][summing_parameter] = \ aggregated_social_contribution_dict[ctp_code][summing_parameter] + employee_ctp[ctp_code][summing_parameter] # Find the payment transaction for the social contributions if len(payment_transaction_list): for payment in payment_transaction_list: corporate_registration_code = payment.getDestinationSectionValue().getCorporateRegistrationCode() if corporate_registration_code == social_contribution_organisation.getCorporateRegistrationCode(): if establishment.isQuaterlyPayment(): amount_list = [] for i in range(3): start_date = addToDate(first_date_of_month, month=-i) stop_date = getLastDateOfMonth(addToDate(first_date_of_month, month=-i)) + 1 amount = -1. * portal.portal_simulation.getInventory( from_date=start_date, to_date=stop_date, section_uid=organisation.getUid(), mirror_section_uid=social_contribution_organisation.getUid(), node_uid=portal.account_module['securite_sociale'].getUid(), ledger_uid=portal.portal_categories.ledger.accounting.general.getUid(), parent_portal_type='Accounting Transaction', ) amount_list.append(amount) dsn_file.append(getDSNBlockDict(block_id='S21.G00.20', target=payment, corporate_registration_code=social_contribution_organisation.getCorporateRegistrationCode(), first_date_of_month=start_date,
from Products.ZSQLCatalog.SQLCatalog import Query from Products.ERP5Type.DateUtils import addToDate portal = context.getPortalObject() portal_preferences = portal.portal_preferences if not portal_preferences.isAuthenticationPolicyEnabled() or \ not portal.portal_preferences.isPreferredSystemRecoverExpiredPassword(): # no policy, no sense to file expire at all or symply system do not configured to return 0 # Prevent creating new recovery if one was recently created recovery_list = portal.portal_catalog( portal_type="Credential Recovery", reference=context.getReference(), default_destination_decision_uid=context.getUid(), creation_date=Query(range="min", creation_date=addToDate(DateTime(), {'day': -1})), limit=1) if (len(recovery_list) > 0): return 0 module = portal.getDefaultModule(portal_type='Credential Recovery') credential_recovery = module.newContent( portal_type="Credential Recovery", reference=context.getReference(), destination_decision_value=context, language=portal.Localizer.get_selected_language()) context.serialize() credential_recovery.submit()
def test_update_frozen_simulation(self): self._prepare() applied_rule_list = self.portal.portal_catalog(portal_type='Applied Rule', causality_uid=self.subscription.getUid()) self.assertEqual(1, len(applied_rule_list)) applied_rule = applied_rule_list[0].getObject() rule = applied_rule.getSpecialiseValue() self.assertEqual('Subscription Item Root Simulation Rule', rule.getPortalType()) self.assertEqual('default_subscription_item_rule', rule.getReference()) simulation_movement_list = self.portal.portal_catalog( portal_type='Simulation Movement', parent_uid=applied_rule.getUid(), sort_on=(('movement.start_date', 'ASC'),) ) # There are 2 movements, for February and March self.assertEqual(2, len(simulation_movement_list)) # Check the list of expected simulation idx = 0 for simulation_movement in simulation_movement_list: simulation_movement = simulation_movement.getObject() movement_start_date = addToDate(self.initial_date, to_add=dict(month=idx)) movement_stop_date = addToDate(self.initial_date, to_add=dict(month=idx+1)) # Check simulation movement property self.assertEqual(movement_start_date, simulation_movement.getStartDate()) self.assertEqual(movement_stop_date, simulation_movement.getStopDate()) self.assertEqual(self.open_order_line.getQuantity(), simulation_movement.getQuantity()) self.assertEqual(self.open_order_line.getQuantityUnit(), simulation_movement.getQuantityUnit()) self.assertEqual(self.open_order_line.getPrice(), simulation_movement.getPrice()) self.assertEqual(self.open_order_line.getPriceCurrency(), simulation_movement.getPriceCurrency()) self.assertEqual(self.open_order_line.getSource(), simulation_movement.getSource()) self.assertEqual(self.open_order_line.getSourceSection(), simulation_movement.getSourceSection()) self.assertEqual(self.open_order_line.getDestination(), simulation_movement.getDestination()) self.assertEqual(self.open_order_line.getDestinationSection(), simulation_movement.getDestinationSection()) self.assertEqual(self.open_order_line.getSpecialise(), simulation_movement.getSpecialise()) self.assertEqual(self.open_order_line.getResource(), simulation_movement.getResource()) self.assertEqual(applied_rule.getSpecialiseValue().getTradePhaseList(), simulation_movement.getTradePhaseList()) self.assertSameSet(self.open_order_line.getAggregateList(), simulation_movement.getAggregateList()) self.assertEqual('planned', simulation_movement.getSimulationState()) self.assertEqual(None, simulation_movement.getDelivery()) # check children rules' type child_applied_rule_type_list = [q.getSpecialiseReference() for q in \ simulation_movement.contentValues(portal_type='Applied Rule')] self.assertSameSet( [], child_applied_rule_type_list) # check next simulation movement idx += 1 def isFrozen(*args, **kwargs): return True try: SimulationMovement.originalIsFrozen = SimulationMovement.isFrozen SimulationMovement.isFrozen = isFrozen # reexpanding non changed will work correctly applied_rule.expand(expand_policy='immediate') self.tic() # reexpanding with change on frozen movement will raise self.subscription.edit(periodicity_month_day=self.subscription\ .getPeriodicityMonthDay() - 1) self.tic() self.assertRaises(NotImplementedError, applied_rule.expand, expand_policy='immediate') finally: SimulationMovement.isFrozen = SimulationMovement.originalIsFrozen delattr(SimulationMovement, 'originalIsFrozen')