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 | * Set session.inventoryItemReorderSearchParamsMax |
---|
40 | */ |
---|
41 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
42 | def setReorderSearchParamsMax = { |
---|
43 | def max = 1000 |
---|
44 | if(params.newMax?.isInteger()) { |
---|
45 | def i = params.newMax.toInteger() |
---|
46 | if(i > 0 && i <= max) |
---|
47 | session.inventoryItemReorderSearchParamsMax = params.newMax |
---|
48 | if(i > max) |
---|
49 | session.inventoryItemReorderSearchParamsMax = max |
---|
50 | } |
---|
51 | forward(action: 'reorder', params: params) |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Display the import view. |
---|
56 | */ |
---|
57 | def importInventoryItemPictures = { |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Handle the import save. |
---|
62 | */ |
---|
63 | def importInventoryItemPicturesSave = { |
---|
64 | def result = inventoryItemService.importInventoryItemPictures(request) |
---|
65 | |
---|
66 | if(!result.error) { |
---|
67 | def logFileLink = g.link(controller: "appCore", action: "appLog") {"log"} |
---|
68 | flash.message = g.message(code: "inventoryItemPictures.import.success", args: [logFileLink]) |
---|
69 | redirect(action:search) |
---|
70 | return |
---|
71 | } |
---|
72 | |
---|
73 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
74 | redirect(action: importInventoryItemPictures) |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Display the import view. |
---|
79 | */ |
---|
80 | def importInventory = { |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Handle the import save. |
---|
85 | */ |
---|
86 | def importInventorySave = { |
---|
87 | def result = inventoryCsvService.importInventory(request) |
---|
88 | |
---|
89 | if(!result.error) { |
---|
90 | flash.message = g.message(code: "inventory.import.success") |
---|
91 | redirect(action:search) |
---|
92 | return |
---|
93 | } |
---|
94 | |
---|
95 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
96 | redirect(action: importInventory) |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Export a csv template. |
---|
101 | * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! |
---|
102 | * This does not appear to be a problem once deployed to Tomcat. |
---|
103 | */ |
---|
104 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
105 | def exportInventoryTemplate = { |
---|
106 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
107 | response.setHeader("Content-disposition", "attachment; filename=InventoryTemplate.csv") |
---|
108 | def s = inventoryCsvService.buildInventoryTemplate() |
---|
109 | render s |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Export a csv test file. |
---|
114 | */ |
---|
115 | def exportInventoryExample = { |
---|
116 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
117 | response.setHeader("Content-disposition", "attachment; filename=InventoryExample.csv") |
---|
118 | def s = inventoryCsvService.buildInventoryExample() |
---|
119 | render s |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Export the entire inventory as a csv file. |
---|
124 | */ |
---|
125 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
126 | def exportInventory = { |
---|
127 | |
---|
128 | def inventoryItemList = InventoryItem.list() |
---|
129 | |
---|
130 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
131 | response.setHeader("Content-disposition", "attachment; filename=Inventory.csv") |
---|
132 | def s = inventoryCsvService.buildInventory(inventoryItemList) |
---|
133 | render s |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | * Display the import view for purchases. |
---|
138 | */ |
---|
139 | def importInventoryItemPurchases = { |
---|
140 | } |
---|
141 | |
---|
142 | /** |
---|
143 | * Handle the inventory purchases import save. |
---|
144 | */ |
---|
145 | def importInventoryItemPurchasesSave = { |
---|
146 | def result = inventoryCsvService.importInventoryItemPurchases(request) |
---|
147 | |
---|
148 | if(!result.error) { |
---|
149 | flash.message = g.message(code: "inventory.import.success") |
---|
150 | redirect(action:search) |
---|
151 | return |
---|
152 | } |
---|
153 | |
---|
154 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
155 | redirect(action: importInventoryItemPurchases) |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Search for Inventory items. |
---|
160 | */ |
---|
161 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
162 | def search = { |
---|
163 | |
---|
164 | if(session.inventoryItemSearchParamsMax) |
---|
165 | params.max = session.inventoryItemSearchParamsMax |
---|
166 | |
---|
167 | // Protect filterPane. |
---|
168 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
169 | |
---|
170 | def inventoryItemInstanceList = [] |
---|
171 | def inventoryItemInstanceTotal |
---|
172 | def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
173 | def isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
174 | |
---|
175 | // Restore default sort if a new text search is requested |
---|
176 | if(params.newTextSearch) { |
---|
177 | params.sort = 'id' |
---|
178 | params.order = 'desc' |
---|
179 | } |
---|
180 | |
---|
181 | // Restore search unless a new search is being requested. |
---|
182 | if(!params.searchText && !params.quickSearch && !filterParams) { |
---|
183 | if(session.inventoryItemSearchText) { |
---|
184 | params.searchText = session.inventoryItemSearchText |
---|
185 | params.searchName = session.inventoryItemSearchName |
---|
186 | params.searchDescription = session.inventoryItemSearchDescription |
---|
187 | params.searchComment = session.inventoryItemSearchComment |
---|
188 | params.searchLocation = session.inventoryItemSearchLocation |
---|
189 | params.searchGroup = session.inventoryItemSearchGroup |
---|
190 | params.searchSpareFor = session.inventoryItemSearchSpareFor |
---|
191 | } |
---|
192 | else if(session.inventoryItemQuickSearch) { |
---|
193 | params.quickSearch = session.inventoryItemQuickSearch |
---|
194 | if(session.inventoryItemQuickSearchDaysBack) |
---|
195 | params.daysBack = session.inventoryItemQuickSearchDaysBack.toString() |
---|
196 | } |
---|
197 | else if(session.inventoryItemSearchFilterParams) { |
---|
198 | session.inventoryItemSearchFilterParams.each() { params[it.key] = it.value } |
---|
199 | params.filter = session.inventoryItemSearchFilter |
---|
200 | isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | // Remember sort if supplied, otherwise try to restore. |
---|
205 | if(params.sort && params.order) { |
---|
206 | session.inventoryItemSearchSort = params.sort |
---|
207 | session.inventoryItemSearchOrder = params.order |
---|
208 | } |
---|
209 | else if(session.inventoryItemSearchSort && session.inventoryItemSearchOrder) { |
---|
210 | params.sort = session.inventoryItemSearchSort |
---|
211 | params.order = session.inventoryItemSearchOrder |
---|
212 | } |
---|
213 | |
---|
214 | if(isFilterApplied) { |
---|
215 | // filterPane: |
---|
216 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
217 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
218 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
219 | // Remember search. |
---|
220 | session.inventoryItemSearchFilterParams = new LinkedHashMap(filterParams) |
---|
221 | session.inventoryItemSearchFilter = new LinkedHashMap(params.filter) |
---|
222 | // Clear any previous search. |
---|
223 | session.removeAttribute("inventoryItemSearchText") |
---|
224 | session.removeAttribute("inventoryItemSearchName") |
---|
225 | session.removeAttribute("inventoryItemSearchDescription") |
---|
226 | session.removeAttribute("inventoryItemSearchComment") |
---|
227 | session.removeAttribute("inventoryItemSearchLocation") |
---|
228 | session.removeAttribute("inventoryItemSearchGroup") |
---|
229 | session.removeAttribute("inventoryItemSearchSpareFor") |
---|
230 | session.removeAttribute("inventoryItemQuickSearch") |
---|
231 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
232 | } |
---|
233 | else if(params.searchText) { |
---|
234 | // Text Search: |
---|
235 | def result = inventoryItemSearchService.getTextSearch(params, RCU.getLocale(request)) |
---|
236 | inventoryItemInstanceList = result.inventoryItemList |
---|
237 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
238 | params.message = result.message |
---|
239 | filterParams.searchText = result.searchText |
---|
240 | // Place limit search selects in filterParams for pagination. |
---|
241 | if(params.searchName) |
---|
242 | filterParams.searchName = params.searchName |
---|
243 | if(params.searchDescription) |
---|
244 | filterParams.searchDescription = params.searchDescription |
---|
245 | if(params.searchComment) |
---|
246 | filterParams.searchComment = params.searchComment |
---|
247 | if(params.searchLocation) |
---|
248 | filterParams.searchLocation = params.searchLocation |
---|
249 | if(params.searchGroup) |
---|
250 | filterParams.searchGroup = params.searchGroup |
---|
251 | if(params.searchSpareFor) |
---|
252 | filterParams.searchSpareFor = params.searchSpareFor |
---|
253 | // Remember search. |
---|
254 | session.inventoryItemSearchText = params.searchText |
---|
255 | session.inventoryItemSearchName = params.searchName |
---|
256 | session.inventoryItemSearchDescription = params.searchDescription |
---|
257 | session.inventoryItemSearchComment = params.searchComment |
---|
258 | session.inventoryItemSearchLocation = params.searchLocation |
---|
259 | session.inventoryItemSearchGroup = params.searchGroup |
---|
260 | session.inventoryItemSearchSpareFor = params.searchSpareFor |
---|
261 | // Clear any previous search. |
---|
262 | session.removeAttribute("inventoryItemQuickSearch") |
---|
263 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
264 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
265 | session.removeAttribute("inventoryItemSearchFilter") |
---|
266 | } |
---|
267 | else { |
---|
268 | // Quick Search Links: |
---|
269 | if(!params.quickSearch) params.quickSearch = "all" |
---|
270 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
271 | inventoryItemInstanceList = result.inventoryItemList |
---|
272 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
273 | params.message = result.message |
---|
274 | filterParams.quickSearch = result.quickSearch |
---|
275 | // Remember search. |
---|
276 | session.inventoryItemQuickSearch = result.quickSearch |
---|
277 | if(result.daysBack) |
---|
278 | session.inventoryItemQuickSearchDaysBack = result.daysBack |
---|
279 | // Clear any previous search. |
---|
280 | session.removeAttribute("inventoryItemSearchText") |
---|
281 | session.removeAttribute("inventoryItemSearchName") |
---|
282 | session.removeAttribute("inventoryItemSearchDescription") |
---|
283 | session.removeAttribute("inventoryItemSearchComment") |
---|
284 | session.removeAttribute("inventoryItemSearchLocation") |
---|
285 | session.removeAttribute("inventoryItemSearchGroup") |
---|
286 | session.removeAttribute("inventoryItemSearchSpareFor") |
---|
287 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
288 | session.removeAttribute("inventoryItemSearchFilter") |
---|
289 | } |
---|
290 | |
---|
291 | // export plugin: |
---|
292 | if(params?.format && params.format != "html") { |
---|
293 | |
---|
294 | def dateFmt = { domain, value -> |
---|
295 | formatDate(format: "EEE, dd-MMM-yyyy", date: value) |
---|
296 | } |
---|
297 | |
---|
298 | String title |
---|
299 | if(params.quickSearch) |
---|
300 | title = params.message |
---|
301 | else |
---|
302 | title = "Filtered Inventory List." |
---|
303 | |
---|
304 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
305 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
306 | List fields = ["name", |
---|
307 | "description", |
---|
308 | "inventoryGroup", |
---|
309 | "unitsInStock", |
---|
310 | "reorderPoint", |
---|
311 | "unitOfMeasure", |
---|
312 | "inventoryLocation", |
---|
313 | "inventoryLocation.inventoryStore"] |
---|
314 | Map labels = ["name": "Name", |
---|
315 | "description": "Description", |
---|
316 | "inventoryGroup": "Group", |
---|
317 | "unitsInStock":"In Stock", |
---|
318 | "reorderPoint":"Reorder Point", |
---|
319 | "unitOfMeasure": "UOM", |
---|
320 | "inventoryLocation": "Location", |
---|
321 | "inventoryLocation.inventoryStore": "Store"] |
---|
322 | |
---|
323 | Map formatters = [:] |
---|
324 | Map parameters = [title: title, separator: ","] |
---|
325 | |
---|
326 | exportService.export(params.format, |
---|
327 | response.outputStream, |
---|
328 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
329 | fields, |
---|
330 | labels, |
---|
331 | formatters, |
---|
332 | parameters) |
---|
333 | } |
---|
334 | |
---|
335 | // Add some basic params to filterParams. |
---|
336 | filterParams.max = params.max |
---|
337 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
338 | filterParams.sort = params.sort ?: "name" |
---|
339 | filterParams.order = params.order ?: "asc" |
---|
340 | |
---|
341 | // Get some associatedProperty values for filterpane. |
---|
342 | def associatedPropertyValues = [:] |
---|
343 | def associatedPropertyMax = 10000 |
---|
344 | associatedPropertyValues.inventoryLocationList = InventoryLocation.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
345 | associatedPropertyValues.assetList = Asset.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
346 | associatedPropertyValues.supplierList = Supplier.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
347 | |
---|
348 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
349 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
350 | filterParams: filterParams, |
---|
351 | params: params, |
---|
352 | associatedPropertyValues: associatedPropertyValues ] |
---|
353 | } // end search() |
---|
354 | |
---|
355 | /** |
---|
356 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
357 | */ |
---|
358 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
359 | def findInventoryItemForMovement = { |
---|
360 | if(!params.task?.id) { |
---|
361 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
362 | redirect(controller: "taskDetailed", action: "search") |
---|
363 | return |
---|
364 | } |
---|
365 | |
---|
366 | session.inventoryMovementTaskId = params.task.id |
---|
367 | flash.message = "Please find and then select the inventory item." |
---|
368 | redirect(action: search) |
---|
369 | } |
---|
370 | |
---|
371 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
372 | def show = { |
---|
373 | |
---|
374 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
375 | if(params._action_Show) |
---|
376 | params.action='show' |
---|
377 | |
---|
378 | def result = inventoryItemService.show(params) |
---|
379 | |
---|
380 | if(!result.error) { |
---|
381 | |
---|
382 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
383 | inventoryMovementList: result.inventoryMovementList, |
---|
384 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
385 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
386 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
387 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
388 | showTab: result.showTab] |
---|
389 | |
---|
390 | if(session.inventoryMovementTaskId) { |
---|
391 | model.inventoryMovementInstance = new InventoryMovement() |
---|
392 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
393 | model.inventoryMovementInstance.quantity = 1 |
---|
394 | } |
---|
395 | |
---|
396 | // Success. |
---|
397 | return model |
---|
398 | } |
---|
399 | |
---|
400 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
401 | redirect(action:search) |
---|
402 | } |
---|
403 | |
---|
404 | def delete = { |
---|
405 | def result = inventoryItemService.delete(params) |
---|
406 | |
---|
407 | if(!result.error) { |
---|
408 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
409 | redirect(action:search) |
---|
410 | return |
---|
411 | } |
---|
412 | |
---|
413 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
414 | |
---|
415 | if(result.error.code == "default.not.found") { |
---|
416 | redirect(action:search) |
---|
417 | return |
---|
418 | } |
---|
419 | |
---|
420 | redirect(action:show, id: params.id) |
---|
421 | } |
---|
422 | |
---|
423 | def edit = { |
---|
424 | |
---|
425 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
426 | if(params._action_Edit) |
---|
427 | params.action='edit' |
---|
428 | |
---|
429 | def result = inventoryItemService.edit(params) |
---|
430 | |
---|
431 | if(!result.error) { |
---|
432 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
433 | |
---|
434 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
435 | suppliers: suppliers] |
---|
436 | } |
---|
437 | |
---|
438 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
439 | redirect(action:search) |
---|
440 | } |
---|
441 | |
---|
442 | def update = { |
---|
443 | def result = inventoryItemService.update(params) |
---|
444 | |
---|
445 | if(!result.error) { |
---|
446 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
447 | redirect(action:show, id: params.id) |
---|
448 | return |
---|
449 | } |
---|
450 | |
---|
451 | if(result.error.code == "default.not.found") { |
---|
452 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
453 | redirect(action:search) |
---|
454 | return |
---|
455 | } |
---|
456 | |
---|
457 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
458 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
459 | suppliers: suppliers]) |
---|
460 | } |
---|
461 | |
---|
462 | def create = { |
---|
463 | def result = inventoryItemService.create(params) |
---|
464 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
465 | |
---|
466 | if(!result.error) |
---|
467 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
468 | suppliers: suppliers] |
---|
469 | |
---|
470 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
471 | redirect(action: search) |
---|
472 | } |
---|
473 | |
---|
474 | def save = { |
---|
475 | def result = inventoryItemService.save(params) |
---|
476 | |
---|
477 | if(!result.error) { |
---|
478 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
479 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
480 | return |
---|
481 | } |
---|
482 | |
---|
483 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
484 | |
---|
485 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
486 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
487 | suppliers: suppliers]) |
---|
488 | } |
---|
489 | |
---|
490 | /** |
---|
491 | * Handles the use inventory item form submit in the show view. |
---|
492 | */ |
---|
493 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
494 | def useInventoryItem = { |
---|
495 | |
---|
496 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
497 | def result = inventoryMovementService.move(params) |
---|
498 | |
---|
499 | if(!result.error) { |
---|
500 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
501 | session.inventoryMovementTaskId = null |
---|
502 | redirect(controller: "taskDetailed", |
---|
503 | action: "show", |
---|
504 | id: result.taskId, |
---|
505 | params: [showTab: "showInventoryTab"]) |
---|
506 | // Success. |
---|
507 | return |
---|
508 | } |
---|
509 | |
---|
510 | // Prepare data for the show view. |
---|
511 | def p = [:] |
---|
512 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
513 | def r = inventoryItemService.show(p) |
---|
514 | |
---|
515 | // Render show view if data was successfully prepared. |
---|
516 | if(!r.error) { |
---|
517 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
518 | inventoryMovementList: r.inventoryMovementList, |
---|
519 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
520 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
521 | inventoryItemPurchases: r.inventoryItemPurchases, |
---|
522 | inventoryItemPurchasesTotal: r.inventoryItemPurchasesTotal, |
---|
523 | showTab: r.showTab] |
---|
524 | |
---|
525 | model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. |
---|
526 | |
---|
527 | render(view: 'show', model: model) |
---|
528 | return |
---|
529 | } |
---|
530 | |
---|
531 | // Could not prepare data for show view so doing the next best thing. |
---|
532 | flash.errorMessage = g.message(code: r.error.code, args: r.error.args) |
---|
533 | redirect(action:search) |
---|
534 | |
---|
535 | } // useInventoryItem |
---|
536 | |
---|
537 | /** |
---|
538 | * Clear the use inventory item form in the show view. |
---|
539 | * Accomplished by clearing the session variable and ajax. |
---|
540 | */ |
---|
541 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
542 | def clearUseInventoryItem = { |
---|
543 | session.inventoryMovementTaskId = null |
---|
544 | render '' |
---|
545 | } |
---|
546 | |
---|
547 | /** |
---|
548 | * Search for Inventory items that require reorder. |
---|
549 | */ |
---|
550 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
551 | def reorder = { |
---|
552 | |
---|
553 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
554 | if(params._action_reorder) |
---|
555 | params.action='reorder' |
---|
556 | |
---|
557 | if(session.inventoryItemReorderSearchParamsMax) |
---|
558 | params.max = session.inventoryItemReorderSearchParamsMax |
---|
559 | |
---|
560 | def inventoryItemInstanceList = [] |
---|
561 | def inventoryItemInstanceTotal |
---|
562 | def filterParams = params |
---|
563 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
564 | def inventoryGroups = InventoryGroup.findAllByIsActive(true) |
---|
565 | |
---|
566 | if(params.selectedGroups) { |
---|
567 | if(params.selectedGroups instanceof String) |
---|
568 | params.selectedGroups = [params.selectedGroups.toLong()] |
---|
569 | else |
---|
570 | params.selectedGroups = params.selectedGroups.collect { it.toLong() } |
---|
571 | } |
---|
572 | else |
---|
573 | params.selectedGroups = [] |
---|
574 | |
---|
575 | // Restore search unless a new search is being requested. |
---|
576 | if(!params.newSearch && !params.quickSearch) { |
---|
577 | if(session.reorderSearchSelectedGroups) { |
---|
578 | params.selectedGroups = session.reorderSearchSelectedGroups |
---|
579 | params.selectedSupplier = session.reorderSearchSelectedSupplier |
---|
580 | params.includeAlternateSuppliers = session.reorderSearchIncludeAlternateSuppliers |
---|
581 | params.includeReorderListingDisabled = session.reorderSearchIncludeReorderListingDisabled |
---|
582 | params.includeOnBackOrder = session.reorderSearchIncludeOnBackOrder |
---|
583 | } |
---|
584 | else if(session.inventoryItemReorderQuickSearch) { |
---|
585 | params.quickSearch = session.inventoryItemReorderQuickSearch |
---|
586 | if(session.inventoryItemReorderQuickSearchDaysBack) |
---|
587 | params.daysBack = session.inventoryItemReorderQuickSearchDaysBack.toString() |
---|
588 | } |
---|
589 | } |
---|
590 | |
---|
591 | // Remember sort if supplied, otherwise try to restore. |
---|
592 | if(params.sort && params.order) { |
---|
593 | session.inventoryItemReorderSearchSort = params.sort |
---|
594 | session.inventoryItemReorderSearchOrder = params.order |
---|
595 | } |
---|
596 | else if(session.inventoryItemReorderSearchSort && session.inventoryItemReorderSearchOrder) { |
---|
597 | params.sort = session.inventoryItemReorderSearchSort |
---|
598 | params.order = session.inventoryItemReorderSearchOrder |
---|
599 | } |
---|
600 | |
---|
601 | if(params.quickSearch) { |
---|
602 | // Quick Search Links: |
---|
603 | if(!params.quickSearch) params.quickSearch = "inventoryBelowReorder" |
---|
604 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
605 | inventoryItemInstanceList = result.inventoryItemList |
---|
606 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
607 | params.message = result.message |
---|
608 | filterParams.quickSearch = result.quickSearch |
---|
609 | // Remember search. |
---|
610 | session.inventoryItemReorderQuickSearch = result.quickSearch |
---|
611 | if(result.daysBack) |
---|
612 | session.inventoryItemReorderQuickSearchDaysBack = result.daysBack |
---|
613 | // Clear any previous search. |
---|
614 | session.removeAttribute("reorderSearchSelectedGroups") |
---|
615 | session.removeAttribute("reorderSearchSelectedSupplier") |
---|
616 | session.removeAttribute("reorderSearchIncludeAlternateSuppliers") |
---|
617 | session.removeAttribute("reorderSearchIncludeReorderListingDisabled") |
---|
618 | session.removeAttribute("reorderSearchIncludeOnBackOrder") |
---|
619 | } |
---|
620 | else { |
---|
621 | // Reorder Search: |
---|
622 | def result = inventoryItemSearchService.getReorderSearch(params, RCU.getLocale(request)) |
---|
623 | inventoryItemInstanceList = result.inventoryItemList |
---|
624 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
625 | params.message = result.message |
---|
626 | // Place limit search selects in filterParams for pagination. |
---|
627 | if(params.selectedGroups) { |
---|
628 | filterParams.selectedGroups = params.selectedGroups |
---|
629 | filterParams.selectedSupplier = params.selectedSupplier |
---|
630 | filterParams.includeAlternateSuppliers = params.includeAlternateSuppliers |
---|
631 | filterParams.includeReorderListingDisabled = params.includeReorderListingDisabled |
---|
632 | filterParams.includeOnBackOrder = params.includeOnBackOrder |
---|
633 | } |
---|
634 | // Remember search. |
---|
635 | session.reorderSearchSelectedGroups = params.selectedGroups |
---|
636 | session.reorderSearchSelectedSupplier = params.selectedSupplier |
---|
637 | session.reorderSearchIncludeAlternateSuppliers = params.includeAlternateSuppliers |
---|
638 | session.reorderSearchIncludeReorderListingDisabled = params.includeReorderListingDisabled |
---|
639 | session.reorderSearchIncludeOnBackOrder = params.includeOnBackOrder |
---|
640 | // Clear any previous search. |
---|
641 | session.removeAttribute("inventoryItemReorderQuickSearch") |
---|
642 | session.removeAttribute("inventoryItemReorderQuickSearchDaysBack") |
---|
643 | } |
---|
644 | |
---|
645 | // export plugin: |
---|
646 | if(params?.format && params.format != "html") { |
---|
647 | |
---|
648 | def dateFmt = { domain, value -> |
---|
649 | formatDate(format: "EEE, dd-MMM-yyyy", date: value) |
---|
650 | } |
---|
651 | |
---|
652 | String title |
---|
653 | if(params.quickSearch) |
---|
654 | title = params.message |
---|
655 | else |
---|
656 | title = "Filtered Inventory List." |
---|
657 | |
---|
658 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
659 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
660 | List fields = ["name", |
---|
661 | "description", |
---|
662 | "inventoryGroup", |
---|
663 | "unitsInStock", |
---|
664 | "reorderPoint", |
---|
665 | "unitOfMeasure", |
---|
666 | "inventoryLocation", |
---|
667 | "inventoryLocation.inventoryStore"] |
---|
668 | Map labels = ["name": "Name", |
---|
669 | "description": "Description", |
---|
670 | "inventoryGroup": "Group", |
---|
671 | "unitsInStock":"In Stock", |
---|
672 | "reorderPoint":"Reorder Point", |
---|
673 | "unitOfMeasure": "UOM", |
---|
674 | "inventoryLocation": "Location", |
---|
675 | "inventoryLocation.inventoryStore": "Store"] |
---|
676 | |
---|
677 | Map formatters = [:] |
---|
678 | Map parameters = [title: title, separator: ","] |
---|
679 | |
---|
680 | exportService.export(params.format, |
---|
681 | response.outputStream, |
---|
682 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
683 | fields, |
---|
684 | labels, |
---|
685 | formatters, |
---|
686 | parameters) |
---|
687 | } |
---|
688 | |
---|
689 | // Add some basic params to filterParams. |
---|
690 | filterParams.max = params.max |
---|
691 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
692 | filterParams.sort = params.sort ?: "name" |
---|
693 | filterParams.order = params.order ?: "asc" |
---|
694 | |
---|
695 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
696 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
697 | filterParams: filterParams, |
---|
698 | params: params, |
---|
699 | inventoryGroups: inventoryGroups, |
---|
700 | suppliers: suppliers] |
---|
701 | } // end reorder() |
---|
702 | |
---|
703 | } // end of class |
---|