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