class TaskRecurringScheduleService {

    boolean transactional = false

    def dateUtilService
    def taskService

    def generateAll() {

        /// @todo: disable recurringSchedule when moving a task to trash.
        def taskRecurringScheduleList = TaskRecurringSchedule.findAllByEnabled(true)

        taskRecurringScheduleList.each() {

            if ( dateUtilService.getTomorrow() > it.nextGenerationDate) {
                    def p = [:]
                    p.targetStartDate = it.nextTargetStartDate
                    p.targetCompletionDate = it.nextTargetCompletionDate
                    def result = taskService.createSubTask(it.task, p)
                    if( !result.error ) {
                        it.lastGeneratedSubTask = result.taskInstance
                        it.subTasksGenerated++
                        it.setNextTargetStartDate()
                        it.setNextGenerationDate()
                        it.setNextTargetCompletionDate()
                    }
                    else {
                        log.error result
                    }
            }

        }
    }

} // end of class
