[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 |
---|
| 23 | if(params.newMax.isInteger()) { |
---|
| 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 | params.sort = params.sort ?: "purchaseOrderNumber" |
---|
| 42 | params.order = params.order ?: "desc" |
---|
| 43 | |
---|
| 44 | def inventoryItemPurchaseList = [] |
---|
| 45 | def inventoryItemPurchaseTotal |
---|
| 46 | def filterParams = [:] |
---|
| 47 | |
---|
| 48 | // Quick Search: |
---|
| 49 | if(!FilterUtils.isFilterApplied(params)) { |
---|
| 50 | |
---|
| 51 | if(params.quickSearch == "searchAllOrders") { |
---|
| 52 | inventoryItemPurchaseList = InventoryItemPurchase.findAllByInventoryItemPurchaseType(InventoryItemPurchaseType.read(1), |
---|
| 53 | [max:params.max, |
---|
| 54 | offset:params.offset, |
---|
| 55 | sort:params.sort, |
---|
| 56 | order:params.order]) |
---|
| 57 | if(inventoryItemPurchaseList.size() > 0) { params.message = "All Orders." } |
---|
| 58 | else { params.message = "No orders found." } |
---|
| 59 | } |
---|
| 60 | else if(params.quickSearch == "searchAllReceived") { |
---|
| 61 | inventoryItemPurchaseList = InventoryItemPurchase.findAllByInventoryItemPurchaseTypeOrInventoryItemPurchaseType(InventoryItemPurchaseType.read(2), |
---|
| 62 | InventoryItemPurchaseType.read(3), |
---|
| 63 | [max:params.max, |
---|
| 64 | offset:params.offset, |
---|
| 65 | sort:params.sort, |
---|
| 66 | order:params.order]) |
---|
| 67 | if(inventoryItemPurchaseList.size() > 0) { params.message = "All Received Complete." } |
---|
| 68 | else { params.message = "No orders found." } |
---|
| 69 | } |
---|
| 70 | else { |
---|
| 71 | //Default: |
---|
| 72 | inventoryItemPurchaseList = InventoryItemPurchase.list(max:params.max, |
---|
| 73 | offset:params.offset, |
---|
| 74 | sort:params.sort, |
---|
| 75 | order:params.order) |
---|
| 76 | if(inventoryItemPurchaseList.size() > 0) { params.message = "All Purchases." } |
---|
| 77 | else { params.message = "No orders found." } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | inventoryItemPurchaseTotal = inventoryItemPurchaseList.size() |
---|
| 81 | filterParams.quickSearch = params.quickSearch |
---|
| 82 | } |
---|
| 83 | else { |
---|
| 84 | // filterPane: |
---|
| 85 | inventoryItemPurchaseList = filterService.filter( params, InventoryItemPurchase ) |
---|
| 86 | inventoryItemPurchaseTotal = filterService.count( params, InventoryItemPurchase ) |
---|
| 87 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | // export plugin: |
---|
| 91 | if(params?.format && params.format != "html") { |
---|
| 92 | |
---|
| 93 | def dateFmt = { date -> |
---|
| 94 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | String title |
---|
| 98 | if(params.quickSearch) |
---|
| 99 | title = "${params.quickSearch} inventory purchases." |
---|
| 100 | else |
---|
| 101 | title = "Filtered inventory purchases." |
---|
| 102 | |
---|
| 103 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
| 104 | response.setHeader("Content-disposition", "attachment; filename=Purchases.${params.extension}") |
---|
| 105 | List fields = ["purchaseOrderNumber", "dateEntered", "costCode", "quantity", "orderValueAmount", "invoiceNumber", |
---|
| 106 | "inventoryItemPurchaseType"] |
---|
| 107 | Map labels = ["purchaseOrderNumber": "Order Number", "dateEntered": "Date", "costCode": "Cost Code", |
---|
| 108 | "quantity": "Quantity", "orderValueAmount": "Order \$", "invoiceNumber": "Invoice Number", |
---|
| 109 | "inventoryItemPurchaseType": "Type"] |
---|
| 110 | Map formatters = [ dateEntered: dateFmt] |
---|
| 111 | Map parameters = [title: title, separator: ","] |
---|
| 112 | |
---|
| 113 | exportService.export(params.format, response.outputStream, inventoryItemPurchaseList, fields, labels, formatters, parameters) |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | // Add some basic params to filterParams. |
---|
| 117 | filterParams.max = params.max |
---|
| 118 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
| 119 | filterParams.sort = params.sort ?: "purchaseOrderNumber" |
---|
| 120 | filterParams.order = params.order ?: "desc" |
---|
| 121 | |
---|
| 122 | return[ inventoryItemPurchaseList: inventoryItemPurchaseList, |
---|
| 123 | inventoryItemPurchaseTotal: inventoryItemPurchaseTotal, |
---|
| 124 | filterParams: filterParams ] |
---|
| 125 | |
---|
| 126 | } // end search() |
---|
| 127 | |
---|
[441] | 128 | def show = { |
---|
| 129 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
| 130 | |
---|
| 131 | if(!inventoryItemPurchaseInstance) { |
---|
| 132 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
| 133 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
| 134 | } |
---|
| 135 | else { return [ inventoryItemPurchaseInstance : inventoryItemPurchaseInstance ] } |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | def delete = { |
---|
| 139 | def result = inventoryPurchaseService.delete(params) |
---|
| 140 | |
---|
| 141 | if(!result.error) { |
---|
| 142 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItemPurchase", params.id]) |
---|
| 143 | redirect(controller: 'inventoryItemDetailed', |
---|
| 144 | action: 'show', |
---|
| 145 | id: result.inventoryItemId, |
---|
| 146 | params: [showTab: "showPurchasingTab"]) |
---|
| 147 | return |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 151 | |
---|
| 152 | if(result.error.code == "default.not.found") { |
---|
| 153 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 154 | return |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | redirect(action:show, id: params.id) |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | def edit = { |
---|
| 161 | def result = inventoryPurchaseService.edit(params) |
---|
| 162 | |
---|
| 163 | if(!result.error) |
---|
| 164 | return [ inventoryItemPurchaseInstance : result.inventoryItemPurchaseInstance ] |
---|
| 165 | |
---|
| 166 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 167 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | def update = { |
---|
| 171 | def result = inventoryPurchaseService.update(params) |
---|
| 172 | |
---|
| 173 | if(!result.error) { |
---|
| 174 | flash.message = g.message(code: "default.update.success", args: ["Inventory Purchase", params.id]) |
---|
| 175 | redirect(action:show, id: params.id) |
---|
| 176 | return |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | if(result.error.code == "default.not.found") { |
---|
| 180 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
| 181 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
| 182 | return |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | render(view:'edit', model:[inventoryItemPurchaseInstance: result.inventoryItemPurchaseInstance.attach()]) |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | def create = { |
---|
| 189 | def inventoryItemPurchaseInstance = new InventoryItemPurchase() |
---|
| 190 | inventoryItemPurchaseInstance.properties = params |
---|
| 191 | |
---|
| 192 | if(!inventoryItemPurchaseInstance.inventoryItem) { |
---|
| 193 | flash.message = "Please select an inventory item then the 'purchasing' tab." |
---|
| 194 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 195 | return |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance] |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | def save = { |
---|
| 202 | def result = inventoryPurchaseService.save(params) |
---|
| 203 | |
---|
| 204 | if(!result.error) { |
---|
| 205 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
| 206 | redirect(controller: 'inventoryItemDetailed', |
---|
| 207 | action: 'show', |
---|
| 208 | id: result.inventoryItemId, |
---|
| 209 | params: [showTab: "showPurchasingTab"]) |
---|
| 210 | return |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | render(view:'create', model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance]) |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | def receive = { |
---|
| 217 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
| 218 | |
---|
| 219 | if(!inventoryItemPurchaseInstance) { |
---|
| 220 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
| 221 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
| 222 | return |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | inventoryItemPurchaseInstance.properties = params |
---|
| 226 | def calcQuantities = inventoryPurchaseService.calcQuantities(inventoryItemPurchaseInstance) |
---|
| 227 | inventoryItemPurchaseInstance.quantity = calcQuantities.thisOrderRemaining |
---|
| 228 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
| 229 | 'orderId': inventoryItemPurchaseInstance.id] |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | def receiveSave = { |
---|
| 233 | def result = inventoryPurchaseService.receiveSave(params) |
---|
| 234 | |
---|
| 235 | if(!result.error) { |
---|
| 236 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
| 237 | redirect(controller: 'inventoryItemDetailed', |
---|
| 238 | action: 'show', |
---|
| 239 | id: result.inventoryItemId, |
---|
| 240 | params: [showTab: "showPurchasingTab"]) |
---|
| 241 | return |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 245 | |
---|
| 246 | if(result.error.code == "default.not.found") { |
---|
| 247 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 248 | return |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | render(view:'receive', |
---|
| 252 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
| 253 | 'orderId': result.orderId]) |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | def approveInvoicePayment = { |
---|
| 257 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
| 258 | |
---|
| 259 | if(!inventoryItemPurchaseInstance) { |
---|
| 260 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
| 261 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
| 262 | return |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | inventoryItemPurchaseInstance.properties = params |
---|
| 266 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
| 267 | 'orderId': inventoryItemPurchaseInstance.id] |
---|
| 268 | } |
---|
| 269 | |
---|
| 270 | def approveInvoicePaymentSave = { |
---|
| 271 | def result = inventoryPurchaseService.approveInvoicePaymentSave(params) |
---|
| 272 | |
---|
| 273 | if(!result.error) { |
---|
| 274 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
| 275 | redirect(controller: 'inventoryItemDetailed', |
---|
| 276 | action: 'show', |
---|
| 277 | id: result.inventoryItemId, |
---|
| 278 | params: [showTab: "showPurchasingTab"]) |
---|
| 279 | return |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | if(result.error.code == "default.not.found") { |
---|
| 283 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
| 284 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
| 285 | return |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | render(view:'approveInvoicePayment', |
---|
| 289 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
| 290 | 'orderId': result.orderId]) |
---|
| 291 | } |
---|
| 292 | } |
---|