[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 | |
---|
[225] | 128 | if(!InventoryItem.exists(params.id)) { |
---|
[116] | 129 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[139] | 130 | redirect(action:search) |
---|
[225] | 131 | return |
---|
[116] | 132 | } |
---|
[225] | 133 | |
---|
| 134 | def result = inventoryItemService.prepareShowData(params) |
---|
| 135 | |
---|
| 136 | if(result.error) { |
---|
| 137 | flash.message = "Could not to prepare the data to show item with id: ${params.id}." |
---|
| 138 | redirect(action:search) |
---|
| 139 | return |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 143 | inventoryMovementList: result.inventoryMovementList, |
---|
| 144 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 145 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
| 146 | showTab: result.showTab] |
---|
| 147 | |
---|
| 148 | if(session.inventoryMovementTaskId) { |
---|
| 149 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 150 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 151 | model.inventoryMovementInstance.quantity = 1 |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | return model |
---|
[116] | 155 | } |
---|
| 156 | |
---|
| 157 | def delete = { |
---|
| 158 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
| 159 | if(inventoryItemInstance) { |
---|
| 160 | try { |
---|
[175] | 161 | inventoryItemInstance.delete(flush:true) |
---|
[116] | 162 | flash.message = "InventoryItem ${params.id} deleted" |
---|
[139] | 163 | redirect(action:search) |
---|
[116] | 164 | } |
---|
| 165 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
| 166 | flash.message = "InventoryItem ${params.id} could not be deleted" |
---|
| 167 | redirect(action:show,id:params.id) |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | else { |
---|
| 171 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[139] | 172 | redirect(action:search) |
---|
[116] | 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | def edit = { |
---|
[375] | 177 | |
---|
[139] | 178 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 179 | if(params._action_Edit) |
---|
[375] | 180 | params.action='edit' |
---|
| 181 | |
---|
[116] | 182 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
| 183 | |
---|
| 184 | if(!inventoryItemInstance) { |
---|
| 185 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[139] | 186 | redirect(action:search) |
---|
[116] | 187 | } |
---|
| 188 | else { |
---|
| 189 | return [ inventoryItemInstance : inventoryItemInstance ] |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | def update = { |
---|
| 194 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
| 195 | if(inventoryItemInstance) { |
---|
| 196 | if(params.version) { |
---|
| 197 | def version = params.version.toLong() |
---|
| 198 | if(inventoryItemInstance.version > version) { |
---|
| 199 | |
---|
| 200 | inventoryItemInstance.errors.rejectValue("version", "inventoryItem.optimistic.locking.failure", "Another user has updated this InventoryItem while you were editing.") |
---|
| 201 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
| 202 | return |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | inventoryItemInstance.properties = params |
---|
[178] | 206 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
[116] | 207 | flash.message = "InventoryItem ${params.id} updated" |
---|
| 208 | redirect(action:show,id:inventoryItemInstance.id) |
---|
| 209 | } |
---|
| 210 | else { |
---|
| 211 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | else { |
---|
| 215 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[175] | 216 | redirect(action:search) |
---|
[116] | 217 | } |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | def create = { |
---|
| 221 | def inventoryItemInstance = new InventoryItem() |
---|
| 222 | inventoryItemInstance.properties = params |
---|
| 223 | return ['inventoryItemInstance':inventoryItemInstance] |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | def save = { |
---|
| 227 | def inventoryItemInstance = new InventoryItem(params) |
---|
[178] | 228 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
[116] | 229 | flash.message = "InventoryItem ${inventoryItemInstance.id} created" |
---|
| 230 | redirect(action:show,id:inventoryItemInstance.id) |
---|
| 231 | } |
---|
| 232 | else { |
---|
| 233 | render(view:'create',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
| 234 | } |
---|
| 235 | } |
---|
[225] | 236 | |
---|
| 237 | /** |
---|
| 238 | * Handles the use inventory item form submit in the show view. |
---|
| 239 | */ |
---|
[298] | 240 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 241 | def useInventoryItem = { |
---|
| 242 | |
---|
[226] | 243 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 244 | def result = inventoryMovementService.move(params) |
---|
| 245 | |
---|
| 246 | if(!result.error) { |
---|
| 247 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
| 248 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
| 249 | } |
---|
| 250 | else { |
---|
| 251 | if(result.inventoryMovementInstance) { |
---|
| 252 | def p = [:] |
---|
| 253 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
| 254 | def r = inventoryItemService.prepareShowData(p) |
---|
| 255 | |
---|
| 256 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
| 257 | inventoryMovementList: r.inventoryMovementList, |
---|
| 258 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
| 259 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
| 260 | showTab: r.showTab] |
---|
| 261 | |
---|
| 262 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
| 263 | |
---|
| 264 | render(view: 'show', model: model) |
---|
| 265 | } |
---|
| 266 | else { |
---|
| 267 | flash.message = "Could not create inventory movement." |
---|
| 268 | redirect(action:"search") |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
[116] | 274 | } |
---|