1 | /** |
---|
2 | * Service class that encapsulates the business logic for InventoryItem searches. |
---|
3 | */ |
---|
4 | class InventoryItemSearchService { |
---|
5 | |
---|
6 | boolean transactional = false |
---|
7 | |
---|
8 | def messageSource |
---|
9 | |
---|
10 | def paramsMax = 100000 |
---|
11 | |
---|
12 | /** |
---|
13 | * Selects and returns the correct search results based on the supplied quickSearch. |
---|
14 | * @param params The request params, may contain params.quickSearch string to specify the search. |
---|
15 | * @param locale The locale to use when generating result.message. |
---|
16 | */ |
---|
17 | def getQuickSearch(params, locale) { |
---|
18 | def result = [:] |
---|
19 | result.quickSearch = params.quickSearch ?: "all" |
---|
20 | |
---|
21 | def getMessage = { Map m -> |
---|
22 | messageSource.getMessage(m.code, m.args == null ? null : m.args.toArray(), locale) |
---|
23 | } |
---|
24 | |
---|
25 | switch (result.quickSearch) { |
---|
26 | case "inventoryBelowReorder": |
---|
27 | result.inventoryItemList = getInventoryBelowReorder(params) |
---|
28 | if(result.inventoryItemList.totalCount > 0) |
---|
29 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.description") |
---|
30 | else |
---|
31 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
32 | break |
---|
33 | case "inventoryBelowReorderAll": |
---|
34 | result.inventoryItemList = getInventoryBelowReorder(params, false) |
---|
35 | if(result.inventoryItemList.totalCount > 0) |
---|
36 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.all.description") |
---|
37 | else |
---|
38 | result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found") |
---|
39 | break |
---|
40 | default: |
---|
41 | result.inventoryItemList = getAll(params) |
---|
42 | if(result.inventoryItemList.totalCount > 0) |
---|
43 | result.message = getMessage(code:"inventoryItem.search.text.all.description") |
---|
44 | else |
---|
45 | result.message = getMessage(code:"inventoryItem.search.text.all.none.found") |
---|
46 | break |
---|
47 | } // switch. |
---|
48 | |
---|
49 | // Success. |
---|
50 | return result |
---|
51 | |
---|
52 | } // getQuickSearch |
---|
53 | |
---|
54 | /** |
---|
55 | * Get all inventory items. |
---|
56 | * @param params The request params. |
---|
57 | */ |
---|
58 | def getAll(params) { |
---|
59 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
60 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
61 | params.sort = params?.sort ?: "name" |
---|
62 | params.order = params?.order ?: "asc" |
---|
63 | |
---|
64 | def inventoryItemList = InventoryItem.createCriteria().list( |
---|
65 | max: params.max, |
---|
66 | offset: params.offset, |
---|
67 | sort: params.sort, |
---|
68 | order: params.order) { |
---|
69 | } // createCriteria |
---|
70 | } // getAll |
---|
71 | |
---|
72 | /** |
---|
73 | * List inventory items that are below reorder point. |
---|
74 | * @param params The request params. |
---|
75 | * @param onlyReorderEnabled Only include items with reorder enabled, defaults to true. |
---|
76 | */ |
---|
77 | def getInventoryBelowReorder(params, onlyReorderEnabled=true) { |
---|
78 | params.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax) |
---|
79 | params.offset = params?.offset?.toInteger() ?: 0 |
---|
80 | params.sort = params?.sort ?: "name" |
---|
81 | params.order = params?.order ?: "asc" |
---|
82 | |
---|
83 | def inventoryItemList = InventoryItem.createCriteria().list( |
---|
84 | max: params.max, |
---|
85 | offset: params.offset, |
---|
86 | sort: params.sort, |
---|
87 | order: params.order) { |
---|
88 | eq("isActive", true) |
---|
89 | if(onlyReorderEnabled) |
---|
90 | eq("enableReorder", true) |
---|
91 | leProperty("unitsInStock", "reorderPoint") |
---|
92 | } // createCriteria |
---|
93 | } // getInventoryBelowReorder |
---|
94 | |
---|
95 | } // end class |
---|