[441] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
[468] | 2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
| 3 | import com.zeddware.grails.plugins.filterpane.FilterUtils |
---|
[441] | 4 | |
---|
| 5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
| 6 | class InventoryItemPurchaseDetailedController extends BaseController { |
---|
| 7 | |
---|
| 8 | def authService |
---|
[468] | 9 | def filterService |
---|
| 10 | def exportService |
---|
| 11 | def dateUtilService |
---|
[441] | 12 | def inventoryPurchaseService |
---|
| 13 | |
---|
| 14 | def index = { |
---|
[468] | 15 | redirect(action:'search', params:params) |
---|
[441] | 16 | } |
---|
| 17 | |
---|
| 18 | // the delete, save and update actions only accept POST requests |
---|
| 19 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
| 20 | |
---|
[468] | 21 | def setSearchParamsMax = { |
---|
| 22 | def max = 1000 |
---|
[615] | 23 | if(params.newMax?.isInteger()) { |
---|
[468] | 24 | def i = params.newMax.toInteger() |
---|
| 25 | if(i > 0 && i <= max) |
---|
| 26 | session.inventoryItemPurchaseSearchParamsMax = params.newMax |
---|
| 27 | if(i > max) |
---|
| 28 | session.inventoryItemPurchaseSearchParamsMax = max |
---|
| 29 | } |
---|
| 30 | forward(action: 'search', params: params) |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | def search = { |
---|
| 34 | |
---|
| 35 | if(session.inventoryItemPurchaseSearchParamsMax) |
---|
| 36 | params.max = session.inventoryItemPurchaseSearchParamsMax |
---|
| 37 | |
---|
| 38 | // Protect filterPane. |
---|
| 39 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 1000 ) |
---|
| 40 | params.offset = params.offset?.toInteger() ?: 0 |
---|
| 41 | |
---|
| 42 | def inventoryItemPurchaseList = [] |
---|
| 43 | def inventoryItemPurchaseTotal |
---|
[594] | 44 | def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 45 | def isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
[468] | 46 | |
---|
[594] | 47 | // Restore search unless a new search is being requested. |
---|
| 48 | if(!params.quickSearch && !filterParams) { |
---|
| 49 | if(session.inventoryItemPurchaseQuickSearch) |
---|
| 50 | params.quickSearch = session.inventoryItemPurchaseQuickSearch |
---|
| 51 | else if(session.inventoryItemPurchaseSearchFilterParams) { |
---|
| 52 | session.inventoryItemPurchaseSearchFilterParams.each() { params[it.key] = it.value } |
---|
| 53 | params.filter = session.inventoryItemPurchaseSearchFilter |
---|
| 54 | isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
| 55 | } |
---|
| 56 | } |
---|
[468] | 57 | |
---|
[594] | 58 | // Remember sort if supplied, otherwise try to restore. |
---|
| 59 | if(params.sort && params.order) { |
---|
| 60 | // Reset to defaultSort if requested. |
---|
| 61 | if(params.sort == 'defaultSort') { |
---|
| 62 | params.sort = null |
---|
| 63 | params.order = null |
---|
| 64 | session.removeAttribute("inventoryItemPurchaseSearchSort") |
---|
| 65 | session.removeAttribute("inventoryItemPurchaseSearchOrder") |
---|
| 66 | } |
---|
| 67 | else { |
---|
| 68 | session.inventoryItemPurchaseSearchSort = params.sort |
---|
| 69 | session.inventoryItemPurchaseSearchOrder = params.order |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | else if(session.inventoryItemPurchaseSearchSort && session.inventoryItemPurchaseSearchOrder) { |
---|
| 73 | params.sort = session.inventoryItemPurchaseSearchSort |
---|
| 74 | params.order = session.inventoryItemPurchaseSearchOrder |
---|
| 75 | } |
---|
| 76 | else { |
---|
[595] | 77 | params.sort = "id" |
---|
| 78 | params.order = "asc" |
---|
[594] | 79 | } |
---|
| 80 | |
---|
| 81 | if(isFilterApplied) { |
---|
| 82 | // filterPane: |
---|
| 83 | //if(params.sort == "attentionFlag") // See ticket #64 in Trac. |
---|
| 84 | //params.sort = "id" |
---|
| 85 | |
---|
| 86 | // Call filterService. |
---|
| 87 | inventoryItemPurchaseList = filterService.filter( params, InventoryItemPurchase ) |
---|
| 88 | inventoryItemPurchaseTotal = filterService.count( params, InventoryItemPurchase ) |
---|
| 89 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 90 | // Remember search. |
---|
| 91 | session.inventoryItemPurchaseSearchFilterParams = new LinkedHashMap(filterParams) |
---|
| 92 | session.inventoryItemPurchaseSearchFilter = new LinkedHashMap(params.filter) |
---|
| 93 | session.removeAttribute("inventoryItemPurchaseQuickSearch") |
---|
| 94 | } |
---|
| 95 | else { |
---|
| 96 | // Quick Search: |
---|
| 97 | |
---|
[468] | 98 | if(params.quickSearch == "searchAllOrders") { |
---|
[614] | 99 | def orderType = InventoryItemPurchaseType.read(1) |
---|
| 100 | inventoryItemPurchaseList = InventoryItemPurchase.findAllByInventoryItemPurchaseType(orderType, |
---|
| 101 | [max:params.max, |
---|
| 102 | offset:params.offset, |
---|
| 103 | sort:params.sort, |
---|
| 104 | order:params.order]) |
---|
| 105 | inventoryItemPurchaseTotal = InventoryItemPurchase.countByInventoryItemPurchaseType(orderType) |
---|
| 106 | if(inventoryItemPurchaseTotal > 0) { params.message = "All Orders." } |
---|
[468] | 107 | else { params.message = "No orders found." } |
---|
| 108 | } |
---|
| 109 | else if(params.quickSearch == "searchAllReceived") { |
---|
[614] | 110 | def receivedBackOrderType = InventoryItemPurchaseType.read(2) |
---|
| 111 | def receivedCompleteType = InventoryItemPurchaseType.read(3) |
---|
| 112 | inventoryItemPurchaseList = InventoryItemPurchase.findAllByInventoryItemPurchaseTypeOrInventoryItemPurchaseType(receivedBackOrderType, |
---|
| 113 | receivedCompleteType, |
---|
| 114 | [max:params.max, |
---|
| 115 | offset:params.offset, |
---|
| 116 | sort:params.sort, |
---|
| 117 | order:params.order]) |
---|
| 118 | inventoryItemPurchaseTotal = InventoryItemPurchase.countByInventoryItemPurchaseTypeOrInventoryItemPurchaseType(receivedBackOrderType, |
---|
| 119 | receivedCompleteType) |
---|
| 120 | if(inventoryItemPurchaseTotal > 0) { params.message = "All Received." } |
---|
[468] | 121 | else { params.message = "No orders found." } |
---|
| 122 | } |
---|
| 123 | else { |
---|
| 124 | //Default: |
---|
| 125 | inventoryItemPurchaseList = InventoryItemPurchase.list(max:params.max, |
---|
[614] | 126 | offset:params.offset, |
---|
| 127 | sort:params.sort, |
---|
| 128 | order:params.order) |
---|
| 129 | inventoryItemPurchaseTotal = InventoryItemPurchase.count() |
---|
| 130 | if(inventoryItemPurchaseTotal > 0) { params.message = "All Purchases." } |
---|
[468] | 131 | else { params.message = "No orders found." } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | filterParams.quickSearch = params.quickSearch |
---|
[594] | 135 | // Remember search. |
---|
| 136 | session.removeAttribute("inventoryItemPurchaseSearchFilterParams") |
---|
| 137 | session.removeAttribute("inventoryItemPurchaseSearchFilter") |
---|
| 138 | session.inventoryItemPurchaseQuickSearch = params.quickSearch |
---|
[468] | 139 | } |
---|
| 140 | |
---|
| 141 | // export plugin: |
---|
| 142 | if(params?.format && params.format != "html") { |
---|
| 143 | |
---|
| 144 | def dateFmt = { date -> |
---|
| 145 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | String title |
---|
| 149 | if(params.quickSearch) |
---|
| 150 | title = "${params.quickSearch} inventory purchases." |
---|
| 151 | else |
---|
| 152 | title = "Filtered inventory purchases." |
---|
| 153 | |
---|
| 154 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 155 | response.setHeader("Content-disposition", "attachment; filename=Purchases.${params.extension}") |
---|
[605] | 156 | List fields = ["purchaseOrderNumber", "date", "costCode", "quantity", "orderValueAmount", "invoiceNumber", |
---|
[468] | 157 | "inventoryItemPurchaseType"] |
---|
[605] | 158 | Map labels = ["purchaseOrderNumber": "Order Number", "date": "Date", "costCode": "Cost Code", |
---|
[468] | 159 | "quantity": "Quantity", "orderValueAmount": "Order \$", "invoiceNumber": "Invoice Number", |
---|
| 160 | "inventoryItemPurchaseType": "Type"] |
---|
[605] | 161 | Map formatters = [ date: dateFmt] |
---|
[468] | 162 | Map parameters = [title: title, separator: ","] |
---|
| 163 | |
---|
| 164 | exportService.export(params.format, response.outputStream, inventoryItemPurchaseList, fields, labels, formatters, parameters) |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | // Add some basic params to filterParams. |
---|
| 168 | filterParams.max = params.max |
---|
| 169 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 170 | filterParams.sort = params.sort ?: "purchaseOrderNumber" |
---|
| 171 | filterParams.order = params.order ?: "desc" |
---|
| 172 | |
---|
[605] | 173 | // Get some associatedProperty values for filterpane. |
---|
| 174 | def associatedPropertyValues = [:] |
---|
| 175 | def startOfYearRange = dateUtilService.getYearFromDate(dateUtilService.plusYear(new Date(), -10)) |
---|
| 176 | def endOfYearRange = dateUtilService.getYearFromDate(dateUtilService.plusYear(new Date(), 10)) |
---|
| 177 | associatedPropertyValues.yearRange = startOfYearRange..endOfYearRange |
---|
| 178 | |
---|
[468] | 179 | return[ inventoryItemPurchaseList: inventoryItemPurchaseList, |
---|
[605] | 180 | inventoryItemPurchaseTotal: inventoryItemPurchaseTotal, |
---|
| 181 | filterParams: filterParams, |
---|
| 182 | associatedPropertyValues: associatedPropertyValues ] |
---|
[468] | 183 | |
---|
| 184 | } // end search() |
---|
| 185 | |
---|
[441] | 186 | def show = { |
---|
| 187 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
[595] | 188 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
[441] | 189 | |
---|
| 190 | if(!inventoryItemPurchaseInstance) { |
---|
| 191 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
| 192 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
| 193 | } |
---|
| 194 | else { return [ inventoryItemPurchaseInstance : inventoryItemPurchaseInstance ] } |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | def delete = { |
---|
| 198 | def result = inventoryPurchaseService.delete(params) |
---|
| 199 | |
---|
| 200 | if(!result.error) { |
---|
| 201 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItemPurchase", params.id]) |
---|
[595] | 202 | if(params.returnTo == 'inventoryItem') { |
---|
| 203 | redirect(controller: 'inventoryItemDetailed', |
---|
| 204 | action: 'show', |
---|
| 205 | id: result.inventoryItemId, |
---|
| 206 | params: [showTab: "showPurchasingTab"]) |
---|
| 207 | } |
---|
| 208 | else { |
---|
| 209 | redirect(action: 'search') |
---|
| 210 | } |
---|
[441] | 211 | return |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 215 | |
---|
| 216 | if(result.error.code == "default.not.found") { |
---|
| 217 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 218 | return |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | redirect(action:show, id: params.id) |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | def edit = { |
---|
| 225 | def result = inventoryPurchaseService.edit(params) |
---|
[595] | 226 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
[441] | 227 | |
---|
[633] | 228 | def costCodes = [] |
---|
[441] | 229 | |
---|
[633] | 230 | if(!result.error) { |
---|
| 231 | if(inventoryPurchaseService.isPersonInPurchasingGroup(result.inventoryItemPurchaseInstance.costCode.purchasingGroup)) |
---|
| 232 | costCodes = inventoryPurchaseService.getCostCodesByPerson() |
---|
| 233 | |
---|
| 234 | return [ inventoryItemPurchaseInstance : result.inventoryItemPurchaseInstance, |
---|
| 235 | 'costCodes': costCodes ] |
---|
| 236 | } |
---|
| 237 | |
---|
[441] | 238 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 239 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | def update = { |
---|
| 243 | def result = inventoryPurchaseService.update(params) |
---|
| 244 | |
---|
| 245 | if(!result.error) { |
---|
| 246 | flash.message = g.message(code: "default.update.success", args: ["Inventory Purchase", params.id]) |
---|
| 247 | redirect(action:show, id: params.id) |
---|
| 248 | return |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | if(result.error.code == "default.not.found") { |
---|
| 252 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
| 253 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
| 254 | return |
---|
| 255 | } |
---|
| 256 | |
---|
[633] | 257 | result.inventoryItemPurchaseInstance.attach() |
---|
| 258 | result.inventoryItemPurchaseInstance.costCode.attach() |
---|
| 259 | result.inventoryItemPurchaseInstance.costCode.purchasingGroup.attach() |
---|
| 260 | |
---|
| 261 | def costCodes = [] |
---|
| 262 | if(inventoryPurchaseService.isPersonInPurchasingGroup(result.inventoryItemPurchaseInstance.costCode.purchasingGroup)) |
---|
| 263 | costCodes = inventoryPurchaseService.getCostCodesByPerson() |
---|
| 264 | |
---|
| 265 | render(view:'edit', model:[inventoryItemPurchaseInstance: result.inventoryItemPurchaseInstance, |
---|
| 266 | 'costCodes': costCodes]) |
---|
[441] | 267 | } |
---|
| 268 | |
---|
| 269 | def create = { |
---|
| 270 | def inventoryItemPurchaseInstance = new InventoryItemPurchase() |
---|
| 271 | inventoryItemPurchaseInstance.properties = params |
---|
[595] | 272 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
[441] | 273 | |
---|
| 274 | if(!inventoryItemPurchaseInstance.inventoryItem) { |
---|
| 275 | flash.message = "Please select an inventory item then the 'purchasing' tab." |
---|
| 276 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 277 | return |
---|
| 278 | } |
---|
| 279 | |
---|
[633] | 280 | def costCodes = inventoryPurchaseService.getCostCodesByPerson() |
---|
| 281 | |
---|
| 282 | return ['inventoryItemPurchaseInstance': inventoryItemPurchaseInstance, |
---|
| 283 | 'costCodes': costCodes] |
---|
[441] | 284 | } |
---|
| 285 | |
---|
| 286 | def save = { |
---|
| 287 | def result = inventoryPurchaseService.save(params) |
---|
| 288 | |
---|
| 289 | if(!result.error) { |
---|
| 290 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
[595] | 291 | if(params.returnTo == 'inventoryItem') { |
---|
| 292 | redirect(controller: 'inventoryItemDetailed', |
---|
| 293 | action: 'show', |
---|
| 294 | id: result.inventoryItemId, |
---|
| 295 | params: [showTab: "showPurchasingTab"]) |
---|
| 296 | } |
---|
| 297 | else { |
---|
| 298 | redirect(action: 'search') |
---|
| 299 | } |
---|
[441] | 300 | return |
---|
| 301 | } |
---|
| 302 | |
---|
[633] | 303 | def costCodes = inventoryPurchaseService.getCostCodesByPerson() |
---|
| 304 | |
---|
[610] | 305 | params.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
[633] | 306 | render(view:'create', model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
| 307 | 'costCodes': costCodes]) |
---|
[441] | 308 | } |
---|
| 309 | |
---|
| 310 | def receive = { |
---|
| 311 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
| 312 | |
---|
| 313 | if(!inventoryItemPurchaseInstance) { |
---|
| 314 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
| 315 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
| 316 | return |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | inventoryItemPurchaseInstance.properties = params |
---|
[595] | 320 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
[441] | 321 | def calcQuantities = inventoryPurchaseService.calcQuantities(inventoryItemPurchaseInstance) |
---|
| 322 | inventoryItemPurchaseInstance.quantity = calcQuantities.thisOrderRemaining |
---|
[596] | 323 | inventoryItemPurchaseInstance.orderValueAmount = calcQuantities.thisOrderRemainingAmount |
---|
[441] | 324 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
| 325 | 'orderId': inventoryItemPurchaseInstance.id] |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | def receiveSave = { |
---|
| 329 | def result = inventoryPurchaseService.receiveSave(params) |
---|
| 330 | |
---|
| 331 | if(!result.error) { |
---|
| 332 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
[595] | 333 | if(params.returnTo == 'inventoryItem') { |
---|
| 334 | redirect(controller: 'inventoryItemDetailed', |
---|
| 335 | action: 'show', |
---|
| 336 | id: result.inventoryItemId, |
---|
| 337 | params: [showTab: "showPurchasingTab"]) |
---|
| 338 | } |
---|
| 339 | else { |
---|
| 340 | redirect(action: 'search') |
---|
| 341 | } |
---|
[441] | 342 | return |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 346 | |
---|
| 347 | if(result.error.code == "default.not.found") { |
---|
| 348 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 349 | return |
---|
| 350 | } |
---|
| 351 | |
---|
[717] | 352 | // Prevent lazy initialization error. |
---|
| 353 | result.inventoryItemPurchaseInstance.costCode.attach() |
---|
| 354 | |
---|
[441] | 355 | render(view:'receive', |
---|
| 356 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
| 357 | 'orderId': result.orderId]) |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | def approveInvoicePayment = { |
---|
[597] | 361 | // Read the values from Order Received. |
---|
[441] | 362 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
| 363 | |
---|
| 364 | if(!inventoryItemPurchaseInstance) { |
---|
| 365 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
| 366 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
| 367 | return |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | inventoryItemPurchaseInstance.properties = params |
---|
[595] | 371 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
[441] | 372 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
[597] | 373 | 'receivedId': inventoryItemPurchaseInstance.id] |
---|
[441] | 374 | } |
---|
| 375 | |
---|
| 376 | def approveInvoicePaymentSave = { |
---|
| 377 | def result = inventoryPurchaseService.approveInvoicePaymentSave(params) |
---|
| 378 | |
---|
| 379 | if(!result.error) { |
---|
| 380 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
[595] | 381 | if(params.returnTo == 'inventoryItem') { |
---|
| 382 | redirect(controller: 'inventoryItemDetailed', |
---|
| 383 | action: 'show', |
---|
| 384 | id: result.inventoryItemId, |
---|
| 385 | params: [showTab: "showPurchasingTab"]) |
---|
| 386 | } |
---|
| 387 | else { |
---|
| 388 | redirect(action: 'search') |
---|
| 389 | } |
---|
[441] | 390 | return |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | if(result.error.code == "default.not.found") { |
---|
| 394 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 395 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 396 | return |
---|
| 397 | } |
---|
| 398 | |
---|
[717] | 399 | // Prevent lazy initialization error. |
---|
| 400 | result.inventoryItemPurchaseInstance.costCode.attach() |
---|
| 401 | |
---|
[441] | 402 | render(view:'approveInvoicePayment', |
---|
| 403 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
[597] | 404 | 'receivedId': result.receivedId]) |
---|
[441] | 405 | } |
---|
| 406 | } |
---|