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 = { date -> |
---|
295 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
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.manufacturerList = Manufacturer.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
347 | associatedPropertyValues.supplierList = Supplier.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
348 | |
---|
349 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
350 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
351 | filterParams: filterParams, |
---|
352 | params: params, |
---|
353 | associatedPropertyValues: associatedPropertyValues ] |
---|
354 | } // end search() |
---|
355 | |
---|
356 | /** |
---|
357 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
358 | */ |
---|
359 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
360 | def findInventoryItemForMovement = { |
---|
361 | if(!params.task?.id) { |
---|
362 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
363 | redirect(controller: "taskDetailed", action: "search") |
---|
364 | return |
---|
365 | } |
---|
366 | |
---|
367 | session.inventoryMovementTaskId = params.task.id |
---|
368 | flash.message = "Please find and then select the inventory item." |
---|
369 | redirect(action: search) |
---|
370 | } |
---|
371 | |
---|
372 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
373 | def show = { |
---|
374 | |
---|
375 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
376 | if(params._action_Show) |
---|
377 | params.action='show' |
---|
378 | |
---|
379 | def result = inventoryItemService.show(params) |
---|
380 | |
---|
381 | if(!result.error) { |
---|
382 | |
---|
383 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
384 | inventoryMovementList: result.inventoryMovementList, |
---|
385 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
386 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
387 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
388 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
389 | showTab: result.showTab] |
---|
390 | |
---|
391 | if(session.inventoryMovementTaskId) { |
---|
392 | model.inventoryMovementInstance = new InventoryMovement() |
---|
393 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
394 | model.inventoryMovementInstance.quantity = 1 |
---|
395 | } |
---|
396 | |
---|
397 | // Success. |
---|
398 | return model |
---|
399 | } |
---|
400 | |
---|
401 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
402 | redirect(action:search) |
---|
403 | } |
---|
404 | |
---|
405 | def delete = { |
---|
406 | def result = inventoryItemService.delete(params) |
---|
407 | |
---|
408 | if(!result.error) { |
---|
409 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
410 | redirect(action:search) |
---|
411 | return |
---|
412 | } |
---|
413 | |
---|
414 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
415 | |
---|
416 | if(result.error.code == "default.not.found") { |
---|
417 | redirect(action:search) |
---|
418 | return |
---|
419 | } |
---|
420 | |
---|
421 | redirect(action:show, id: params.id) |
---|
422 | } |
---|
423 | |
---|
424 | def edit = { |
---|
425 | |
---|
426 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
427 | if(params._action_Edit) |
---|
428 | params.action='edit' |
---|
429 | |
---|
430 | def result = inventoryItemService.edit(params) |
---|
431 | |
---|
432 | if(!result.error) { |
---|
433 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
434 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
435 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
436 | |
---|
437 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
438 | possibleAlternateItems: possibleAlternateItems, |
---|
439 | suppliers: suppliers, |
---|
440 | manufacturers: manufacturers] |
---|
441 | } |
---|
442 | |
---|
443 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
444 | redirect(action:search) |
---|
445 | } |
---|
446 | |
---|
447 | def update = { |
---|
448 | def result = inventoryItemService.update(params) |
---|
449 | |
---|
450 | if(!result.error) { |
---|
451 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
452 | redirect(action:show, id: params.id) |
---|
453 | return |
---|
454 | } |
---|
455 | |
---|
456 | if(result.error.code == "default.not.found") { |
---|
457 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
458 | redirect(action:search) |
---|
459 | return |
---|
460 | } |
---|
461 | |
---|
462 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
463 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
464 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
465 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), |
---|
466 | possibleAlternateItems: possibleAlternateItems, |
---|
467 | suppliers: suppliers, |
---|
468 | manufacturers: manufacturers]) |
---|
469 | } |
---|
470 | |
---|
471 | def create = { |
---|
472 | def result = inventoryItemService.create(params) |
---|
473 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
474 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
475 | |
---|
476 | if(!result.error) |
---|
477 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
478 | suppliers: suppliers, |
---|
479 | manufacturers: manufacturers] |
---|
480 | |
---|
481 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
482 | redirect(action: search) |
---|
483 | } |
---|
484 | |
---|
485 | def save = { |
---|
486 | def result = inventoryItemService.save(params) |
---|
487 | |
---|
488 | if(!result.error) { |
---|
489 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
490 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
491 | return |
---|
492 | } |
---|
493 | |
---|
494 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
495 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
496 | |
---|
497 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
498 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
499 | suppliers: suppliers, |
---|
500 | manufacturers: manufacturers]) |
---|
501 | } |
---|
502 | |
---|
503 | /** |
---|
504 | * Handles the use inventory item form submit in the show view. |
---|
505 | */ |
---|
506 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
507 | def useInventoryItem = { |
---|
508 | |
---|
509 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
510 | def result = inventoryMovementService.move(params) |
---|
511 | |
---|
512 | if(!result.error) { |
---|
513 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
514 | session.inventoryMovementTaskId = null |
---|
515 | redirect(controller: "taskDetailed", |
---|
516 | action: "show", |
---|
517 | id: result.taskId, |
---|
518 | params: [showTab: "showInventoryTab"]) |
---|
519 | // Success. |
---|
520 | return |
---|
521 | } |
---|
522 | |
---|
523 | // Prepare data for the show view. |
---|
524 | def p = [:] |
---|
525 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
526 | def r = inventoryItemService.show(p) |
---|
527 | |
---|
528 | // Render show view if data was successfully prepared. |
---|
529 | if(!r.error) { |
---|
530 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
531 | inventoryMovementList: r.inventoryMovementList, |
---|
532 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
533 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
534 | inventoryItemPurchases: r.inventoryItemPurchases, |
---|
535 | inventoryItemPurchasesTotal: r.inventoryItemPurchasesTotal, |
---|
536 | showTab: r.showTab] |
---|
537 | |
---|
538 | model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. |
---|
539 | |
---|
540 | render(view: 'show', model: model) |
---|
541 | return |
---|
542 | } |
---|
543 | |
---|
544 | // Could not prepare data for show view so doing the next best thing. |
---|
545 | flash.errorMessage = g.message(code: r.error.code, args: r.error.args) |
---|
546 | redirect(action:search) |
---|
547 | |
---|
548 | } // useInventoryItem |
---|
549 | |
---|
550 | /** |
---|
551 | * Clear the use inventory item form in the show view. |
---|
552 | * Accomplished by clearing the session variable and ajax. |
---|
553 | */ |
---|
554 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
555 | def clearUseInventoryItem = { |
---|
556 | session.inventoryMovementTaskId = null |
---|
557 | render '' |
---|
558 | } |
---|
559 | |
---|
560 | /** |
---|
561 | * Search for Inventory items that require reorder. |
---|
562 | */ |
---|
563 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
564 | def reorder = { |
---|
565 | |
---|
566 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
567 | if(params._action_reorder) |
---|
568 | params.action='reorder' |
---|
569 | |
---|
570 | if(session.inventoryItemReorderSearchParamsMax) |
---|
571 | params.max = session.inventoryItemReorderSearchParamsMax |
---|
572 | |
---|
573 | def inventoryItemInstanceList = [] |
---|
574 | def inventoryItemInstanceTotal |
---|
575 | def filterParams = params |
---|
576 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
577 | def inventoryGroups = InventoryGroup.findAllByIsActive(true) |
---|
578 | if(!params.selectedGroups) |
---|
579 | params.selectedGroups = inventoryGroups.collect{it.id} |
---|
580 | else |
---|
581 | params.selectedGroups = params.selectedGroups.collect { it.toLong() } |
---|
582 | |
---|
583 | // Restore search unless a new search is being requested. |
---|
584 | if(!params.newSearch && !params.quickSearch) { |
---|
585 | if(session.reorderSearchSelectedGroups) { |
---|
586 | params.selectedGroups = session.reorderSearchSelectedGroups |
---|
587 | params.selectedSupplier = session.reorderSearchSelectedSupplier |
---|
588 | params.includeAlternateSuppliers = session.reorderSearchIncludeAlternateSuppliers |
---|
589 | params.includeReorderListingDisabled = session.reorderSearchIncludeReorderListingDisabled |
---|
590 | params.includeOnBackOrder = session.reorderSearchIncludeOnBackOrder |
---|
591 | } |
---|
592 | else if(session.inventoryItemReorderQuickSearch) { |
---|
593 | params.quickSearch = session.inventoryItemReorderQuickSearch |
---|
594 | if(session.inventoryItemReorderQuickSearchDaysBack) |
---|
595 | params.daysBack = session.inventoryItemReorderQuickSearchDaysBack.toString() |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | // Remember sort if supplied, otherwise try to restore. |
---|
600 | if(params.sort && params.order) { |
---|
601 | session.inventoryItemReorderSearchSort = params.sort |
---|
602 | session.inventoryItemReorderSearchOrder = params.order |
---|
603 | } |
---|
604 | else if(session.inventoryItemReorderSearchSort && session.inventoryItemReorderSearchOrder) { |
---|
605 | params.sort = session.inventoryItemReorderSearchSort |
---|
606 | params.order = session.inventoryItemReorderSearchOrder |
---|
607 | } |
---|
608 | |
---|
609 | if(params.quickSearch) { |
---|
610 | // Quick Search Links: |
---|
611 | if(!params.quickSearch) params.quickSearch = "inventoryBelowReorder" |
---|
612 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
613 | inventoryItemInstanceList = result.inventoryItemList |
---|
614 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
615 | params.message = result.message |
---|
616 | filterParams.quickSearch = result.quickSearch |
---|
617 | // Remember search. |
---|
618 | session.inventoryItemReorderQuickSearch = result.quickSearch |
---|
619 | if(result.daysBack) |
---|
620 | session.inventoryItemReorderQuickSearchDaysBack = result.daysBack |
---|
621 | // Clear any previous search. |
---|
622 | session.removeAttribute("reorderSearchSelectedGroups") |
---|
623 | session.removeAttribute("reorderSearchSelectedSupplier") |
---|
624 | session.removeAttribute("reorderSearchIncludeAlternateSuppliers") |
---|
625 | session.removeAttribute("reorderSearchIncludeReorderListingDisabled") |
---|
626 | session.removeAttribute("reorderSearchIncludeOnBackOrder") |
---|
627 | } |
---|
628 | else { |
---|
629 | // Reorder Search: |
---|
630 | def result = inventoryItemSearchService.getReorderSearch(params, RCU.getLocale(request)) |
---|
631 | inventoryItemInstanceList = result.inventoryItemList |
---|
632 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
633 | params.message = result.message |
---|
634 | // Place limit search selects in filterParams for pagination. |
---|
635 | if(params.selectedGroups) { |
---|
636 | filterParams.selectedGroups = params.selectedGroups |
---|
637 | filterParams.selectedSupplier = params.selectedSupplier |
---|
638 | filterParams.includeAlternateSuppliers = params.includeAlternateSuppliers |
---|
639 | filterParams.includeReorderListingDisabled = params.includeReorderListingDisabled |
---|
640 | filterParams.includeOnBackOrder = params.includeOnBackOrder |
---|
641 | } |
---|
642 | // Remember search. |
---|
643 | session.reorderSearchSelectedGroups = params.selectedGroups |
---|
644 | session.reorderSearchSelectedSupplier = params.selectedSupplier |
---|
645 | session.reorderSearchIncludeAlternateSuppliers = params.includeAlternateSuppliers |
---|
646 | session.reorderSearchIncludeReorderListingDisabled = params.includeReorderListingDisabled |
---|
647 | session.reorderSearchIncludeOnBackOrder = params.includeOnBackOrder |
---|
648 | // Clear any previous search. |
---|
649 | session.removeAttribute("inventoryItemReorderQuickSearch") |
---|
650 | session.removeAttribute("inventoryItemReorderQuickSearchDaysBack") |
---|
651 | } |
---|
652 | |
---|
653 | // export plugin: |
---|
654 | if(params?.format && params.format != "html") { |
---|
655 | |
---|
656 | def dateFmt = { date -> |
---|
657 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
658 | } |
---|
659 | |
---|
660 | String title |
---|
661 | if(params.quickSearch) |
---|
662 | title = params.message |
---|
663 | else |
---|
664 | title = "Filtered Inventory List." |
---|
665 | |
---|
666 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
667 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
668 | List fields = ["name", |
---|
669 | "description", |
---|
670 | "inventoryGroup", |
---|
671 | "unitsInStock", |
---|
672 | "reorderPoint", |
---|
673 | "unitOfMeasure", |
---|
674 | "inventoryLocation", |
---|
675 | "inventoryLocation.inventoryStore"] |
---|
676 | Map labels = ["name": "Name", |
---|
677 | "description": "Description", |
---|
678 | "inventoryGroup": "Group", |
---|
679 | "unitsInStock":"In Stock", |
---|
680 | "reorderPoint":"Reorder Point", |
---|
681 | "unitOfMeasure": "UOM", |
---|
682 | "inventoryLocation": "Location", |
---|
683 | "inventoryLocation.inventoryStore": "Store"] |
---|
684 | |
---|
685 | Map formatters = [:] |
---|
686 | Map parameters = [title: title, separator: ","] |
---|
687 | |
---|
688 | exportService.export(params.format, |
---|
689 | response.outputStream, |
---|
690 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
691 | fields, |
---|
692 | labels, |
---|
693 | formatters, |
---|
694 | parameters) |
---|
695 | } |
---|
696 | |
---|
697 | // Add some basic params to filterParams. |
---|
698 | filterParams.max = params.max |
---|
699 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
700 | filterParams.sort = params.sort ?: "name" |
---|
701 | filterParams.order = params.order ?: "asc" |
---|
702 | |
---|
703 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
704 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
705 | filterParams: filterParams, |
---|
706 | params: params, |
---|
707 | inventoryGroups: inventoryGroups, |
---|
708 | suppliers: suppliers] |
---|
709 | } // end reorder() |
---|
710 | |
---|
711 | } // end of class |
---|