[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 | |
---|
[913] | 294 | def dateFmt = { domain, value -> |
---|
| 295 | formatDate(format: "EEE, dd-MMM-yyyy", date: value) |
---|
[392] | 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.supplierList = Supplier.findAllByIsActive(true, [max:associatedPropertyMax, sort:'name']) |
---|
| 347 | |
---|
[392] | 348 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
[554] | 349 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
| 350 | filterParams: filterParams, |
---|
| 351 | params: params, |
---|
| 352 | associatedPropertyValues: associatedPropertyValues ] |
---|
[392] | 353 | } // end search() |
---|
| 354 | |
---|
[225] | 355 | /** |
---|
| 356 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 357 | */ |
---|
[298] | 358 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 359 | def findInventoryItemForMovement = { |
---|
| 360 | if(!params.task?.id) { |
---|
| 361 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
| 362 | redirect(controller: "taskDetailed", action: "search") |
---|
| 363 | return |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | session.inventoryMovementTaskId = params.task.id |
---|
| 367 | flash.message = "Please find and then select the inventory item." |
---|
| 368 | redirect(action: search) |
---|
| 369 | } |
---|
| 370 | |
---|
[298] | 371 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 372 | def show = { |
---|
[225] | 373 | |
---|
[139] | 374 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 375 | if(params._action_Show) |
---|
[375] | 376 | params.action='show' |
---|
[116] | 377 | |
---|
[405] | 378 | def result = inventoryItemService.show(params) |
---|
[225] | 379 | |
---|
[405] | 380 | if(!result.error) { |
---|
[225] | 381 | |
---|
[405] | 382 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 383 | inventoryMovementList: result.inventoryMovementList, |
---|
| 384 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 385 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
[441] | 386 | inventoryItemPurchases: result.inventoryItemPurchases, |
---|
| 387 | inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, |
---|
[405] | 388 | showTab: result.showTab] |
---|
[225] | 389 | |
---|
[405] | 390 | if(session.inventoryMovementTaskId) { |
---|
| 391 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 392 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 393 | model.inventoryMovementInstance.quantity = 1 |
---|
| 394 | } |
---|
[225] | 395 | |
---|
[405] | 396 | // Success. |
---|
| 397 | return model |
---|
[225] | 398 | } |
---|
| 399 | |
---|
[405] | 400 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 401 | redirect(action:search) |
---|
[116] | 402 | } |
---|
| 403 | |
---|
| 404 | def delete = { |
---|
[405] | 405 | def result = inventoryItemService.delete(params) |
---|
| 406 | |
---|
| 407 | if(!result.error) { |
---|
| 408 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) |
---|
[408] | 409 | redirect(action:search) |
---|
[405] | 410 | return |
---|
[116] | 411 | } |
---|
[405] | 412 | |
---|
| 413 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 414 | |
---|
| 415 | if(result.error.code == "default.not.found") { |
---|
[139] | 416 | redirect(action:search) |
---|
[405] | 417 | return |
---|
[116] | 418 | } |
---|
[405] | 419 | |
---|
| 420 | redirect(action:show, id: params.id) |
---|
[116] | 421 | } |
---|
| 422 | |
---|
| 423 | def edit = { |
---|
[375] | 424 | |
---|
[139] | 425 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 426 | if(params._action_Edit) |
---|
[375] | 427 | params.action='edit' |
---|
| 428 | |
---|
[405] | 429 | def result = inventoryItemService.edit(params) |
---|
[116] | 430 | |
---|
[425] | 431 | if(!result.error) { |
---|
[435] | 432 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 433 | |
---|
| 434 | return [ inventoryItemInstance : result.inventoryItemInstance, |
---|
[719] | 435 | suppliers: suppliers] |
---|
[425] | 436 | } |
---|
[405] | 437 | |
---|
| 438 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 439 | redirect(action:search) |
---|
[116] | 440 | } |
---|
| 441 | |
---|
| 442 | def update = { |
---|
[405] | 443 | def result = inventoryItemService.update(params) |
---|
| 444 | |
---|
| 445 | if(!result.error) { |
---|
| 446 | flash.message = g.message(code: "default.update.success", args: ["InventoryItem", params.id]) |
---|
| 447 | redirect(action:show, id: params.id) |
---|
| 448 | return |
---|
[116] | 449 | } |
---|
[405] | 450 | |
---|
| 451 | if(result.error.code == "default.not.found") { |
---|
| 452 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[175] | 453 | redirect(action:search) |
---|
[405] | 454 | return |
---|
[116] | 455 | } |
---|
[405] | 456 | |
---|
[435] | 457 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
[727] | 458 | render(view:'edit', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
[719] | 459 | suppliers: suppliers]) |
---|
[116] | 460 | } |
---|
| 461 | |
---|
| 462 | def create = { |
---|
[405] | 463 | def result = inventoryItemService.create(params) |
---|
[435] | 464 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
[405] | 465 | |
---|
| 466 | if(!result.error) |
---|
[435] | 467 | return [inventoryItemInstance: result.inventoryItemInstance, |
---|
[719] | 468 | suppliers: suppliers] |
---|
[405] | 469 | |
---|
| 470 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 471 | redirect(action: search) |
---|
[116] | 472 | } |
---|
| 473 | |
---|
| 474 | def save = { |
---|
[405] | 475 | def result = inventoryItemService.save(params) |
---|
| 476 | |
---|
| 477 | if(!result.error) { |
---|
| 478 | flash.message = g.message(code: "default.create.success", args: ["InventoryItem", result.inventoryItemInstance.id]) |
---|
| 479 | redirect(action:show, id: result.inventoryItemInstance.id) |
---|
| 480 | return |
---|
[116] | 481 | } |
---|
[405] | 482 | |
---|
[435] | 483 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 484 | |
---|
[405] | 485 | //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[435] | 486 | render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, |
---|
[719] | 487 | suppliers: suppliers]) |
---|
[116] | 488 | } |
---|
[225] | 489 | |
---|
| 490 | /** |
---|
| 491 | * Handles the use inventory item form submit in the show view. |
---|
| 492 | */ |
---|
[298] | 493 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 494 | def useInventoryItem = { |
---|
| 495 | |
---|
[226] | 496 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 497 | def result = inventoryMovementService.move(params) |
---|
| 498 | |
---|
| 499 | if(!result.error) { |
---|
| 500 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
[453] | 501 | session.inventoryMovementTaskId = null |
---|
| 502 | redirect(controller: "taskDetailed", |
---|
| 503 | action: "show", |
---|
| 504 | id: result.taskId, |
---|
| 505 | params: [showTab: "showInventoryTab"]) |
---|
| 506 | // Success. |
---|
| 507 | return |
---|
[225] | 508 | } |
---|
| 509 | |
---|
[453] | 510 | // Prepare data for the show view. |
---|
| 511 | def p = [:] |
---|
| 512 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
| 513 | def r = inventoryItemService.show(p) |
---|
[225] | 514 | |
---|
[453] | 515 | // Render show view if data was successfully prepared. |
---|
| 516 | if(!r.error) { |
---|
| 517 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
| 518 | inventoryMovementList: r.inventoryMovementList, |
---|
| 519 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
| 520 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
| 521 | inventoryItemPurchases: r.inventoryItemPurchases, |
---|
| 522 | inventoryItemPurchasesTotal: r.inventoryItemPurchasesTotal, |
---|
| 523 | showTab: r.showTab] |
---|
[225] | 524 | |
---|
[453] | 525 | model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. |
---|
[225] | 526 | |
---|
[453] | 527 | render(view: 'show', model: model) |
---|
| 528 | return |
---|
[225] | 529 | } |
---|
[453] | 530 | |
---|
| 531 | // Could not prepare data for show view so doing the next best thing. |
---|
| 532 | flash.errorMessage = g.message(code: r.error.code, args: r.error.args) |
---|
| 533 | redirect(action:search) |
---|
| 534 | |
---|
| 535 | } // useInventoryItem |
---|
| 536 | |
---|
| 537 | /** |
---|
| 538 | * Clear the use inventory item form in the show view. |
---|
| 539 | * Accomplished by clearing the session variable and ajax. |
---|
| 540 | */ |
---|
| 541 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 542 | def clearUseInventoryItem = { |
---|
| 543 | session.inventoryMovementTaskId = null |
---|
| 544 | render '' |
---|
[225] | 545 | } |
---|
| 546 | |
---|
[646] | 547 | /** |
---|
| 548 | * Search for Inventory items that require reorder. |
---|
| 549 | */ |
---|
| 550 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
| 551 | def reorder = { |
---|
| 552 | |
---|
| 553 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 554 | if(params._action_reorder) |
---|
| 555 | params.action='reorder' |
---|
| 556 | |
---|
| 557 | if(session.inventoryItemReorderSearchParamsMax) |
---|
| 558 | params.max = session.inventoryItemReorderSearchParamsMax |
---|
| 559 | |
---|
| 560 | def inventoryItemInstanceList = [] |
---|
| 561 | def inventoryItemInstanceTotal |
---|
| 562 | def filterParams = params |
---|
| 563 | def suppliers = Supplier.findAllByIsActive(true).sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) } |
---|
| 564 | def inventoryGroups = InventoryGroup.findAllByIsActive(true) |
---|
[647] | 565 | |
---|
| 566 | if(params.selectedGroups) { |
---|
| 567 | if(params.selectedGroups instanceof String) |
---|
| 568 | params.selectedGroups = [params.selectedGroups.toLong()] |
---|
| 569 | else |
---|
| 570 | params.selectedGroups = params.selectedGroups.collect { it.toLong() } |
---|
| 571 | } |
---|
[646] | 572 | else |
---|
[647] | 573 | params.selectedGroups = [] |
---|
[646] | 574 | |
---|
| 575 | // Restore search unless a new search is being requested. |
---|
| 576 | if(!params.newSearch && !params.quickSearch) { |
---|
| 577 | if(session.reorderSearchSelectedGroups) { |
---|
| 578 | params.selectedGroups = session.reorderSearchSelectedGroups |
---|
| 579 | params.selectedSupplier = session.reorderSearchSelectedSupplier |
---|
| 580 | params.includeAlternateSuppliers = session.reorderSearchIncludeAlternateSuppliers |
---|
| 581 | params.includeReorderListingDisabled = session.reorderSearchIncludeReorderListingDisabled |
---|
| 582 | params.includeOnBackOrder = session.reorderSearchIncludeOnBackOrder |
---|
| 583 | } |
---|
| 584 | else if(session.inventoryItemReorderQuickSearch) { |
---|
| 585 | params.quickSearch = session.inventoryItemReorderQuickSearch |
---|
| 586 | if(session.inventoryItemReorderQuickSearchDaysBack) |
---|
| 587 | params.daysBack = session.inventoryItemReorderQuickSearchDaysBack.toString() |
---|
| 588 | } |
---|
| 589 | } |
---|
| 590 | |
---|
| 591 | // Remember sort if supplied, otherwise try to restore. |
---|
| 592 | if(params.sort && params.order) { |
---|
| 593 | session.inventoryItemReorderSearchSort = params.sort |
---|
| 594 | session.inventoryItemReorderSearchOrder = params.order |
---|
| 595 | } |
---|
| 596 | else if(session.inventoryItemReorderSearchSort && session.inventoryItemReorderSearchOrder) { |
---|
| 597 | params.sort = session.inventoryItemReorderSearchSort |
---|
| 598 | params.order = session.inventoryItemReorderSearchOrder |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | if(params.quickSearch) { |
---|
| 602 | // Quick Search Links: |
---|
| 603 | if(!params.quickSearch) params.quickSearch = "inventoryBelowReorder" |
---|
| 604 | def result = inventoryItemSearchService.getQuickSearch(params, RCU.getLocale(request)) |
---|
| 605 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 606 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 607 | params.message = result.message |
---|
| 608 | filterParams.quickSearch = result.quickSearch |
---|
| 609 | // Remember search. |
---|
| 610 | session.inventoryItemReorderQuickSearch = result.quickSearch |
---|
| 611 | if(result.daysBack) |
---|
| 612 | session.inventoryItemReorderQuickSearchDaysBack = result.daysBack |
---|
| 613 | // Clear any previous search. |
---|
| 614 | session.removeAttribute("reorderSearchSelectedGroups") |
---|
| 615 | session.removeAttribute("reorderSearchSelectedSupplier") |
---|
| 616 | session.removeAttribute("reorderSearchIncludeAlternateSuppliers") |
---|
| 617 | session.removeAttribute("reorderSearchIncludeReorderListingDisabled") |
---|
| 618 | session.removeAttribute("reorderSearchIncludeOnBackOrder") |
---|
| 619 | } |
---|
| 620 | else { |
---|
| 621 | // Reorder Search: |
---|
| 622 | def result = inventoryItemSearchService.getReorderSearch(params, RCU.getLocale(request)) |
---|
| 623 | inventoryItemInstanceList = result.inventoryItemList |
---|
| 624 | inventoryItemInstanceTotal = result.inventoryItemList.totalCount |
---|
| 625 | params.message = result.message |
---|
| 626 | // Place limit search selects in filterParams for pagination. |
---|
| 627 | if(params.selectedGroups) { |
---|
| 628 | filterParams.selectedGroups = params.selectedGroups |
---|
| 629 | filterParams.selectedSupplier = params.selectedSupplier |
---|
| 630 | filterParams.includeAlternateSuppliers = params.includeAlternateSuppliers |
---|
| 631 | filterParams.includeReorderListingDisabled = params.includeReorderListingDisabled |
---|
| 632 | filterParams.includeOnBackOrder = params.includeOnBackOrder |
---|
| 633 | } |
---|
| 634 | // Remember search. |
---|
| 635 | session.reorderSearchSelectedGroups = params.selectedGroups |
---|
| 636 | session.reorderSearchSelectedSupplier = params.selectedSupplier |
---|
| 637 | session.reorderSearchIncludeAlternateSuppliers = params.includeAlternateSuppliers |
---|
| 638 | session.reorderSearchIncludeReorderListingDisabled = params.includeReorderListingDisabled |
---|
| 639 | session.reorderSearchIncludeOnBackOrder = params.includeOnBackOrder |
---|
| 640 | // Clear any previous search. |
---|
| 641 | session.removeAttribute("inventoryItemReorderQuickSearch") |
---|
| 642 | session.removeAttribute("inventoryItemReorderQuickSearchDaysBack") |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | // export plugin: |
---|
| 646 | if(params?.format && params.format != "html") { |
---|
| 647 | |
---|
[913] | 648 | def dateFmt = { domain, value -> |
---|
| 649 | formatDate(format: "EEE, dd-MMM-yyyy", date: value) |
---|
[646] | 650 | } |
---|
| 651 | |
---|
| 652 | String title |
---|
| 653 | if(params.quickSearch) |
---|
| 654 | title = params.message |
---|
| 655 | else |
---|
| 656 | title = "Filtered Inventory List." |
---|
| 657 | |
---|
| 658 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 659 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
| 660 | List fields = ["name", |
---|
| 661 | "description", |
---|
| 662 | "inventoryGroup", |
---|
| 663 | "unitsInStock", |
---|
| 664 | "reorderPoint", |
---|
| 665 | "unitOfMeasure", |
---|
| 666 | "inventoryLocation", |
---|
| 667 | "inventoryLocation.inventoryStore"] |
---|
| 668 | Map labels = ["name": "Name", |
---|
| 669 | "description": "Description", |
---|
| 670 | "inventoryGroup": "Group", |
---|
| 671 | "unitsInStock":"In Stock", |
---|
| 672 | "reorderPoint":"Reorder Point", |
---|
| 673 | "unitOfMeasure": "UOM", |
---|
| 674 | "inventoryLocation": "Location", |
---|
| 675 | "inventoryLocation.inventoryStore": "Store"] |
---|
| 676 | |
---|
| 677 | Map formatters = [:] |
---|
| 678 | Map parameters = [title: title, separator: ","] |
---|
| 679 | |
---|
| 680 | exportService.export(params.format, |
---|
| 681 | response.outputStream, |
---|
| 682 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
| 683 | fields, |
---|
| 684 | labels, |
---|
| 685 | formatters, |
---|
| 686 | parameters) |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | // Add some basic params to filterParams. |
---|
| 690 | filterParams.max = params.max |
---|
| 691 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 692 | filterParams.sort = params.sort ?: "name" |
---|
| 693 | filterParams.order = params.order ?: "asc" |
---|
| 694 | |
---|
| 695 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
| 696 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
| 697 | filterParams: filterParams, |
---|
| 698 | params: params, |
---|
| 699 | inventoryGroups: inventoryGroups, |
---|
| 700 | suppliers: suppliers] |
---|
| 701 | } // end reorder() |
---|
| 702 | |
---|
[453] | 703 | } // end of class |
---|