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 | |
---|
5 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
6 | class InventoryItemPurchaseDetailedController extends BaseController { |
---|
7 | |
---|
8 | def authService |
---|
9 | def filterService |
---|
10 | def exportService |
---|
11 | def dateUtilService |
---|
12 | def inventoryPurchaseService |
---|
13 | |
---|
14 | def index = { |
---|
15 | redirect(action:'search', params:params) |
---|
16 | } |
---|
17 | |
---|
18 | // the delete, save and update actions only accept POST requests |
---|
19 | static allowedMethods = [delete:'POST', save:'POST', update:'POST'] |
---|
20 | |
---|
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 | |
---|
42 | def inventoryItemPurchaseList = [] |
---|
43 | def inventoryItemPurchaseTotal |
---|
44 | def filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
45 | def isFilterApplied = FilterUtils.isFilterApplied(params) |
---|
46 | |
---|
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 | } |
---|
57 | |
---|
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 { |
---|
77 | params.sort = "id" |
---|
78 | params.order = "asc" |
---|
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 | |
---|
98 | if(params.quickSearch == "searchAllOrders") { |
---|
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." } |
---|
107 | else { params.message = "No orders found." } |
---|
108 | } |
---|
109 | else if(params.quickSearch == "searchAllReceived") { |
---|
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." } |
---|
121 | else { params.message = "No orders found." } |
---|
122 | } |
---|
123 | else { |
---|
124 | //Default: |
---|
125 | inventoryItemPurchaseList = InventoryItemPurchase.list(max:params.max, |
---|
126 | offset:params.offset, |
---|
127 | sort:params.sort, |
---|
128 | order:params.order) |
---|
129 | inventoryItemPurchaseTotal = InventoryItemPurchase.count() |
---|
130 | if(inventoryItemPurchaseTotal > 0) { params.message = "All Purchases." } |
---|
131 | else { params.message = "No orders found." } |
---|
132 | } |
---|
133 | |
---|
134 | filterParams.quickSearch = params.quickSearch |
---|
135 | // Remember search. |
---|
136 | session.removeAttribute("inventoryItemPurchaseSearchFilterParams") |
---|
137 | session.removeAttribute("inventoryItemPurchaseSearchFilter") |
---|
138 | session.inventoryItemPurchaseQuickSearch = params.quickSearch |
---|
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}") |
---|
156 | List fields = ["purchaseOrderNumber", "date", "costCode", "quantity", "orderValueAmount", "invoiceNumber", |
---|
157 | "inventoryItemPurchaseType"] |
---|
158 | Map labels = ["purchaseOrderNumber": "Order Number", "date": "Date", "costCode": "Cost Code", |
---|
159 | "quantity": "Quantity", "orderValueAmount": "Order \$", "invoiceNumber": "Invoice Number", |
---|
160 | "inventoryItemPurchaseType": "Type"] |
---|
161 | Map formatters = [ date: dateFmt] |
---|
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 | |
---|
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 | |
---|
179 | return[ inventoryItemPurchaseList: inventoryItemPurchaseList, |
---|
180 | inventoryItemPurchaseTotal: inventoryItemPurchaseTotal, |
---|
181 | filterParams: filterParams, |
---|
182 | associatedPropertyValues: associatedPropertyValues ] |
---|
183 | |
---|
184 | } // end search() |
---|
185 | |
---|
186 | def show = { |
---|
187 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
188 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
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]) |
---|
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 | } |
---|
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) |
---|
226 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
227 | |
---|
228 | def costCodes = [] |
---|
229 | |
---|
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 | |
---|
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 | |
---|
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]) |
---|
267 | } |
---|
268 | |
---|
269 | def create = { |
---|
270 | def inventoryItemPurchaseInstance = new InventoryItemPurchase() |
---|
271 | inventoryItemPurchaseInstance.properties = params |
---|
272 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
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 | |
---|
280 | def costCodes = inventoryPurchaseService.getCostCodesByPerson() |
---|
281 | |
---|
282 | return ['inventoryItemPurchaseInstance': inventoryItemPurchaseInstance, |
---|
283 | 'costCodes': costCodes] |
---|
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", '']) |
---|
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 | } |
---|
300 | return |
---|
301 | } |
---|
302 | |
---|
303 | def costCodes = inventoryPurchaseService.getCostCodesByPerson() |
---|
304 | |
---|
305 | params.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
306 | render(view:'create', model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
307 | 'costCodes': costCodes]) |
---|
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 |
---|
320 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
321 | def calcQuantities = inventoryPurchaseService.calcQuantities(inventoryItemPurchaseInstance) |
---|
322 | inventoryItemPurchaseInstance.quantity = calcQuantities.thisOrderRemaining |
---|
323 | inventoryItemPurchaseInstance.orderValueAmount = calcQuantities.thisOrderRemainingAmount |
---|
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", '']) |
---|
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 | } |
---|
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 | |
---|
352 | render(view:'receive', |
---|
353 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
354 | 'orderId': result.orderId]) |
---|
355 | } |
---|
356 | |
---|
357 | def approveInvoicePayment = { |
---|
358 | // Read the values from Order Received. |
---|
359 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
360 | |
---|
361 | if(!inventoryItemPurchaseInstance) { |
---|
362 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
363 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
364 | return |
---|
365 | } |
---|
366 | |
---|
367 | inventoryItemPurchaseInstance.properties = params |
---|
368 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
369 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
370 | 'receivedId': inventoryItemPurchaseInstance.id] |
---|
371 | } |
---|
372 | |
---|
373 | def approveInvoicePaymentSave = { |
---|
374 | def result = inventoryPurchaseService.approveInvoicePaymentSave(params) |
---|
375 | |
---|
376 | if(!result.error) { |
---|
377 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
378 | if(params.returnTo == 'inventoryItem') { |
---|
379 | redirect(controller: 'inventoryItemDetailed', |
---|
380 | action: 'show', |
---|
381 | id: result.inventoryItemId, |
---|
382 | params: [showTab: "showPurchasingTab"]) |
---|
383 | } |
---|
384 | else { |
---|
385 | redirect(action: 'search') |
---|
386 | } |
---|
387 | return |
---|
388 | } |
---|
389 | |
---|
390 | if(result.error.code == "default.not.found") { |
---|
391 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
392 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
393 | return |
---|
394 | } |
---|
395 | |
---|
396 | render(view:'approveInvoicePayment', |
---|
397 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
398 | 'receivedId': result.receivedId]) |
---|
399 | } |
---|
400 | } |
---|