| 1 | |
|---|
| 2 | /** |
|---|
| 3 | * Service class that encapsulates the business logic for Inventory Reports. |
|---|
| 4 | */ |
|---|
| 5 | class InventoryReportService { |
|---|
| 6 | |
|---|
| 7 | boolean transactional = false |
|---|
| 8 | |
|---|
| 9 | // def authService |
|---|
| 10 | // def dateUtilService |
|---|
| 11 | // def messageSource |
|---|
| 12 | |
|---|
| 13 | def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib() |
|---|
| 14 | |
|---|
| 15 | // def paramsMax = 100000 |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Get the data for the inventory stock take overiew report. |
|---|
| 19 | * @param params The request params, may contain params to specify the search. |
|---|
| 20 | * @param locale The locale to use when generating result.message. |
|---|
| 21 | */ |
|---|
| 22 | def getStockTakeOverview(params, locale) { |
|---|
| 23 | def result = [:] |
|---|
| 24 | |
|---|
| 25 | result.summaryOfCalculationMethod = 'This report should be used in conjunction with the `Stock Take (By Location)` Report.' |
|---|
| 26 | |
|---|
| 27 | def namedParams = [:] |
|---|
| 28 | |
|---|
| 29 | result.query = "from InventoryLocation as inventoryLocation \ |
|---|
| 30 | left join inventoryLocation.inventoryStore as inventoryStore \ |
|---|
| 31 | where (inventoryLocation.isActive = true \ |
|---|
| 32 | ) \ |
|---|
| 33 | order by inventoryStore.name, inventoryLocation.name" |
|---|
| 34 | |
|---|
| 35 | result.query = "select new Map(inventoryLocation.name as location, inventoryStore.name as store) " + result.query |
|---|
| 36 | result.queryResult = InventoryLocation.executeQuery(result.query, namedParams) |
|---|
| 37 | result.inventoryLocationCount = result.queryResult.size() |
|---|
| 38 | |
|---|
| 39 | result.inventoryLocationList = result.queryResult |
|---|
| 40 | |
|---|
| 41 | // Success. |
|---|
| 42 | return result |
|---|
| 43 | |
|---|
| 44 | } // getStockTakeOverview() |
|---|
| 45 | |
|---|
| 46 | /** |
|---|
| 47 | * Get the data for the inventory stock take by location report. |
|---|
| 48 | * @param params The request params, may contain params to specify the search. |
|---|
| 49 | * @param locale The locale to use when generating result.message. |
|---|
| 50 | */ |
|---|
| 51 | def getStockTakeByLocation(params, locale) { |
|---|
| 52 | def result = [:] |
|---|
| 53 | |
|---|
| 54 | result.summaryOfCalculationMethod = 'This report should be used in conjunction with the `Stock Take (Overview)` Report.' |
|---|
| 55 | |
|---|
| 56 | // Sanitise the locations string and convert to a list. |
|---|
| 57 | result.locations = params.locationString.trim() |
|---|
| 58 | if(result.locations.startsWith('e.g:')) |
|---|
| 59 | result.locations = result.locations.split(':')[-1].trim() |
|---|
| 60 | result.locations = result.locations.split(',') |
|---|
| 61 | result.locations = result.locations.collect {it.trim()} |
|---|
| 62 | |
|---|
| 63 | def namedParams = [:] |
|---|
| 64 | namedParams.locationList = [null] // null protects against HQL unexpected end of subtree exception with an empty list. |
|---|
| 65 | |
|---|
| 66 | // Fill namedParams.locationList |
|---|
| 67 | result.locations.each() { |
|---|
| 68 | InventoryLocation.findAllByNameIlike(it).each() { |
|---|
| 69 | namedParams.locationList << it |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | // Note: HQL docs advise not using fetch aliases in where clause (or any other clause). |
|---|
| 74 | // Access is via the parent object, however that does not work for the order by clause in this case. |
|---|
| 75 | result.query = "from InventoryItem as inventoryItem \ |
|---|
| 76 | left join fetch inventoryItem.unitOfMeasure as unitOfMeasure \ |
|---|
| 77 | left join fetch inventoryItem.inventoryLocation as inventoryLocation \ |
|---|
| 78 | left join fetch inventoryLocation.inventoryStore as inventoryStore \ |
|---|
| 79 | left join fetch inventoryItem.picture as picture \ |
|---|
| 80 | left join fetch picture.images as Image \ |
|---|
| 81 | where (inventoryItem.isActive = true \ |
|---|
| 82 | and inventoryItem.inventoryLocation in (:locationList) \ |
|---|
| 83 | ) \ |
|---|
| 84 | order by inventoryStore.name, inventoryLocation.name" |
|---|
| 85 | |
|---|
| 86 | result.query = "select distinct inventoryItem " + result.query |
|---|
| 87 | result.queryResult = InventoryLocation.executeQuery(result.query, namedParams) |
|---|
| 88 | result.inventoryItemCount = result.queryResult.size() |
|---|
| 89 | |
|---|
| 90 | // Return the actual locations as a string. |
|---|
| 91 | if(namedParams.locationList.size() > 1) |
|---|
| 92 | result.locations = namedParams.locationList[1..-1].toString()[1..-2] |
|---|
| 93 | else |
|---|
| 94 | result.locations = g.message(code: 'default.none.text') |
|---|
| 95 | |
|---|
| 96 | result.inventoryItemList = result.queryResult |
|---|
| 97 | |
|---|
| 98 | // Success. |
|---|
| 99 | return result |
|---|
| 100 | |
|---|
| 101 | } // getStockTakeOverview() |
|---|
| 102 | |
|---|
| 103 | } // end class |
|---|