import org.codehaus.groovy.grails.plugins.springsecurity.Secured

class InventoryMovementDetailedController extends BaseController {

    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST']

    def list = {
        flash.message = flash.message
        redirect(controller:"taskDetailed", action:"search")
    }

    def show = {
        def inventoryMovementInstance = InventoryMovement.get( params.id )

        if(!inventoryMovementInstance) {
            flash.message = "InventoryMovement not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ inventoryMovementInstance : inventoryMovementInstance ] }
    }

    def delete = {
        def inventoryMovementInstance = InventoryMovement.get( params.id )
        if(inventoryMovementInstance) {
            try {
                def taskId = inventoryMovementInstance.task?.id
                inventoryMovementInstance.delete(flush:true)
                flash.message = "InventoryMovement ${params.id} deleted"
                redirect(controller:"taskDetailed", action:"show", id:taskId)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "InventoryMovement ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "InventoryMovement not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        if(!params.task?.id) {
            flash.message = "Please select a task, then the Inventory tab."
            redirect(controller:"taskDetailed", action:"search")
        }
        def inventoryMovementInstance = new InventoryMovement()
        inventoryMovementInstance.properties = params
        return ['inventoryMovementInstance':inventoryMovementInstance]
    }

    def save = {
        def inventoryMovementInstance = new InventoryMovement(params)
        if(!inventoryMovementInstance.hasErrors() && inventoryMovementInstance.save()) {
            flash.message = "Inventory Movement for ${inventoryMovementInstance.inventoryItem.name} created."
            redirect(controller:"taskDetailed", action:"show", id: params.task.id)
        }
        else {
            render(view:'create',model:[inventoryMovementInstance:inventoryMovementInstance])
        }
    }
}
