| 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured | 
|---|
| 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder | 
|---|
| 3 | import com.zeddware.grails.plugins.filterpane.FilterUtils | 
|---|
| 4 | import org.springframework.web.servlet.support.RequestContextUtils as RCU | 
|---|
| 5 |  | 
|---|
| 6 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) | 
|---|
| 7 | class InventoryItemDetailedController extends BaseController { | 
|---|
| 8 |  | 
|---|
| 9 |     def filterService | 
|---|
| 10 |     def exportService | 
|---|
| 11 |     def inventoryCsvService | 
|---|
| 12 |     def inventoryItemService | 
|---|
| 13 |     def inventoryItemSearchService | 
|---|
| 14 |     def inventoryMovementService | 
|---|
| 15 |  | 
|---|
| 16 |     // the delete, save and update actions only accept POST requests | 
|---|
| 17 |     static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] | 
|---|
| 18 |  | 
|---|
| 19 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 20 |     def index = { redirect(action:search, params:params) } | 
|---|
| 21 |  | 
|---|
| 22 |     /** | 
|---|
| 23 |     * Set session.inventoryItemSearchParamsMax | 
|---|
| 24 |     */ | 
|---|
| 25 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 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 |  | 
|---|
| 38 |     /** | 
|---|
| 39 |     * Display the import view. | 
|---|
| 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 |     */ | 
|---|
| 65 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 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 |  | 
|---|
| 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 |  | 
|---|
| 119 |     /** | 
|---|
| 120 |     * Search for Inventory items. | 
|---|
| 121 |     */ | 
|---|
| 122 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 123 |     def search = { | 
|---|
| 124 |  | 
|---|
| 125 |         if(session.inventoryItemSearchParamsMax) | 
|---|
| 126 |             params.max = session.inventoryItemSearchParamsMax | 
|---|
| 127 |  | 
|---|
| 128 |         // Protect filterPane. | 
|---|
| 129 |         params.max = Math.min( params.max ? params.max.toInteger() : 10,  1000) | 
|---|
| 130 |  | 
|---|
| 131 |         def inventoryItemInstanceList = [] | 
|---|
| 132 |         def inventoryItemInstanceTotal | 
|---|
| 133 |         def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) | 
|---|
| 134 |         def isFilterApplied = FilterUtils.isFilterApplied(params) | 
|---|
| 135 |  | 
|---|
| 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 |  | 
|---|
| 142 |         // Restore search unless a new search is being requested. | 
|---|
| 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) { | 
|---|
| 153 |                 params.quickSearch = session.inventoryItemQuickSearch | 
|---|
| 154 |                 if(session.inventoryItemQuickSearchDaysBack) | 
|---|
| 155 |                     params.daysBack = session.inventoryItemQuickSearchDaysBack.toString() | 
|---|
| 156 |             } | 
|---|
| 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 |             } | 
|---|
| 162 |         } | 
|---|
| 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: | 
|---|
| 176 |             inventoryItemInstanceList = filterService.filter( params, InventoryItem ) | 
|---|
| 177 |             inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) | 
|---|
| 178 |             filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) | 
|---|
| 179 |             // Remember search. | 
|---|
| 180 |             session.inventoryItemSearchFilterParams = new LinkedHashMap(filterParams) | 
|---|
| 181 |             session.inventoryItemSearchFilter = new LinkedHashMap(params.filter) | 
|---|
| 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") | 
|---|
| 189 |             session.removeAttribute("inventoryItemQuickSearch") | 
|---|
| 190 |             session.removeAttribute("inventoryItemQuickSearchDaysBack") | 
|---|
| 191 |         } | 
|---|
| 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 |         } | 
|---|
| 212 |         else { | 
|---|
| 213 |             // Quick Search Links: | 
|---|
| 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 | 
|---|
| 222 |             if(result.daysBack) | 
|---|
| 223 |                 session.inventoryItemQuickSearchDaysBack = result.daysBack | 
|---|
| 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") | 
|---|
| 233 |         } | 
|---|
| 234 |  | 
|---|
| 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 |  | 
|---|
| 242 |             String title | 
|---|
| 243 |             if(params.quickSearch) | 
|---|
| 244 |                 title = params.message | 
|---|
| 245 |             else | 
|---|
| 246 |                 title = "Filtered Inventory List." | 
|---|
| 247 |  | 
|---|
| 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", | 
|---|
| 252 |                                 "inventoryGroup", | 
|---|
| 253 |                                 "unitsInStock", | 
|---|
| 254 |                                 "reorderPoint", | 
|---|
| 255 |                                 "unitOfMeasure", | 
|---|
| 256 |                                 "inventoryLocation", | 
|---|
| 257 |                                 "inventoryLocation.inventoryStore"] | 
|---|
| 258 |             Map labels = ["name": "Name", | 
|---|
| 259 |                                 "description": "Description", | 
|---|
| 260 |                                 "inventoryGroup": "Group", | 
|---|
| 261 |                                 "unitsInStock":"In Stock", | 
|---|
| 262 |                                 "reorderPoint":"Reorder Point", | 
|---|
| 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 | 
|---|
| 282 |         filterParams.sort = params.sort ?: "name" | 
|---|
| 283 |         filterParams.order = params.order ?: "asc" | 
|---|
| 284 |  | 
|---|
| 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 |  | 
|---|
| 293 |         return[ inventoryItemInstanceList: inventoryItemInstanceList, | 
|---|
| 294 |                         inventoryItemInstanceTotal: inventoryItemInstanceTotal, | 
|---|
| 295 |                         filterParams: filterParams, | 
|---|
| 296 |                         params: params, | 
|---|
| 297 |                         associatedPropertyValues: associatedPropertyValues ] | 
|---|
| 298 |     } // end search() | 
|---|
| 299 |  | 
|---|
| 300 |     /** | 
|---|
| 301 |     * Simply assigns a passed in task id to a session variable and redirects to search. | 
|---|
| 302 |     */ | 
|---|
| 303 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 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 |  | 
|---|
| 316 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 317 |     def show = { | 
|---|
| 318 |  | 
|---|
| 319 |         // In the case of an actionSubmit button, rewrite action name from 'index'. | 
|---|
| 320 |         if(params._action_Show) | 
|---|
| 321 |             params.action='show' | 
|---|
| 322 |  | 
|---|
| 323 |         def result = inventoryItemService.show(params) | 
|---|
| 324 |  | 
|---|
| 325 |         if(!result.error) { | 
|---|
| 326 |  | 
|---|
| 327 |             def model = [ inventoryItemInstance: result.inventoryItemInstance, | 
|---|
| 328 |                                     inventoryMovementList: result.inventoryMovementList, | 
|---|
| 329 |                                     inventoryMovementListTotal: result.inventoryMovementListTotal, | 
|---|
| 330 |                                     inventoryMovementListMax: result.inventoryMovementListMax, | 
|---|
| 331 |                                     inventoryItemPurchases: result.inventoryItemPurchases, | 
|---|
| 332 |                                     inventoryItemPurchasesTotal: result.inventoryItemPurchasesTotal, | 
|---|
| 333 |                                     showTab: result.showTab] | 
|---|
| 334 |  | 
|---|
| 335 |             if(session.inventoryMovementTaskId) { | 
|---|
| 336 |                 model.inventoryMovementInstance = new InventoryMovement() | 
|---|
| 337 |                 model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) | 
|---|
| 338 |                 model.inventoryMovementInstance.quantity = 1 | 
|---|
| 339 |             } | 
|---|
| 340 |  | 
|---|
| 341 |             // Success. | 
|---|
| 342 |             return model | 
|---|
| 343 |         } | 
|---|
| 344 |  | 
|---|
| 345 |         flash.errorMessage = g.message(code: result.error.code, args: result.error.args) | 
|---|
| 346 |         redirect(action:search) | 
|---|
| 347 |     } | 
|---|
| 348 |  | 
|---|
| 349 |     def delete = { | 
|---|
| 350 |         def result = inventoryItemService.delete(params) | 
|---|
| 351 |  | 
|---|
| 352 |         if(!result.error) { | 
|---|
| 353 |             flash.message = g.message(code: "default.delete.success", args: ["InventoryItem", params.id]) | 
|---|
| 354 |             redirect(action:search) | 
|---|
| 355 |             return | 
|---|
| 356 |         } | 
|---|
| 357 |  | 
|---|
| 358 |         flash.errorMessage = g.message(code: result.error.code, args: result.error.args) | 
|---|
| 359 |  | 
|---|
| 360 |         if(result.error.code == "default.not.found") { | 
|---|
| 361 |             redirect(action:search) | 
|---|
| 362 |             return | 
|---|
| 363 |         } | 
|---|
| 364 |  | 
|---|
| 365 |         redirect(action:show, id: params.id) | 
|---|
| 366 |     } | 
|---|
| 367 |  | 
|---|
| 368 |     def edit = { | 
|---|
| 369 |  | 
|---|
| 370 |         // In the case of an actionSubmit button, rewrite action name from 'index'. | 
|---|
| 371 |         if(params._action_Edit) | 
|---|
| 372 |             params.action='edit' | 
|---|
| 373 |  | 
|---|
| 374 |         def result = inventoryItemService.edit(params) | 
|---|
| 375 |  | 
|---|
| 376 |         if(!result.error) { | 
|---|
| 377 |             def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) | 
|---|
| 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] | 
|---|
| 385 |         } | 
|---|
| 386 |  | 
|---|
| 387 |         flash.errorMessage = g.message(code: result.error.code, args: result.error.args) | 
|---|
| 388 |         redirect(action:search) | 
|---|
| 389 |     } | 
|---|
| 390 |  | 
|---|
| 391 |     def update = { | 
|---|
| 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 | 
|---|
| 398 |         } | 
|---|
| 399 |  | 
|---|
| 400 |         if(result.error.code == "default.not.found") { | 
|---|
| 401 |             flash.errorMessage = g.message(code: result.error.code, args: result.error.args) | 
|---|
| 402 |             redirect(action:search) | 
|---|
| 403 |             return | 
|---|
| 404 |         } | 
|---|
| 405 |  | 
|---|
| 406 |         def possibleAlternateItems = inventoryItemService.getPossibleAlternateItems(result.inventoryItemInstance) | 
|---|
| 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]) | 
|---|
| 413 |     } | 
|---|
| 414 |  | 
|---|
| 415 |     def create = { | 
|---|
| 416 |         def result = inventoryItemService.create(params) | 
|---|
| 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) } | 
|---|
| 419 |  | 
|---|
| 420 |         if(!result.error) | 
|---|
| 421 |             return [inventoryItemInstance: result.inventoryItemInstance, | 
|---|
| 422 |                             suppliers: suppliers, | 
|---|
| 423 |                             manufacturers: manufacturers] | 
|---|
| 424 |  | 
|---|
| 425 |         //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) | 
|---|
| 426 |         redirect(action: search) | 
|---|
| 427 |     } | 
|---|
| 428 |  | 
|---|
| 429 |     def save = { | 
|---|
| 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 | 
|---|
| 436 |         } | 
|---|
| 437 |  | 
|---|
| 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 |  | 
|---|
| 441 |         //flash.errorMessage = g.message(code: result.error.code, args: result.error.args) | 
|---|
| 442 |         render(view:'create', model:[inventoryItemInstance: result.inventoryItemInstance, | 
|---|
| 443 |                                                     suppliers: suppliers, | 
|---|
| 444 |                                                     manufacturers: manufacturers]) | 
|---|
| 445 |     } | 
|---|
| 446 |  | 
|---|
| 447 |     /** | 
|---|
| 448 |     * Handles the use inventory item form submit in the show view. | 
|---|
| 449 |     */ | 
|---|
| 450 |     @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) | 
|---|
| 451 |     def useInventoryItem = { | 
|---|
| 452 |  | 
|---|
| 453 |         params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". | 
|---|
| 454 |         def result = inventoryMovementService.move(params) | 
|---|
| 455 |  | 
|---|
| 456 |         if(!result.error) { | 
|---|
| 457 |             flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." | 
|---|
| 458 |             session.inventoryMovementTaskId = null | 
|---|
| 459 |             redirect(controller: "taskDetailed", | 
|---|
| 460 |                             action: "show", | 
|---|
| 461 |                             id: result.taskId, | 
|---|
| 462 |                             params: [showTab: "showInventoryTab"]) | 
|---|
| 463 |             // Success. | 
|---|
| 464 |             return | 
|---|
| 465 |         } | 
|---|
| 466 |  | 
|---|
| 467 |         // Prepare data for the show view. | 
|---|
| 468 |         def p = [:] | 
|---|
| 469 |         p.id = result.inventoryMovementInstance.inventoryItem?.id | 
|---|
| 470 |         def r = inventoryItemService.show(p) | 
|---|
| 471 |  | 
|---|
| 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] | 
|---|
| 481 |  | 
|---|
| 482 |             model.inventoryMovementInstance = result.inventoryMovementInstance // This will pass in the errors. | 
|---|
| 483 |  | 
|---|
| 484 |             render(view: 'show', model: model) | 
|---|
| 485 |             return | 
|---|
| 486 |         } | 
|---|
| 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 '' | 
|---|
| 502 |     } | 
|---|
| 503 |  | 
|---|
| 504 | } // end of class | 
|---|