-
-
Notifications
You must be signed in to change notification settings - Fork 400
/
DashboardService.groovy
536 lines (469 loc) · 24.4 KB
/
DashboardService.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/**
* Copyright (c) 2012 Partners In Health. All rights reserved.
* The use and distribution terms for this software are covered by the
* Eclipse Public License 1.0 (http:https://opensource.org/licenses/eclipse-1.0.php)
* which can be found in the file epl-v10.html at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
package org.pih.warehouse.user
import grails.converters.JSON
import groovy.sql.Sql
import org.hibernate.criterion.CriteriaSpecification
import org.pih.warehouse.core.Location
import org.pih.warehouse.core.User
import org.pih.warehouse.inventory.InventoryItemSummary
import org.pih.warehouse.inventory.Transaction
import org.pih.warehouse.inventory.TransactionEntry
import org.pih.warehouse.product.Product
import org.pih.warehouse.requisition.Requisition
import org.pih.warehouse.requisition.RequisitionItem
import org.pih.warehouse.shipping.Shipment
import org.pih.warehouse.util.LocalizationUtil
class DashboardService {
def dataSource
def grailsApplication
boolean transactional = false
/**
* Get the recent activities for the given location to be displayed on the dashboard.
*
* @param location
* @param daysToInclude
* @return
*/
def getRecentActivities(Location location, int daysToInclude) {
def activityList = []
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def format = grailsApplication.mainContext.getBean('org.pih.warehouse.FormatTagLib')
def warehouse = grailsApplication.mainContext.getBean('org.pih.warehouse.MessageTagLib')
// Find recent requisition activity
def requisitions = Requisition.executeQuery("""select distinct r from Requisition r where (r.isTemplate = false or r.isTemplate is null) and r.lastUpdated >= :lastUpdated and (r.origin = :origin or r.destination = :destination)""",
['lastUpdated':new Date()-daysToInclude, 'origin':location, 'destination': location])
requisitions.each {
def link = "${g.createLink(controller: 'requisition', action: 'show', id: it.id)}"
def user = (it.dateCreated == it.lastUpdated) ? it?.createdBy : it?.updatedBy
def activityType = (it.dateCreated == it.lastUpdated) ? "dashboard.activity.created.label" : "dashboard.activity.updated.label"
def username = user?.name ?: "${warehouse.message(code: 'default.nobody.label', default: 'nobody')}"
activityType = "${warehouse.message(code: activityType)}"
activityList << [
type: "basket",
label: "${warehouse.message(code:'dashboard.activity.requisition.label', args: [link, it.name, activityType, username])}",
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/basket.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
// Add recent shipments
def shipments = Shipment.executeQuery( "select distinct s from Shipment s where s.lastUpdated >= :lastUpdated and \
(s.origin = :origin or s.destination = :destination)", ['lastUpdated':new Date()-daysToInclude, 'origin':location, 'destination':location] );
shipments.each {
def link = "${g.createLink(controller: 'shipment', action: 'showDetails', id: it.id)}"
def activityType = (it.dateCreated == it.lastUpdated) ? "dashboard.activity.created.label" : "dashboard.activity.updated.label"
activityType = "${warehouse.message(code: activityType)}"
activityList << [
type: "lorry",
label: "${warehouse.message(code:'dashboard.activity.shipment.label', args: [link, it.name, activityType])}",
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/lorry.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
//order by e.createdDate desc
//[max:params.max.toInteger(), offset:params.offset.toInteger ()]
def shippedShipments = Shipment.executeQuery("SELECT s FROM Shipment s JOIN s.events e WHERE e.eventDate >= :eventDate and e.eventType.eventCode = 'SHIPPED'", ['eventDate':new Date()-daysToInclude])
shippedShipments.each {
def link = "${g.createLink(controller: 'shipment', action: 'showDetails', id: it.id)}"
def activityType = "dashboard.activity.shipped.label"
activityType = "${warehouse.message(code: activityType, args: [link, it.name, activityType, it.destination.name])}"
activityList << [
type: "lorry_go",
label: activityType,
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/lorry_go.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
def receivedShipment = Shipment.executeQuery("SELECT s FROM Shipment s JOIN s.events e WHERE e.eventDate >= :eventDate and e.eventType.eventCode = 'RECEIVED'", ['eventDate':new Date()-daysToInclude])
receivedShipment.each {
def link = "${g.createLink(controller: 'shipment', action: 'showDetails', id: it.id)}"
def activityType = "dashboard.activity.received.label"
activityType = "${warehouse.message(code: activityType, args: [link, it.name, activityType, it.origin.name])}"
activityList << [
type: "lorry_stop",
label: activityType,
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/lorry_stop.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
def products = Product.executeQuery( "select distinct p from Product p where p.lastUpdated >= :lastUpdated", ['lastUpdated':new Date()-daysToInclude] );
products.each {
def link = "${g.createLink(controller: 'inventoryItem', action: 'showStockCard', params:['product.id': it.id])}"
def user = (it.dateCreated == it.lastUpdated) ? it?.createdBy : it.updatedBy
def activityType = (it.dateCreated == it.lastUpdated) ? "dashboard.activity.created.label" : "dashboard.activity.updated.label"
activityType = "${warehouse.message(code: activityType)}"
def username = user?.name ?: "${warehouse.message(code: 'default.nobody.label', default: 'nobody')}"
activityList << [
type: "package",
label: "${warehouse.message(code:'dashboard.activity.product.label', args: [link, it.name, activityType, username])}",
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/package.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
// If the current location has an inventory, add recent transactions associated with that location to the activity list
if (location?.inventory) {
def transactions = Transaction.executeQuery("select distinct t from Transaction t where t.lastUpdated >= :lastUpdated and \
t.inventory = :inventory", ['lastUpdated':new Date()-daysToInclude, 'inventory':location?.inventory] );
transactions.each {
def link = "${g.createLink(controller: 'inventory', action: 'showTransaction', id: it.id)}"
def user = (it.dateCreated == it.lastUpdated) ? it?.createdBy : it?.updatedBy
def activityType = (it.dateCreated == it.lastUpdated) ? "dashboard.activity.created.label" : "dashboard.activity.updated.label"
activityType = "${warehouse.message(code: activityType)}"
def label = LocalizationUtil.getLocalizedString(it)
def username = user?.name ?: "${warehouse.message(code: 'default.nobody.label', default: 'nobody')}"
activityList << [
type: "arrow_switch_bluegreen",
label: "${warehouse.message(code:'dashboard.activity.transaction.label', args: [link, label, activityType, username])}",
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/arrow_switch_bluegreen.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
}
def users = User.executeQuery( "select distinct u from User u where u.lastUpdated >= :lastUpdated", ['lastUpdated':new Date()-daysToInclude], [max: 10] );
users.each {
def link = "${g.createLink(controller: 'user', action: 'show', id: it.id)}"
def activityType = (it.dateCreated == it.lastUpdated) ? "dashboard.activity.created.label" : "dashboard.activity.updated.label"
if (it.lastUpdated == it.lastLoginDate) {
activityType = "dashboard.activity.loggedIn.label"
}
activityType = "${warehouse.message(code: activityType)}"
activityList << [
type: "user",
label: "${warehouse.message(code:'dashboard.activity.user.label', args: [link, it.name, activityType])}",
url: link,
date: "${format.date(obj:it.lastUpdated,format:'MMM d hh:mma')}",
thumbnailUrl: "${g.createLinkTo(dir:'images/icons/silk/user.png')}",
dateCreated: it.dateCreated,
lastUpdated: it.lastUpdated]
}
activityList = activityList.sort { it.lastUpdated }.reverse()
return activityList
}
def getProductSummary(Location location) {
long startTime = System.currentTimeMillis()
Sql sql = new Sql(dataSource)
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def results = sql.rows(productAggregationQuery, [locationId:location.id]);
def productSummary = results.collect {
def status = it.status ?: ""
def styleClass = (!it.status_code && !it.status) ? "total" : (!it.status) ? "subtotal" : "data"
def labelCode = (!it.status_code && !it.status) ? "total" : (!it.status) ? "subtotal" : it.status
labelCode = "dashboard.${labelCode}.label"
def cost = "${g.formatNumber(number: it.cost, type: 'currency')}"
def url = "${g.createLink(uri: '/inventory/productSummary/' + status)}"
def label = "${g.message(code: labelCode)}"
[label: label, code: it.status_code, status: status, count: it.count, cost: cost, url: url, styleClass: styleClass]
}
//dashboardAlerts = dashboardAlerts.groupBy { it.status }
//if (sql) sql.close();
log.info "getProductSummary(): " + (System.currentTimeMillis() - startTime) + " ms"
return productSummary
}
def productDetailsQuery = """
SELECT
product.id as id,
product.product_code as product_code,
product.name as product_name,
category.name as category_name,
product.price_per_unit as price_per_unit,
product.unit_of_measure as unit_of_measure,
location.name as location_name,
greatest(sum(quantity), 0) as quantity,
ifnull(inventory_level.min_quantity, 0) as min_quantity,
ifnull(inventory_level.reorder_quantity, 0) as reorder_quantity,
ifnull(inventory_level.max_quantity, 0) as max_quantity
FROM inventory_item_summary
JOIN product on inventory_item_summary.product_id = product.id
JOIN category on product.category_id = category.id
JOIN location on inventory_item_summary.location_id = location.id
JOIN inventory on inventory.id = location.inventory_id
LEFT OUTER JOIN inventory_level on inventory_level.product_id = product.id AND inventory_level.inventory_id = inventory.id
WHERE location.id = :locationId
GROUP BY product.id, location.id
"""
def productSummaryQuery = """
SELECT *,
(case when quantity > max_quantity then 'a_normal'
when quantity <= max_quantity and quantity > reorder_quantity then 'a_normal'
when quantity <= reorder_quantity and quantity > min_quantity then 'b_warning'
when quantity <= min_quantity and quantity > 0 then 'b_warning'
when quantity <= 0 then 'c_danger'
end) as status_code
,
(case when quantity > max_quantity then 'overstock'
when quantity <= max_quantity and quantity > reorder_quantity then 'healthy'
when quantity <= reorder_quantity and quantity > min_quantity then 'reorder'
when quantity <= min_quantity and quantity > 0 then 'low'
when quantity <= 0 then 'outofstock'
end) as status
,
(case when quantity > max_quantity then '0'
when quantity <= max_quantity and quantity > reorder_quantity then '1'
when quantity <= reorder_quantity and quantity > min_quantity then '2'
when quantity <= min_quantity and quantity > 0 then '3'
when quantity <= 0 then '4'
end) as sort_order
,
(quantity * price_per_unit) as total_cost
FROM (
${productDetailsQuery}
) as product_details
"""
def productAggregationQuery = """
SELECT status_code, status, count(*) as count, ceiling(sum(total_cost)) as cost
FROM (
${productSummaryQuery}
) as product_summary
GROUP BY status_code, status WITH ROLLUP
"""
/**
*
* @param location
* @return
*/
def getProductDetails(Location location, String status) {
def dashboardAlerts = []
long startTime = System.currentTimeMillis()
Sql sql = new Sql(dataSource)
def query = productSummaryQuery
if (status) {
query += " having status = :status"
}
def results = sql.rows(query, [locationId:location.id, status: status]);
def productDetailsList = results.collect { [
id: it.id,
status: it.status,
statusCode: it.status_code,
productCode: it.product_code,
productName: it.product_name,
unitOfMeasure: it.unit_of_measure,
genericProductName: "",
categoryName: it.category_name,
manufacturer: "",
manufacturerCode: "",
vendor: "",
vendorCode: "",
binLocation: "",
abcClass: "",
quantity: it.quantity,
pricePerUnit: it.price_per_unit,
totalCost: it.total_cost,
minQuantity: 0,
reorderQuantity: 0,
maxQuantity: 0
] }
log.info "quantityMap(): " + (System.currentTimeMillis() - startTime) + " ms"
return productDetailsList
}
String expirationDetailsQuery = """
SELECT
product.id as product_id,
product.product_code as product_code,
product.name as product_name,
category.name as category_name,
inventory_item.lot_number as lot_number,
inventory_item.expiration_date as expiration_date,
datediff(inventory_item.expiration_date, now()) as days_until_expiry,
product.price_per_unit as price_per_unit,
product.unit_of_measure as unit_of_measure,
location.id as location_id,
location.name as location_name,
greatest(sum(quantity), 0) as quantity,
ifnull(inventory_level.min_quantity, 0) as min_quantity,
ifnull(inventory_level.reorder_quantity, 0) as reorder_quantity,
ifnull(inventory_level.max_quantity, 0) as max_quantity
FROM inventory_item_summary
JOIN inventory_item on inventory_item_summary.inventory_item_id = inventory_item.id
JOIN product on inventory_item_summary.product_id = product.id
JOIN category on product.category_id = category.id
JOIN location on inventory_item_summary.location_id = location.id
JOIN inventory on inventory.id = location.inventory_id
LEFT OUTER JOIN inventory_level on inventory_level.product_id = product.id AND inventory_level.inventory_id = inventory.id
WHERE location.id = :locationId
AND quantity > 0
GROUP BY inventory_item.id, product.id, location.id
"""
final expirationSummaryQuery = """
SELECT *,
(case
when expiration_date is null then 'a_normal'
when days_until_expiry <= 0 then 'c_danger'
when days_until_expiry <= 30 then 'b_warning'
when days_until_expiry <= 60 then 'b_warning'
when days_until_expiry <= 90 then 'b_warning'
when days_until_expiry <= 180 then 'b_warning'
when days_until_expiry <= 365 then 'a_normal'
when days_until_expiry > 365 then 'a_normal'
end) as status_code,
(case
when expiration_date is null then 'does_not_expire'
when days_until_expiry <= 0 then 'expired'
when days_until_expiry <= 30 then 'within_one_month'
when days_until_expiry <= 60 then 'within_two_months'
when days_until_expiry <= 90 then 'within_three_months'
when days_until_expiry <= 180 then 'within_six_months'
when days_until_expiry <= 365 then 'within_year'
when days_until_expiry > 365 then 'over_year'
end) as status,
(quantity * price_per_unit) as total_cost
FROM (
${expirationDetailsQuery}
) as expiration_summary
"""
def expirationAggregationQuery = """
SELECT status_code, status, count(*) as count, ceiling(sum(total_cost)) as cost
FROM (
${expirationSummaryQuery}
) as expiration_summary
GROUP BY status_code, status WITH ROLLUP
"""
def getExpirationSummary(Location location) {
long startTime = System.currentTimeMillis()
Sql sql = new Sql(dataSource)
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def results = sql.rows(expirationAggregationQuery, [locationId:location.id]);
def expirationSummary = results.collect {
def status = it.status ?: ""
def styleClass = (!it.status_code && !it.status) ? "total" : (!it.status) ? "subtotal" : "data"
def labelCode = (!it.status_code && !it.status) ? "total" : (!it.status) ? "subtotal" : it.status
labelCode = "dashboard.${labelCode}.label"
def cost = "${g.formatNumber(number: it.cost, type: 'currency')}"
def url = "${g.createLink(uri: '/inventory/inventoryItemSummary/' + status)}"
//def label = ${g.message(code:'')}
def label = "${g.message(code: labelCode)}"
[label: label, code: it.status_code, status: status, count: it.count, cost: cost, url: url, styleClass: styleClass]
}
log.info "getExpirationSummary(): " + (System.currentTimeMillis() - startTime) + " ms"
return expirationSummary
}
def getExpirationDetails(Location location, String status) {
long startTime = System.currentTimeMillis()
Sql sql = new Sql(dataSource)
def query = expirationSummaryQuery
if (status) {
query += " having status = :status"
}
log.info "Query: " + query
def results = sql.rows(query, [locationId:location.id, status: status]);
def productDetailsList = results.collect { [
status: it.status,
statusCode: it.status_code,
productCode: it.product_code,
productName: it.product_name,
unitOfMeasure: it.unit_of_measure,
lotNumber: it.lot_number,
expirationDate: it.expiration_date,
genericProductName: "",
categoryName: it.category_name,
manufacturer: "",
manufacturerCode: "",
vendor: "",
vendorCode: "",
binLocation: "",
abcClass: "",
quantity: it.quantity,
pricePerUnit: it.price_per_unit,
totalCost: it.total_cost,
minQuantity: 0,
reorderQuantity: 0,
maxQuantity: 0
] }
log.info "getExpirationDetails(): " + (System.currentTimeMillis() - startTime) + " ms"
return productDetailsList
}
/**
* Returns a list of
* @param currentLocation
* @return
*/
def getQuantityByLocation(Location currentLocation) {
def results = InventoryItemSummary.createCriteria().list {
//maxResults 5
resultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP)
projections {
sum('quantity', 'quantity')
groupProperty('product.id', 'productId')
}
location {
eq 'id', currentLocation.id
}
}
// Transform result set to map of quantities indexed by product ID
def map = results.inject([:]) { map, entry ->
map[entry.productId] = entry.quantity
return map
}
return map
}
/**
* Get fast moving items based on requisition data.
*
* @param location
* @param date
* @param max
* @return
*/
def getFastMovers(location, date, max) {
log.info "Get fast movers for location ${location}"
def startTime = System.currentTimeMillis()
def data = [:]
try {
data.location = location.id
data.startDate = date-30
data.endDate = date
def criteria = RequisitionItem.createCriteria()
def results = criteria.list {
requisition {
eq("destination", location)
between("dateRequested", date-30, date)
}
projections {
groupProperty("product")
countDistinct('id', "occurrences")
sum("quantity", "quantity")
}
order('occurrences','desc')
order('quantity','desc')
if (max) { maxResults(max) }
}
//log.info "Results: " + results
def quantityMap = getQuantityByLocation(location)
def count = 1;
data.results = results.collect {[
rank: count++,
id: it[0].id,
productCode: it[0].productCode,
name: it[0].name,
category: it[0]?.category?.name?:"",
requisitionCount: it[1],
quantityRequested: it[2],
quantityOnHand: (quantityMap[it[0].id]?:0),
]
}
data.responseTime = (System.currentTimeMillis() - startTime) + " ms"
} catch (Exception e) {
log.error("Error occurred while getting requisition items " + e.message, e)
data.message = e.message
}
return data
}
}