[489] | 1 | import grails.orm.PagedResultList |
---|
| 2 | |
---|
[485] | 3 | /** |
---|
| 4 | * Service class that encapsulates the business logic for InventoryItem searches. |
---|
| 5 | */ |
---|
| 6 | class InventoryItemSearchService { |
---|
| 7 | |
---|
| 8 | boolean transactional = false |
---|
| 9 | |
---|
[489] | 10 | def dateUtilService |
---|
[485] | 11 | def messageSource |
---|
| 12 | |
---|
| 13 | def paramsMax = 100000 |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * Selects and returns the correct search results based on the supplied quickSearch. |
---|
| 17 | * @param params The request params, may contain params.quickSearch string to specify the search. |
---|
| 18 | * @param locale The locale to use when generating result.message. |
---|
| 19 | */ |
---|
| 20 | def getQuickSearch(params, locale) { |
---|
| 21 | def result = [:] |
---|
| 22 | result.quickSearch = params.quickSearch ?: "all" |
---|
| 23 | |
---|
| 24 | def getMessage = { Map m -> |
---|
| 25 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | switch (result.quickSearch) { |
---|
| 29 | case "inventoryBelowReorder": |
---|
| 30 | result.inventoryItemList = getInventoryBelowReorder(params) |
---|
| 31 | if(result.inventoryItemList.totalCount > 0) |
---|
| 32 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.description") |
---|
| 33 | else |
---|
| 34 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
| 35 | break |
---|
| 36 | case "inventoryBelowReorderAll": |
---|
| 37 | result.inventoryItemList = getInventoryBelowReorder(params, false) |
---|
| 38 | if(result.inventoryItemList.totalCount > 0) |
---|
| 39 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.all.description") |
---|
| 40 | else |
---|
| 41 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
| 42 | break |
---|
[489] | 43 | case "recentlyUsed": |
---|
| 44 | result.daysBack = params.daysBack?.isInteger() ? params.daysBack.toInteger() : 14 |
---|
| 45 | result.inventoryItemList = getRecentlyUsed(params, result.daysBack) |
---|
| 46 | if(result.inventoryItemList.totalCount > 0) |
---|
| 47 | result.message = getMessage(code:"inventoryItem.search.text.recently.used.description", args:[result.daysBack]) |
---|
| 48 | else |
---|
| 49 | result.message = getMessage(code:"inventoryItem.search.text.recently.used.none.found", args:[result.daysBack]) |
---|
| 50 | break |
---|
[485] | 51 | default: |
---|
| 52 | result.inventoryItemList = getAll(params) |
---|
| 53 | if(result.inventoryItemList.totalCount > 0) |
---|
| 54 | result.message = getMessage(code:"inventoryItem.search.text.all.description") |
---|
| 55 | else |
---|
| 56 | result.message = getMessage(code:"inventoryItem.search.text.all.none.found") |
---|
| 57 | break |
---|
| 58 | } // switch. |
---|
| 59 | |
---|
| 60 | // Success. |
---|
| 61 | return result |
---|
| 62 | |
---|
| 63 | } // getQuickSearch |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Get all inventory items. |
---|
| 67 | * @param params The request params. |
---|
| 68 | */ |
---|
| 69 | def getAll(params) { |
---|
| 70 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
| 71 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
| 72 | params.sort = params?.sort ?: "name" |
---|
| 73 | params.order = params?.order ?: "asc" |
---|
| 74 | |
---|
| 75 | def inventoryItemList = InventoryItem.createCriteria().list( |
---|
| 76 | max: params.max, |
---|
| 77 | offset: params.offset, |
---|
| 78 | sort: params.sort, |
---|
| 79 | order: params.order) { |
---|
| 80 | } // createCriteria |
---|
| 81 | } // getAll |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * List inventory items that are below reorder point. |
---|
| 85 | * @param params The request params. |
---|
| 86 | * @param onlyReorderEnabled Only include items with reorder enabled, defaults to true. |
---|
| 87 | */ |
---|
| 88 | def getInventoryBelowReorder(params, onlyReorderEnabled=true) { |
---|
| 89 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
| 90 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
| 91 | params.sort = params?.sort ?: "name" |
---|
| 92 | params.order = params?.order ?: "asc" |
---|
| 93 | |
---|
| 94 | def inventoryItemList = InventoryItem.createCriteria().list( |
---|
| 95 | max: params.max, |
---|
| 96 | offset: params.offset, |
---|
| 97 | sort: params.sort, |
---|
| 98 | order: params.order) { |
---|
| 99 | eq("isActive", true) |
---|
| 100 | if(onlyReorderEnabled) |
---|
| 101 | eq("enableReorder", true) |
---|
| 102 | leProperty("unitsInStock", "reorderPoint") |
---|
| 103 | } // createCriteria |
---|
| 104 | } // getInventoryBelowReorder |
---|
| 105 | |
---|
[489] | 106 | /** |
---|
| 107 | * Get a list of recently used inventory items. |
---|
| 108 | * @param params The request params. |
---|
| 109 | * @param daysBack The number of days back to get results for. |
---|
| 110 | */ |
---|
| 111 | def getRecentlyUsed(params, daysBack) { |
---|
| 112 | def paginateParams = [:] |
---|
| 113 | paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
| 114 | paginateParams.offset = params?.offset?.toInteger() ?: 0 |
---|
| 115 | |
---|
| 116 | def sort = "inventoryItem." + (params?.sort ?: "name") |
---|
| 117 | def order = params?.order == "desc" ? "desc" : "asc" |
---|
| 118 | def orderBy = " order by " + sort + ' ' + order |
---|
| 119 | |
---|
| 120 | def namedParams = [:] |
---|
| 121 | namedParams.startOfDay = dateUtilService.today - daysBack |
---|
| 122 | |
---|
| 123 | def baseQuery = "from InventoryItem as inventoryItem \ |
---|
| 124 | left join inventoryItem.inventoryMovements as inventoryMovement \ |
---|
| 125 | where (inventoryItem.isActive = true \ |
---|
| 126 | and inventoryMovement.date > :startOfDay \ |
---|
| 127 | and inventoryMovement.inventoryMovementType = 1 \ |
---|
| 128 | )" |
---|
| 129 | |
---|
| 130 | def searchQuery = "select distinct inventoryItem " + baseQuery + orderBy |
---|
| 131 | def list = InventoryItem.executeQuery(searchQuery, namedParams, paginateParams) |
---|
| 132 | |
---|
| 133 | def countQuery = "select count(distinct inventoryItem) as inventoryItemCount " + baseQuery |
---|
| 134 | def totalCount = InventoryItem.executeQuery(countQuery, namedParams)[0].toInteger() |
---|
| 135 | |
---|
| 136 | def inventoryItemInstanceList = new PagedResultList(list, totalCount) |
---|
| 137 | return inventoryItemInstanceList |
---|
| 138 | } // getRecentlyUsed |
---|
| 139 | |
---|
[485] | 140 | } // end class |
---|