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 | * Disaply 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 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
95 | def search = { |
---|
96 | |
---|
97 | if(session.inventoryItemSearchParamsMax) |
---|
98 | params.max = session.inventoryItemSearchParamsMax |
---|
99 | |
---|
100 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
101 | |
---|
102 | def inventoryItemInstanceList = [] |
---|
103 | def inventoryItemInstanceTotal |
---|
104 | def filterParams = [:] |
---|
105 | |
---|
106 | // Quick Search: |
---|
107 | if(!params.filter) { |
---|
108 | inventoryItemInstanceList = InventoryItem.list( params ) |
---|
109 | inventoryItemInstanceTotal = InventoryItem.count() |
---|
110 | filterParams = params |
---|
111 | } |
---|
112 | else { |
---|
113 | // filterPane: |
---|
114 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
115 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
116 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
117 | } |
---|
118 | |
---|
119 | // export plugin: |
---|
120 | if(params?.format && params.format != "html") { |
---|
121 | |
---|
122 | def dateFmt = { date -> |
---|
123 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
124 | } |
---|
125 | String title = "Inventory List." |
---|
126 | |
---|
127 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
128 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
129 | List fields = ["name", |
---|
130 | "description", |
---|
131 | "unitsInStock", |
---|
132 | "unitOfMeasure", |
---|
133 | "inventoryLocation", |
---|
134 | "inventoryLocation.inventoryStore"] |
---|
135 | Map labels = ["name": "Name", |
---|
136 | "description": "Description", |
---|
137 | "unitsInStock":"In Stock", |
---|
138 | "unitOfMeasure": "UOM", |
---|
139 | "inventoryLocation": "Location", |
---|
140 | "inventoryLocation.inventoryStore": "Store"] |
---|
141 | |
---|
142 | Map formatters = [:] |
---|
143 | Map parameters = [title: title, separator: ","] |
---|
144 | |
---|
145 | exportService.export(params.format, |
---|
146 | response.outputStream, |
---|
147 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
148 | fields, |
---|
149 | labels, |
---|
150 | formatters, |
---|
151 | parameters) |
---|
152 | } |
---|
153 | |
---|
154 | // Add some basic params to filterParams. |
---|
155 | filterParams.max = params.max |
---|
156 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
157 | filterParams.sort = params.sort ?: "id" |
---|
158 | filterParams.order = params.order ?: "desc" |
---|
159 | |
---|
160 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
161 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
162 | filterParams: filterParams ] |
---|
163 | } // end search() |
---|
164 | |
---|
165 | /** |
---|
166 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
167 | */ |
---|
168 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
169 | def findInventoryItemForMovement = { |
---|
170 | if(!params.task?.id) { |
---|
171 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
172 | redirect(controller: "taskDetailed", action: "search") |
---|
173 | return |
---|
174 | } |
---|
175 | |
---|
176 | session.inventoryMovementTaskId = params.task.id |
---|
177 | flash.message = "Please find and then select the inventory item." |
---|
178 | redirect(action: search) |
---|
179 | } |
---|
180 | |
---|
181 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
182 | def show = { |
---|
183 | |
---|
184 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
185 | if(params._action_Show) |
---|
186 | params.action='show' |
---|
187 | |
---|
188 | def result = inventoryItemService.show(params) |
---|
189 | |
---|
190 | if(!result.error) { |
---|
191 | |
---|
192 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
193 | inventoryMovementList: result.inventoryMovementList, |
---|
194 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
195 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
196 | showTab: result.showTab] |
---|
197 | |
---|
198 | if(session.inventoryMovementTaskId) { |
---|
199 | model.inventoryMovementInstance = new InventoryMovement() |
---|
200 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
201 | model.inventoryMovementInstance.quantity = 1 |
---|
202 | } |
---|
203 | |
---|
204 | // Success. |
---|
205 | return model |
---|
206 | } |
---|
207 | |
---|
208 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
209 | redirect(action:search) |
---|
210 | return |
---|
211 | } |
---|
212 | |
---|
213 | def delete = { |
---|
214 | def result = inventoryItemService.delete(params) |
---|
215 | |
---|
216 | if(!result.error) { |
---|
217 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
218 | redirect(action:search) |
---|
219 | return |
---|
220 | } |
---|
221 | |
---|
222 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
223 | |
---|
224 | if(result.error.code == "default.not.found") { |
---|
225 | redirect(action:search) |
---|
226 | return |
---|
227 | } |
---|
228 | |
---|
229 | redirect(action:show, id: params.id) |
---|
230 | } |
---|
231 | |
---|
232 | def edit = { |
---|
233 | |
---|
234 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
235 | if(params._action_Edit) |
---|
236 | params.action='edit' |
---|
237 | |
---|
238 | def result = inventoryItemService.edit(params) |
---|
239 | |
---|
240 | if(!result.error) { |
---|
241 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
242 | return [ inventoryItemInstance : result.inventoryItemInstance, possibleAlternateItems: possibleAlternateItems] |
---|
243 | } |
---|
244 | |
---|
245 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
246 | redirect(action:search) |
---|
247 | } |
---|
248 | |
---|
249 | def update = { |
---|
250 | def result = inventoryItemService.update(params) |
---|
251 | |
---|
252 | if(!result.error) { |
---|
253 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
254 | redirect(action:show, id: params.id) |
---|
255 | return |
---|
256 | } |
---|
257 | |
---|
258 | if(result.error.code == "default.not.found") { |
---|
259 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
260 | redirect(action:search) |
---|
261 | return |
---|
262 | } |
---|
263 | |
---|
264 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
265 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), possibleAlternateItems: possibleAlternateItems]) |
---|
266 | } |
---|
267 | |
---|
268 | def create = { |
---|
269 | def result = inventoryItemService.create(params) |
---|
270 | |
---|
271 | if(!result.error) |
---|
272 | return [inventoryItemInstance: result.inventoryItemInstance] |
---|
273 | |
---|
274 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
275 | redirect(action: search) |
---|
276 | } |
---|
277 | |
---|
278 | def save = { |
---|
279 | def result = inventoryItemService.save(params) |
---|
280 | |
---|
281 | if(!result.error) { |
---|
282 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
283 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
284 | return |
---|
285 | } |
---|
286 | |
---|
287 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
288 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance]) |
---|
289 | } |
---|
290 | |
---|
291 | /** |
---|
292 | * Handles the use inventory item form submit in the show view. |
---|
293 | */ |
---|
294 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
295 | def useInventoryItem = { |
---|
296 | |
---|
297 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
298 | def result = inventoryMovementService.move(params) |
---|
299 | |
---|
300 | if(!result.error) { |
---|
301 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
302 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
303 | } |
---|
304 | else { |
---|
305 | if(result.inventoryMovementInstance) { |
---|
306 | def p = [:] |
---|
307 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
308 | def r = inventoryItemService.show(p) |
---|
309 | |
---|
310 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
311 | inventoryMovementList: r.inventoryMovementList, |
---|
312 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
313 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
314 | showTab: r.showTab] |
---|
315 | |
---|
316 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
317 | |
---|
318 | render(view: 'show', model: model) |
---|
319 | } |
---|
320 | else { |
---|
321 | flash.message = "Could not create inventory movement." |
---|
322 | redirect(action:"search") |
---|
323 | } |
---|
324 | |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | } |
---|