Ignore:
Timestamp:
Dec 13, 2010, 10:14:05 PM (13 years ago)
Author:
gav
Message:

Add mandatoryRequirements report.

File:
1 edited

Legend:

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

    r741 r743  
    300300    } // getEquipmentRegister
    301301
    302 
    303302    /**
    304303    * Selects and returns assets regulatory requirements as specified in recurring regulatory tasks.
     
    330329
    331330        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."
     331        result.summary += "Tasks must have recurrence enabled and regulatory requirement set."
    333332
    334333        // Subquery to count subTasks..
     
    388387    } // getRegulatoryRequirements
    389388
     389    /**
     390    * Selects and returns assets mandatory requirements as specified in recurring mandatory tasks.
     391    * @param params The request params, may contain params to specify the search.
     392    * @param locale The locale to use when generating result.message.
     393    */
     394    def getMandatoryRequirements(params, locale) {
     395        def result = [:]
     396
     397        def fail = { Map m ->
     398            result.error = [ code: m.code, args: [] ]
     399            return result
     400        }
     401
     402        result.section = Section.get(params.section.id.toLong())
     403        result.site = result.section.site
     404
     405        result.startDate = params.startDate ?: dateUtilService.oneWeekAgo
     406        result.endDate = params.endDate ?: dateUtilService.today
     407        // Auto swap date range.
     408        if(result.startDate > result.endDate) {
     409            def tempStartDate = result.startDate
     410            result.startDate = result.endDate
     411            result.endDate = tempStartDate
     412        }
     413
     414        result.startDateString = g.formatDate(format: "EEE, dd-MMM-yyyy", date: result.startDate)
     415        result.endDateString = g.formatDate(format: "EEE, dd-MMM-yyyy", date: result.endDate)
     416
     417        result.summary = "This report only selects primary assets and not associated assets. \n"
     418        result.summary += "Tasks must have recurrence enabled and mandatory requirement set."
     419
     420        // Subquery to count subTasks..
     421        def subTaskQ = new HqlBuilder().query {
     422            select 'count(subTask)'
     423            from 'task.subTasks as subTask'
     424            where 'subTask.trash = false'
     425                and 'subTask.targetStartDate < :endDate'
     426                and 'subTask.targetCompletionDate >= :startDate'
     427        }
     428        def subTaskTotalQ = subTaskQ.query
     429
     430        subTaskQ.and 'subTask.taskStatus.id = 3' // Complete.
     431        def subTaskCompletedQ = subTaskQ.query
     432
     433        def mandatoryTaskQ = new HqlBuilder().query {
     434            select 'new map(primaryAsset.name as assetName',
     435                        'primaryAsset.description as assetDescription',
     436                        'primaryAsset.isActive as assetIsActive',
     437                        'task.id as taskId',
     438                        'task.description as taskDescription',
     439                        "($subTaskTotalQ) as subTaskTotalCount",
     440                        "($subTaskCompletedQ) as subTaskCompletedCount)"
     441                        namedParams.startDate = result.startDate
     442                        namedParams.endDate = result.endDate
     443            from 'Task task',
     444                    'left join task.primaryAsset as primaryAsset',
     445                    'left join task.taskRecurringSchedule as taskRecurringSchedule'
     446            where 'task.mandatoryRequirement = true'
     447                and 'taskRecurringSchedule.enabled = true'
     448                and 'task.trash = false'
     449                        namedParams.sectionId = result.section.id
     450                and 'primaryAsset.section.id = :sectionId'
     451        }
     452        result.tasks = Task.executeQuery(mandatoryTaskQ.query, mandatoryTaskQ.namedParams)
     453
     454        // Build the report table row for each task.
     455        result.tasks.each { task ->
     456
     457            // Caluculate percentages and build description.
     458            def percentComplete
     459            def completionFigures
     460            if(task.subTaskTotalCount) {
     461                percentComplete = (task.subTaskCompletedCount / task.subTaskTotalCount) * 100
     462                task.completionFigures = "${percentComplete.toInteger()}% ($task.subTaskCompletedCount/$task.subTaskTotalCount)"
     463            }
     464            else
     465                task.completionFigures = '0 sub tasks in date range'
     466        } // tasks.each
     467
     468        result.dataList = result.tasks
     469        result.dataList.sort { p1, p2 -> p1.assetName.compareToIgnoreCase(p2.assetName) }
     470
     471        // Success.
     472        return result
     473
     474    } // getMandatoryRequirements
     475
    390476} // end class
Note: See TracChangeset for help on using the changeset viewer.