[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 |
---|
[615] | 28 | if(params.newMax?.isInteger()) { |
---|
[392] | 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 | /** |
---|
[646] | 39 | * Set session.inventoryItemReorderSearchParamsMax |
---|
| 40 | */ |
---|
| 41 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 42 | def setReorderSearchParamsMax = { |
---|
| 43 | def max = 1000 |
---|
| 44 | if(params.newMax?.isInteger()) { |
---|
| 45 | def i = params.newMax.toInteger() |
---|
| 46 | if(i > 0 && i <= max) |
---|
| 47 | session.inventoryItemReorderSearchParamsMax = params.newMax |
---|
| 48 | if(i > max) |
---|
| 49 | session.inventoryItemReorderSearchParamsMax = max |
---|
| 50 | } |
---|
| 51 | forward(action: 'reorder', params: params) |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | /** |
---|
[441] | 55 | * Display the import view. |
---|
[423] | 56 | */ |
---|
[635] | 57 | def importInventoryItemPictures = { |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Handle the import save. |
---|
| 62 | */ |
---|
| 63 | def importInventoryItemPicturesSave = { |
---|
| 64 | def result = inventoryItemService.importInventoryItemPictures(request) |
---|
| 65 | |
---|
| 66 | if(!result.error) { |
---|
| 67 | def logFileLink = g.link(controller: "appCore", action: "appLog") {"log"} |
---|
| 68 | flash.message = g.message(code: "inventoryItemPictures.import.success", args: [logFileLink]) |
---|
| 69 | redirect(action:search) |
---|
| 70 | return |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 74 | redirect(action: importInventoryItemPictures) |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Display the import view. |
---|
| 79 | */ |
---|
[423] | 80 | def importInventory = { |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Handle the import save. |
---|
| 85 | */ |
---|
| 86 | def importInventorySave = { |
---|
| 87 | def result = inventoryCsvService.importInventory(request) |
---|
| 88 | |
---|
| 89 | if(!result.error) { |
---|
| 90 | flash.message = g.message(code: "inventory.import.success") |
---|
| 91 | redirect(action:search) |
---|
| 92 | return |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 96 | redirect(action: importInventory) |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Export a csv template. |
---|
| 101 | * NOTE: IE has a 'validating' bug in dev mode that causes the export to take a long time! |
---|
| 102 | * This does not appear to be a problem once deployed to Tomcat. |
---|
| 103 | */ |
---|
[392] | 104 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[423] | 105 | def exportInventoryTemplate = { |
---|
| 106 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 107 | response.setHeader("Content-disposition", "attachment; filename=InventoryTemplate.csv") |
---|
| 108 | def s = inventoryCsvService.buildInventoryTemplate() |
---|
| 109 | render s |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /** |
---|
| 113 | * Export a csv test file. |
---|
| 114 | */ |
---|
| 115 | def exportInventoryExample = { |
---|
| 116 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 117 | response.setHeader("Content-disposition", "attachment; filename=InventoryExample.csv") |
---|
| 118 | def s = inventoryCsvService.buildInventoryExample() |
---|
| 119 | render s |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
| 123 | * Export the entire inventory as a csv file. |
---|
| 124 | */ |
---|
| 125 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 126 | def exportInventory = { |
---|
| 127 | |
---|
| 128 | def inventoryItemList = InventoryItem.list() |
---|
| 129 | |
---|
| 130 | response.contentType = ConfigurationHolder.config.grails.mime.types["csv"] |
---|
| 131 | response.setHeader("Content-disposition", "attachment; filename=Inventory.csv") |
---|
| 132 | def s = inventoryCsvService.buildInventory(inventoryItemList) |
---|
| 133 | render s |
---|
| 134 | } |
---|
| 135 | |
---|
[441] | 136 | /** |
---|
| 137 | * Display the import view for purchases. |
---|
| 138 | */ |
---|
| 139 | def importInventoryItemPurchases = { |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /** |
---|
| 143 | * Handle the inventory purchases import save. |
---|
| 144 | */ |
---|
| 145 | def importInventoryItemPurchasesSave = { |
---|
| 146 | def result = inventoryCsvService.importInventoryItemPurchases(request) |
---|
| 147 | |
---|
| 148 | if(!result.error) { |
---|
| 149 | flash.message = g.message(code: "inventory.import.success") |
---|
| 150 | redirect(action:search) |
---|
| 151 | return |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 155 | redirect(action: importInventoryItemPurchases) |
---|
| 156 | } |
---|
| 157 | |
---|
[485] | 158 | /** |
---|
| 159 | * Search for Inventory items. |
---|
| 160 | */ |
---|
[423] | 161 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[139] | 162 | def search = { |
---|
[392] | 163 | |
---|
| 164 | if(session.inventoryItemSearchParamsMax) |
---|
| 165 | params.max = session.inventoryItemSearchParamsMax |
---|
| 166 | |
---|
[485] | 167 | // Protect filterPane. |
---|
| 168 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000) |
---|
[156] | 169 | |
---|
[392] | 170 | def inventoryItemInstanceList = [] |
---|
| 171 | def inventoryItemInstanceTotal |
---|
[485] | 172 | def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 173 | def isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
[392] | 174 | |
---|
[562] | 175 | // Restore default sort if a new text search is requested |
---|
| 176 | if(params.newTextSearch) { |
---|
| 177 | params.sort = 'id' |
---|
| 178 | params.order = 'desc' |
---|
| 179 | } |
---|
| 180 | |
---|
[485] | 181 | // Restore search unless a new search is being requested. |
---|
[562] | 182 | if(!params.searchText && !params.quickSearch && !filterParams) { |
---|
| 183 | if(session.inventoryItemSearchText) { |
---|
| 184 | params.searchText = session.inventoryItemSearchText |
---|
| 185 | params.searchName = session.inventoryItemSearchName |
---|
| 186 | params.searchDescription = session.inventoryItemSearchDescription |
---|
| 187 | params.searchComment = session.inventoryItemSearchComment |
---|
| 188 | params.searchLocation = session.inventoryItemSearchLocation |
---|
[566] | 189 | params.searchGroup = session.inventoryItemSearchGroup |
---|
[562] | 190 | params.searchSpareFor = session.inventoryItemSearchSpareFor |
---|
| 191 | } |
---|
| 192 | else if(session.inventoryItemQuickSearch) { |
---|
[485] | 193 | params.quickSearch = session.inventoryItemQuickSearch |
---|
[489] | 194 | if(session.inventoryItemQuickSearchDaysBack) |
---|
| 195 | params.daysBack = session.inventoryItemQuickSearchDaysBack.toString() |
---|
| 196 | } |
---|
[485] | 197 | else if(session.inventoryItemSearchFilterParams) { |
---|
| 198 | session.inventoryItemSearchFilterParams.each() { params[it.key] = it.value } |
---|
| 199 | params.filter = session.inventoryItemSearchFilter |
---|
| 200 | isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
| 201 | } |
---|
[156] | 202 | } |
---|
[485] | 203 | |
---|
| 204 | // Remember sort if supplied, otherwise try to restore. |
---|
| 205 | if(params.sort && params.order) { |
---|
| 206 | session.inventoryItemSearchSort = params.sort |
---|
| 207 | session.inventoryItemSearchOrder = params.order |
---|
| 208 | } |
---|
| 209 | else if(session.inventoryItemSearchSort && session.inventoryItemSearchOrder) { |
---|
| 210 | params.sort = session.inventoryItemSearchSort |
---|
| 211 | params.order = session.inventoryItemSearchOrder |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | if(isFilterApplied) { |
---|
| 215 | // filterPane: |
---|
[392] | 216 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
| 217 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
| 218 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
[485] | 219 | // Remember search. |
---|
| 220 | session.inventoryItemSearchFilterParams = new LinkedHashMap(filterParams) |
---|
| 221 | session.inventoryItemSearchFilter = new LinkedHashMap(params.filter) |
---|
[562] | 222 | // Clear any previous search. |
---|
| 223 | session.removeAttribute("inventoryItemSearchText") |
---|
| 224 | session.removeAttribute("inventoryItemSearchName") |
---|
| 225 | session.removeAttribute("inventoryItemSearchDescription") |
---|
| 226 | session.removeAttribute("inventoryItemSearchComment") |
---|
| 227 | session.removeAttribute("inventoryItemSearchLocation") |
---|
[566] | 228 | session.removeAttribute("inventoryItemSearchGroup") |
---|
[562] | 229 | session.removeAttribute("inventoryItemSearchSpareFor") |
---|
[489] | 230 | session.removeAttribute("inventoryItemQuickSearch") |
---|
| 231 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
[392] | 232 | } |
---|
[562] | 233 | else if(params.searchText) { |
---|
[634] | 234 | // Text Search: |
---|
[562] | 235 | def result = inventoryItemSearchService.getTextSearch(params, RCU.getLocale(request)) |
---|
| 236 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 237 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 238 | params.message = result.message |
---|
| 239 | filterParams.searchText = result.searchText |
---|
[634] | 240 | // Place limit search selects in filterParams for pagination. |
---|
| 241 | if(params.searchName) |
---|
| 242 | filterParams.searchName = params.searchName |
---|
| 243 | if(params.searchDescription) |
---|
| 244 | filterParams.searchDescription = params.searchDescription |
---|
| 245 | if(params.searchComment) |
---|
| 246 | filterParams.searchComment = params.searchComment |
---|
| 247 | if(params.searchLocation) |
---|
| 248 | filterParams.searchLocation = params.searchLocation |
---|
| 249 | if(params.searchGroup) |
---|
| 250 | filterParams.searchGroup = params.searchGroup |
---|
| 251 | if(params.searchSpareFor) |
---|
| 252 | filterParams.searchSpareFor = params.searchSpareFor |
---|
[562] | 253 | // Remember search. |
---|
| 254 | session.inventoryItemSearchText = params.searchText |
---|
| 255 | session.inventoryItemSearchName = params.searchName |
---|
| 256 | session.inventoryItemSearchDescription = params.searchDescription |
---|
| 257 | session.inventoryItemSearchComment = params.searchComment |
---|
| 258 | session.inventoryItemSearchLocation = params.searchLocation |
---|
[566] | 259 | session.inventoryItemSearchGroup = params.searchGroup |
---|
[562] | 260 | session.inventoryItemSearchSpareFor = params.searchSpareFor |
---|
| 261 | // Clear any previous search. |
---|
| 262 | session.removeAttribute("inventoryItemQuickSearch") |
---|
| 263 | session.removeAttribute("inventoryItemQuickSearchDaysBack") |
---|
| 264 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
| 265 | session.removeAttribute("inventoryItemSearchFilter") |
---|
| 266 | } |
---|
[485] | 267 | else { |
---|
[562] | 268 | // Quick Search Links: |
---|
[485] | 269 | if(!params.quickSearch) params.quickSearch = "all" |
---|
| 270 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
| 271 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 272 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 273 | params.message = result.message |
---|
| 274 | filterParams.quickSearch = result.quickSearch |
---|
| 275 | // Remember search. |
---|
| 276 | session.inventoryItemQuickSearch = result.quickSearch |
---|
[489] | 277 | if(result.daysBack) |
---|
| 278 | session.inventoryItemQuickSearchDaysBack = result.daysBack |
---|
[562] | 279 | // Clear any previous search. |
---|
| 280 | session.removeAttribute("inventoryItemSearchText") |
---|
| 281 | session.removeAttribute("inventoryItemSearchName") |
---|
| 282 | session.removeAttribute("inventoryItemSearchDescription") |
---|
| 283 | session.removeAttribute("inventoryItemSearchComment") |
---|
| 284 | session.removeAttribute("inventoryItemSearchLocation") |
---|
[566] | 285 | session.removeAttribute("inventoryItemSearchGroup") |
---|
[562] | 286 | session.removeAttribute("inventoryItemSearchSpareFor") |
---|
| 287 | session.removeAttribute("inventoryItemSearchFilterParams") |
---|
| 288 | session.removeAttribute("inventoryItemSearchFilter") |
---|
[485] | 289 | } |
---|
[139] | 290 | |
---|
[392] | 291 | // export plugin: |
---|
| 292 | if(params?.format && params.format != "html") { |
---|
| 293 | |
---|
| 294 | def dateFmt = { date -> |
---|
| 295 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 296 | } |
---|
| 297 | |
---|
[485] | 298 | String title |
---|
| 299 | if(params.quickSearch) |
---|
| 300 | title = params.message |
---|
| 301 | else |
---|
| 302 | title = "Filtered Inventory List." |
---|
| 303 | |
---|
[392] | 304 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 305 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
| 306 | List fields = ["name", |
---|
| 307 | "description", |
---|
[507] | 308 | "inventoryGroup", |
---|
[392] | 309 | "unitsInStock", |
---|
[485] | 310 | "reorderPoint", |
---|
[392] | 311 | "unitOfMeasure", |
---|
| 312 | "inventoryLocation", |
---|
| 313 | "inventoryLocation.inventoryStore"] |
---|
| 314 | Map labels = ["name": "Name", |
---|
| 315 | "description": "Description", |
---|
[507] | 316 | "inventoryGroup": "Group", |
---|
[392] | 317 | "unitsInStock":"In Stock", |
---|
[485] | 318 | "reorderPoint":"Reorder Point", |
---|
[392] | 319 | "unitOfMeasure": "UOM", |
---|
| 320 | "inventoryLocation": "Location", |
---|
| 321 | "inventoryLocation.inventoryStore": "Store"] |
---|
| 322 | |
---|
| 323 | Map formatters = [:] |
---|
| 324 | Map parameters = [title: title, separator: ","] |
---|
| 325 | |
---|
| 326 | exportService.export(params.format, |
---|
| 327 | response.outputStream, |
---|
| 328 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 329 | fields, |
---|
| 330 | labels, |
---|
| 331 | formatters, |
---|
| 332 | parameters) |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | // Add some basic params to filterParams. |
---|
| 336 | filterParams.max = params.max |
---|
| 337 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
[485] | 338 | filterParams.sort = params.sort ?: "name" |
---|
| 339 | filterParams.order = params.order ?: "asc" |
---|
[392] | 340 | |
---|
[554] | 341 | // Get some associatedProperty values for filterpane. |
---|
| 342 | def associatedPropertyValues = [:] |
---|
| 343 | def associatedPropertyMax = 10000 |
---|
| 344 | associatedPropertyValues.inventoryLocationList = InventoryLocation.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 345 | associatedPropertyValues.assetList = Asset.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 346 | associatedPropertyValues.manufacturerList = Manufacturer.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 347 | associatedPropertyValues.supplierList = Supplier.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 348 | |
---|
[392] | 349 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
[554] | 350 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
| 351 | filterParams: filterParams, |
---|
| 352 | params: params, |
---|
| 353 | associatedPropertyValues: associatedPropertyValues ] |
---|
[392] | 354 | } // end search() |
---|
| 355 | |
---|
[225] | 356 | /** |
---|
| 357 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 358 | */ |
---|
[298] | 359 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 360 | def findInventoryItemForMovement = { |
---|
| 361 | if(!params.task?.id) { |
---|
| 362 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
| 363 | redirect(controller: "taskDetailed", action: "search") |
---|
| 364 | return |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | session.inventoryMovementTaskId = params.task.id |
---|
| 368 | flash.message = "Please find and then select the inventory item." |
---|
| 369 | redirect(action: search) |
---|
| 370 | } |
---|
| 371 | |
---|
[298] | 372 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 373 | def show = { |
---|
[225] | 374 | |
---|
[139] | 375 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 376 | if(params._action_Show) |
---|
[375] | 377 | params.action='show' |
---|
[116] | 378 | |
---|
[405] | 379 | def result = inventoryItemService.show(params) |
---|
[225] | 380 | |
---|
[405] | 381 | if(!result.error) { |
---|
[225] | 382 | |
---|
[405] | 383 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 384 | inventoryMovementList: result.inventoryMovementList, |
---|
| 385 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 386 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
[441] | 387 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
| 388 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
[405] | 389 | showTab: result.showTab] |
---|
[225] | 390 | |
---|
[405] | 391 | if(session.inventoryMovementTaskId) { |
---|
| 392 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 393 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 394 | model.inventoryMovementInstance.quantity = 1 |
---|
| 395 | } |
---|
[225] | 396 | |
---|
[405] | 397 | // Success. |
---|
| 398 | return model |
---|
[225] | 399 | } |
---|
| 400 | |
---|
[405] | 401 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 402 | redirect(action:search) |
---|
[116] | 403 | } |
---|
| 404 | |
---|
| 405 | def delete = { |
---|
[405] | 406 | def result = inventoryItemService.delete(params) |
---|
| 407 | |
---|
| 408 | if(!result.error) { |
---|
| 409 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
[408] | 410 | redirect(action:search) |
---|
[405] | 411 | return |
---|
[116] | 412 | } |
---|
[405] | 413 | |
---|
| 414 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 415 | |
---|
| 416 | if(result.error.code == "default.not.found") { |
---|
[139] | 417 | redirect(action:search) |
---|
[405] | 418 | return |
---|
[116] | 419 | } |
---|
[405] | 420 | |
---|
| 421 | redirect(action:show, id: params.id) |
---|
[116] | 422 | } |
---|
| 423 | |
---|
| 424 | def edit = { |
---|
[375] | 425 | |
---|
[139] | 426 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 427 | if(params._action_Edit) |
---|
[375] | 428 | params.action='edit' |
---|
| 429 | |
---|
[405] | 430 | def result = inventoryItemService.edit(params) |
---|
[116] | 431 | |
---|
[425] | 432 | if(!result.error) { |
---|
| 433 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
[435] | 434 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 435 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 436 | |
---|
| 437 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
| 438 | possibleAlternateItems: possibleAlternateItems, |
---|
| 439 | suppliers: suppliers, |
---|
| 440 | manufacturers: manufacturers] |
---|
[425] | 441 | } |
---|
[405] | 442 | |
---|
| 443 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 444 | redirect(action:search) |
---|
[116] | 445 | } |
---|
| 446 | |
---|
| 447 | def update = { |
---|
[405] | 448 | def result = inventoryItemService.update(params) |
---|
| 449 | |
---|
| 450 | if(!result.error) { |
---|
| 451 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
| 452 | redirect(action:show, id: params.id) |
---|
| 453 | return |
---|
[116] | 454 | } |
---|
[405] | 455 | |
---|
| 456 | if(result.error.code == "default.not.found") { |
---|
| 457 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[175] | 458 | redirect(action:search) |
---|
[405] | 459 | return |
---|
[116] | 460 | } |
---|
[405] | 461 | |
---|
[425] | 462 | def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) |
---|
[435] | 463 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 464 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 465 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance.attach(), |
---|
| 466 | possibleAlternateItems: possibleAlternateItems, |
---|
| 467 | suppliers: suppliers, |
---|
| 468 | manufacturers: manufacturers]) |
---|
[116] | 469 | } |
---|
| 470 | |
---|
| 471 | def create = { |
---|
[405] | 472 | def result = inventoryItemService.create(params) |
---|
[435] | 473 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 474 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
[405] | 475 | |
---|
| 476 | if(!result.error) |
---|
[435] | 477 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
| 478 | suppliers: suppliers, |
---|
| 479 | manufacturers: manufacturers] |
---|
[405] | 480 | |
---|
| 481 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 482 | redirect(action: search) |
---|
[116] | 483 | } |
---|
| 484 | |
---|
| 485 | def save = { |
---|
[405] | 486 | def result = inventoryItemService.save(params) |
---|
| 487 | |
---|
| 488 | if(!result.error) { |
---|
| 489 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
| 490 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
| 491 | return |
---|
[116] | 492 | } |
---|
[405] | 493 | |
---|
[435] | 494 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 495 | def manufacturers = Manufacturer.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 496 | |
---|
[405] | 497 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[435] | 498 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
| 499 | suppliers: suppliers, |
---|
| 500 | manufacturers: manufacturers]) |
---|
[116] | 501 | } |
---|
[225] | 502 | |
---|
| 503 | /** |
---|
| 504 | * Handles the use inventory item form submit in the show view. |
---|
| 505 | */ |
---|
[298] | 506 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 507 | def useInventoryItem = { |
---|
| 508 | |
---|
[226] | 509 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 510 | def result = inventoryMovementService.move(params) |
---|
| 511 | |
---|
| 512 | if(!result.error) { |
---|
| 513 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
[453] | 514 | session.inventoryMovementTaskId = null |
---|
| 515 | redirect(controller: "taskDetailed", |
---|
| 516 | action: "show", |
---|
| 517 | id: result.taskId, |
---|
| 518 | params: [showTab: "showInventoryTab"]) |
---|
| 519 | // Success. |
---|
| 520 | return |
---|
[225] | 521 | } |
---|
| 522 | |
---|
[453] | 523 | // Prepare data for the show view. |
---|
| 524 | def p = [:] |
---|
| 525 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
| 526 | def r = inventoryItemService.show(p) |
---|
[225] | 527 | |
---|
[453] | 528 | // Render show view if data was successfully prepared. |
---|
| 529 | if(!r.error) { |
---|
| 530 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
| 531 | inventoryMovementList: r.inventoryMovementList, |
---|
| 532 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
| 533 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
| 534 | inventoryItemPurchases: r.inventoryItemPurchases, |
---|
| 535 | inventoryItemPurchasesTotal: r.inventoryItemPurchasesTotal, |
---|
| 536 | showTab: r.showTab] |
---|
[225] | 537 | |
---|
[453] | 538 | model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. |
---|
[225] | 539 | |
---|
[453] | 540 | render(view: 'show', model: model) |
---|
| 541 | return |
---|
[225] | 542 | } |
---|
[453] | 543 | |
---|
| 544 | // Could not prepare data for show view so doing the next best thing. |
---|
| 545 | flash.errorMessage = g.message(code: r.error.code, args: r.error.args) |
---|
| 546 | redirect(action:search) |
---|
| 547 | |
---|
| 548 | } // useInventoryItem |
---|
| 549 | |
---|
| 550 | /** |
---|
| 551 | * Clear the use inventory item form in the show view. |
---|
| 552 | * Accomplished by clearing the session variable and ajax. |
---|
| 553 | */ |
---|
| 554 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 555 | def clearUseInventoryItem = { |
---|
| 556 | session.inventoryMovementTaskId = null |
---|
| 557 | render '' |
---|
[225] | 558 | } |
---|
| 559 | |
---|
[646] | 560 | /** |
---|
| 561 | * Search for Inventory items that require reorder. |
---|
| 562 | */ |
---|
| 563 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
| 564 | def reorder = { |
---|
| 565 | |
---|
| 566 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 567 | if(params._action_reorder) |
---|
| 568 | params.action='reorder' |
---|
| 569 | |
---|
| 570 | if(session.inventoryItemReorderSearchParamsMax) |
---|
| 571 | params.max = session.inventoryItemReorderSearchParamsMax |
---|
| 572 | |
---|
| 573 | def inventoryItemInstanceList = [] |
---|
| 574 | def inventoryItemInstanceTotal |
---|
| 575 | def filterParams = params |
---|
| 576 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 577 | def inventoryGroups = InventoryGroup.findAllByIsActive(true) |
---|
[647] | 578 | |
---|
| 579 | if(params.selectedGroups) { |
---|
| 580 | if(params.selectedGroups instanceof String) |
---|
| 581 | params.selectedGroups = [params.selectedGroups.toLong()] |
---|
| 582 | else |
---|
| 583 | params.selectedGroups = params.selectedGroups.collect { it.toLong() } |
---|
| 584 | } |
---|
[646] | 585 | else |
---|
[647] | 586 | params.selectedGroups = [] |
---|
[646] | 587 | |
---|
| 588 | // Restore search unless a new search is being requested. |
---|
| 589 | if(!params.newSearch && !params.quickSearch) { |
---|
| 590 | if(session.reorderSearchSelectedGroups) { |
---|
| 591 | params.selectedGroups = session.reorderSearchSelectedGroups |
---|
| 592 | params.selectedSupplier = session.reorderSearchSelectedSupplier |
---|
| 593 | params.includeAlternateSuppliers = session.reorderSearchIncludeAlternateSuppliers |
---|
| 594 | params.includeReorderListingDisabled = session.reorderSearchIncludeReorderListingDisabled |
---|
| 595 | params.includeOnBackOrder = session.reorderSearchIncludeOnBackOrder |
---|
| 596 | } |
---|
| 597 | else if(session.inventoryItemReorderQuickSearch) { |
---|
| 598 | params.quickSearch = session.inventoryItemReorderQuickSearch |
---|
| 599 | if(session.inventoryItemReorderQuickSearchDaysBack) |
---|
| 600 | params.daysBack = session.inventoryItemReorderQuickSearchDaysBack.toString() |
---|
| 601 | } |
---|
| 602 | } |
---|
| 603 | |
---|
| 604 | // Remember sort if supplied, otherwise try to restore. |
---|
| 605 | if(params.sort && params.order) { |
---|
| 606 | session.inventoryItemReorderSearchSort = params.sort |
---|
| 607 | session.inventoryItemReorderSearchOrder = params.order |
---|
| 608 | } |
---|
| 609 | else if(session.inventoryItemReorderSearchSort && session.inventoryItemReorderSearchOrder) { |
---|
| 610 | params.sort = session.inventoryItemReorderSearchSort |
---|
| 611 | params.order = session.inventoryItemReorderSearchOrder |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | if(params.quickSearch) { |
---|
| 615 | // Quick Search Links: |
---|
| 616 | if(!params.quickSearch) params.quickSearch = "inventoryBelowReorder" |
---|
| 617 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
| 618 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 619 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 620 | params.message = result.message |
---|
| 621 | filterParams.quickSearch = result.quickSearch |
---|
| 622 | // Remember search. |
---|
| 623 | session.inventoryItemReorderQuickSearch = result.quickSearch |
---|
| 624 | if(result.daysBack) |
---|
| 625 | session.inventoryItemReorderQuickSearchDaysBack = result.daysBack |
---|
| 626 | // Clear any previous search. |
---|
| 627 | session.removeAttribute("reorderSearchSelectedGroups") |
---|
| 628 | session.removeAttribute("reorderSearchSelectedSupplier") |
---|
| 629 | session.removeAttribute("reorderSearchIncludeAlternateSuppliers") |
---|
| 630 | session.removeAttribute("reorderSearchIncludeReorderListingDisabled") |
---|
| 631 | session.removeAttribute("reorderSearchIncludeOnBackOrder") |
---|
| 632 | } |
---|
| 633 | else { |
---|
| 634 | // Reorder Search: |
---|
| 635 | def result = inventoryItemSearchService.getReorderSearch(params, RCU.getLocale(request)) |
---|
| 636 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 637 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 638 | params.message = result.message |
---|
| 639 | // Place limit search selects in filterParams for pagination. |
---|
| 640 | if(params.selectedGroups) { |
---|
| 641 | filterParams.selectedGroups = params.selectedGroups |
---|
| 642 | filterParams.selectedSupplier = params.selectedSupplier |
---|
| 643 | filterParams.includeAlternateSuppliers = params.includeAlternateSuppliers |
---|
| 644 | filterParams.includeReorderListingDisabled = params.includeReorderListingDisabled |
---|
| 645 | filterParams.includeOnBackOrder = params.includeOnBackOrder |
---|
| 646 | } |
---|
| 647 | // Remember search. |
---|
| 648 | session.reorderSearchSelectedGroups = params.selectedGroups |
---|
| 649 | session.reorderSearchSelectedSupplier = params.selectedSupplier |
---|
| 650 | session.reorderSearchIncludeAlternateSuppliers = params.includeAlternateSuppliers |
---|
| 651 | session.reorderSearchIncludeReorderListingDisabled = params.includeReorderListingDisabled |
---|
| 652 | session.reorderSearchIncludeOnBackOrder = params.includeOnBackOrder |
---|
| 653 | // Clear any previous search. |
---|
| 654 | session.removeAttribute("inventoryItemReorderQuickSearch") |
---|
| 655 | session.removeAttribute("inventoryItemReorderQuickSearchDaysBack") |
---|
| 656 | } |
---|
| 657 | |
---|
| 658 | // export plugin: |
---|
| 659 | if(params?.format && params.format != "html") { |
---|
| 660 | |
---|
| 661 | def dateFmt = { date -> |
---|
| 662 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 663 | } |
---|
| 664 | |
---|
| 665 | String title |
---|
| 666 | if(params.quickSearch) |
---|
| 667 | title = params.message |
---|
| 668 | else |
---|
| 669 | title = "Filtered Inventory List." |
---|
| 670 | |
---|
| 671 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 672 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
| 673 | List fields = ["name", |
---|
| 674 | "description", |
---|
| 675 | "inventoryGroup", |
---|
| 676 | "unitsInStock", |
---|
| 677 | "reorderPoint", |
---|
| 678 | "unitOfMeasure", |
---|
| 679 | "inventoryLocation", |
---|
| 680 | "inventoryLocation.inventoryStore"] |
---|
| 681 | Map labels = ["name": "Name", |
---|
| 682 | "description": "Description", |
---|
| 683 | "inventoryGroup": "Group", |
---|
| 684 | "unitsInStock":"In Stock", |
---|
| 685 | "reorderPoint":"Reorder Point", |
---|
| 686 | "unitOfMeasure": "UOM", |
---|
| 687 | "inventoryLocation": "Location", |
---|
| 688 | "inventoryLocation.inventoryStore": "Store"] |
---|
| 689 | |
---|
| 690 | Map formatters = [:] |
---|
| 691 | Map parameters = [title: title, separator: ","] |
---|
| 692 | |
---|
| 693 | exportService.export(params.format, |
---|
| 694 | response.outputStream, |
---|
| 695 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 696 | fields, |
---|
| 697 | labels, |
---|
| 698 | formatters, |
---|
| 699 | parameters) |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | // Add some basic params to filterParams. |
---|
| 703 | filterParams.max = params.max |
---|
| 704 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 705 | filterParams.sort = params.sort ?: "name" |
---|
| 706 | filterParams.order = params.order ?: "asc" |
---|
| 707 | |
---|
| 708 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
| 709 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
| 710 | filterParams: filterParams, |
---|
| 711 | params: params, |
---|
| 712 | inventoryGroups: inventoryGroups, |
---|
| 713 | suppliers: suppliers] |
---|
| 714 | } // end reorder() |
---|
| 715 | |
---|
[453] | 716 | } // end of class |
---|