class InventoryMovementService {

    boolean transactional = false

    def personService

    def reverseMove(params) {
        InventoryMovement.withTransaction { status ->
            def result = [:]
            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()
                        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(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) {
                    inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity
                }
                else {
                    if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) {
                        inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity
                    }
                    else {
                        status.setRollbackOnly()
                        result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock")
                        result.error = true
                        return result
                    }
                }

                if(inventoryItem.save() ) {
                    // All went well if we get to this point.
                    result.inventoryMovementInstance.delete()
                    return result
                }
                else {
                    status.setRollbackOnly()
                    result.error = true
                    return result
                }

            }
            else {
                result.error = true
                return result
            }

        } // end withTransaction
    } //end reverseMove()

    def move(params) {
        InventoryMovement.withTransaction { status ->
            def result = [:]
            result.inventoryMovementInstance = new InventoryMovement(params)

            result.inventoryMovementInstance.person = personService.currentUser

            if(result.inventoryMovementInstance.validate()) {
                def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id)
                result.taskId = result.inventoryMovementInstance.task.id

                if(!inventoryItem) {
                    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(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) {
                    inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity
                }
                else {
                    if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) {
                        inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity
                    }
                    else {
                        status.setRollbackOnly()
                        result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock")
                        result.error = true
                        return result
                    }
                }

                if(inventoryItem.save() && result.inventoryMovementInstance.save()) {
                    // All went well if we get to this point.
                    return result
                }
                else {
                    status.setRollbackOnly()
                    result.error = true
                    return result
                }

            }
            else {
                result.error = true
                return result
            }

        } // end withTransaction
    } // end move()

} // end class
