1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
3 | |
---|
4 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
5 | class InventoryItemDetailedController extends BaseController { |
---|
6 | |
---|
7 | def filterService |
---|
8 | def exportService |
---|
9 | def inventoryItemService |
---|
10 | def inventoryMovementService |
---|
11 | |
---|
12 | // the delete, save and update actions only accept POST requests |
---|
13 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
14 | |
---|
15 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
16 | def index = { redirect(action:search, params:params) } |
---|
17 | |
---|
18 | /** |
---|
19 | * Set session.inventoryItemSearchParamsMax |
---|
20 | */ |
---|
21 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
22 | def setSearchParamsMax = { |
---|
23 | def max = 1000 |
---|
24 | if(params.newMax.isInteger()) { |
---|
25 | def i = params.newMax.toInteger() |
---|
26 | if(i > 0 && i <= max) |
---|
27 | session.inventoryItemSearchParamsMax = params.newMax |
---|
28 | if(i > max) |
---|
29 | session.inventoryItemSearchParamsMax = max |
---|
30 | } |
---|
31 | forward(action: 'search', params: params) |
---|
32 | } |
---|
33 | |
---|
34 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
35 | def search = { |
---|
36 | |
---|
37 | if(session.inventoryItemSearchParamsMax) |
---|
38 | params.max = session.inventoryItemSearchParamsMax |
---|
39 | |
---|
40 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
41 | |
---|
42 | def inventoryItemInstanceList = [] |
---|
43 | def inventoryItemInstanceTotal |
---|
44 | def filterParams = [:] |
---|
45 | |
---|
46 | // Quick Search: |
---|
47 | if(!params.filter) { |
---|
48 | inventoryItemInstanceList = InventoryItem.list( params ) |
---|
49 | inventoryItemInstanceTotal = InventoryItem.count() |
---|
50 | filterParams = params |
---|
51 | } |
---|
52 | else { |
---|
53 | // filterPane: |
---|
54 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
55 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
56 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
57 | } |
---|
58 | |
---|
59 | // export plugin: |
---|
60 | if(params?.format && params.format != "html") { |
---|
61 | |
---|
62 | def dateFmt = { date -> |
---|
63 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
64 | } |
---|
65 | String title = "Inventory List." |
---|
66 | |
---|
67 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
68 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
69 | List fields = ["name", |
---|
70 | "description", |
---|
71 | "unitsInStock", |
---|
72 | "unitOfMeasure", |
---|
73 | "inventoryLocation", |
---|
74 | "inventoryLocation.inventoryStore"] |
---|
75 | Map labels = ["name": "Name", |
---|
76 | "description": "Description", |
---|
77 | "unitsInStock":"In Stock", |
---|
78 | "unitOfMeasure": "UOM", |
---|
79 | "inventoryLocation": "Location", |
---|
80 | "inventoryLocation.inventoryStore": "Store"] |
---|
81 | |
---|
82 | Map formatters = [:] |
---|
83 | Map parameters = [title: title, separator: ","] |
---|
84 | |
---|
85 | exportService.export(params.format, |
---|
86 | response.outputStream, |
---|
87 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
88 | fields, |
---|
89 | labels, |
---|
90 | formatters, |
---|
91 | parameters) |
---|
92 | } |
---|
93 | |
---|
94 | // Add some basic params to filterParams. |
---|
95 | filterParams.max = params.max |
---|
96 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
97 | filterParams.sort = params.sort ?: "id" |
---|
98 | filterParams.order = params.order ?: "desc" |
---|
99 | |
---|
100 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
101 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
102 | filterParams: filterParams ] |
---|
103 | } // end search() |
---|
104 | |
---|
105 | /** |
---|
106 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
107 | */ |
---|
108 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
109 | def findInventoryItemForMovement = { |
---|
110 | if(!params.task?.id) { |
---|
111 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
112 | redirect(controller: "taskDetailed", action: "search") |
---|
113 | return |
---|
114 | } |
---|
115 | |
---|
116 | session.inventoryMovementTaskId = params.task.id |
---|
117 | flash.message = "Please find and then select the inventory item." |
---|
118 | redirect(action: search) |
---|
119 | } |
---|
120 | |
---|
121 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
122 | def show = { |
---|
123 | |
---|
124 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
125 | if(params._action_Show) |
---|
126 | params.action='show' |
---|
127 | |
---|
128 | def result = inventoryItemService.show(params) |
---|
129 | |
---|
130 | if(!result.error) { |
---|
131 | |
---|
132 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
133 | inventoryMovementList: result.inventoryMovementList, |
---|
134 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
135 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
136 | showTab: result.showTab] |
---|
137 | |
---|
138 | if(session.inventoryMovementTaskId) { |
---|
139 | model.inventoryMovementInstance = new InventoryMovement() |
---|
140 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
141 | model.inventoryMovementInstance.quantity = 1 |
---|
142 | } |
---|
143 | |
---|
144 | // Success. |
---|
145 | return model |
---|
146 | } |
---|
147 | |
---|
148 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
149 | redirect(action:search) |
---|
150 | return |
---|
151 | } |
---|
152 | |
---|
153 | def delete = { |
---|
154 | def result = inventoryItemService.delete(params) |
---|
155 | |
---|
156 | if(!result.error) { |
---|
157 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
158 | redirect(action:search) |
---|
159 | return |
---|
160 | } |
---|
161 | |
---|
162 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
163 | |
---|
164 | if(result.error.code == "default.not.found") { |
---|
165 | redirect(action:search) |
---|
166 | return |
---|
167 | } |
---|
168 | |
---|
169 | redirect(action:show, id: params.id) |
---|
170 | } |
---|
171 | |
---|
172 | def edit = { |
---|
173 | |
---|
174 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
175 | if(params._action_Edit) |
---|
176 | params.action='edit' |
---|
177 | |
---|
178 | def result = inventoryItemService.edit(params) |
---|
179 | |
---|
180 | if(!result.error) |
---|
181 | return [ inventoryItemInstance : result.inventoryItemInstance ] |
---|
182 | |
---|
183 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
184 | redirect(action:search) |
---|
185 | } |
---|
186 | |
---|
187 | def update = { |
---|
188 | def result = inventoryItemService.update(params) |
---|
189 | |
---|
190 | if(!result.error) { |
---|
191 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
192 | redirect(action:show, id: params.id) |
---|
193 | return |
---|
194 | } |
---|
195 | |
---|
196 | if(result.error.code == "default.not.found") { |
---|
197 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
198 | redirect(action:search) |
---|
199 | return |
---|
200 | } |
---|
201 | |
---|
202 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach()]) |
---|
203 | } |
---|
204 | |
---|
205 | def create = { |
---|
206 | def result = inventoryItemService.create(params) |
---|
207 | |
---|
208 | if(!result.error) |
---|
209 | return [inventoryItemInstance: result.inventoryItemInstance] |
---|
210 | |
---|
211 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
212 | redirect(action: search) |
---|
213 | } |
---|
214 | |
---|
215 | def save = { |
---|
216 | def result = inventoryItemService.save(params) |
---|
217 | |
---|
218 | if(!result.error) { |
---|
219 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
220 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
221 | return |
---|
222 | } |
---|
223 | |
---|
224 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
225 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance]) |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Handles the use inventory item form submit in the show view. |
---|
230 | */ |
---|
231 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
232 | def useInventoryItem = { |
---|
233 | |
---|
234 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
235 | def result = inventoryMovementService.move(params) |
---|
236 | |
---|
237 | if(!result.error) { |
---|
238 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
239 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
240 | } |
---|
241 | else { |
---|
242 | if(result.inventoryMovementInstance) { |
---|
243 | def p = [:] |
---|
244 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
245 | def r = inventoryItemService.show(p) |
---|
246 | |
---|
247 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
248 | inventoryMovementList: r.inventoryMovementList, |
---|
249 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
250 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
251 | showTab: r.showTab] |
---|
252 | |
---|
253 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
254 | |
---|
255 | render(view: 'show', model: model) |
---|
256 | } |
---|
257 | else { |
---|
258 | flash.message = "Could not create inventory movement." |
---|
259 | redirect(action:"search") |
---|
260 | } |
---|
261 | |
---|
262 | } |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|