source: trunk/grails-app/controllers/InventoryMovementDetailedController.groovy @ 807

Last change on this file since 807 was 448, checked in by gav, 15 years ago

Small correction to inventoryMovement redirect on delete.

File size: 4.7 KB
RevLine 
[175]1import org.codehaus.groovy.grails.plugins.springsecurity.Secured
2
[298]3@Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager'])
[175]4class InventoryMovementDetailedController extends BaseController {
5
[177]6    def inventoryMovementService
7
[175]8    // the delete, save and update actions only accept POST requests
9    static allowedMethods = [delete:'POST', save:'POST']
10
[298]11    def index = { redirect(action:list,params:params) }
12
[175]13    def list = {
14        flash.message = flash.message
15        redirect(controller:"taskDetailed", action:"search")
16    }
17
[298]18    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser'])
[175]19    def show = {
20        def inventoryMovementInstance = InventoryMovement.get( params.id )
21
22        if(!inventoryMovementInstance) {
23            flash.message = "InventoryMovement not found with id ${params.id}"
24            redirect(action:list)
25        }
26        else { return [ inventoryMovementInstance : inventoryMovementInstance ] }
27    }
28
[224]29    /**
30    * List the inventory movements for an InventoryItem.
[226]31    * @param params.inventoryItem.id The id of an existing inventory item.
[224]32    */
[298]33    @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser'])
[224]34    def listInventoryMovements = {
[442]35        def inventoryItemInstance
36        if(params.id)
37            inventoryItemInstance = InventoryItem.get(params.id)
38        else if(params.inventoryItem?.id)
39            inventoryItemInstance = InventoryItem.get(params.inventoryItem.id)
[224]40
41        if(!inventoryItemInstance) {
42            flash.message = "Inventory item not found with id ${params.id}"
43            redirect(controller:'inventoryItemDetailed', action: 'search')
[442]44            return
[224]45        }
46
47        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
48        def inventoryMovementList = InventoryMovement.findAllByInventoryItem(inventoryItemInstance, params)
49        def inventoryMovementListTotal = InventoryMovement.countByInventoryItem(inventoryItemInstance)
50
51        [ inventoryMovementList: inventoryMovementList,
52            inventoryMovementListTotal:  inventoryMovementListTotal,
53            inventoryItemInstance: inventoryItemInstance]
54    }
55
[175]56    def delete = {
[177]57        def result = inventoryMovementService.reverseMove(params)
58
59        if(!result.error) {
60            flash.message = "InventoryMovement ${params.id} deleted"
[448]61            if(result.taskId) {
[406]62                redirect(controller:"taskDetailed", action:"show", id:result.taskId)
[448]63                return
64            }
[406]65            redirect(controller:"inventoryItemDetailed", action:"show", id:result.inventoryItemInstance?.id)
[177]66        }
67        else {
68            if(result.inventoryMovementInstance) {
[205]69                render(view:'show',model:[inventoryMovementInstance:result.inventoryMovementInstance.attach()])
[175]70            }
[177]71            else {
72                flash.message = "Could not delete inventory movement."
[191]73                redirect(controller:"taskDetailed", action:"search")
[175]74            }
75        }
76    }
77
78    def create = {
79        def inventoryMovementInstance = new InventoryMovement()
80        inventoryMovementInstance.properties = params
[226]81        def inventoryMovementTypeList = InventoryMovementType.withCriteria { gt("id", 1L) } // Don't include "Used".
82
83        return ['inventoryMovementInstance': inventoryMovementInstance,
84                        inventoryMovementTypeList: inventoryMovementTypeList]
[175]85    }
86
[226]87    /**
88    * Handles all inventory movements except those of type "Used".
89     * The "Used" type is handled directly by InventoryItemDetailedController and useInventoryItem.
90    */
[175]91    def save = {
[177]92        def result = inventoryMovementService.move(params)
93
94        if(!result.error) {
95            flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name} created."
[226]96            if(result.taskId) {
97                redirect(controller:"taskDetailed", action:"show", id: result.taskId)
98                return
99            }
100            redirect(controller:"inventoryItemDetailed", action:"show", id: result.inventoryMovementInstance.inventoryItem.id)
[175]101        }
102        else {
[177]103            if(result.inventoryMovementInstance) {
[226]104                def inventoryMovementTypeList = InventoryMovementType.withCriteria { gt("id", 1L) } // Don't include "Used".
105                render(view:'create', model:[inventoryMovementInstance: result.inventoryMovementInstance,
106                                                            inventoryMovementTypeList: inventoryMovementTypeList])
[177]107            }
108            else {
109                flash.message = "Could not create inventory movement."
110                redirect(controller:"taskDetailed", action:"search", id:result.taskId)
111            }
112
[175]113        }
114    }
[177]115
[175]116}
Note: See TracBrowser for help on using the repository browser.