[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 |
---|
[225] | 9 | def inventoryItemService |
---|
| 10 | def inventoryMovementService |
---|
[156] | 11 | |
---|
[116] | 12 | // the delete, save and update actions only accept POST requests |
---|
[225] | 13 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
[116] | 14 | |
---|
[298] | 15 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 16 | def index = { redirect(action:search, params:params) } |
---|
| 17 | |
---|
[392] | 18 | /** |
---|
| 19 | * Set session.inventoryItemSearchParamsMax |
---|
| 20 | */ |
---|
[298] | 21 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[392] | 22 | def setSearchParamsMax = { |
---|
| 23 | def max = 1000 |
---|
| 24 | if(params.newMax.isInteger()) { |
---|
| 25 | def i = params.newMax.toInteger() |
---|
| 26 | if(i > 0 && i <= max) |
---|
| 27 | session.inventoryItemSearchParamsMax = params.newMax |
---|
| 28 | if(i > max) |
---|
| 29 | session.inventoryItemSearchParamsMax = max |
---|
| 30 | } |
---|
| 31 | forward(action: 'search', params: params) |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[139] | 35 | def search = { |
---|
[392] | 36 | |
---|
| 37 | if(session.inventoryItemSearchParamsMax) |
---|
| 38 | params.max = session.inventoryItemSearchParamsMax |
---|
| 39 | |
---|
[139] | 40 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
[156] | 41 | |
---|
[392] | 42 | def inventoryItemInstanceList = [] |
---|
| 43 | def inventoryItemInstanceTotal |
---|
| 44 | def filterParams = [:] |
---|
| 45 | |
---|
[156] | 46 | // Quick Search: |
---|
| 47 | if(!params.filter) { |
---|
[392] | 48 | inventoryItemInstanceList = InventoryItem.list( params ) |
---|
| 49 | inventoryItemInstanceTotal = InventoryItem.count() |
---|
| 50 | filterParams = params |
---|
[156] | 51 | } |
---|
[392] | 52 | else { |
---|
[156] | 53 | // filterPane: |
---|
[392] | 54 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
| 55 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
| 56 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 57 | } |
---|
[139] | 58 | |
---|
[392] | 59 | // export plugin: |
---|
| 60 | if(params?.format && params.format != "html") { |
---|
| 61 | |
---|
| 62 | def dateFmt = { date -> |
---|
| 63 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 64 | } |
---|
| 65 | String title = "Inventory List." |
---|
| 66 | |
---|
| 67 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 68 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
| 69 | List fields = ["name", |
---|
| 70 | "description", |
---|
| 71 | "unitsInStock", |
---|
| 72 | "unitOfMeasure", |
---|
| 73 | "inventoryLocation", |
---|
| 74 | "inventoryLocation.inventoryStore"] |
---|
| 75 | Map labels = ["name": "Name", |
---|
| 76 | "description": "Description", |
---|
| 77 | "unitsInStock":"In Stock", |
---|
| 78 | "unitOfMeasure": "UOM", |
---|
| 79 | "inventoryLocation": "Location", |
---|
| 80 | "inventoryLocation.inventoryStore": "Store"] |
---|
| 81 | |
---|
| 82 | Map formatters = [:] |
---|
| 83 | Map parameters = [title: title, separator: ","] |
---|
| 84 | |
---|
| 85 | exportService.export(params.format, |
---|
| 86 | response.outputStream, |
---|
| 87 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 88 | fields, |
---|
| 89 | labels, |
---|
| 90 | formatters, |
---|
| 91 | parameters) |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // Add some basic params to filterParams. |
---|
| 95 | filterParams.max = params.max |
---|
| 96 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 97 | filterParams.sort = params.sort ?: "id" |
---|
| 98 | filterParams.order = params.order ?: "desc" |
---|
| 99 | |
---|
| 100 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
| 101 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
| 102 | filterParams: filterParams ] |
---|
| 103 | } // end search() |
---|
| 104 | |
---|
[225] | 105 | /** |
---|
| 106 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 107 | */ |
---|
[298] | 108 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 109 | def findInventoryItemForMovement = { |
---|
| 110 | if(!params.task?.id) { |
---|
| 111 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
| 112 | redirect(controller: "taskDetailed", action: "search") |
---|
| 113 | return |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | session.inventoryMovementTaskId = params.task.id |
---|
| 117 | flash.message = "Please find and then select the inventory item." |
---|
| 118 | redirect(action: search) |
---|
| 119 | } |
---|
| 120 | |
---|
[298] | 121 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 122 | def show = { |
---|
[225] | 123 | |
---|
[139] | 124 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 125 | if(params._action_Show) |
---|
[375] | 126 | params.action='show' |
---|
[116] | 127 | |
---|
[405] | 128 | def result = inventoryItemService.show(params) |
---|
[225] | 129 | |
---|
[405] | 130 | if(!result.error) { |
---|
[225] | 131 | |
---|
[405] | 132 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 133 | inventoryMovementList: result.inventoryMovementList, |
---|
| 134 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 135 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
| 136 | showTab: result.showTab] |
---|
[225] | 137 | |
---|
[405] | 138 | if(session.inventoryMovementTaskId) { |
---|
| 139 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 140 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 141 | model.inventoryMovementInstance.quantity = 1 |
---|
| 142 | } |
---|
[225] | 143 | |
---|
[405] | 144 | // Success. |
---|
| 145 | return model |
---|
[225] | 146 | } |
---|
| 147 | |
---|
[405] | 148 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 149 | redirect(action:search) |
---|
| 150 | return |
---|
[116] | 151 | } |
---|
| 152 | |
---|
| 153 | def delete = { |
---|
[405] | 154 | def result = inventoryItemService.delete(params) |
---|
| 155 | |
---|
| 156 | if(!result.error) { |
---|
| 157 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
[408] | 158 | redirect(action:search) |
---|
[405] | 159 | return |
---|
[116] | 160 | } |
---|
[405] | 161 | |
---|
| 162 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 163 | |
---|
| 164 | if(result.error.code == "default.not.found") { |
---|
[139] | 165 | redirect(action:search) |
---|
[405] | 166 | return |
---|
[116] | 167 | } |
---|
[405] | 168 | |
---|
| 169 | redirect(action:show, id: params.id) |
---|
[116] | 170 | } |
---|
| 171 | |
---|
| 172 | def edit = { |
---|
[375] | 173 | |
---|
[139] | 174 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 175 | if(params._action_Edit) |
---|
[375] | 176 | params.action='edit' |
---|
| 177 | |
---|
[405] | 178 | def result = inventoryItemService.edit(params) |
---|
[116] | 179 | |
---|
[405] | 180 | if(!result.error) |
---|
| 181 | return [ inventoryItemInstance : result.inventoryItemInstance ] |
---|
| 182 | |
---|
| 183 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 184 | redirect(action:search) |
---|
[116] | 185 | } |
---|
| 186 | |
---|
| 187 | def update = { |
---|
[405] | 188 | def result = inventoryItemService.update(params) |
---|
| 189 | |
---|
| 190 | if(!result.error) { |
---|
| 191 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
| 192 | redirect(action:show, id: params.id) |
---|
| 193 | return |
---|
[116] | 194 | } |
---|
[405] | 195 | |
---|
| 196 | if(result.error.code == "default.not.found") { |
---|
| 197 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[175] | 198 | redirect(action:search) |
---|
[405] | 199 | return |
---|
[116] | 200 | } |
---|
[405] | 201 | |
---|
| 202 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach()]) |
---|
[116] | 203 | } |
---|
| 204 | |
---|
| 205 | def create = { |
---|
[405] | 206 | def result = inventoryItemService.create(params) |
---|
| 207 | |
---|
| 208 | if(!result.error) |
---|
| 209 | return [inventoryItemInstance: result.inventoryItemInstance] |
---|
| 210 | |
---|
| 211 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 212 | redirect(action: search) |
---|
[116] | 213 | } |
---|
| 214 | |
---|
| 215 | def save = { |
---|
[405] | 216 | def result = inventoryItemService.save(params) |
---|
| 217 | |
---|
| 218 | if(!result.error) { |
---|
| 219 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
| 220 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
| 221 | return |
---|
[116] | 222 | } |
---|
[405] | 223 | |
---|
| 224 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 225 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance]) |
---|
[116] | 226 | } |
---|
[225] | 227 | |
---|
| 228 | /** |
---|
| 229 | * Handles the use inventory item form submit in the show view. |
---|
| 230 | */ |
---|
[298] | 231 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 232 | def useInventoryItem = { |
---|
| 233 | |
---|
[226] | 234 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 235 | def result = inventoryMovementService.move(params) |
---|
| 236 | |
---|
| 237 | if(!result.error) { |
---|
| 238 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
| 239 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
| 240 | } |
---|
| 241 | else { |
---|
| 242 | if(result.inventoryMovementInstance) { |
---|
| 243 | def p = [:] |
---|
| 244 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
[405] | 245 | def r = inventoryItemService.show(p) |
---|
[225] | 246 | |
---|
| 247 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
| 248 | inventoryMovementList: r.inventoryMovementList, |
---|
| 249 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
| 250 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
| 251 | showTab: r.showTab] |
---|
| 252 | |
---|
| 253 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
| 254 | |
---|
| 255 | render(view: 'show', model: model) |
---|
| 256 | } |
---|
| 257 | else { |
---|
| 258 | flash.message = "Could not create inventory movement." |
---|
| 259 | redirect(action:"search") |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | } |
---|
| 263 | } |
---|
| 264 | |
---|
[116] | 265 | } |
---|