Index: trunk/grails-app/services/InventoryMovementService.groovy
===================================================================
--- trunk/grails-app/services/InventoryMovementService.groovy	(revision 177)
+++ trunk/grails-app/services/InventoryMovementService.groovy	(revision 191)
@@ -6,29 +6,34 @@
         InventoryMovement.withTransaction { status ->
             def result = [:]
-            def inventoryMovementInstance = InventoryMovement.lock(params.id)
-            if(inventoryMovementInstance) {
-                def inventoryItem = InventoryItem.lock(inventoryMovementInstance.inventoryItem.id)
-                result.taskId = inventoryMovementInstance.task.id
+            result.inventoryMovementInstance = InventoryMovement.lock(params.id)
+            if(result.inventoryMovementInstance) {
+                def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id)
+                result.taskId = result.inventoryMovementInstance.task.id
 
                 if(!inventoryItem) {
                         status.setRollbackOnly()
-                        inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound")
-                        result.inventoryMovementInstance = inventoryMovementInstance
+                        result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound")
                         result.error = true
                         return result
                 }
 
+                if(result.inventoryMovementInstance.task.taskStatus.id == 3) {
+                    status.setRollbackOnly()
+                    result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask")
+                    result.error = true
+                    return result
+                }
+
                 // Reverse the movement of inventory.
-                if(!inventoryMovementInstance.inventoryMovementType.incrementsInventory) {
-                    inventoryItem.unitsInStock += inventoryMovementInstance.quantity
+                if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) {
+                    inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity
                 }
                 else {
-                    if(inventoryItem.unitsInStock >= inventoryMovementInstance.quantity) {
-                        inventoryItem.unitsInStock -= inventoryMovementInstance.quantity
+                    if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) {
+                        inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity
                     }
                     else {
                         status.setRollbackOnly()
-                        inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock")
-                        result.inventoryMovementInstance = inventoryMovementInstance
+                        result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock")
                         result.error = true
                         return result
@@ -37,5 +42,6 @@
 
                 if(inventoryItem.save() ) {
-                    inventoryMovementInstance.delete()
+                    // All went well if we get to this point.
+                    result.inventoryMovementInstance.delete()
                     return result
                 }
@@ -58,30 +64,35 @@
         InventoryMovement.withTransaction { status ->
             def result = [:]
-            def inventoryMovementInstance = new InventoryMovement(params)
+            result.inventoryMovementInstance = new InventoryMovement(params)
 
-            if(inventoryMovementInstance.validate()) {
-                def inventoryItem = InventoryItem.lock(inventoryMovementInstance.inventoryItem.id)
-                result.taskId = inventoryMovementInstance.task.id
+            if(result.inventoryMovementInstance.validate()) {
+                def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id)
+                result.taskId = result.inventoryMovementInstance.task.id
 
                 if(!inventoryItem) {
-                        status.setRollbackOnly()
-                        inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound")
-                        result.inventoryMovementInstance = inventoryMovementInstance
-                        result.error = true
-                        return result
+                    status.setRollbackOnly()
+                    result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound")
+                    result.error = true
+                    return result
+                }
+
+                if(result.inventoryMovementInstance.task.taskStatus.id == 3) {
+                    status.setRollbackOnly()
+                    result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask")
+                    result.error = true
+                    return result
                 }
 
                 // Perform the movement of inventory.
-                if(inventoryMovementInstance.inventoryMovementType.incrementsInventory) {
-                    inventoryItem.unitsInStock += inventoryMovementInstance.quantity
+                if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) {
+                    inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity
                 }
                 else {
-                    if(inventoryItem.unitsInStock >= inventoryMovementInstance.quantity) {
-                        inventoryItem.unitsInStock -= inventoryMovementInstance.quantity
+                    if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) {
+                        inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity
                     }
                     else {
                         status.setRollbackOnly()
-                        inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock")
-                        result.inventoryMovementInstance = inventoryMovementInstance
+                        result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock")
                         result.error = true
                         return result
@@ -89,11 +100,10 @@
                 }
 
-                if(inventoryItem.save() && inventoryMovementInstance.save()) {
-                    result.inventoryMovementInstance = inventoryMovementInstance
+                if(inventoryItem.save() && result.inventoryMovementInstance.save()) {
+                    // All went well if we get to this point.
                     return result
                 }
                 else {
                     status.setRollbackOnly()
-                    result.inventoryMovementInstance = inventoryMovementInstance
                     result.error = true
                     return result
@@ -102,5 +112,4 @@
             }
             else {
-                result.inventoryMovementInstance = inventoryMovementInstance
                 result.error = true
                 return result
