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 | return |
---|
235 | } |
---|
236 | |
---|
237 | def delete = { |
---|
238 | def result = inventoryItemService.delete(params) |
---|
239 | |
---|
240 | if(!result.error) { |
---|
241 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
242 | redirect(action:search) |
---|
243 | return |
---|
244 | } |
---|
245 | |
---|
246 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
247 | |
---|
248 | if(result.error.code == "default.not.found") { |
---|
249 | redirect(action:search) |
---|
250 | return |
---|
251 | } |
---|
252 | |
---|
253 | redirect(action:show, id: params.id) |
---|
254 | } |
---|
255 | |
---|
256 | def edit = { |
---|
257 | |
---|
258 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
259 | if(params._action_Edit) |
---|
260 | params.action='edit' |
---|
261 | |
---|
262 | def result = inventoryItemService.edit(params) |
---|
263 | |
---|
264 | if(!result.error) { |
---|
265 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
266 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
267 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
268 | |
---|
269 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
270 | possibleAlternateItems: possibleAlternateItems, |
---|
271 | suppliers: suppliers, |
---|
272 | manufacturers: manufacturers] |
---|
273 | } |
---|
274 | |
---|
275 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
276 | redirect(action:search) |
---|
277 | } |
---|
278 | |
---|
279 | def update = { |
---|
280 | def result = inventoryItemService.update(params) |
---|
281 | |
---|
282 | if(!result.error) { |
---|
283 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
284 | redirect(action:show, id: params.id) |
---|
285 | return |
---|
286 | } |
---|
287 | |
---|
288 | if(result.error.code == "default.not.found") { |
---|
289 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
290 | redirect(action:search) |
---|
291 | return |
---|
292 | } |
---|
293 | |
---|
294 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
295 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
296 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
297 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), |
---|
298 | possibleAlternateItems: possibleAlternateItems, |
---|
299 | suppliers: suppliers, |
---|
300 | manufacturers: manufacturers]) |
---|
301 | } |
---|
302 | |
---|
303 | def create = { |
---|
304 | def result = inventoryItemService.create(params) |
---|
305 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
306 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
307 | |
---|
308 | if(!result.error) |
---|
309 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
310 | suppliers: suppliers, |
---|
311 | manufacturers: manufacturers] |
---|
312 | |
---|
313 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
314 | redirect(action: search) |
---|
315 | } |
---|
316 | |
---|
317 | def save = { |
---|
318 | def result = inventoryItemService.save(params) |
---|
319 | |
---|
320 | if(!result.error) { |
---|
321 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
322 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
323 | return |
---|
324 | } |
---|
325 | |
---|
326 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
327 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
328 | |
---|
329 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
330 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
331 | suppliers: suppliers, |
---|
332 | manufacturers: manufacturers]) |
---|
333 | } |
---|
334 | |
---|
335 | /** |
---|
336 | * Handles the use inventory item form submit in the show view. |
---|
337 | */ |
---|
338 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
339 | def useInventoryItem = { |
---|
340 | |
---|
341 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
342 | def result = inventoryMovementService.move(params) |
---|
343 | |
---|
344 | if(!result.error) { |
---|
345 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
346 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
347 | } |
---|
348 | else { |
---|
349 | if(result.inventoryMovementInstance) { |
---|
350 | def p = [:] |
---|
351 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
352 | def r = inventoryItemService.show(p) |
---|
353 | |
---|
354 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
355 | inventoryMovementList: r.inventoryMovementList, |
---|
356 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
357 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
358 | showTab: r.showTab] |
---|
359 | |
---|
360 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
361 | |
---|
362 | render(view: 'show', model: model) |
---|
363 | } |
---|
364 | else { |
---|
365 | flash.message = "Could not create inventory movement." |
---|
366 | redirect(action:"search") |
---|
367 | } |
---|
368 | |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | } |
---|