1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | import org.codehaus.groovy.grails.commons.ConfigurationHolder |
---|
3 | |
---|
4 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
5 | class InventoryItemDetailedController extends BaseController { |
---|
6 | |
---|
7 | def filterService |
---|
8 | def exportService |
---|
9 | def inventoryItemService |
---|
10 | def inventoryMovementService |
---|
11 | |
---|
12 | // the delete, save and update actions only accept POST requests |
---|
13 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
14 | |
---|
15 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
16 | def index = { redirect(action:search, params:params) } |
---|
17 | |
---|
18 | /** |
---|
19 | * Set session.inventoryItemSearchParamsMax |
---|
20 | */ |
---|
21 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
22 | def setSearchParamsMax = { |
---|
23 | def max = 1000 |
---|
24 | if(params.newMax.isInteger()) { |
---|
25 | def i = params.newMax.toInteger() |
---|
26 | if(i > 0 && i <= max) |
---|
27 | session.inventoryItemSearchParamsMax = params.newMax |
---|
28 | if(i > max) |
---|
29 | session.inventoryItemSearchParamsMax = max |
---|
30 | } |
---|
31 | forward(action: 'search', params: params) |
---|
32 | } |
---|
33 | |
---|
34 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
35 | def search = { |
---|
36 | |
---|
37 | if(session.inventoryItemSearchParamsMax) |
---|
38 | params.max = session.inventoryItemSearchParamsMax |
---|
39 | |
---|
40 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
41 | |
---|
42 | def inventoryItemInstanceList = [] |
---|
43 | def inventoryItemInstanceTotal |
---|
44 | def filterParams = [:] |
---|
45 | |
---|
46 | // Quick Search: |
---|
47 | if(!params.filter) { |
---|
48 | inventoryItemInstanceList = InventoryItem.list( params ) |
---|
49 | inventoryItemInstanceTotal = InventoryItem.count() |
---|
50 | filterParams = params |
---|
51 | } |
---|
52 | else { |
---|
53 | // filterPane: |
---|
54 | inventoryItemInstanceList = filterService.filter( params, InventoryItem ) |
---|
55 | inventoryItemInstanceTotal = filterService.count( params, InventoryItem ) |
---|
56 | filterParams = com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params) |
---|
57 | } |
---|
58 | |
---|
59 | // export plugin: |
---|
60 | if(params?.format && params.format != "html") { |
---|
61 | |
---|
62 | def dateFmt = { date -> |
---|
63 | formatDate(format: "EEE, dd-MMM-yyyy", date: date) |
---|
64 | } |
---|
65 | String title = "Inventory List." |
---|
66 | |
---|
67 | response.contentType = ConfigurationHolder.config.grails.mime.types[params.format] |
---|
68 | response.setHeader("Content-disposition", "attachment; filename=Inventory.${params.extension}") |
---|
69 | List fields = ["name", |
---|
70 | "description", |
---|
71 | "unitsInStock", |
---|
72 | "unitOfMeasure", |
---|
73 | "inventoryLocation", |
---|
74 | "inventoryLocation.inventoryStore"] |
---|
75 | Map labels = ["name": "Name", |
---|
76 | "description": "Description", |
---|
77 | "unitsInStock":"In Stock", |
---|
78 | "unitOfMeasure": "UOM", |
---|
79 | "inventoryLocation": "Location", |
---|
80 | "inventoryLocation.inventoryStore": "Store"] |
---|
81 | |
---|
82 | Map formatters = [:] |
---|
83 | Map parameters = [title: title, separator: ","] |
---|
84 | |
---|
85 | exportService.export(params.format, |
---|
86 | response.outputStream, |
---|
87 | inventoryItemInstanceList.sort { p1, p2 -> p1.name.compareToIgnoreCase(p2.name) }, |
---|
88 | fields, |
---|
89 | labels, |
---|
90 | formatters, |
---|
91 | parameters) |
---|
92 | } |
---|
93 | |
---|
94 | // Add some basic params to filterParams. |
---|
95 | filterParams.max = params.max |
---|
96 | filterParams.offset = params.offset?.toInteger() ?: 0 |
---|
97 | filterParams.sort = params.sort ?: "id" |
---|
98 | filterParams.order = params.order ?: "desc" |
---|
99 | |
---|
100 | return[ inventoryItemInstanceList: inventoryItemInstanceList, |
---|
101 | inventoryItemInstanceTotal: inventoryItemInstanceTotal, |
---|
102 | filterParams: filterParams ] |
---|
103 | } // end search() |
---|
104 | |
---|
105 | /** |
---|
106 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
107 | */ |
---|
108 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
109 | def findInventoryItemForMovement = { |
---|
110 | if(!params.task?.id) { |
---|
111 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
112 | redirect(controller: "taskDetailed", action: "search") |
---|
113 | return |
---|
114 | } |
---|
115 | |
---|
116 | session.inventoryMovementTaskId = params.task.id |
---|
117 | flash.message = "Please find and then select the inventory item." |
---|
118 | redirect(action: search) |
---|
119 | } |
---|
120 | |
---|
121 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
122 | def show = { |
---|
123 | |
---|
124 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
125 | if(params._action_Show) |
---|
126 | params.action='show' |
---|
127 | |
---|
128 | if(!InventoryItem.exists(params.id)) { |
---|
129 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
130 | redirect(action:search) |
---|
131 | return |
---|
132 | } |
---|
133 | |
---|
134 | def result = inventoryItemService.prepareShowData(params) |
---|
135 | |
---|
136 | if(result.error) { |
---|
137 | flash.message = "Could not to prepare the data to show item with id: ${params.id}." |
---|
138 | redirect(action:search) |
---|
139 | return |
---|
140 | } |
---|
141 | |
---|
142 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
143 | inventoryMovementList: result.inventoryMovementList, |
---|
144 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
145 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
146 | showTab: result.showTab] |
---|
147 | |
---|
148 | if(session.inventoryMovementTaskId) { |
---|
149 | model.inventoryMovementInstance = new InventoryMovement() |
---|
150 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
151 | model.inventoryMovementInstance.quantity = 1 |
---|
152 | } |
---|
153 | |
---|
154 | return model |
---|
155 | } |
---|
156 | |
---|
157 | def delete = { |
---|
158 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
159 | if(inventoryItemInstance) { |
---|
160 | try { |
---|
161 | inventoryItemInstance.delete(flush:true) |
---|
162 | flash.message = "InventoryItem ${params.id} deleted" |
---|
163 | redirect(action:search) |
---|
164 | } |
---|
165 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
166 | flash.message = "InventoryItem ${params.id} could not be deleted" |
---|
167 | redirect(action:show,id:params.id) |
---|
168 | } |
---|
169 | } |
---|
170 | else { |
---|
171 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
172 | redirect(action:search) |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | def edit = { |
---|
177 | |
---|
178 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
179 | if(params._action_Edit) |
---|
180 | params.action='edit' |
---|
181 | |
---|
182 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
183 | |
---|
184 | if(!inventoryItemInstance) { |
---|
185 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
186 | redirect(action:search) |
---|
187 | } |
---|
188 | else { |
---|
189 | return [ inventoryItemInstance : inventoryItemInstance ] |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | def update = { |
---|
194 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
195 | if(inventoryItemInstance) { |
---|
196 | if(params.version) { |
---|
197 | def version = params.version.toLong() |
---|
198 | if(inventoryItemInstance.version > version) { |
---|
199 | |
---|
200 | inventoryItemInstance.errors.rejectValue("version", "inventoryItem.optimistic.locking.failure", "Another user has updated this InventoryItem while you were editing.") |
---|
201 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
202 | return |
---|
203 | } |
---|
204 | } |
---|
205 | inventoryItemInstance.properties = params |
---|
206 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
207 | flash.message = "InventoryItem ${params.id} updated" |
---|
208 | redirect(action:show,id:inventoryItemInstance.id) |
---|
209 | } |
---|
210 | else { |
---|
211 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
212 | } |
---|
213 | } |
---|
214 | else { |
---|
215 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
216 | redirect(action:search) |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | def create = { |
---|
221 | def inventoryItemInstance = new InventoryItem() |
---|
222 | inventoryItemInstance.properties = params |
---|
223 | return ['inventoryItemInstance':inventoryItemInstance] |
---|
224 | } |
---|
225 | |
---|
226 | def save = { |
---|
227 | def inventoryItemInstance = new InventoryItem(params) |
---|
228 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
229 | flash.message = "InventoryItem ${inventoryItemInstance.id} created" |
---|
230 | redirect(action:show,id:inventoryItemInstance.id) |
---|
231 | } |
---|
232 | else { |
---|
233 | render(view:'create',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | /** |
---|
238 | * Handles the use inventory item form submit in the show view. |
---|
239 | */ |
---|
240 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
241 | def useInventoryItem = { |
---|
242 | |
---|
243 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
244 | def result = inventoryMovementService.move(params) |
---|
245 | |
---|
246 | if(!result.error) { |
---|
247 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
248 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
249 | } |
---|
250 | else { |
---|
251 | if(result.inventoryMovementInstance) { |
---|
252 | def p = [:] |
---|
253 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
254 | def r = inventoryItemService.prepareShowData(p) |
---|
255 | |
---|
256 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
257 | inventoryMovementList: r.inventoryMovementList, |
---|
258 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
259 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
260 | showTab: r.showTab] |
---|
261 | |
---|
262 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
263 | |
---|
264 | render(view: 'show', model: model) |
---|
265 | } |
---|
266 | else { |
---|
267 | flash.message = "Could not create inventory movement." |
---|
268 | redirect(action:"search") |
---|
269 | } |
---|
270 | |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | } |
---|