1 | import grails.orm.PagedResultList |
---|
2 | |
---|
3 | /** |
---|
4 | * Service class that encapsulates the business logic for Task searches. |
---|
5 | */ |
---|
6 | class TaskReportService { |
---|
7 | |
---|
8 | boolean transactional = false |
---|
9 | |
---|
10 | def authService |
---|
11 | def dateUtilService |
---|
12 | // def messageSource |
---|
13 | |
---|
14 | // def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() |
---|
15 | |
---|
16 | def paramsMax = 100000 |
---|
17 | |
---|
18 | /** |
---|
19 | * Selects and returns the reactive ratio. |
---|
20 | * @param params The request params, may contain param to specify the search. |
---|
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 |
---|
28 | namedParams.endDate = params.endDate ?: dateUtilService.today |
---|
29 | namedParams.endDate++ // Start of next day required. |
---|
30 | namedParams.immediateCallout = TaskType.read(1) |
---|
31 | namedParams.unscheduledBreakin = TaskType.read(2) |
---|
32 | namedParams.preventativeMaintenance = TaskType.read(4) |
---|
33 | namedParams.notStarted = TaskStatus.read(1) |
---|
34 | |
---|
35 | result.taskQuery = "from Task as task \ |
---|
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 | )" |
---|
46 | |
---|
47 | result.taskQuery = "select distinct task " + result.taskQuery |
---|
48 | result.taskList = Task.executeQuery(result.taskQuery, namedParams) |
---|
49 | result.taskCount = result.taskList.size() |
---|
50 | |
---|
51 | // Assets on Tasks Count. |
---|
52 | result.totalAssetsOnTasksCount = 0 |
---|
53 | result.immediateCalloutCount = 0 |
---|
54 | result.unscheduledBreakinCount = 0 |
---|
55 | result.preventativeMaintenanceCount = 0 |
---|
56 | |
---|
57 | result.summaryOfCalculationMethod = 'HQL query: \n\n' |
---|
58 | def tempStringArray = result.taskQuery.split(' ') |
---|
59 | tempStringArray.each() { |
---|
60 | if(it != '') result.summaryOfCalculationMethod += it +'\n' |
---|
61 | } |
---|
62 | result.summaryOfCalculationMethod += '\n'+'Calculations: '+'\n\n' |
---|
63 | |
---|
64 | result.summaryOfCalculationMethod += 'totalAssetsOnTasksCount = A count of unique assets on each task. \n' |
---|
65 | result.taskList.each() { task -> |
---|
66 | if(task.primaryAsset) { |
---|
67 | result.totalAssetsOnTasksCount++ |
---|
68 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
---|
69 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
---|
70 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
---|
71 | } |
---|
72 | task.associatedAssets.each() { associatedAsset -> |
---|
73 | if(associatedAsset.id != task.primaryAsset?.id) { |
---|
74 | result.totalAssetsOnTasksCount++ |
---|
75 | if(task.taskType == namedParams.immediateCallout) result.immediateCalloutCount++ |
---|
76 | if(task.taskType == namedParams.unscheduledBreakin) result.unscheduledBreakinCount++ |
---|
77 | if(task.taskType == namedParams.preventativeMaintenance) result.preventativeMaintenanceCount++ |
---|
78 | } |
---|
79 | } |
---|
80 | } // each() task |
---|
81 | |
---|
82 | // Percentage of counts. |
---|
83 | result.immediateCalloutPercentage = 0 |
---|
84 | result.totalPreventativePercentage = 0 |
---|
85 | |
---|
86 | result.summaryOfCalculationMethod += 'totalPreventativeCount = unscheduledBreakinCount + preventativeMaintenanceCount\n' |
---|
87 | result.totalPreventativeCount = result.unscheduledBreakinCount + result.preventativeMaintenanceCount |
---|
88 | try { |
---|
89 | result.summaryOfCalculationMethod += 'immediateCalloutPercentage = (immediateCalloutCount / totalAssetsOnTasksCount)*100 \n' |
---|
90 | result.summaryOfCalculationMethod += 'totalPreventativePercentage = (totalPreventativeCount / totalAssetsOnTasksCount)*100 \n' |
---|
91 | result.immediateCalloutPercentage = (result.immediateCalloutCount / result.totalAssetsOnTasksCount)*100 |
---|
92 | result.totalPreventativePercentage = (result.totalPreventativeCount / result.totalAssetsOnTasksCount)*100 |
---|
93 | } |
---|
94 | catch(ArithmeticException e) { |
---|
95 | log.error "Could not calculate: Assets on Tasks Percentages: "+e |
---|
96 | } |
---|
97 | |
---|
98 | // Work Done. |
---|
99 | result.immediateCalloutWorkDone = [total:0, hours:0, minutes:0, percentage:0] |
---|
100 | result.unscheduledBreakinWorkDone = [total:0, hours:0, minutes:0] |
---|
101 | result.preventativeMaintenanceWorkDone = [total:0, hours:0, minutes:0] |
---|
102 | result.totalPreventativeWorkDone = [total:0, hours:0, minutes:0, percentage:0] |
---|
103 | result.totalWorkDone = [total:0, hours:0, minutes:0] |
---|
104 | |
---|
105 | result.taskList.each() { task -> |
---|
106 | task.entries.each() { entry -> |
---|
107 | // Has assets assigned and is Work Done. |
---|
108 | if( (task.primaryAsset || task.associatedAssets) && entry.entryType.id == 3L ) { |
---|
109 | if(task.taskType == namedParams.immediateCallout) |
---|
110 | result.immediateCalloutWorkDone.total += (entry.durationHour*60) + entry.durationMinute |
---|
111 | if(task.taskType == namedParams.unscheduledBreakin) |
---|
112 | result.unscheduledBreakinWorkDone.total += (entry.durationHour*60) + entry.durationMinute |
---|
113 | if(task.taskType == namedParams.preventativeMaintenance) |
---|
114 | result.preventativeMaintenanceWorkDone.total += (entry.durationHour*60) + entry.durationMinute |
---|
115 | } |
---|
116 | } // each() entry |
---|
117 | } // each() task |
---|
118 | |
---|
119 | // Work Done hours and minutes. |
---|
120 | result.immediateCalloutWorkDone.hours = (result.immediateCalloutWorkDone.total / 60).toInteger() |
---|
121 | result.immediateCalloutWorkDone.minutes = result.immediateCalloutWorkDone.total % 60 |
---|
122 | |
---|
123 | result.unscheduledBreakinWorkDone.hours = (result.unscheduledBreakinWorkDone.total / 60).toInteger() |
---|
124 | result.unscheduledBreakinWorkDone.minutes = result.unscheduledBreakinWorkDone.total % 60 |
---|
125 | |
---|
126 | result.preventativeMaintenanceWorkDone.hours = (result.preventativeMaintenanceWorkDone.total / 60).toInteger() |
---|
127 | result.preventativeMaintenanceWorkDone.minutes = result.preventativeMaintenanceWorkDone.total % 60 |
---|
128 | |
---|
129 | // Work Done Totals. |
---|
130 | result.totalPreventativeWorkDone.total = result.unscheduledBreakinWorkDone.total + result.preventativeMaintenanceWorkDone.total |
---|
131 | result.totalPreventativeWorkDone.hours = (result.totalPreventativeWorkDone.total / 60).toInteger() |
---|
132 | result.totalPreventativeWorkDone.minutes = result.totalPreventativeWorkDone.total % 60 |
---|
133 | |
---|
134 | result.totalWorkDone.total = result.immediateCalloutWorkDone.total + result.totalPreventativeWorkDone.total |
---|
135 | result.totalWorkDone.hours = (result.totalWorkDone.total / 60).toInteger() |
---|
136 | result.totalWorkDone.minutes = result.totalWorkDone.total % 60 |
---|
137 | |
---|
138 | // Work Done Percentages. |
---|
139 | try { |
---|
140 | result.immediateCalloutWorkDone.percentage = (result.immediateCalloutWorkDone.total / result.totalWorkDone.total)*100 |
---|
141 | result.totalPreventativeWorkDone.percentage = (result.totalPreventativeWorkDone.total / result.totalWorkDone.total)*100 |
---|
142 | } |
---|
143 | catch(ArithmeticException e) { |
---|
144 | log.error "Could not calculate: Work Done Percentages: "+e |
---|
145 | } |
---|
146 | |
---|
147 | // Success. |
---|
148 | return result |
---|
149 | |
---|
150 | } // getQuickSearch |
---|
151 | |
---|
152 | |
---|
153 | } // end class |
---|