source: trunk/grails-app/controllers/InventoryItemDetailedController.groovy @ 268

Last change on this file since 268 was 226, checked in by gav, 15 years ago

Complete functionality for inventory movement types other than "Used".

File size: 7.5 KB
RevLine 
[116]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
3class InventoryItemDetailedController extends BaseController {
[156]4
5    def filterService
[225]6    def inventoryItemService
7    def inventoryMovementService
[156]8
[139]9    def index = { redirect(action:search, params:params) }
[116]10
11    // the delete, save and update actions only accept POST requests
[225]12    static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST']
[116]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
[139]19    def search = {
20        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
[156]21
22        // Quick Search:
23        if(!params.filter) {
[178]24            return[ inventoryItemInstanceList: InventoryItem.list( params ), inventoryItemInstanceTotal: InventoryItem.count(), filterParams: params ]
[156]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 ]
[139]31    }
32
[225]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
[116]48    def show = {
[225]49
[139]50        // In the case of an actionSubmit button, rewrite action name from 'index'.
51        if(params._action_Show)
52        { params.action='show' }
[116]53
[225]54        if(!InventoryItem.exists(params.id)) {
[116]55            flash.message = "InventoryItem not found with id ${params.id}"
[139]56            redirect(action:search)
[225]57            return
[116]58        }
[225]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
[116]81    }
82
83    def delete = {
84        def inventoryItemInstance = InventoryItem.get( params.id )
85        if(inventoryItemInstance) {
86            try {
[175]87                inventoryItemInstance.delete(flush:true)
[116]88                flash.message = "InventoryItem ${params.id} deleted"
[139]89                redirect(action:search)
[116]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}"
[139]98            redirect(action:search)
[116]99        }
100    }
101
102    def edit = {
[139]103       
104        // In the case of an actionSubmit button, rewrite action name from 'index'.
105        if(params._action_Edit)
106        { params.action='edit' }
107       
[116]108        def inventoryItemInstance = InventoryItem.get( params.id )
109
110        if(!inventoryItemInstance) {
111            flash.message = "InventoryItem not found with id ${params.id}"
[139]112            redirect(action:search)
[116]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
[178]132            if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) {
[116]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}"
[175]142            redirect(action:search)
[116]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)
[178]154        if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) {
[116]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    }
[225]162
163    /**
164    * Handles the use inventory item form submit in the show view.
165    */
166    def useInventoryItem = {
167
[226]168        params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used".
[225]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
[116]199}
Note: See TracBrowser for help on using the repository browser.