Index: trunk/grails-app/controllers/InventoryItemDetailedController.groovy
===================================================================
--- trunk/grails-app/controllers/InventoryItemDetailedController.groovy	(revision 178)
+++ trunk/grails-app/controllers/InventoryItemDetailedController.groovy	(revision 225)
@@ -4,9 +4,11 @@
 
     def filterService
+    def inventoryItemService
+    def inventoryMovementService
 
     def index = { redirect(action:search, params:params) }
 
     // the delete, save and update actions only accept POST requests
-    static allowedMethods = [delete:'POST', save:'POST', update:'POST']
+    static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST']
 
     def list = {
@@ -29,17 +31,52 @@
     }
 
+    /**
+    * Simply assigns a passed in task id to a session variable and redirects to search.
+    */
+    def findInventoryItemForMovement = {
+        if(!params.task?.id) {
+            flash.message = "No task id supplied, please select a task then the inventory tab."
+            redirect(controller: "taskDetailed", action: "search")
+            return
+        }
+
+        session.inventoryMovementTaskId = params.task.id
+        flash.message = "Please find and then select the inventory item."
+        redirect(action: search)
+    }
+
     def show = {
-        
+
         // In the case of an actionSubmit button, rewrite action name from 'index'.
         if(params._action_Show)
         { params.action='show' }
-        
-        def inventoryItemInstance = InventoryItem.get( params.id )
 
-        if(!inventoryItemInstance) {
+        if(!InventoryItem.exists(params.id)) {
             flash.message = "InventoryItem not found with id ${params.id}"
             redirect(action:search)
+            return
         }
-        else { return [ inventoryItemInstance : inventoryItemInstance ] }
+
+        def result = inventoryItemService.prepareShowData(params)
+
+        if(result.error) {
+            flash.message = "Could not to prepare the data to show item with id: ${params.id}."
+            redirect(action:search)
+            return
+        }
+
+        def model = [ inventoryItemInstance: result.inventoryItemInstance,
+                                inventoryMovementList: result.inventoryMovementList,
+                                inventoryMovementListTotal: result.inventoryMovementListTotal,
+                                inventoryMovementListMax: result.inventoryMovementListMax,
+                                showTab: result.showTab]
+
+        if(session.inventoryMovementTaskId) {
+            model.inventoryMovementInstance = new InventoryMovement()
+            model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId)
+            model.inventoryMovementInstance.quantity = 1
+        }
+
+        return model
     }
 
@@ -123,3 +160,40 @@
         }
     }
+
+    /**
+    * Handles the use inventory item form submit in the show view.
+    */
+    def useInventoryItem = {
+
+        params.inventoryMovementType = InventoryMovementType.get(1) // used.
+        def result = inventoryMovementService.move(params)
+
+        if(!result.error) {
+            flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created."
+            redirect(controller:"taskDetailed", action:"show", id: result.taskId)
+        }
+        else {
+            if(result.inventoryMovementInstance) {
+                def p = [:]
+                p.id = result.inventoryMovementInstance.inventoryItem?.id
+                def r = inventoryItemService.prepareShowData(p)
+
+                def model = [ inventoryItemInstance: r.inventoryItemInstance,
+                                        inventoryMovementList: r.inventoryMovementList,
+                                        inventoryMovementListTotal: r.inventoryMovementListTotal,
+                                        inventoryMovementListMax: r.inventoryMovementListMax,
+                                        showTab: r.showTab]
+
+                model.inventoryMovementInstance = result.inventoryMovementInstance
+
+                render(view: 'show', model: model)
+            }
+            else {
+                flash.message = "Could not create inventory movement."
+                redirect(action:"search")
+            }
+
+        }
+    }
+
 }
