[116] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[392] | 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
[485] | 3 | import com.zeddware.grails.plugins.filterpane.FilterUtils |
---|
| 4 | import org.springframework.web.servlet.support.RequestContextUtils as RCU |
---|
[116] | 5 | |
---|
[298] | 6 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
[116] | 7 | class InventoryItemDetailedController extends BaseController { |
---|
[156] | 8 | |
---|
| 9 | def filterService |
---|
[392] | 10 | def exportService |
---|
[423] | 11 | def inventoryCsvService |
---|
[225] | 12 | def inventoryItemService |
---|
[485] | 13 | def inventoryItemSearchService |
---|
[225] | 14 | def inventoryMovementService |
---|
[156] | 15 | |
---|
[116] | 16 | // the delete, save and update actions only accept POST requests |
---|
[225] | 17 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
[116] | 18 | |
---|
[298] | 19 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 20 | def index = { redirect(action:search, params:params) } |
---|
| 21 | |
---|
[392] | 22 | /** |
---|
| 23 | * Set session.inventoryItemSearchParamsMax |
---|
| 24 | */ |
---|
[298] | 25 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[392] | 26 | def setSearchParamsMax = { |
---|
| 27 | def max = 1000 |
---|
| 28 | if(params.newMax.isInteger()) { |
---|
| 29 | def i = params.newMax.toInteger() |
---|
| 30 | if(i > 0 && i <= max) |
---|
| 31 | session.inventoryItemSearchParamsMax = params.newMax |
---|
| 32 | if(i > max) |
---|
| 33 | session.inventoryItemSearchParamsMax = max |
---|
| 34 | } |
---|
| 35 | forward(action: 'search', params: params) |
---|
| 36 | } |
---|
| 37 | |
---|
[423] | 38 | /** |
---|
[441] | 39 | * Display the import view. |
---|
[423] | 40 | */ |
---|
| 41 | def importInventory = { |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * Handle the import save. |
---|
| 46 | */ |
---|
| 47 | def importInventorySave = { |
---|
| 48 | def result = inventoryCsvService.importInventory(request) |
---|
| 49 | |
---|
| 50 | if(!result.error) { |
---|
| 51 | flash.message = g.message(code: "inventory.import.success") |
---|
| 52 | redirect(action:search) |
---|
| 53 | return |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 57 | redirect(action: importInventory) |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Export a csv template. |
---|
| 62 | * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! |
---|
| 63 | * This does not appear to be a problem once deployed to Tomcat. |
---|
| 64 | */ |
---|
[392] | 65 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[423] | 66 | def exportInventoryTemplate = { |
---|
| 67 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 68 | response.setHeader("Content-disposition", "attachment; filename=InventoryTemplate.csv") |
---|
| 69 | def s = inventoryCsvService.buildInventoryTemplate() |
---|
| 70 | render s |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Export a csv test file. |
---|
| 75 | */ |
---|
| 76 | def exportInventoryExample = { |
---|
| 77 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 78 | response.setHeader("Content-disposition", "attachment; filename=InventoryExample.csv") |
---|
| 79 | def s = inventoryCsvService.buildInventoryExample() |
---|
| 80 | render s |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Export the entire inventory as a csv file. |
---|
| 85 | */ |
---|
| 86 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 87 | def exportInventory = { |
---|
| 88 | |
---|
| 89 | def inventoryItemList = InventoryItem.list() |
---|
| 90 | |
---|
| 91 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 92 | response.setHeader("Content-disposition", "attachment; filename=Inventory.csv") |
---|
| 93 | def s = inventoryCsvService.buildInventory(inventoryItemList) |
---|
| 94 | render s |
---|
| 95 | } |
---|
| 96 | |
---|
[441] | 97 | /** |
---|
| 98 | * Display the import view for purchases. |
---|
| 99 | */ |
---|
| 100 | def importInventoryItemPurchases = { |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Handle the inventory purchases import save. |
---|
| 105 | */ |
---|
| 106 | def importInventoryItemPurchasesSave = { |
---|
| 107 | def result = inventoryCsvService.importInventoryItemPurchases(request) |
---|
| 108 | |
---|
| 109 | if(!result.error) { |
---|
| 110 | flash.message = g.message(code: "inventory.import.success") |
---|
| 111 | redirect(action:search) |
---|
| 112 | return |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 116 | redirect(action: importInventoryItemPurchases) |
---|
| 117 | } |
---|
| 118 | |
---|
[485] | 119 | /** |
---|
| 120 | * Search for Inventory items. |
---|
| 121 | */ |
---|
[423] | 122 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[139] | 123 | def search = { |
---|
[392] | 124 | |
---|
| 125 | if(session.inventoryItemSearchParamsMax) |
---|
| 126 | params.max = session.inventoryItemSearchParamsMax |
---|
| 127 | |
---|
[485] | 128 | // Protect filterPane. |
---|
| 129 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
[156] | 130 | |
---|
[392] | 131 | def inventoryItemInstanceList = [] |
---|
| 132 | def inventoryItemInstanceTotal |
---|
[485] | 133 | def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 134 | def isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
[392] | 135 | |
---|
[562] | 136 | // Restore default sort if a new text search is requested |
---|
| 137 | if(params.newTextSearch) { |
---|
| 138 | params.sort = 'id' |
---|
| 139 | params.order = 'desc' |
---|
| 140 | } |
---|
| 141 | |
---|
[485] | 142 | // Restore search unless a new search is being requested. |
---|
[562] | 143 | if(!params.searchText && !params.quickSearch && !filterParams) { |
---|
| 144 | if(session.inventoryItemSearchText) { |
---|
| 145 | params.searchText = session.inventoryItemSearchText |
---|
| 146 | params.searchName = session.inventoryItemSearchName |
---|
| 147 | params.searchDescription = session.inventoryItemSearchDescription |
---|
| 148 | params.searchComment = session.inventoryItemSearchComment |
---|
| 149 | params.searchLocation = session.inventoryItemSearchLocation |
---|
| 150 | params.searchSpareFor = session.inventoryItemSearchSpareFor |
---|
| 151 | } |
---|
| 152 | else if(session.inventoryItemQuickSearch) { |
---|
[485] | 153 | params.quickSearch = session.inventoryItemQuickSearch |
---|
[489] | 154 | if(session.inventoryItemQuickSearchDaysBack) |
---|
| 155 | params.daysBack = session.inventoryItemQuickSearchDaysBack.toString() |
---|
| 156 | } |
---|
[485] | 157 | else if(session.inventoryItemSearchFilterParams) { |
---|
| 158 | session.inventoryItemSearchFilterParams.each() { params[it.key] = it.value } |
---|
| 159 | params.filter = session.inventoryItemSearchFilter |
---|
| 160 | isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
| 161 | } |
---|
[156] | 162 | } |
---|
[485] | 163 | |
---|
| 164 | // Remember sort if supplied, otherwise try to restore. |
---|
| 165 | if(params.sort && params.order) { |
---|
| 166 | session.inventoryItemSearchSort = params.sort |
---|
| 167 | session.inventoryItemSearchOrder = params.order |
---|
| 168 | } |
---|
| 169 | else if(session.inventoryItemSearchSort && session.inventoryItemSearchOrder) { |
---|
| 170 | params.sort = session.inventoryItemSearchSort |
---|
| 171 | params.order = session.inventoryItemSearchOrder |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | if(isFilterApplied) { |
---|
| 175 | // filterPane: |
---|
[392] | 176 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
| 177 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
| 178 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
[485] | 179 | // Remember search. |
---|
| 180 | session.inventoryItemSearchFilterParams = new LinkedHashMap(filterParams) |
---|
| 181 | session.inventoryItemSearchFilter = new LinkedHashMap(params.filter) |
---|
[562] | 182 | // Clear any previous search. |
---|
| 183 | session.removeAttribute("inventoryItemSearchText") |
---|
| 184 | session.removeAttribute("inventoryItemSearchName") |
---|
| 185 | session.removeAttribute("inventoryItemSearchDescription") |
---|
| 186 | session.removeAttribute("inventoryItemSearchComment") |
---|
| 187 | session.removeAttribute("inventoryItemSearchLocation") |
---|
| 188 | session.removeAttribute("inventoryItemSearchSpareFor") |
---|
[489] | 189 | session.removeAttribute("inventoryItemQuickSearch") |
---|
| 190 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
[392] | 191 | } |
---|
[562] | 192 | else if(params.searchText) { |
---|
| 193 | // Quick Search Text: |
---|
| 194 | def result = inventoryItemSearchService.getTextSearch(params, RCU.getLocale(request)) |
---|
| 195 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 196 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 197 | params.message = result.message |
---|
| 198 | filterParams.searchText = result.searchText |
---|
| 199 | // Remember search. |
---|
| 200 | session.inventoryItemSearchText = params.searchText |
---|
| 201 | session.inventoryItemSearchName = params.searchName |
---|
| 202 | session.inventoryItemSearchDescription = params.searchDescription |
---|
| 203 | session.inventoryItemSearchComment = params.searchComment |
---|
| 204 | session.inventoryItemSearchLocation = params.searchLocation |
---|
| 205 | session.inventoryItemSearchSpareFor = params.searchSpareFor |
---|
| 206 | // Clear any previous search. |
---|
| 207 | session.removeAttribute("inventoryItemQuickSearch") |
---|
| 208 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
| 209 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
| 210 | session.removeAttribute("inventoryItemSearchFilter") |
---|
| 211 | } |
---|
[485] | 212 | else { |
---|
[562] | 213 | // Quick Search Links: |
---|
[485] | 214 | if(!params.quickSearch) params.quickSearch = "all" |
---|
| 215 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
| 216 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 217 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 218 | params.message = result.message |
---|
| 219 | filterParams.quickSearch = result.quickSearch |
---|
| 220 | // Remember search. |
---|
| 221 | session.inventoryItemQuickSearch = result.quickSearch |
---|
[489] | 222 | if(result.daysBack) |
---|
| 223 | session.inventoryItemQuickSearchDaysBack = result.daysBack |
---|
[562] | 224 | // Clear any previous search. |
---|
| 225 | session.removeAttribute("inventoryItemSearchText") |
---|
| 226 | session.removeAttribute("inventoryItemSearchName") |
---|
| 227 | session.removeAttribute("inventoryItemSearchDescription") |
---|
| 228 | session.removeAttribute("inventoryItemSearchComment") |
---|
| 229 | session.removeAttribute("inventoryItemSearchLocation") |
---|
| 230 | session.removeAttribute("inventoryItemSearchSpareFor") |
---|
| 231 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
| 232 | session.removeAttribute("inventoryItemSearchFilter") |
---|
[485] | 233 | } |
---|
[139] | 234 | |
---|
[392] | 235 | // export plugin: |
---|
| 236 | if(params?.format && params.format != "html") { |
---|
| 237 | |
---|
| 238 | def dateFmt = { date -> |
---|
| 239 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 240 | } |
---|
| 241 | |
---|
[485] | 242 | String title |
---|
| 243 | if(params.quickSearch) |
---|
| 244 | title = params.message |
---|
| 245 | else |
---|
| 246 | title = "Filtered Inventory List." |
---|
| 247 | |
---|
[392] | 248 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 249 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
| 250 | List fields = ["name", |
---|
| 251 | "description", |
---|
[507] | 252 | "inventoryGroup", |
---|
[392] | 253 | "unitsInStock", |
---|
[485] | 254 | "reorderPoint", |
---|
[392] | 255 | "unitOfMeasure", |
---|
| 256 | "inventoryLocation", |
---|
| 257 | "inventoryLocation.inventoryStore"] |
---|
| 258 | Map labels = ["name": "Name", |
---|
| 259 | "description": "Description", |
---|
[507] | 260 | "inventoryGroup": "Group", |
---|
[392] | 261 | "unitsInStock":"In Stock", |
---|
[485] | 262 | "reorderPoint":"Reorder Point", |
---|
[392] | 263 | "unitOfMeasure": "UOM", |
---|
| 264 | "inventoryLocation": "Location", |
---|
| 265 | "inventoryLocation.inventoryStore": "Store"] |
---|
| 266 | |
---|
| 267 | Map formatters = [:] |
---|
| 268 | Map parameters = [title: title, separator: ","] |
---|
| 269 | |
---|
| 270 | exportService.export(params.format, |
---|
| 271 | response.outputStream, |
---|
| 272 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 273 | fields, |
---|
| 274 | labels, |
---|
| 275 | formatters, |
---|
| 276 | parameters) |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | // Add some basic params to filterParams. |
---|
| 280 | filterParams.max = params.max |
---|
| 281 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
[485] | 282 | filterParams.sort = params.sort ?: "name" |
---|
| 283 | filterParams.order = params.order ?: "asc" |
---|
[392] | 284 | |
---|
[554] | 285 | // Get some associatedProperty values for filterpane. |
---|
| 286 | def associatedPropertyValues = [:] |
---|
| 287 | def associatedPropertyMax = 10000 |
---|
| 288 | associatedPropertyValues.inventoryLocationList = InventoryLocation.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 289 | associatedPropertyValues.assetList = Asset.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 290 | associatedPropertyValues.manufacturerList = Manufacturer.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 291 | associatedPropertyValues.supplierList = Supplier.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 292 | |
---|
[392] | 293 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
[554] | 294 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
| 295 | filterParams: filterParams, |
---|
| 296 | params: params, |
---|
| 297 | associatedPropertyValues: associatedPropertyValues ] |
---|
[392] | 298 | } // end search() |
---|
| 299 | |
---|
[225] | 300 | /** |
---|
| 301 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 302 | */ |
---|
[298] | 303 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 304 | def findInventoryItemForMovement = { |
---|
| 305 | if(!params.task?.id) { |
---|
| 306 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
| 307 | redirect(controller: "taskDetailed", action: "search") |
---|
| 308 | return |
---|
| 309 | } |
---|
| 310 | |
---|
| 311 | session.inventoryMovementTaskId = params.task.id |
---|
| 312 | flash.message = "Please find and then select the inventory item." |
---|
| 313 | redirect(action: search) |
---|
| 314 | } |
---|
| 315 | |
---|
[298] | 316 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 317 | def show = { |
---|
[225] | 318 | |
---|
[139] | 319 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 320 | if(params._action_Show) |
---|
[375] | 321 | params.action='show' |
---|
[116] | 322 | |
---|
[405] | 323 | def result = inventoryItemService.show(params) |
---|
[225] | 324 | |
---|
[405] | 325 | if(!result.error) { |
---|
[225] | 326 | |
---|
[405] | 327 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 328 | inventoryMovementList: result.inventoryMovementList, |
---|
| 329 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 330 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
[441] | 331 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
| 332 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
[405] | 333 | showTab: result.showTab] |
---|
[225] | 334 | |
---|
[405] | 335 | if(session.inventoryMovementTaskId) { |
---|
| 336 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 337 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 338 | model.inventoryMovementInstance.quantity = 1 |
---|
| 339 | } |
---|
[225] | 340 | |
---|
[405] | 341 | // Success. |
---|
| 342 | return model |
---|
[225] | 343 | } |
---|
| 344 | |
---|
[405] | 345 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 346 | redirect(action:search) |
---|
[116] | 347 | } |
---|
| 348 | |
---|
| 349 | def delete = { |
---|
[405] | 350 | def result = inventoryItemService.delete(params) |
---|
| 351 | |
---|
| 352 | if(!result.error) { |
---|
| 353 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
[408] | 354 | redirect(action:search) |
---|
[405] | 355 | return |
---|
[116] | 356 | } |
---|
[405] | 357 | |
---|
| 358 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 359 | |
---|
| 360 | if(result.error.code == "default.not.found") { |
---|
[139] | 361 | redirect(action:search) |
---|
[405] | 362 | return |
---|
[116] | 363 | } |
---|
[405] | 364 | |
---|
| 365 | redirect(action:show, id: params.id) |
---|
[116] | 366 | } |
---|
| 367 | |
---|
| 368 | def edit = { |
---|
[375] | 369 | |
---|
[139] | 370 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 371 | if(params._action_Edit) |
---|
[375] | 372 | params.action='edit' |
---|
| 373 | |
---|
[405] | 374 | def result = inventoryItemService.edit(params) |
---|
[116] | 375 | |
---|
[425] | 376 | if(!result.error) { |
---|
| 377 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
[435] | 378 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 379 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 380 | |
---|
| 381 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
| 382 | possibleAlternateItems: possibleAlternateItems, |
---|
| 383 | suppliers: suppliers, |
---|
| 384 | manufacturers: manufacturers] |
---|
[425] | 385 | } |
---|
[405] | 386 | |
---|
| 387 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 388 | redirect(action:search) |
---|
[116] | 389 | } |
---|
| 390 | |
---|
| 391 | def update = { |
---|
[405] | 392 | def result = inventoryItemService.update(params) |
---|
| 393 | |
---|
| 394 | if(!result.error) { |
---|
| 395 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
| 396 | redirect(action:show, id: params.id) |
---|
| 397 | return |
---|
[116] | 398 | } |
---|
[405] | 399 | |
---|
| 400 | if(result.error.code == "default.not.found") { |
---|
| 401 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[175] | 402 | redirect(action:search) |
---|
[405] | 403 | return |
---|
[116] | 404 | } |
---|
[405] | 405 | |
---|
[425] | 406 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
[435] | 407 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 408 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 409 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), |
---|
| 410 | possibleAlternateItems: possibleAlternateItems, |
---|
| 411 | suppliers: suppliers, |
---|
| 412 | manufacturers: manufacturers]) |
---|
[116] | 413 | } |
---|
| 414 | |
---|
| 415 | def create = { |
---|
[405] | 416 | def result = inventoryItemService.create(params) |
---|
[435] | 417 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 418 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
[405] | 419 | |
---|
| 420 | if(!result.error) |
---|
[435] | 421 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
| 422 | suppliers: suppliers, |
---|
| 423 | manufacturers: manufacturers] |
---|
[405] | 424 | |
---|
| 425 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 426 | redirect(action: search) |
---|
[116] | 427 | } |
---|
| 428 | |
---|
| 429 | def save = { |
---|
[405] | 430 | def result = inventoryItemService.save(params) |
---|
| 431 | |
---|
| 432 | if(!result.error) { |
---|
| 433 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
| 434 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
| 435 | return |
---|
[116] | 436 | } |
---|
[405] | 437 | |
---|
[435] | 438 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 439 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 440 | |
---|
[405] | 441 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[435] | 442 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
| 443 | suppliers: suppliers, |
---|
| 444 | manufacturers: manufacturers]) |
---|
[116] | 445 | } |
---|
[225] | 446 | |
---|
| 447 | /** |
---|
| 448 | * Handles the use inventory item form submit in the show view. |
---|
| 449 | */ |
---|
[298] | 450 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 451 | def useInventoryItem = { |
---|
| 452 | |
---|
[226] | 453 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 454 | def result = inventoryMovementService.move(params) |
---|
| 455 | |
---|
| 456 | if(!result.error) { |
---|
| 457 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
[453] | 458 | session.inventoryMovementTaskId = null |
---|
| 459 | redirect(controller: "taskDetailed", |
---|
| 460 | action: "show", |
---|
| 461 | id: result.taskId, |
---|
| 462 | params: [showTab: "showInventoryTab"]) |
---|
| 463 | // Success. |
---|
| 464 | return |
---|
[225] | 465 | } |
---|
| 466 | |
---|
[453] | 467 | // Prepare data for the show view. |
---|
| 468 | def p = [:] |
---|
| 469 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
| 470 | def r = inventoryItemService.show(p) |
---|
[225] | 471 | |
---|
[453] | 472 | // Render show view if data was successfully prepared. |
---|
| 473 | if(!r.error) { |
---|
| 474 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
| 475 | inventoryMovementList: r.inventoryMovementList, |
---|
| 476 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
| 477 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
| 478 | inventoryItemPurchases: r.inventoryItemPurchases, |
---|
| 479 | inventoryItemPurchasesTotal: r.inventoryItemPurchasesTotal, |
---|
| 480 | showTab: r.showTab] |
---|
[225] | 481 | |
---|
[453] | 482 | model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. |
---|
[225] | 483 | |
---|
[453] | 484 | render(view: 'show', model: model) |
---|
| 485 | return |
---|
[225] | 486 | } |
---|
[453] | 487 | |
---|
| 488 | // Could not prepare data for show view so doing the next best thing. |
---|
| 489 | flash.errorMessage = g.message(code: r.error.code, args: r.error.args) |
---|
| 490 | redirect(action:search) |
---|
| 491 | |
---|
| 492 | } // useInventoryItem |
---|
| 493 | |
---|
| 494 | /** |
---|
| 495 | * Clear the use inventory item form in the show view. |
---|
| 496 | * Accomplished by clearing the session variable and ajax. |
---|
| 497 | */ |
---|
| 498 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 499 | def clearUseInventoryItem = { |
---|
| 500 | session.inventoryMovementTaskId = null |
---|
| 501 | render '' |
---|
[225] | 502 | } |
---|
| 503 | |
---|
[453] | 504 | } // end of class |
---|