1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
3 | import com.zeddware.grails.plugins.filterpane.FilterUtils |
---|
4 | import org.springframework.web.servlet.support.RequestContextUtils as RCU |
---|
5 | |
---|
6 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
7 | class InventoryItemDetailedController extends BaseController { |
---|
8 | |
---|
9 | def filterService |
---|
10 | def exportService |
---|
11 | def inventoryCsvService |
---|
12 | def inventoryItemService |
---|
13 | def inventoryItemSearchService |
---|
14 | def inventoryMovementService |
---|
15 | |
---|
16 | // the delete, save and update actions only accept POST requests |
---|
17 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
18 | |
---|
19 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
20 | def index = { redirect(action:search, params:params) } |
---|
21 | |
---|
22 | /** |
---|
23 | * Set session.inventoryItemSearchParamsMax |
---|
24 | */ |
---|
25 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
26 | def setSearchParamsMax = { |
---|
27 | def max = 1000 |
---|
28 | if(params.newMax.isInteger()) { |
---|
29 | def i = params.newMax.toInteger() |
---|
30 | if(i > 0 && i <= max) |
---|
31 | session.inventoryItemSearchParamsMax = params.newMax |
---|
32 | if(i > max) |
---|
33 | session.inventoryItemSearchParamsMax = max |
---|
34 | } |
---|
35 | forward(action: 'search', params: params) |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * Display the import view. |
---|
40 | */ |
---|
41 | def importInventory = { |
---|
42 | } |
---|
43 | |
---|
44 | /** |
---|
45 | * Handle the import save. |
---|
46 | */ |
---|
47 | def importInventorySave = { |
---|
48 | def result = inventoryCsvService.importInventory(request) |
---|
49 | |
---|
50 | if(!result.error) { |
---|
51 | flash.message = g.message(code: "inventory.import.success") |
---|
52 | redirect(action:search) |
---|
53 | return |
---|
54 | } |
---|
55 | |
---|
56 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
57 | redirect(action: importInventory) |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Export a csv template. |
---|
62 | * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! |
---|
63 | * This does not appear to be a problem once deployed to Tomcat. |
---|
64 | */ |
---|
65 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
66 | def exportInventoryTemplate = { |
---|
67 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
68 | response.setHeader("Content-disposition", "attachment; filename=InventoryTemplate.csv") |
---|
69 | def s = inventoryCsvService.buildInventoryTemplate() |
---|
70 | render s |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Export a csv test file. |
---|
75 | */ |
---|
76 | def exportInventoryExample = { |
---|
77 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
78 | response.setHeader("Content-disposition", "attachment; filename=InventoryExample.csv") |
---|
79 | def s = inventoryCsvService.buildInventoryExample() |
---|
80 | render s |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Export the entire inventory as a csv file. |
---|
85 | */ |
---|
86 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
87 | def exportInventory = { |
---|
88 | |
---|
89 | def inventoryItemList = InventoryItem.list() |
---|
90 | |
---|
91 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
92 | response.setHeader("Content-disposition", "attachment; filename=Inventory.csv") |
---|
93 | def s = inventoryCsvService.buildInventory(inventoryItemList) |
---|
94 | render s |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Display the import view for purchases. |
---|
99 | */ |
---|
100 | def importInventoryItemPurchases = { |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Handle the inventory purchases import save. |
---|
105 | */ |
---|
106 | def importInventoryItemPurchasesSave = { |
---|
107 | def result = inventoryCsvService.importInventoryItemPurchases(request) |
---|
108 | |
---|
109 | if(!result.error) { |
---|
110 | flash.message = g.message(code: "inventory.import.success") |
---|
111 | redirect(action:search) |
---|
112 | return |
---|
113 | } |
---|
114 | |
---|
115 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
116 | redirect(action: importInventoryItemPurchases) |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * Search for Inventory items. |
---|
121 | */ |
---|
122 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
123 | def search = { |
---|
124 | |
---|
125 | if(session.inventoryItemSearchParamsMax) |
---|
126 | params.max = session.inventoryItemSearchParamsMax |
---|
127 | |
---|
128 | // Protect filterPane. |
---|
129 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
130 | |
---|
131 | def inventoryItemInstanceList = [] |
---|
132 | def inventoryItemInstanceTotal |
---|
133 | def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
134 | def isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
135 | |
---|
136 | // Restore search unless a new search is being requested. |
---|
137 | if(!params.quickSearch && !filterParams) { |
---|
138 | if(session.inventoryItemQuickSearch) { |
---|
139 | params.quickSearch = session.inventoryItemQuickSearch |
---|
140 | if(session.inventoryItemQuickSearchDaysBack) |
---|
141 | params.daysBack = session.inventoryItemQuickSearchDaysBack.toString() |
---|
142 | } |
---|
143 | else if(session.inventoryItemSearchFilterParams) { |
---|
144 | session.inventoryItemSearchFilterParams.each() { params[it.key] = it.value } |
---|
145 | params.filter = session.inventoryItemSearchFilter |
---|
146 | isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | // Remember sort if supplied, otherwise try to restore. |
---|
151 | if(params.sort && params.order) { |
---|
152 | session.inventoryItemSearchSort = params.sort |
---|
153 | session.inventoryItemSearchOrder = params.order |
---|
154 | } |
---|
155 | else if(session.inventoryItemSearchSort && session.inventoryItemSearchOrder) { |
---|
156 | params.sort = session.inventoryItemSearchSort |
---|
157 | params.order = session.inventoryItemSearchOrder |
---|
158 | } |
---|
159 | |
---|
160 | if(isFilterApplied) { |
---|
161 | // filterPane: |
---|
162 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
163 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
164 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
165 | // Remember search. |
---|
166 | session.inventoryItemSearchFilterParams = new LinkedHashMap(filterParams) |
---|
167 | session.inventoryItemSearchFilter = new LinkedHashMap(params.filter) |
---|
168 | session.removeAttribute("inventoryItemQuickSearch") |
---|
169 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
170 | } |
---|
171 | else { |
---|
172 | // Quick Search: |
---|
173 | if(!params.quickSearch) params.quickSearch = "all" |
---|
174 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
175 | inventoryItemInstanceList = result.inventoryItemList |
---|
176 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
177 | params.message = result.message |
---|
178 | filterParams.quickSearch = result.quickSearch |
---|
179 | // Remember search. |
---|
180 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
181 | session.removeAttribute("inventoryItemSearchFilter") |
---|
182 | session.inventoryItemQuickSearch = result.quickSearch |
---|
183 | if(result.daysBack) |
---|
184 | session.inventoryItemQuickSearchDaysBack = result.daysBack |
---|
185 | } |
---|
186 | |
---|
187 | // export plugin: |
---|
188 | if(params?.format && params.format != "html") { |
---|
189 | |
---|
190 | def dateFmt = { date -> |
---|
191 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
192 | } |
---|
193 | |
---|
194 | String title |
---|
195 | if(params.quickSearch) |
---|
196 | title = params.message |
---|
197 | else |
---|
198 | title = "Filtered Inventory List." |
---|
199 | |
---|
200 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
201 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
202 | List fields = ["name", |
---|
203 | "description", |
---|
204 | "unitsInStock", |
---|
205 | "reorderPoint", |
---|
206 | "unitOfMeasure", |
---|
207 | "inventoryLocation", |
---|
208 | "inventoryLocation.inventoryStore"] |
---|
209 | Map labels = ["name": "Name", |
---|
210 | "description": "Description", |
---|
211 | "unitsInStock":"In Stock", |
---|
212 | "reorderPoint":"Reorder Point", |
---|
213 | "unitOfMeasure": "UOM", |
---|
214 | "inventoryLocation": "Location", |
---|
215 | "inventoryLocation.inventoryStore": "Store"] |
---|
216 | |
---|
217 | Map formatters = [:] |
---|
218 | Map parameters = [title: title, separator: ","] |
---|
219 | |
---|
220 | exportService.export(params.format, |
---|
221 | response.outputStream, |
---|
222 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
223 | fields, |
---|
224 | labels, |
---|
225 | formatters, |
---|
226 | parameters) |
---|
227 | } |
---|
228 | |
---|
229 | // Add some basic params to filterParams. |
---|
230 | filterParams.max = params.max |
---|
231 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
232 | filterParams.sort = params.sort ?: "name" |
---|
233 | filterParams.order = params.order ?: "asc" |
---|
234 | |
---|
235 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
236 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
237 | filterParams: filterParams, |
---|
238 | params: params ] |
---|
239 | } // end search() |
---|
240 | |
---|
241 | /** |
---|
242 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
243 | */ |
---|
244 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
245 | def findInventoryItemForMovement = { |
---|
246 | if(!params.task?.id) { |
---|
247 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
248 | redirect(controller: "taskDetailed", action: "search") |
---|
249 | return |
---|
250 | } |
---|
251 | |
---|
252 | session.inventoryMovementTaskId = params.task.id |
---|
253 | flash.message = "Please find and then select the inventory item." |
---|
254 | redirect(action: search) |
---|
255 | } |
---|
256 | |
---|
257 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
258 | def show = { |
---|
259 | |
---|
260 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
261 | if(params._action_Show) |
---|
262 | params.action='show' |
---|
263 | |
---|
264 | def result = inventoryItemService.show(params) |
---|
265 | |
---|
266 | if(!result.error) { |
---|
267 | |
---|
268 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
269 | inventoryMovementList: result.inventoryMovementList, |
---|
270 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
271 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
272 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
273 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
274 | showTab: result.showTab] |
---|
275 | |
---|
276 | if(session.inventoryMovementTaskId) { |
---|
277 | model.inventoryMovementInstance = new InventoryMovement() |
---|
278 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
279 | model.inventoryMovementInstance.quantity = 1 |
---|
280 | } |
---|
281 | |
---|
282 | // Success. |
---|
283 | return model |
---|
284 | } |
---|
285 | |
---|
286 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
287 | redirect(action:search) |
---|
288 | } |
---|
289 | |
---|
290 | def delete = { |
---|
291 | def result = inventoryItemService.delete(params) |
---|
292 | |
---|
293 | if(!result.error) { |
---|
294 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
295 | redirect(action:search) |
---|
296 | return |
---|
297 | } |
---|
298 | |
---|
299 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
300 | |
---|
301 | if(result.error.code == "default.not.found") { |
---|
302 | redirect(action:search) |
---|
303 | return |
---|
304 | } |
---|
305 | |
---|
306 | redirect(action:show, id: params.id) |
---|
307 | } |
---|
308 | |
---|
309 | def edit = { |
---|
310 | |
---|
311 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
312 | if(params._action_Edit) |
---|
313 | params.action='edit' |
---|
314 | |
---|
315 | def result = inventoryItemService.edit(params) |
---|
316 | |
---|
317 | if(!result.error) { |
---|
318 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
319 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
320 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
321 | |
---|
322 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
323 | possibleAlternateItems: possibleAlternateItems, |
---|
324 | suppliers: suppliers, |
---|
325 | manufacturers: manufacturers] |
---|
326 | } |
---|
327 | |
---|
328 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
329 | redirect(action:search) |
---|
330 | } |
---|
331 | |
---|
332 | def update = { |
---|
333 | def result = inventoryItemService.update(params) |
---|
334 | |
---|
335 | if(!result.error) { |
---|
336 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
337 | redirect(action:show, id: params.id) |
---|
338 | return |
---|
339 | } |
---|
340 | |
---|
341 | if(result.error.code == "default.not.found") { |
---|
342 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
343 | redirect(action:search) |
---|
344 | return |
---|
345 | } |
---|
346 | |
---|
347 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
348 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
349 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
350 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), |
---|
351 | possibleAlternateItems: possibleAlternateItems, |
---|
352 | suppliers: suppliers, |
---|
353 | manufacturers: manufacturers]) |
---|
354 | } |
---|
355 | |
---|
356 | def create = { |
---|
357 | def result = inventoryItemService.create(params) |
---|
358 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
359 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
360 | |
---|
361 | if(!result.error) |
---|
362 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
363 | suppliers: suppliers, |
---|
364 | manufacturers: manufacturers] |
---|
365 | |
---|
366 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
367 | redirect(action: search) |
---|
368 | } |
---|
369 | |
---|
370 | def save = { |
---|
371 | def result = inventoryItemService.save(params) |
---|
372 | |
---|
373 | if(!result.error) { |
---|
374 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
375 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
376 | return |
---|
377 | } |
---|
378 | |
---|
379 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
380 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
381 | |
---|
382 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
383 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
384 | suppliers: suppliers, |
---|
385 | manufacturers: manufacturers]) |
---|
386 | } |
---|
387 | |
---|
388 | /** |
---|
389 | * Handles the use inventory item form submit in the show view. |
---|
390 | */ |
---|
391 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
392 | def useInventoryItem = { |
---|
393 | |
---|
394 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
395 | def result = inventoryMovementService.move(params) |
---|
396 | |
---|
397 | if(!result.error) { |
---|
398 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
399 | session.inventoryMovementTaskId = null |
---|
400 | redirect(controller: "taskDetailed", |
---|
401 | action: "show", |
---|
402 | id: result.taskId, |
---|
403 | params: [showTab: "showInventoryTab"]) |
---|
404 | // Success. |
---|
405 | return |
---|
406 | } |
---|
407 | |
---|
408 | // Prepare data for the show view. |
---|
409 | def p = [:] |
---|
410 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
411 | def r = inventoryItemService.show(p) |
---|
412 | |
---|
413 | // Render show view if data was successfully prepared. |
---|
414 | if(!r.error) { |
---|
415 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
416 | inventoryMovementList: r.inventoryMovementList, |
---|
417 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
418 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
419 | inventoryItemPurchases: r.inventoryItemPurchases, |
---|
420 | inventoryItemPurchasesTotal: r.inventoryItemPurchasesTotal, |
---|
421 | showTab: r.showTab] |
---|
422 | |
---|
423 | model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. |
---|
424 | |
---|
425 | render(view: 'show', model: model) |
---|
426 | return |
---|
427 | } |
---|
428 | |
---|
429 | // Could not prepare data for show view so doing the next best thing. |
---|
430 | flash.errorMessage = g.message(code: r.error.code, args: r.error.args) |
---|
431 | redirect(action:search) |
---|
432 | |
---|
433 | } // useInventoryItem |
---|
434 | |
---|
435 | /** |
---|
436 | * Clear the use inventory item form in the show view. |
---|
437 | * Accomplished by clearing the session variable and ajax. |
---|
438 | */ |
---|
439 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
440 | def clearUseInventoryItem = { |
---|
441 | session.inventoryMovementTaskId = null |
---|
442 | render '' |
---|
443 | } |
---|
444 | |
---|
445 | } // end of class |
---|