[116] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[392] | 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
[116] | 3 | |
---|
[298] | 4 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
[116] | 5 | class InventoryItemDetailedController extends BaseController { |
---|
[156] | 6 | |
---|
| 7 | def filterService |
---|
[392] | 8 | def exportService |
---|
[423] | 9 | def inventoryCsvService |
---|
[225] | 10 | def inventoryItemService |
---|
| 11 | def inventoryMovementService |
---|
[156] | 12 | |
---|
[116] | 13 | // the delete, save and update actions only accept POST requests |
---|
[225] | 14 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
[116] | 15 | |
---|
[298] | 16 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 17 | def index = { redirect(action:search, params:params) } |
---|
| 18 | |
---|
[392] | 19 | /** |
---|
| 20 | * Set session.inventoryItemSearchParamsMax |
---|
| 21 | */ |
---|
[298] | 22 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[392] | 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 | |
---|
[423] | 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 | */ |
---|
[392] | 62 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[423] | 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']) |
---|
[139] | 95 | def search = { |
---|
[392] | 96 | |
---|
| 97 | if(session.inventoryItemSearchParamsMax) |
---|
| 98 | params.max = session.inventoryItemSearchParamsMax |
---|
| 99 | |
---|
[139] | 100 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
[156] | 101 | |
---|
[392] | 102 | def inventoryItemInstanceList = [] |
---|
| 103 | def inventoryItemInstanceTotal |
---|
| 104 | def filterParams = [:] |
---|
| 105 | |
---|
[156] | 106 | // Quick Search: |
---|
| 107 | if(!params.filter) { |
---|
[392] | 108 | inventoryItemInstanceList = InventoryItem.list( params ) |
---|
| 109 | inventoryItemInstanceTotal = InventoryItem.count() |
---|
| 110 | filterParams = params |
---|
[156] | 111 | } |
---|
[392] | 112 | else { |
---|
[156] | 113 | // filterPane: |
---|
[392] | 114 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
| 115 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
| 116 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 117 | } |
---|
[139] | 118 | |
---|
[392] | 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 | |
---|
[225] | 165 | /** |
---|
| 166 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 167 | */ |
---|
[298] | 168 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 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 | |
---|
[298] | 181 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 182 | def show = { |
---|
[225] | 183 | |
---|
[139] | 184 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 185 | if(params._action_Show) |
---|
[375] | 186 | params.action='show' |
---|
[116] | 187 | |
---|
[405] | 188 | def result = inventoryItemService.show(params) |
---|
[225] | 189 | |
---|
[405] | 190 | if(!result.error) { |
---|
[225] | 191 | |
---|
[405] | 192 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 193 | inventoryMovementList: result.inventoryMovementList, |
---|
| 194 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 195 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
| 196 | showTab: result.showTab] |
---|
[225] | 197 | |
---|
[405] | 198 | if(session.inventoryMovementTaskId) { |
---|
| 199 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 200 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 201 | model.inventoryMovementInstance.quantity = 1 |
---|
| 202 | } |
---|
[225] | 203 | |
---|
[405] | 204 | // Success. |
---|
| 205 | return model |
---|
[225] | 206 | } |
---|
| 207 | |
---|
[405] | 208 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 209 | redirect(action:search) |
---|
| 210 | return |
---|
[116] | 211 | } |
---|
| 212 | |
---|
| 213 | def delete = { |
---|
[405] | 214 | def result = inventoryItemService.delete(params) |
---|
| 215 | |
---|
| 216 | if(!result.error) { |
---|
| 217 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
[408] | 218 | redirect(action:search) |
---|
[405] | 219 | return |
---|
[116] | 220 | } |
---|
[405] | 221 | |
---|
| 222 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 223 | |
---|
| 224 | if(result.error.code == "default.not.found") { |
---|
[139] | 225 | redirect(action:search) |
---|
[405] | 226 | return |
---|
[116] | 227 | } |
---|
[405] | 228 | |
---|
| 229 | redirect(action:show, id: params.id) |
---|
[116] | 230 | } |
---|
| 231 | |
---|
| 232 | def edit = { |
---|
[375] | 233 | |
---|
[139] | 234 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 235 | if(params._action_Edit) |
---|
[375] | 236 | params.action='edit' |
---|
| 237 | |
---|
[405] | 238 | def result = inventoryItemService.edit(params) |
---|
[116] | 239 | |
---|
[425] | 240 | if(!result.error) { |
---|
| 241 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
| 242 | return [ inventoryItemInstance : result.inventoryItemInstance, possibleAlternateItems: possibleAlternateItems] |
---|
| 243 | } |
---|
[405] | 244 | |
---|
| 245 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 246 | redirect(action:search) |
---|
[116] | 247 | } |
---|
| 248 | |
---|
| 249 | def update = { |
---|
[405] | 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 |
---|
[116] | 256 | } |
---|
[405] | 257 | |
---|
| 258 | if(result.error.code == "default.not.found") { |
---|
| 259 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[175] | 260 | redirect(action:search) |
---|
[405] | 261 | return |
---|
[116] | 262 | } |
---|
[405] | 263 | |
---|
[425] | 264 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
| 265 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), possibleAlternateItems: possibleAlternateItems]) |
---|
[116] | 266 | } |
---|
| 267 | |
---|
| 268 | def create = { |
---|
[405] | 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) |
---|
[116] | 276 | } |
---|
| 277 | |
---|
| 278 | def save = { |
---|
[405] | 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 |
---|
[116] | 285 | } |
---|
[405] | 286 | |
---|
| 287 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 288 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance]) |
---|
[116] | 289 | } |
---|
[225] | 290 | |
---|
| 291 | /** |
---|
| 292 | * Handles the use inventory item form submit in the show view. |
---|
| 293 | */ |
---|
[298] | 294 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 295 | def useInventoryItem = { |
---|
| 296 | |
---|
[226] | 297 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 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 |
---|
[405] | 308 | def r = inventoryItemService.show(p) |
---|
[225] | 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 | |
---|
[116] | 328 | } |
---|