Ignore:
Timestamp:
Nov 10, 2010, 5:30:28 AM (14 years ago)
Author:
gav
Message:

Implement ticket #88 - "Implement Regulatory Task Completion on Equipmet Register Report OH&S"

File:
1 edited

Legend:

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

    r687 r706  
    137137        result.site = result.section.site
    138138
     139        // Start date.
     140        if(params.startDate)
     141            params.startDate = dateUtilService.makeDate(params.startDate_year, params.startDate_month, params.startDate_day)
     142        else
     143            params.startDate = dateUtilService.oneWeekAgo
     144
     145        // End date.
     146        if(params.endDate)
     147            params.endDate = dateUtilService.makeDate(params.endDate_year, params.endDate_month, params.endDate_day)
     148        else
     149            params.endDate = dateUtilService.today
     150
     151        // Normalise date range.
     152        if(params.endDate < params.startDate)
     153            params.endDate = params.startDate
     154
    139155        // Inner join used to return only attribTypes that are used by AssetSubItemExtendedAttributes.
    140156        // So the result is only assetSubItem extendedAttributeTypes.
     
    166182        result.assetsWithoutEquipment = AssetSubItem.executeQuery(assetsWithoutEquipmentQ.query, assetsWithoutEquipmentQ.namedParams)
    167183
     184        // Subquery to count regulatory tasks.
     185        def regulatoryTaskCountQ = new HqlBuilder().query {
     186
     187            select 'count (distinct task)'
     188            from 'Task as task',
     189                    'left join task.associatedAssets as associatedAsset'
     190            where 'task.mandatoryRegulatory = true'
     191                and 'task.targetStartDate < :endDate'
     192                and 'task.targetCompletionDate >= :startDate'
     193                and '(task.primaryAsset.id = asset.id or associatedAsset.id = asset.id)'
     194                and 'task.trash = false'
     195        }
     196
     197        def totalRegulatoryTaskCountQ = regulatoryTaskCountQ.query
     198
     199        regulatoryTaskCountQ.and 'task.taskStatus.id = 3'
     200        def completedRegulatoryTaskCountQ = regulatoryTaskCountQ.query
     201
    168202        // A result is returned for every level 1 assetSubItem and for any extended attributes.
    169203        def q = new HqlBuilder().query {
     204
    170205            select 'new map(asset.name as assetName',
    171206                        'assetSubItem.name as name',
    172207                        'assetSubItem.description as description',
    173208                        'assetSubItem.comment as comment',
     209                        "0 as totalRegulatoryTaskCount",
     210                        "0 as completedRegulatoryTaskCount",
    174211                        'attribT.name as attribType',
    175212                        'attrib.value as attribValue)'
     
    187224        // A result is returned for every asset and for any extended attributes.
    188225        def assetResultsQ = new HqlBuilder().query {
     226
     227            // Subquery namedParams.
     228            namedParams.startDate = params.startDate
     229            namedParams.endDate = params.endDate+1
     230
    189231            select 'new map(asset.name as assetName',
    190232                        "'   Asset Details' as name", // Place holder 'equipment' name, 3 leading spaces for sorting.
    191233                        'asset.description as description',
    192234                        'asset.comment as comment',
     235                        "($totalRegulatoryTaskCountQ) as totalRegulatoryTaskCount",
     236                        "($completedRegulatoryTaskCountQ) as completedRegulatoryTaskCount",
    193237                        'attribT.name as attribType',
    194238                        'attrib.value as attribValue)'
     
    211255        def rows = [:]
    212256        equipmentResults.each { equipmentResult ->
    213             // Create row if it does not exist yet.
     257
    214258            def rowKey = equipmentResult.assetName+equipmentResult.name
     259
     260            // Create new row if it does not exist yet.
    215261            if(!rows.containsKey(rowKey)) {
    216262                rows[rowKey] = ['assetName': equipmentResult.assetName,
    217                                                             'name':equipmentResult.name,
    218                                                             'description':equipmentResult.description,
    219                                                             'comment':equipmentResult.comment]
     263                                            'name':equipmentResult.name,
     264                                            'description':equipmentResult.description,
     265                                            'comment':equipmentResult.comment,
     266                                            'Regulatory Task Completion': ' ']
    220267
    221268                // Add all attribType columns.
     
    223270                    rows[rowKey][column] = ' '
    224271                }
    225             }
     272
     273                // Caluculate and assign RegulatoryTaskCompletion, only for Assets.
     274                if(params.calculateRegulatoryTaskCompletion) {
     275
     276                    if(equipmentResult.totalRegulatoryTaskCount) {
     277                        def percentComplete = (equipmentResult.completedRegulatoryTaskCount / equipmentResult.totalRegulatoryTaskCount)*100
     278                        rows[rowKey]['Regulatory Task Completion'] = "${percentComplete.toInteger()}% (${equipmentResult.completedRegulatoryTaskCount}/${equipmentResult.totalRegulatoryTaskCount})"
     279                    }
     280                    else if(equipmentResult.name == '   Asset Details')
     281                        rows[rowKey]['Regulatory Task Completion'] = 'N/A'
     282                }
     283
     284            } // Create new row.
    226285
    227286            // Assign value to column.
    228287            rows[rowKey][equipmentResult.attribType] = equipmentResult.attribValue
    229         }
     288        } // each.
    230289
    231290        // The value of each row is the dataList used by the report table.
Note: See TracChangeset for help on using the changeset viewer.