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