[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 | /** |
---|
[441] | 36 | * Display the import view. |
---|
[423] | 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 | |
---|
[441] | 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 | |
---|
[423] | 116 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[139] | 117 | def search = { |
---|
[392] | 118 | |
---|
| 119 | if(session.inventoryItemSearchParamsMax) |
---|
| 120 | params.max = session.inventoryItemSearchParamsMax |
---|
| 121 | |
---|
[139] | 122 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
[156] | 123 | |
---|
[392] | 124 | def inventoryItemInstanceList = [] |
---|
| 125 | def inventoryItemInstanceTotal |
---|
| 126 | def filterParams = [:] |
---|
| 127 | |
---|
[156] | 128 | // Quick Search: |
---|
| 129 | if(!params.filter) { |
---|
[392] | 130 | inventoryItemInstanceList = InventoryItem.list( params ) |
---|
| 131 | inventoryItemInstanceTotal = InventoryItem.count() |
---|
| 132 | filterParams = params |
---|
[156] | 133 | } |
---|
[392] | 134 | else { |
---|
[156] | 135 | // filterPane: |
---|
[392] | 136 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
| 137 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
| 138 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 139 | } |
---|
[139] | 140 | |
---|
[392] | 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 | |
---|
[225] | 187 | /** |
---|
| 188 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 189 | */ |
---|
[298] | 190 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 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 | |
---|
[298] | 203 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 204 | def show = { |
---|
[225] | 205 | |
---|
[139] | 206 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 207 | if(params._action_Show) |
---|
[375] | 208 | params.action='show' |
---|
[116] | 209 | |
---|
[405] | 210 | def result = inventoryItemService.show(params) |
---|
[225] | 211 | |
---|
[405] | 212 | if(!result.error) { |
---|
[225] | 213 | |
---|
[405] | 214 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 215 | inventoryMovementList: result.inventoryMovementList, |
---|
| 216 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 217 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
[441] | 218 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
| 219 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
[405] | 220 | showTab: result.showTab] |
---|
[225] | 221 | |
---|
[405] | 222 | if(session.inventoryMovementTaskId) { |
---|
| 223 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 224 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 225 | model.inventoryMovementInstance.quantity = 1 |
---|
| 226 | } |
---|
[225] | 227 | |
---|
[405] | 228 | // Success. |
---|
| 229 | return model |
---|
[225] | 230 | } |
---|
| 231 | |
---|
[405] | 232 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 233 | redirect(action:search) |
---|
| 234 | return |
---|
[116] | 235 | } |
---|
| 236 | |
---|
| 237 | def delete = { |
---|
[405] | 238 | def result = inventoryItemService.delete(params) |
---|
| 239 | |
---|
| 240 | if(!result.error) { |
---|
| 241 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
[408] | 242 | redirect(action:search) |
---|
[405] | 243 | return |
---|
[116] | 244 | } |
---|
[405] | 245 | |
---|
| 246 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 247 | |
---|
| 248 | if(result.error.code == "default.not.found") { |
---|
[139] | 249 | redirect(action:search) |
---|
[405] | 250 | return |
---|
[116] | 251 | } |
---|
[405] | 252 | |
---|
| 253 | redirect(action:show, id: params.id) |
---|
[116] | 254 | } |
---|
| 255 | |
---|
| 256 | def edit = { |
---|
[375] | 257 | |
---|
[139] | 258 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 259 | if(params._action_Edit) |
---|
[375] | 260 | params.action='edit' |
---|
| 261 | |
---|
[405] | 262 | def result = inventoryItemService.edit(params) |
---|
[116] | 263 | |
---|
[425] | 264 | if(!result.error) { |
---|
| 265 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
[435] | 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] |
---|
[425] | 273 | } |
---|
[405] | 274 | |
---|
| 275 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 276 | redirect(action:search) |
---|
[116] | 277 | } |
---|
| 278 | |
---|
| 279 | def update = { |
---|
[405] | 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 |
---|
[116] | 286 | } |
---|
[405] | 287 | |
---|
| 288 | if(result.error.code == "default.not.found") { |
---|
| 289 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[175] | 290 | redirect(action:search) |
---|
[405] | 291 | return |
---|
[116] | 292 | } |
---|
[405] | 293 | |
---|
[425] | 294 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
[435] | 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]) |
---|
[116] | 301 | } |
---|
| 302 | |
---|
| 303 | def create = { |
---|
[405] | 304 | def result = inventoryItemService.create(params) |
---|
[435] | 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) } |
---|
[405] | 307 | |
---|
| 308 | if(!result.error) |
---|
[435] | 309 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
| 310 | suppliers: suppliers, |
---|
| 311 | manufacturers: manufacturers] |
---|
[405] | 312 | |
---|
| 313 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 314 | redirect(action: search) |
---|
[116] | 315 | } |
---|
| 316 | |
---|
| 317 | def save = { |
---|
[405] | 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 |
---|
[116] | 324 | } |
---|
[405] | 325 | |
---|
[435] | 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 | |
---|
[405] | 329 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[435] | 330 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
| 331 | suppliers: suppliers, |
---|
| 332 | manufacturers: manufacturers]) |
---|
[116] | 333 | } |
---|
[225] | 334 | |
---|
| 335 | /** |
---|
| 336 | * Handles the use inventory item form submit in the show view. |
---|
| 337 | */ |
---|
[298] | 338 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 339 | def useInventoryItem = { |
---|
| 340 | |
---|
[226] | 341 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 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 |
---|
[405] | 352 | def r = inventoryItemService.show(p) |
---|
[225] | 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 | |
---|
[116] | 372 | } |
---|