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