Ignore:
Timestamp:
Dec 13, 2010, 4:46:31 AM (13 years ago)
Author:
gav
Message:

Add regulatoryRequirements report.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/AssetReportService.groovy

    r735 r740  
    300300    } // getEquipmentRegister
    301301
     302
     303    /**
     304    * Selects and returns assets regulatory requirements as specified in recurring regulatory tasks.
     305    * @param params The request params, may contain params to specify the search.
     306    * @param locale The locale to use when generating result.message.
     307    */
     308    def getRegulatoryRequirements(params, locale) {
     309        def result = [:]
     310
     311        def fail = { Map m ->
     312            result.error = [ code: m.code, args: [] ]
     313            return result
     314        }
     315
     316        result.section = Section.get(params.section.id.toLong())
     317        result.site = result.section.site
     318
     319        result.startDate = params.startDate ?: dateUtilService.oneWeekAgo
     320        result.endDate = params.endDate ?: dateUtilService.today
     321        // Auto swap date range.
     322        if(result.startDate > result.endDate) {
     323            def tempStartDate = result.startDate
     324            result.startDate = result.endDate
     325            result.endDate = tempStartDate
     326        }
     327
     328        result.startDateString = g.formatDate(format: "EEE, dd-MMM-yyyy", date: result.startDate)
     329        result.endDateString = g.formatDate(format: "EEE, dd-MMM-yyyy", date: result.endDate)
     330
     331        result.summary = "This report only selects primary assets and not associated assets. \n"
     332        result.summary += "Tasks must have a recurrence enabled and regulatory requirement set."
     333
     334        // Subquery to count subTasks..
     335        def subTaskQ = new HqlBuilder().query {
     336            select 'count(subTask)'
     337            from 'task.subTasks as subTask'
     338            where 'subTask.trash = false'
     339                and 'subTask.targetStartDate < :endDate'
     340                and 'subTask.targetCompletionDate >= :startDate'
     341        }
     342        def subTaskTotalQ = subTaskQ.query
     343
     344        subTaskQ.and 'subTask.taskStatus.id = 3' // Complete.
     345        def subTaskCompletedQ = subTaskQ.query
     346
     347        def regulatoryTaskQ = new HqlBuilder().query {
     348            select 'new map(primaryAsset.name as assetName',
     349                        'primaryAsset.description as assetDescription',
     350                        'primaryAsset.isActive as assetIsActive',
     351                        'task.id as taskId',
     352                        'task.description as taskDescription',
     353                        "($subTaskTotalQ) as subTaskTotalCount",
     354                        "($subTaskCompletedQ) as subTaskCompletedCount)"
     355                        namedParams.startDate = result.startDate
     356                        namedParams.endDate = result.endDate
     357            from 'Task task',
     358                    'left join task.primaryAsset as primaryAsset',
     359                    'left join task.taskRecurringSchedule as taskRecurringSchedule'
     360            where 'task.regulatoryRequirement = true'
     361                and 'taskRecurringSchedule.enabled = true'
     362                and 'task.trash = false'
     363                        namedParams.sectionId = result.section.id
     364                and 'primaryAsset.section.id = :sectionId'
     365        }
     366        result.tasks = Task.executeQuery(regulatoryTaskQ.query, regulatoryTaskQ.namedParams)
     367
     368        // Build the report table row for each task.
     369        result.tasks.each { task ->
     370
     371            // Caluculate percentages and build description.
     372            def percentComplete
     373            def completionFigures
     374            if(task.subTaskTotalCount) {
     375                percentComplete = (task.subTaskCompletedCount / task.subTaskTotalCount) * 100
     376                task.completionFigures = "$percentComplete% ($task.subTaskCompletedCount/$task.subTaskTotalCount)"
     377            }
     378            else
     379                task.completionFigures = '0 sub tasks in date range'
     380        } // tasks.each
     381
     382        result.dataList = result.tasks
     383        result.dataList.sort { p1, p2 -> p1.assetName.compareToIgnoreCase(p2.assetName) }
     384
     385        // Success.
     386        return result
     387
     388    } // getRegulatoryRequirements
     389
    302390} // end class
Note: See TracChangeset for help on using the changeset viewer.