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 | inventoryItemPurchaseList = InventoryItemPurchase.findAllByInventoryItemPurchaseType(InventoryItemPurchaseType.read(1), |
---|
100 | [max:params.max, |
---|
101 | offset:params.offset, |
---|
102 | sort:params.sort, |
---|
103 | order:params.order]) |
---|
104 | if(inventoryItemPurchaseList.size() > 0) { params.message = "All Orders." } |
---|
105 | else { params.message = "No orders found." } |
---|
106 | } |
---|
107 | else if(params.quickSearch == "searchAllReceived") { |
---|
108 | inventoryItemPurchaseList = InventoryItemPurchase.findAllByInventoryItemPurchaseTypeOrInventoryItemPurchaseType(InventoryItemPurchaseType.read(2), |
---|
109 | InventoryItemPurchaseType.read(3), |
---|
110 | [max:params.max, |
---|
111 | offset:params.offset, |
---|
112 | sort:params.sort, |
---|
113 | order:params.order]) |
---|
114 | if(inventoryItemPurchaseList.size() > 0) { params.message = "All Received Complete." } |
---|
115 | else { params.message = "No orders found." } |
---|
116 | } |
---|
117 | else { |
---|
118 | //Default: |
---|
119 | inventoryItemPurchaseList = InventoryItemPurchase.list(max:params.max, |
---|
120 | offset:params.offset, |
---|
121 | sort:params.sort, |
---|
122 | order:params.order) |
---|
123 | if(inventoryItemPurchaseList.size() > 0) { params.message = "All Purchases." } |
---|
124 | else { params.message = "No orders found." } |
---|
125 | } |
---|
126 | |
---|
127 | inventoryItemPurchaseTotal = inventoryItemPurchaseList.size() |
---|
128 | filterParams.quickSearch = params.quickSearch |
---|
129 | // Remember search. |
---|
130 | session.removeAttribute("inventoryItemPurchaseSearchFilterParams") |
---|
131 | session.removeAttribute("inventoryItemPurchaseSearchFilter") |
---|
132 | session.inventoryItemPurchaseQuickSearch = params.quickSearch |
---|
133 | } |
---|
134 | |
---|
135 | // export plugin: |
---|
136 | if(params?.format && params.format != "html") { |
---|
137 | |
---|
138 | def dateFmt = { date -> |
---|
139 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
140 | } |
---|
141 | |
---|
142 | String title |
---|
143 | if(params.quickSearch) |
---|
144 | title = "${params.quickSearch} inventory purchases." |
---|
145 | else |
---|
146 | title = "Filtered inventory purchases." |
---|
147 | |
---|
148 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
149 | response.setHeader("Content-disposition", "attachment; filename=Purchases.${params.extension}") |
---|
150 | List fields = ["purchaseOrderNumber", "date", "costCode", "quantity", "orderValueAmount", "invoiceNumber", |
---|
151 | "inventoryItemPurchaseType"] |
---|
152 | Map labels = ["purchaseOrderNumber": "Order Number", "date": "Date", "costCode": "Cost Code", |
---|
153 | "quantity": "Quantity", "orderValueAmount": "Order \$", "invoiceNumber": "Invoice Number", |
---|
154 | "inventoryItemPurchaseType": "Type"] |
---|
155 | Map formatters = [ date: dateFmt] |
---|
156 | Map parameters = [title: title, separator: ","] |
---|
157 | |
---|
158 | exportService.export(params.format, response.outputStream, inventoryItemPurchaseList, fields, labels, formatters, parameters) |
---|
159 | } |
---|
160 | |
---|
161 | // Add some basic params to filterParams. |
---|
162 | filterParams.max = params.max |
---|
163 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
164 | filterParams.sort = params.sort ?: "purchaseOrderNumber" |
---|
165 | filterParams.order = params.order ?: "desc" |
---|
166 | |
---|
167 | // Get some associatedProperty values for filterpane. |
---|
168 | def associatedPropertyValues = [:] |
---|
169 | def startOfYearRange = dateUtilService.getYearFromDate(dateUtilService.plusYear(new Date(), -10)) |
---|
170 | def endOfYearRange = dateUtilService.getYearFromDate(dateUtilService.plusYear(new Date(), 10)) |
---|
171 | associatedPropertyValues.yearRange = startOfYearRange..endOfYearRange |
---|
172 | |
---|
173 | return[ inventoryItemPurchaseList: inventoryItemPurchaseList, |
---|
174 | inventoryItemPurchaseTotal: inventoryItemPurchaseTotal, |
---|
175 | filterParams: filterParams, |
---|
176 | associatedPropertyValues: associatedPropertyValues ] |
---|
177 | |
---|
178 | } // end search() |
---|
179 | |
---|
180 | def show = { |
---|
181 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
182 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
183 | |
---|
184 | if(!inventoryItemPurchaseInstance) { |
---|
185 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
186 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
187 | } |
---|
188 | else { return [ inventoryItemPurchaseInstance : inventoryItemPurchaseInstance ] } |
---|
189 | } |
---|
190 | |
---|
191 | def delete = { |
---|
192 | def result = inventoryPurchaseService.delete(params) |
---|
193 | |
---|
194 | if(!result.error) { |
---|
195 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItemPurchase", params.id]) |
---|
196 | if(params.returnTo == 'inventoryItem') { |
---|
197 | redirect(controller: 'inventoryItemDetailed', |
---|
198 | action: 'show', |
---|
199 | id: result.inventoryItemId, |
---|
200 | params: [showTab: "showPurchasingTab"]) |
---|
201 | } |
---|
202 | else { |
---|
203 | redirect(action: 'search') |
---|
204 | } |
---|
205 | return |
---|
206 | } |
---|
207 | |
---|
208 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
209 | |
---|
210 | if(result.error.code == "default.not.found") { |
---|
211 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
212 | return |
---|
213 | } |
---|
214 | |
---|
215 | redirect(action:show, id: params.id) |
---|
216 | } |
---|
217 | |
---|
218 | def edit = { |
---|
219 | def result = inventoryPurchaseService.edit(params) |
---|
220 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
221 | |
---|
222 | if(!result.error) |
---|
223 | return [ inventoryItemPurchaseInstance : result.inventoryItemPurchaseInstance ] |
---|
224 | |
---|
225 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
226 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
227 | } |
---|
228 | |
---|
229 | def update = { |
---|
230 | def result = inventoryPurchaseService.update(params) |
---|
231 | |
---|
232 | if(!result.error) { |
---|
233 | flash.message = g.message(code: "default.update.success", args: ["Inventory Purchase", params.id]) |
---|
234 | redirect(action:show, id: params.id) |
---|
235 | return |
---|
236 | } |
---|
237 | |
---|
238 | if(result.error.code == "default.not.found") { |
---|
239 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
240 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
241 | return |
---|
242 | } |
---|
243 | |
---|
244 | render(view:'edit', model:[inventoryItemPurchaseInstance: result.inventoryItemPurchaseInstance.attach()]) |
---|
245 | } |
---|
246 | |
---|
247 | def create = { |
---|
248 | def inventoryItemPurchaseInstance = new InventoryItemPurchase() |
---|
249 | inventoryItemPurchaseInstance.properties = params |
---|
250 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
251 | |
---|
252 | if(!inventoryItemPurchaseInstance.inventoryItem) { |
---|
253 | flash.message = "Please select an inventory item then the 'purchasing' tab." |
---|
254 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
255 | return |
---|
256 | } |
---|
257 | |
---|
258 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance] |
---|
259 | } |
---|
260 | |
---|
261 | def save = { |
---|
262 | def result = inventoryPurchaseService.save(params) |
---|
263 | |
---|
264 | if(!result.error) { |
---|
265 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
266 | if(params.returnTo == 'inventoryItem') { |
---|
267 | redirect(controller: 'inventoryItemDetailed', |
---|
268 | action: 'show', |
---|
269 | id: result.inventoryItemId, |
---|
270 | params: [showTab: "showPurchasingTab"]) |
---|
271 | } |
---|
272 | else { |
---|
273 | redirect(action: 'search') |
---|
274 | } |
---|
275 | return |
---|
276 | } |
---|
277 | |
---|
278 | params.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
279 | render(view:'create', model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance]) |
---|
280 | } |
---|
281 | |
---|
282 | def receive = { |
---|
283 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
284 | |
---|
285 | if(!inventoryItemPurchaseInstance) { |
---|
286 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
287 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
288 | return |
---|
289 | } |
---|
290 | |
---|
291 | inventoryItemPurchaseInstance.properties = params |
---|
292 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
293 | def calcQuantities = inventoryPurchaseService.calcQuantities(inventoryItemPurchaseInstance) |
---|
294 | inventoryItemPurchaseInstance.quantity = calcQuantities.thisOrderRemaining |
---|
295 | inventoryItemPurchaseInstance.orderValueAmount = calcQuantities.thisOrderRemainingAmount |
---|
296 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
297 | 'orderId': inventoryItemPurchaseInstance.id] |
---|
298 | } |
---|
299 | |
---|
300 | def receiveSave = { |
---|
301 | def result = inventoryPurchaseService.receiveSave(params) |
---|
302 | |
---|
303 | if(!result.error) { |
---|
304 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
305 | if(params.returnTo == 'inventoryItem') { |
---|
306 | redirect(controller: 'inventoryItemDetailed', |
---|
307 | action: 'show', |
---|
308 | id: result.inventoryItemId, |
---|
309 | params: [showTab: "showPurchasingTab"]) |
---|
310 | } |
---|
311 | else { |
---|
312 | redirect(action: 'search') |
---|
313 | } |
---|
314 | return |
---|
315 | } |
---|
316 | |
---|
317 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
318 | |
---|
319 | if(result.error.code == "default.not.found") { |
---|
320 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
321 | return |
---|
322 | } |
---|
323 | |
---|
324 | render(view:'receive', |
---|
325 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
326 | 'orderId': result.orderId]) |
---|
327 | } |
---|
328 | |
---|
329 | def approveInvoicePayment = { |
---|
330 | // Read the values from Order Received. |
---|
331 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
332 | |
---|
333 | if(!inventoryItemPurchaseInstance) { |
---|
334 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
335 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
336 | return |
---|
337 | } |
---|
338 | |
---|
339 | inventoryItemPurchaseInstance.properties = params |
---|
340 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
341 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
342 | 'receivedId': inventoryItemPurchaseInstance.id] |
---|
343 | } |
---|
344 | |
---|
345 | def approveInvoicePaymentSave = { |
---|
346 | def result = inventoryPurchaseService.approveInvoicePaymentSave(params) |
---|
347 | |
---|
348 | if(!result.error) { |
---|
349 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
350 | if(params.returnTo == 'inventoryItem') { |
---|
351 | redirect(controller: 'inventoryItemDetailed', |
---|
352 | action: 'show', |
---|
353 | id: result.inventoryItemId, |
---|
354 | params: [showTab: "showPurchasingTab"]) |
---|
355 | } |
---|
356 | else { |
---|
357 | redirect(action: 'search') |
---|
358 | } |
---|
359 | return |
---|
360 | } |
---|
361 | |
---|
362 | if(result.error.code == "default.not.found") { |
---|
363 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
364 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
365 | return |
---|
366 | } |
---|
367 | |
---|
368 | render(view:'approveInvoicePayment', |
---|
369 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
370 | 'receivedId': result.receivedId]) |
---|
371 | } |
---|
372 | } |
---|