[585] | 1 | import org.gnumims.RichUiCalendarItem |
---|
[533] | 2 | |
---|
| 3 | /** |
---|
[651] | 4 | * Service class that encapsulates the business logic for Task Reports. |
---|
[533] | 5 | */ |
---|
| 6 | class TaskReportService { |
---|
| 7 | |
---|
| 8 | boolean transactional = false |
---|
| 9 | |
---|
| 10 | def authService |
---|
| 11 | def dateUtilService |
---|
| 12 | // def messageSource |
---|
| 13 | |
---|
[544] | 14 | def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() |
---|
[533] | 15 | |
---|
| 16 | def paramsMax = 100000 |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | * Selects and returns the reactive ratio. |
---|
[542] | 20 | * @param params The request params, may contain params to specify the search. |
---|
[533] | 21 | * @param locale The locale to use when generating result.message. |
---|
| 22 | */ |
---|
| 23 | def getReactiveRatio(params, locale) { |
---|
| 24 | def result = [:] |
---|
| 25 | |
---|
| 26 | def namedParams = [:] |
---|
| 27 | namedParams.startDate = params.startDate ?: dateUtilService.today |
---|
[535] | 28 | namedParams.endDate = params.endDate ?: dateUtilService.today |
---|
| 29 | namedParams.endDate++ // Start of next day required. |
---|
[533] | 30 | namedParams.immediateCallout = TaskType.read(1) |
---|
| 31 | namedParams.unscheduledBreakin = TaskType.read(2) |
---|
| 32 | namedParams.preventativeMaintenance = TaskType.read(4) |
---|
[535] | 33 | namedParams.notStarted = TaskStatus.read(1) |
---|
[533] | 34 | |
---|
| 35 | result.taskQuery = "from Task as task \ |
---|
[542] | 36 | where (task.trash = false \ |
---|
| 37 | and task.taskStatus != :notStarted \ |
---|
| 38 | and task.targetStartDate < :endDate \ |
---|
| 39 | and task.targetStartDate >= :startDate \ |
---|
| 40 | and ( \ |
---|
| 41 | task.taskType = :immediateCallout \ |
---|
| 42 | or task.taskType = :unscheduledBreakin \ |
---|
| 43 | or task.taskType = :preventativeMaintenance \ |
---|
| 44 | ) \ |
---|
| 45 | )" |
---|
[533] | 46 | |
---|
[535] | 47 | result.taskQuery = "select distinct task " + result.taskQuery |
---|
| 48 | result.taskList = Task.executeQuery(result.taskQuery, namedParams) |
---|
[533] | 49 | result.taskCount = result.taskList.size() |
---|
| 50 | |
---|
[536] | 51 | // Assets on Tasks Count. |
---|
[535] | 52 | result.totalAssetsOnTasksCount = 0 |
---|
[533] | 53 | result.immediateCalloutCount = 0 |
---|
| 54 | result.unscheduledBreakinCount = 0 |
---|
| 55 | result.preventativeMaintenanceCount = 0 |
---|
| 56 | |
---|
[542] | 57 | // Summary Of Calculations. |
---|
[535] | 58 | result.summaryOfCalculationMethod = 'HQL query: \n\n' |
---|
| 59 | def tempStringArray = result.taskQuery.split(' ') |
---|
| 60 | tempStringArray.each() { |
---|
| 61 | if(it != '') result.summaryOfCalculationMethod += it +'\n' |
---|
| 62 | } |
---|
| 63 | result.summaryOfCalculationMethod += '\n'+'Calculations: '+'\n\n' |
---|
| 64 | |
---|
| 65 | result.summaryOfCalculationMethod += 'totalAssetsOnTasksCount = A count of unique assets on each task. \n' |
---|
[533] | 66 | result.taskList.each() { task -> |
---|
| 67 | if(task.primaryAsset) { |
---|
[535] | 68 | result.totalAssetsOnTasksCount++ |
---|
[533] | 69 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
---|
| 70 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
---|
| 71 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
---|
| 72 | } |
---|
| 73 | task.associatedAssets.each() { associatedAsset -> |
---|
| 74 | if(associatedAsset.id != task.primaryAsset?.id) { |
---|
[535] | 75 | result.totalAssetsOnTasksCount++ |
---|
[533] | 76 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
---|
| 77 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
---|
| 78 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } // each() task |
---|
| 82 | |
---|
[536] | 83 | // Percentage of counts. |
---|
[533] | 84 | result.immediateCalloutPercentage = 0 |
---|
| 85 | result.totalPreventativePercentage = 0 |
---|
| 86 | |
---|
[535] | 87 | result.summaryOfCalculationMethod += 'totalPreventativeCount = unscheduledBreakinCount + preventativeMaintenanceCount\n' |
---|
[533] | 88 | result.totalPreventativeCount = result.unscheduledBreakinCount + result.preventativeMaintenanceCount |
---|
| 89 | try { |
---|
[535] | 90 | result.summaryOfCalculationMethod += 'immediateCalloutPercentage = (immediateCalloutCount / totalAssetsOnTasksCount)*100 \n' |
---|
| 91 | result.summaryOfCalculationMethod += 'totalPreventativePercentage = (totalPreventativeCount / totalAssetsOnTasksCount)*100 \n' |
---|
| 92 | result.immediateCalloutPercentage = (result.immediateCalloutCount / result.totalAssetsOnTasksCount)*100 |
---|
| 93 | result.totalPreventativePercentage = (result.totalPreventativeCount / result.totalAssetsOnTasksCount)*100 |
---|
[533] | 94 | } |
---|
| 95 | catch(ArithmeticException e) { |
---|
[536] | 96 | log.error "Could not calculate: Assets on Tasks Percentages: "+e |
---|
[533] | 97 | } |
---|
| 98 | |
---|
[536] | 99 | // Work Done. |
---|
| 100 | result.immediateCalloutWorkDone = [total:0, hours:0, minutes:0, percentage:0] |
---|
| 101 | result.unscheduledBreakinWorkDone = [total:0, hours:0, minutes:0] |
---|
| 102 | result.preventativeMaintenanceWorkDone = [total:0, hours:0, minutes:0] |
---|
| 103 | result.totalPreventativeWorkDone = [total:0, hours:0, minutes:0, percentage:0] |
---|
| 104 | result.totalWorkDone = [total:0, hours:0, minutes:0] |
---|
| 105 | |
---|
| 106 | result.taskList.each() { task -> |
---|
| 107 | task.entries.each() { entry -> |
---|
| 108 | // Has assets assigned and is Work Done. |
---|
| 109 | if( (task.primaryAsset || task.associatedAssets) && entry.entryType.id == 3L ) { |
---|
| 110 | if(task.taskType == namedParams.immediateCallout) |
---|
| 111 | result.immediateCalloutWorkDone.total += (entry.durationHour*60) + entry.durationMinute |
---|
| 112 | if(task.taskType == namedParams.unscheduledBreakin) |
---|
| 113 | result.unscheduledBreakinWorkDone.total += (entry.durationHour*60) + entry.durationMinute |
---|
| 114 | if(task.taskType == namedParams.preventativeMaintenance) |
---|
| 115 | result.preventativeMaintenanceWorkDone.total += (entry.durationHour*60) + entry.durationMinute |
---|
| 116 | } |
---|
| 117 | } // each() entry |
---|
| 118 | } // each() task |
---|
| 119 | |
---|
| 120 | // Work Done hours and minutes. |
---|
| 121 | result.immediateCalloutWorkDone.hours = (result.immediateCalloutWorkDone.total / 60).toInteger() |
---|
| 122 | result.immediateCalloutWorkDone.minutes = result.immediateCalloutWorkDone.total % 60 |
---|
| 123 | |
---|
| 124 | result.unscheduledBreakinWorkDone.hours = (result.unscheduledBreakinWorkDone.total / 60).toInteger() |
---|
| 125 | result.unscheduledBreakinWorkDone.minutes = result.unscheduledBreakinWorkDone.total % 60 |
---|
| 126 | |
---|
| 127 | result.preventativeMaintenanceWorkDone.hours = (result.preventativeMaintenanceWorkDone.total / 60).toInteger() |
---|
| 128 | result.preventativeMaintenanceWorkDone.minutes = result.preventativeMaintenanceWorkDone.total % 60 |
---|
| 129 | |
---|
| 130 | // Work Done Totals. |
---|
| 131 | result.totalPreventativeWorkDone.total = result.unscheduledBreakinWorkDone.total + result.preventativeMaintenanceWorkDone.total |
---|
| 132 | result.totalPreventativeWorkDone.hours = (result.totalPreventativeWorkDone.total / 60).toInteger() |
---|
| 133 | result.totalPreventativeWorkDone.minutes = result.totalPreventativeWorkDone.total % 60 |
---|
| 134 | |
---|
| 135 | result.totalWorkDone.total = result.immediateCalloutWorkDone.total + result.totalPreventativeWorkDone.total |
---|
| 136 | result.totalWorkDone.hours = (result.totalWorkDone.total / 60).toInteger() |
---|
| 137 | result.totalWorkDone.minutes = result.totalWorkDone.total % 60 |
---|
| 138 | |
---|
| 139 | // Work Done Percentages. |
---|
| 140 | try { |
---|
| 141 | result.immediateCalloutWorkDone.percentage = (result.immediateCalloutWorkDone.total / result.totalWorkDone.total)*100 |
---|
| 142 | result.totalPreventativeWorkDone.percentage = (result.totalPreventativeWorkDone.total / result.totalWorkDone.total)*100 |
---|
| 143 | } |
---|
| 144 | catch(ArithmeticException e) { |
---|
| 145 | log.error "Could not calculate: Work Done Percentages: "+e |
---|
| 146 | } |
---|
| 147 | |
---|
[533] | 148 | // Success. |
---|
| 149 | return result |
---|
| 150 | |
---|
[542] | 151 | } // getReactiveRatio |
---|
[533] | 152 | |
---|
[542] | 153 | /** |
---|
| 154 | * Selects and returns Immediate Callouts, grouped by Asset. |
---|
| 155 | * @param params The request params, may contain params to specify the search. |
---|
| 156 | * @param locale The locale to use when generating result.message. |
---|
| 157 | */ |
---|
| 158 | def getImmediateCallouts(params, locale) { |
---|
| 159 | def result = [:] |
---|
[533] | 160 | |
---|
[542] | 161 | def namedParams = [:] |
---|
| 162 | namedParams.startDate = params.startDate ?: dateUtilService.today |
---|
| 163 | namedParams.endDatePlusOne = (params.endDate ?: dateUtilService.today)+1 |
---|
| 164 | namedParams.immediateCallout = TaskType.read(1) |
---|
| 165 | |
---|
| 166 | result.taskQuery = "from Task as task \ |
---|
| 167 | where (task.trash = false \ |
---|
| 168 | and task.targetStartDate < :endDatePlusOne \ |
---|
| 169 | and task.targetStartDate >= :startDate \ |
---|
| 170 | and task.taskType = :immediateCallout \ |
---|
| 171 | ) \ |
---|
| 172 | )" |
---|
| 173 | |
---|
| 174 | result.taskQuery = "select distinct task " + result.taskQuery |
---|
[544] | 175 | result.taskQueryList = Task.executeQuery(result.taskQuery, namedParams) |
---|
| 176 | result.taskCount = result.taskQueryList.size() |
---|
[542] | 177 | |
---|
| 178 | // Assets on Tasks Count. |
---|
| 179 | result.totalAssetsOnTasksCount = 0 |
---|
[544] | 180 | result.totalDownTime = [total: 0, hours: 0, minutes:0] |
---|
[542] | 181 | result.assetList = [] |
---|
| 182 | |
---|
[544] | 183 | // Task Details |
---|
| 184 | result.taskList = [] |
---|
[542] | 185 | |
---|
[544] | 186 | // Add or update lists. |
---|
| 187 | def addToLists = { asset, task -> |
---|
| 188 | |
---|
[542] | 189 | def downTime = 0 |
---|
| 190 | def faultEntries = Entry.findAllByTaskAndEntryType(task, EntryType.read(1)) |
---|
[544] | 191 | def causeEntries = Entry.findAllByTaskAndEntryType(task, EntryType.read(2)) |
---|
| 192 | def workDoneEntries = Entry.findAllByTaskAndEntryType(task, EntryType.read(3)) |
---|
| 193 | def taskDetails = 'Task #'+task.id+' - '+task.description+'\n' |
---|
| 194 | faultEntries.each(){ |
---|
| 195 | taskDetails += ' Faults: '+it.comment+' - '+it.enteredBy+', '+g.formatDate(format: "EEE, dd-MMM-yyyy", date: it.dateDone)+'.\n' |
---|
| 196 | } |
---|
| 197 | causeEntries.each(){ |
---|
| 198 | taskDetails += ' Causes: '+it.comment+' - '+it.enteredBy+', '+g.formatDate(format: "EEE, dd-MMM-yyyy", date: it.dateDone)+'.\n' |
---|
| 199 | } |
---|
| 200 | workDoneEntries.each(){ |
---|
| 201 | taskDetails += ' Work Done: '+it.comment+' - '+it.enteredBy+', '+g.formatDate(format: "EEE, dd-MMM-yyyy", date: it.dateDone)+'.\n' |
---|
| 202 | } |
---|
| 203 | |
---|
[542] | 204 | faultEntries.each() { downTime += (it.durationHour*60 + it.durationMinute) } |
---|
[544] | 205 | result.totalDownTime.total += downTime |
---|
[542] | 206 | |
---|
| 207 | def assetDetails = result.assetList.find { it.id == asset.id } |
---|
| 208 | if(assetDetails) { |
---|
| 209 | assetDetails.immediateCalloutCount++ |
---|
[544] | 210 | assetDetails.downTime += downTime |
---|
| 211 | assetDetails.tasks += taskDetails |
---|
[542] | 212 | } |
---|
| 213 | else { |
---|
| 214 | assetDetails = [id: asset.id, |
---|
| 215 | name: asset.name, |
---|
| 216 | immediateCalloutCount: 1, |
---|
| 217 | downTime: downTime, |
---|
[544] | 218 | tasks: taskDetails] |
---|
[542] | 219 | |
---|
| 220 | result.assetList << assetDetails |
---|
| 221 | } |
---|
[544] | 222 | } // addAToLists |
---|
[542] | 223 | |
---|
| 224 | // Summary Of Calculations. |
---|
| 225 | result.summaryOfCalculationMethod = 'HQL query: \n\n' |
---|
| 226 | def tempStringArray = result.taskQuery.split(' ') |
---|
| 227 | tempStringArray.each() { |
---|
| 228 | if(it != '') result.summaryOfCalculationMethod += it +'\n' |
---|
| 229 | } |
---|
| 230 | result.summaryOfCalculationMethod += '\n'+'Calculations: '+'\n\n' |
---|
| 231 | |
---|
| 232 | result.summaryOfCalculationMethod += 'totalAssetsOnTasksCount = A count of unique assets on each task. \n' |
---|
[544] | 233 | result.taskQueryList.each() { task -> |
---|
[542] | 234 | if(task.primaryAsset) { |
---|
| 235 | result.totalAssetsOnTasksCount++ |
---|
[544] | 236 | addToLists(task.primaryAsset, task) |
---|
[542] | 237 | } |
---|
| 238 | task.associatedAssets.each() { associatedAsset -> |
---|
| 239 | if(associatedAsset.id != task.primaryAsset?.id) { |
---|
| 240 | result.totalAssetsOnTasksCount++ |
---|
[544] | 241 | addToLists(associatedAsset, task) |
---|
[542] | 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | } // each() task |
---|
| 246 | |
---|
[544] | 247 | // Sort assetList by callout count. |
---|
[542] | 248 | result.assetList.sort {a, b -> b.immediateCalloutCount.compareTo(a.immediateCalloutCount)} |
---|
| 249 | |
---|
[544] | 250 | // Calculate hours and minutes. |
---|
| 251 | result.totalDownTime.hours = (result.totalDownTime.total / 60).toInteger() |
---|
| 252 | result.totalDownTime.minutes = result.totalDownTime.total % 60 |
---|
| 253 | |
---|
[542] | 254 | // Success. |
---|
| 255 | return result |
---|
| 256 | |
---|
| 257 | } // getImmediateCallouts() |
---|
| 258 | |
---|
[585] | 259 | /** |
---|
| 260 | * Builds and returns a list of RichUiCalendarItem's. |
---|
| 261 | * @param params The request params, may contain params to specify the search. |
---|
| 262 | * @param locale The locale to use when generating result.message. |
---|
| 263 | */ |
---|
| 264 | def getWorkLoadSummary(params, locale) { |
---|
[542] | 265 | |
---|
[585] | 266 | def result = [:] |
---|
| 267 | result.displayList = [] |
---|
| 268 | |
---|
| 269 | def listItem |
---|
| 270 | |
---|
| 271 | def dayMap = [:] |
---|
| 272 | def assignedGroupMap = [:] |
---|
| 273 | |
---|
| 274 | params.taskInstanceList.each { task -> |
---|
| 275 | def dayKey = task.targetStartDate /// @todo: make this key more stable. |
---|
| 276 | def assignedTime = 0 |
---|
| 277 | |
---|
| 278 | task.assignedGroups.each{ assignedGroup -> |
---|
| 279 | assignedTime = assignedGroup.estimatedHour*60 + assignedGroup.estimatedMinute |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | if(dayMap.containsKey(dayKey)) { |
---|
| 283 | if(task.description) { |
---|
| 284 | dayMap[dayKey] = dayMap[dayKey] + assignedTime |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | else { |
---|
| 288 | dayMap[(dayKey)] = assignedTime |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | dayMap.each { k, v -> |
---|
| 293 | listItem = new RichUiCalendarItem(date:k, text:('assignedTime: '+v.toString())) |
---|
| 294 | result.displayList << listItem |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | // Success. |
---|
| 298 | return result |
---|
| 299 | |
---|
| 300 | } // getWorkLoadSummary() |
---|
| 301 | |
---|
[533] | 302 | } // end class |
---|