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 | if(!result.error) |
---|
229 | return [ inventoryItemPurchaseInstance : result.inventoryItemPurchaseInstance ] |
---|
230 | |
---|
231 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
232 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
233 | } |
---|
234 | |
---|
235 | def update = { |
---|
236 | def result = inventoryPurchaseService.update(params) |
---|
237 | |
---|
238 | if(!result.error) { |
---|
239 | flash.message = g.message(code: "default.update.success", args: ["Inventory Purchase", params.id]) |
---|
240 | redirect(action:show, id: params.id) |
---|
241 | return |
---|
242 | } |
---|
243 | |
---|
244 | if(result.error.code == "default.not.found") { |
---|
245 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
246 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
247 | return |
---|
248 | } |
---|
249 | |
---|
250 | render(view:'edit', model:[inventoryItemPurchaseInstance: result.inventoryItemPurchaseInstance.attach()]) |
---|
251 | } |
---|
252 | |
---|
253 | def create = { |
---|
254 | def inventoryItemPurchaseInstance = new InventoryItemPurchase() |
---|
255 | inventoryItemPurchaseInstance.properties = params |
---|
256 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
257 | |
---|
258 | if(!inventoryItemPurchaseInstance.inventoryItem) { |
---|
259 | flash.message = "Please select an inventory item then the 'purchasing' tab." |
---|
260 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
261 | return |
---|
262 | } |
---|
263 | |
---|
264 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance] |
---|
265 | } |
---|
266 | |
---|
267 | def save = { |
---|
268 | def result = inventoryPurchaseService.save(params) |
---|
269 | |
---|
270 | if(!result.error) { |
---|
271 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
272 | if(params.returnTo == 'inventoryItem') { |
---|
273 | redirect(controller: 'inventoryItemDetailed', |
---|
274 | action: 'show', |
---|
275 | id: result.inventoryItemId, |
---|
276 | params: [showTab: "showPurchasingTab"]) |
---|
277 | } |
---|
278 | else { |
---|
279 | redirect(action: 'search') |
---|
280 | } |
---|
281 | return |
---|
282 | } |
---|
283 | |
---|
284 | params.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
285 | render(view:'create', model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance]) |
---|
286 | } |
---|
287 | |
---|
288 | def receive = { |
---|
289 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
290 | |
---|
291 | if(!inventoryItemPurchaseInstance) { |
---|
292 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
293 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
294 | return |
---|
295 | } |
---|
296 | |
---|
297 | inventoryItemPurchaseInstance.properties = params |
---|
298 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
299 | def calcQuantities = inventoryPurchaseService.calcQuantities(inventoryItemPurchaseInstance) |
---|
300 | inventoryItemPurchaseInstance.quantity = calcQuantities.thisOrderRemaining |
---|
301 | inventoryItemPurchaseInstance.orderValueAmount = calcQuantities.thisOrderRemainingAmount |
---|
302 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
303 | 'orderId': inventoryItemPurchaseInstance.id] |
---|
304 | } |
---|
305 | |
---|
306 | def receiveSave = { |
---|
307 | def result = inventoryPurchaseService.receiveSave(params) |
---|
308 | |
---|
309 | if(!result.error) { |
---|
310 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
311 | if(params.returnTo == 'inventoryItem') { |
---|
312 | redirect(controller: 'inventoryItemDetailed', |
---|
313 | action: 'show', |
---|
314 | id: result.inventoryItemId, |
---|
315 | params: [showTab: "showPurchasingTab"]) |
---|
316 | } |
---|
317 | else { |
---|
318 | redirect(action: 'search') |
---|
319 | } |
---|
320 | return |
---|
321 | } |
---|
322 | |
---|
323 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
324 | |
---|
325 | if(result.error.code == "default.not.found") { |
---|
326 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
327 | return |
---|
328 | } |
---|
329 | |
---|
330 | render(view:'receive', |
---|
331 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
332 | 'orderId': result.orderId]) |
---|
333 | } |
---|
334 | |
---|
335 | def approveInvoicePayment = { |
---|
336 | // Read the values from Order Received. |
---|
337 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
338 | |
---|
339 | if(!inventoryItemPurchaseInstance) { |
---|
340 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
341 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
342 | return |
---|
343 | } |
---|
344 | |
---|
345 | inventoryItemPurchaseInstance.properties = params |
---|
346 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
347 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
348 | 'receivedId': inventoryItemPurchaseInstance.id] |
---|
349 | } |
---|
350 | |
---|
351 | def approveInvoicePaymentSave = { |
---|
352 | def result = inventoryPurchaseService.approveInvoicePaymentSave(params) |
---|
353 | |
---|
354 | if(!result.error) { |
---|
355 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
356 | if(params.returnTo == 'inventoryItem') { |
---|
357 | redirect(controller: 'inventoryItemDetailed', |
---|
358 | action: 'show', |
---|
359 | id: result.inventoryItemId, |
---|
360 | params: [showTab: "showPurchasingTab"]) |
---|
361 | } |
---|
362 | else { |
---|
363 | redirect(action: 'search') |
---|
364 | } |
---|
365 | return |
---|
366 | } |
---|
367 | |
---|
368 | if(result.error.code == "default.not.found") { |
---|
369 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
370 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
371 | return |
---|
372 | } |
---|
373 | |
---|
374 | render(view:'approveInvoicePayment', |
---|
375 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
376 | 'receivedId': result.receivedId]) |
---|
377 | } |
---|
378 | } |
---|