Changeset 407 for trunk/grails-app/services
- Timestamp:
- Feb 24, 2010, 10:59:17 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/services/InventoryMovementService.groovy
r406 r407 8 8 InventoryMovement.withTransaction { status -> 9 9 def result = [:] 10 11 def fail = { Map m -> 12 status.setRollbackOnly() 13 if(result.inventoryMovementInstance && m.field) 14 result.inventoryMovementInstance.errors.rejectValue(m.field, m.code) 15 result.error = [ code: m.code, args: ["InventoryMovement", params.id] ] 16 return result 17 } 18 10 19 result.inventoryMovementInstance = InventoryMovement.lock(params.id) 11 if(result.inventoryMovementInstance) {12 20 13 // Used type must have a task that is not complete or in the trash14 if(result.inventoryMovementInstance.inventoryMovementType.id == 1) {21 if(!result.inventoryMovementInstance) 22 fail(code:"default.not.found") 15 23 16 // Unlike move, if the task does not exist at all then deletion is still allowed. 17 if(result.inventoryMovementInstance.task?.trash) { 18 status.setRollbackOnly() 19 result.inventoryMovementInstance.errors.rejectValue("task", "task.operationNotPermittedOnTaskInTrash") 20 result.error = true 21 return result 22 } 24 // Fetch to prevent lazy initialization error. 25 result.inventoryMovementInstance.inventoryItem.unitOfMeasure 23 26 24 if(result.inventoryMovementInstance.task?.taskStatus?.id == 3) { 25 status.setRollbackOnly() 26 result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask") 27 result.error = true 28 return result 29 } 30 } 27 // Used type must have a task that is not complete or in the trash 28 if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { 31 29 32 result.inventoryItemInstance = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) 33 result.taskId = result.inventoryMovementInstance.task?.id 30 // Unlike move, if the task does not exist at all then deletion is still allowed. 31 if(result.inventoryMovementInstance.task?.trash) 32 return fail(field:"task", code:"task.operationNotPermittedOnTaskInTrash") 34 33 35 if(!result.inventoryItemInstance) { 36 status.setRollbackOnly() 37 result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") 38 result.error = true 39 return result 40 } 34 if(result.inventoryMovementInstance.task?.taskStatus?.id == 3) 35 return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") 36 } 41 37 42 // Reverse the movement of inventory. 43 if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { 44 result.inventoryItemInstance.unitsInStock += result.inventoryMovementInstance.quantity 38 result.inventoryItemInstance = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) 39 result.taskId = result.inventoryMovementInstance.task?.id 40 41 if(!result.inventoryItemInstance) 42 return fail(field:"inventoryItem", code:"inventoryMovement.inventoryItem.notFound") 43 44 // Reverse the movement of inventory. 45 if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { 46 result.inventoryItemInstance.unitsInStock += result.inventoryMovementInstance.quantity 47 } 48 else { 49 if(result.inventoryItemInstance.unitsInStock >= result.inventoryMovementInstance.quantity) { 50 result.inventoryItemInstance.unitsInStock -= result.inventoryMovementInstance.quantity 45 51 } 46 52 else { 47 if(result.inventoryItemInstance.unitsInStock >= result.inventoryMovementInstance.quantity) { 48 result.inventoryItemInstance.unitsInStock -= result.inventoryMovementInstance.quantity 49 } 50 else { 51 status.setRollbackOnly() 52 result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") 53 result.error = true 54 return result 55 } 53 return fail(field:"quantity", code:"inventoryMovement.quantity.insufficientItemsInStock") 56 54 } 55 } 57 56 58 if(result.inventoryItemInstance.save() ) { 59 // All went well if we get to this point. 60 result.inventoryMovementInstance.delete() 61 return result 62 } 63 else { 64 status.setRollbackOnly() 65 result.error = true 66 return result 67 } 57 if(!result.inventoryItemInstance.save()) 58 return fail(code: "default.delete.failure") 68 59 69 } 70 else { 71 result.error = true 72 return result 73 } 60 // Success.. 61 result.inventoryMovementInstance.delete() 62 return result 74 63 75 64 } // end withTransaction … … 79 68 InventoryMovement.withTransaction { status -> 80 69 def result = [:] 70 71 def fail = { Map m -> 72 status.setRollbackOnly() 73 if(result.inventoryMovementInstance && m.field) 74 result.inventoryMovementInstance.errors.rejectValue(m.field, m.code) 75 result.error = [ code: m.code, args: ["InventoryMovement", params.id] ] 76 return result 77 } 78 81 79 result.inventoryMovementInstance = new InventoryMovement(params) 82 80 83 81 result.inventoryMovementInstance.person = authService.currentUser 84 82 83 // Fetch to prevent lazy initialization error. 84 result.inventoryMovementInstance.inventoryItem.unitOfMeasure 85 85 86 // Used type must have a task that is not complete or in the trash 86 87 if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { 87 88 88 if(!result.inventoryMovementInstance.task) { 89 status.setRollbackOnly() 90 result.inventoryMovementInstance.errors.rejectValue('task', "task.notFound") 91 result.error = true 92 return result 89 if(!result.inventoryMovementInstance.task) 90 return fail(field:"task", code:"task.notFound") 91 92 if(result.inventoryMovementInstance.task.trash) 93 return fail(field:"task", code:"task.operationNotPermittedOnTaskInTrash") 94 95 if(result.inventoryMovementInstance.task.taskStatus.id == 3) 96 return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") 97 } 98 99 // Bail early if validation fails. 100 if(!result.inventoryMovementInstance.validate()) 101 return fail(code:"default.create.failure") 102 103 def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) 104 result.taskId = result.inventoryMovementInstance.task?.id 105 106 if(!inventoryItem) 107 return fail(field:"inventoryItem", code:"inventoryMovement.inventoryItem.notFound") 108 109 // Perform the movement of inventory. 110 if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { 111 inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity 112 } 113 else { 114 if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { 115 inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity 93 116 } 94 95 if(result.inventoryMovementInstance.task.trash) { 96 status.setRollbackOnly() 97 result.inventoryMovementInstance.errors.rejectValue("task", "task.operationNotPermittedOnTaskInTrash") 98 result.error = true 99 return result 100 } 101 102 if(result.inventoryMovementInstance.task.taskStatus.id == 3) { 103 status.setRollbackOnly() 104 result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask") 105 result.error = true 106 return result 117 else { 118 return fail(field:"quantity", code:"inventoryMovement.quantity.insufficientItemsInStock") 107 119 } 108 120 } 109 121 110 if(result.inventoryMovementInstance.validate()) { 111 def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) 112 result.taskId = result.inventoryMovementInstance.task?.id 122 if(!inventoryItem.save() || !result.inventoryMovementInstance.save()) 123 return fail(code:"default.create.failure") 113 124 114 if(!inventoryItem) { 115 status.setRollbackOnly() 116 result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") 117 result.error = true 118 return result 119 } 120 121 // Perform the movement of inventory. 122 if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { 123 inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity 124 } 125 else { 126 if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { 127 inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity 128 } 129 else { 130 status.setRollbackOnly() 131 result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") 132 result.error = true 133 return result 134 } 135 } 136 137 if(inventoryItem.save() && result.inventoryMovementInstance.save()) { 138 // All went well if we get to this point. 139 return result 140 } 141 else { 142 status.setRollbackOnly() 143 result.error = true 144 return result 145 } 146 147 } 148 else { 149 result.error = true 150 return result 151 } 125 // Success.. 126 return result 152 127 153 128 } // end withTransaction
Note: See TracChangeset
for help on using the changeset viewer.