1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
4 | class InventoryItemDetailedController extends BaseController { |
---|
5 | |
---|
6 | def filterService |
---|
7 | def inventoryItemService |
---|
8 | def inventoryMovementService |
---|
9 | |
---|
10 | // the delete, save and update actions only accept POST requests |
---|
11 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
12 | |
---|
13 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
14 | def index = { redirect(action:search, params:params) } |
---|
15 | |
---|
16 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
17 | def list = { |
---|
18 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
19 | [ inventoryItemInstanceList: InventoryItem.list( params ), inventoryItemInstanceTotal: InventoryItem.count() ] |
---|
20 | } |
---|
21 | |
---|
22 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
23 | def search = { |
---|
24 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
25 | |
---|
26 | // Quick Search: |
---|
27 | if(!params.filter) { |
---|
28 | return[ inventoryItemInstanceList: InventoryItem.list( params ), inventoryItemInstanceTotal: InventoryItem.count(), filterParams: params ] |
---|
29 | } |
---|
30 | // filterPane: |
---|
31 | return[ inventoryItemInstanceList: filterService.filter( params, InventoryItem ), |
---|
32 | inventoryItemInstanceTotal: filterService.count( params, InventoryItem ), |
---|
33 | filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params), |
---|
34 | params:params ] |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
39 | */ |
---|
40 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
41 | def findInventoryItemForMovement = { |
---|
42 | if(!params.task?.id) { |
---|
43 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
44 | redirect(controller: "taskDetailed", action: "search") |
---|
45 | return |
---|
46 | } |
---|
47 | |
---|
48 | session.inventoryMovementTaskId = params.task.id |
---|
49 | flash.message = "Please find and then select the inventory item." |
---|
50 | redirect(action: search) |
---|
51 | } |
---|
52 | |
---|
53 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
54 | def show = { |
---|
55 | |
---|
56 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
57 | if(params._action_Show) |
---|
58 | { params.action='show' } |
---|
59 | |
---|
60 | if(!InventoryItem.exists(params.id)) { |
---|
61 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
62 | redirect(action:search) |
---|
63 | return |
---|
64 | } |
---|
65 | |
---|
66 | def result = inventoryItemService.prepareShowData(params) |
---|
67 | |
---|
68 | if(result.error) { |
---|
69 | flash.message = "Could not to prepare the data to show item with id: ${params.id}." |
---|
70 | redirect(action:search) |
---|
71 | return |
---|
72 | } |
---|
73 | |
---|
74 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
75 | inventoryMovementList: result.inventoryMovementList, |
---|
76 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
77 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
78 | showTab: result.showTab] |
---|
79 | |
---|
80 | if(session.inventoryMovementTaskId) { |
---|
81 | model.inventoryMovementInstance = new InventoryMovement() |
---|
82 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
83 | model.inventoryMovementInstance.quantity = 1 |
---|
84 | } |
---|
85 | |
---|
86 | return model |
---|
87 | } |
---|
88 | |
---|
89 | def delete = { |
---|
90 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
91 | if(inventoryItemInstance) { |
---|
92 | try { |
---|
93 | inventoryItemInstance.delete(flush:true) |
---|
94 | flash.message = "InventoryItem ${params.id} deleted" |
---|
95 | redirect(action:search) |
---|
96 | } |
---|
97 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
98 | flash.message = "InventoryItem ${params.id} could not be deleted" |
---|
99 | redirect(action:show,id:params.id) |
---|
100 | } |
---|
101 | } |
---|
102 | else { |
---|
103 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
104 | redirect(action:search) |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | def edit = { |
---|
109 | |
---|
110 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
111 | if(params._action_Edit) |
---|
112 | { params.action='edit' } |
---|
113 | |
---|
114 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
115 | |
---|
116 | if(!inventoryItemInstance) { |
---|
117 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
118 | redirect(action:search) |
---|
119 | } |
---|
120 | else { |
---|
121 | return [ inventoryItemInstance : inventoryItemInstance ] |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | def update = { |
---|
126 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
127 | if(inventoryItemInstance) { |
---|
128 | if(params.version) { |
---|
129 | def version = params.version.toLong() |
---|
130 | if(inventoryItemInstance.version > version) { |
---|
131 | |
---|
132 | inventoryItemInstance.errors.rejectValue("version", "inventoryItem.optimistic.locking.failure", "Another user has updated this InventoryItem while you were editing.") |
---|
133 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
134 | return |
---|
135 | } |
---|
136 | } |
---|
137 | inventoryItemInstance.properties = params |
---|
138 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
139 | flash.message = "InventoryItem ${params.id} updated" |
---|
140 | redirect(action:show,id:inventoryItemInstance.id) |
---|
141 | } |
---|
142 | else { |
---|
143 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
144 | } |
---|
145 | } |
---|
146 | else { |
---|
147 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
148 | redirect(action:search) |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | def create = { |
---|
153 | def inventoryItemInstance = new InventoryItem() |
---|
154 | inventoryItemInstance.properties = params |
---|
155 | return ['inventoryItemInstance':inventoryItemInstance] |
---|
156 | } |
---|
157 | |
---|
158 | def save = { |
---|
159 | def inventoryItemInstance = new InventoryItem(params) |
---|
160 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
161 | flash.message = "InventoryItem ${inventoryItemInstance.id} created" |
---|
162 | redirect(action:show,id:inventoryItemInstance.id) |
---|
163 | } |
---|
164 | else { |
---|
165 | render(view:'create',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Handles the use inventory item form submit in the show view. |
---|
171 | */ |
---|
172 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
173 | def useInventoryItem = { |
---|
174 | |
---|
175 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
176 | def result = inventoryMovementService.move(params) |
---|
177 | |
---|
178 | if(!result.error) { |
---|
179 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
180 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
181 | } |
---|
182 | else { |
---|
183 | if(result.inventoryMovementInstance) { |
---|
184 | def p = [:] |
---|
185 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
186 | def r = inventoryItemService.prepareShowData(p) |
---|
187 | |
---|
188 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
189 | inventoryMovementList: r.inventoryMovementList, |
---|
190 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
191 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
192 | showTab: r.showTab] |
---|
193 | |
---|
194 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
195 | |
---|
196 | render(view: 'show', model: model) |
---|
197 | } |
---|
198 | else { |
---|
199 | flash.message = "Could not create inventory movement." |
---|
200 | redirect(action:"search") |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | } |
---|