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", "dateEntered", "costCode", "quantity", "orderValueAmount", "invoiceNumber", |
---|
151 | "inventoryItemPurchaseType"] |
---|
152 | Map labels = ["purchaseOrderNumber": "Order Number", "dateEntered": "Date", "costCode": "Cost Code", |
---|
153 | "quantity": "Quantity", "orderValueAmount": "Order \$", "invoiceNumber": "Invoice Number", |
---|
154 | "inventoryItemPurchaseType": "Type"] |
---|
155 | Map formatters = [ dateEntered: 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 | return[ inventoryItemPurchaseList: inventoryItemPurchaseList, |
---|
168 | inventoryItemPurchaseTotal: inventoryItemPurchaseTotal, |
---|
169 | filterParams: filterParams ] |
---|
170 | |
---|
171 | } // end search() |
---|
172 | |
---|
173 | def show = { |
---|
174 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
175 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
176 | |
---|
177 | if(!inventoryItemPurchaseInstance) { |
---|
178 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
179 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
180 | } |
---|
181 | else { return [ inventoryItemPurchaseInstance : inventoryItemPurchaseInstance ] } |
---|
182 | } |
---|
183 | |
---|
184 | def delete = { |
---|
185 | def result = inventoryPurchaseService.delete(params) |
---|
186 | |
---|
187 | if(!result.error) { |
---|
188 | flash.message = g.message(code: "default.delete.success", args: ["InventoryItemPurchase", params.id]) |
---|
189 | if(params.returnTo == 'inventoryItem') { |
---|
190 | redirect(controller: 'inventoryItemDetailed', |
---|
191 | action: 'show', |
---|
192 | id: result.inventoryItemId, |
---|
193 | params: [showTab: "showPurchasingTab"]) |
---|
194 | } |
---|
195 | else { |
---|
196 | redirect(action: 'search') |
---|
197 | } |
---|
198 | return |
---|
199 | } |
---|
200 | |
---|
201 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
202 | |
---|
203 | if(result.error.code == "default.not.found") { |
---|
204 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
205 | return |
---|
206 | } |
---|
207 | |
---|
208 | redirect(action:show, id: params.id) |
---|
209 | } |
---|
210 | |
---|
211 | def edit = { |
---|
212 | def result = inventoryPurchaseService.edit(params) |
---|
213 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
214 | |
---|
215 | if(!result.error) |
---|
216 | return [ inventoryItemPurchaseInstance : result.inventoryItemPurchaseInstance ] |
---|
217 | |
---|
218 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
219 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
220 | } |
---|
221 | |
---|
222 | def update = { |
---|
223 | def result = inventoryPurchaseService.update(params) |
---|
224 | |
---|
225 | if(!result.error) { |
---|
226 | flash.message = g.message(code: "default.update.success", args: ["Inventory Purchase", params.id]) |
---|
227 | redirect(action:show, id: params.id) |
---|
228 | return |
---|
229 | } |
---|
230 | |
---|
231 | if(result.error.code == "default.not.found") { |
---|
232 | flash.message = g.message(code: result.error.code, args: result.error.args) |
---|
233 | redirect(controller: 'inventoryItemDetailed', action:'search', params:params) |
---|
234 | return |
---|
235 | } |
---|
236 | |
---|
237 | render(view:'edit', model:[inventoryItemPurchaseInstance: result.inventoryItemPurchaseInstance.attach()]) |
---|
238 | } |
---|
239 | |
---|
240 | def create = { |
---|
241 | def inventoryItemPurchaseInstance = new InventoryItemPurchase() |
---|
242 | inventoryItemPurchaseInstance.properties = params |
---|
243 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
244 | |
---|
245 | if(!inventoryItemPurchaseInstance.inventoryItem) { |
---|
246 | flash.message = "Please select an inventory item then the 'purchasing' tab." |
---|
247 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
248 | return |
---|
249 | } |
---|
250 | |
---|
251 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance] |
---|
252 | } |
---|
253 | |
---|
254 | def save = { |
---|
255 | def result = inventoryPurchaseService.save(params) |
---|
256 | |
---|
257 | if(!result.error) { |
---|
258 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
259 | if(params.returnTo == 'inventoryItem') { |
---|
260 | redirect(controller: 'inventoryItemDetailed', |
---|
261 | action: 'show', |
---|
262 | id: result.inventoryItemId, |
---|
263 | params: [showTab: "showPurchasingTab"]) |
---|
264 | } |
---|
265 | else { |
---|
266 | redirect(action: 'search') |
---|
267 | } |
---|
268 | return |
---|
269 | } |
---|
270 | |
---|
271 | render(view:'create', model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance]) |
---|
272 | } |
---|
273 | |
---|
274 | def receive = { |
---|
275 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
276 | |
---|
277 | if(!inventoryItemPurchaseInstance) { |
---|
278 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
279 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
280 | return |
---|
281 | } |
---|
282 | |
---|
283 | inventoryItemPurchaseInstance.properties = params |
---|
284 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
285 | def calcQuantities = inventoryPurchaseService.calcQuantities(inventoryItemPurchaseInstance) |
---|
286 | inventoryItemPurchaseInstance.quantity = calcQuantities.thisOrderRemaining |
---|
287 | inventoryItemPurchaseInstance.orderValueAmount = calcQuantities.thisOrderRemainingAmount |
---|
288 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
289 | 'orderId': inventoryItemPurchaseInstance.id] |
---|
290 | } |
---|
291 | |
---|
292 | def receiveSave = { |
---|
293 | def result = inventoryPurchaseService.receiveSave(params) |
---|
294 | |
---|
295 | if(!result.error) { |
---|
296 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
297 | if(params.returnTo == 'inventoryItem') { |
---|
298 | redirect(controller: 'inventoryItemDetailed', |
---|
299 | action: 'show', |
---|
300 | id: result.inventoryItemId, |
---|
301 | params: [showTab: "showPurchasingTab"]) |
---|
302 | } |
---|
303 | else { |
---|
304 | redirect(action: 'search') |
---|
305 | } |
---|
306 | return |
---|
307 | } |
---|
308 | |
---|
309 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
310 | |
---|
311 | if(result.error.code == "default.not.found") { |
---|
312 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
313 | return |
---|
314 | } |
---|
315 | |
---|
316 | render(view:'receive', |
---|
317 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
318 | 'orderId': result.orderId]) |
---|
319 | } |
---|
320 | |
---|
321 | def approveInvoicePayment = { |
---|
322 | // Read the values from Order Received. |
---|
323 | def inventoryItemPurchaseInstance = InventoryItemPurchase.read( params.id ) |
---|
324 | |
---|
325 | if(!inventoryItemPurchaseInstance) { |
---|
326 | flash.message = "InventoryItemPurchase not found with id ${params.id}" |
---|
327 | redirect(controller: 'inventoryItemDetailed', action:'search') |
---|
328 | return |
---|
329 | } |
---|
330 | |
---|
331 | inventoryItemPurchaseInstance.properties = params |
---|
332 | params.returnTo = params.returnTo ?: 'inventoryItem' |
---|
333 | return ['inventoryItemPurchaseInstance':inventoryItemPurchaseInstance, |
---|
334 | 'receivedId': inventoryItemPurchaseInstance.id] |
---|
335 | } |
---|
336 | |
---|
337 | def approveInvoicePaymentSave = { |
---|
338 | def result = inventoryPurchaseService.approveInvoicePaymentSave(params) |
---|
339 | |
---|
340 | if(!result.error) { |
---|
341 | flash.message = g.message(code: "default.create.success", args: ["Inventory Purchase", '']) |
---|
342 | if(params.returnTo == 'inventoryItem') { |
---|
343 | redirect(controller: 'inventoryItemDetailed', |
---|
344 | action: 'show', |
---|
345 | id: result.inventoryItemId, |
---|
346 | params: [showTab: "showPurchasingTab"]) |
---|
347 | } |
---|
348 | else { |
---|
349 | redirect(action: 'search') |
---|
350 | } |
---|
351 | return |
---|
352 | } |
---|
353 | |
---|
354 | if(result.error.code == "default.not.found") { |
---|
355 | flash.errorMessage = g.message(code: result.error.code, args: result.error.args) |
---|
356 | redirect(controller: 'inventoryItemDetailed', action: 'search') |
---|
357 | return |
---|
358 | } |
---|
359 | |
---|
360 | render(view:'approveInvoicePayment', |
---|
361 | model:['inventoryItemPurchaseInstance': result.inventoryItemPurchaseInstance, |
---|
362 | 'receivedId': result.receivedId]) |
---|
363 | } |
---|
364 | } |
---|