[177] | 1 | class InventoryMovementService { |
---|
| 2 | |
---|
| 3 | boolean transactional = false |
---|
| 4 | |
---|
[291] | 5 | def authService |
---|
[217] | 6 | |
---|
[177] | 7 | def reverseMove(params) { |
---|
| 8 | InventoryMovement.withTransaction { status -> |
---|
| 9 | def result = [:] |
---|
[191] | 10 | result.inventoryMovementInstance = InventoryMovement.lock(params.id) |
---|
| 11 | if(result.inventoryMovementInstance) { |
---|
[225] | 12 | |
---|
| 13 | // Used type must have a task that is not complete or in the trash |
---|
| 14 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
| 15 | |
---|
| 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 | } |
---|
| 23 | |
---|
| 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 | } |
---|
| 31 | |
---|
[191] | 32 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
[225] | 33 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
[177] | 34 | |
---|
| 35 | if(!inventoryItem) { |
---|
| 36 | status.setRollbackOnly() |
---|
[191] | 37 | result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") |
---|
[177] | 38 | result.error = true |
---|
| 39 | return result |
---|
| 40 | } |
---|
| 41 | |
---|
[191] | 42 | if(result.inventoryMovementInstance.task.taskStatus.id == 3) { |
---|
| 43 | status.setRollbackOnly() |
---|
| 44 | result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask") |
---|
| 45 | result.error = true |
---|
| 46 | return result |
---|
| 47 | } |
---|
| 48 | |
---|
[177] | 49 | // Reverse the movement of inventory. |
---|
[191] | 50 | if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
| 51 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
[177] | 52 | } |
---|
| 53 | else { |
---|
[191] | 54 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
| 55 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
[177] | 56 | } |
---|
| 57 | else { |
---|
| 58 | status.setRollbackOnly() |
---|
[191] | 59 | result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") |
---|
[177] | 60 | result.error = true |
---|
| 61 | return result |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | if(inventoryItem.save() ) { |
---|
[191] | 66 | // All went well if we get to this point. |
---|
| 67 | result.inventoryMovementInstance.delete() |
---|
[177] | 68 | return result |
---|
| 69 | } |
---|
| 70 | else { |
---|
| 71 | status.setRollbackOnly() |
---|
| 72 | result.error = true |
---|
| 73 | return result |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | } |
---|
| 77 | else { |
---|
| 78 | result.error = true |
---|
| 79 | return result |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | } // end withTransaction |
---|
| 83 | } //end reverseMove() |
---|
| 84 | |
---|
| 85 | def move(params) { |
---|
| 86 | InventoryMovement.withTransaction { status -> |
---|
| 87 | def result = [:] |
---|
[191] | 88 | result.inventoryMovementInstance = new InventoryMovement(params) |
---|
[177] | 89 | |
---|
[291] | 90 | result.inventoryMovementInstance.person = authService.currentUser |
---|
[217] | 91 | |
---|
[225] | 92 | // Used type must have a task that is not complete or in the trash |
---|
| 93 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
[177] | 94 | |
---|
[225] | 95 | if(!result.inventoryMovementInstance.task) { |
---|
[191] | 96 | status.setRollbackOnly() |
---|
[225] | 97 | result.inventoryMovementInstance.errors.rejectValue('task', "task.notFound") |
---|
[191] | 98 | result.error = true |
---|
| 99 | return result |
---|
[177] | 100 | } |
---|
| 101 | |
---|
[225] | 102 | if(result.inventoryMovementInstance.task.trash) { |
---|
| 103 | status.setRollbackOnly() |
---|
| 104 | result.inventoryMovementInstance.errors.rejectValue("task", "task.operationNotPermittedOnTaskInTrash") |
---|
| 105 | result.error = true |
---|
| 106 | return result |
---|
| 107 | } |
---|
| 108 | |
---|
[191] | 109 | if(result.inventoryMovementInstance.task.taskStatus.id == 3) { |
---|
| 110 | status.setRollbackOnly() |
---|
| 111 | result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask") |
---|
| 112 | result.error = true |
---|
| 113 | return result |
---|
| 114 | } |
---|
[225] | 115 | } |
---|
[191] | 116 | |
---|
[225] | 117 | if(result.inventoryMovementInstance.validate()) { |
---|
| 118 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
[226] | 119 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
[225] | 120 | |
---|
| 121 | if(!inventoryItem) { |
---|
| 122 | status.setRollbackOnly() |
---|
| 123 | result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") |
---|
| 124 | result.error = true |
---|
| 125 | return result |
---|
| 126 | } |
---|
| 127 | |
---|
[177] | 128 | // Perform the movement of inventory. |
---|
[191] | 129 | if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
| 130 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
[177] | 131 | } |
---|
| 132 | else { |
---|
[191] | 133 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
| 134 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
[177] | 135 | } |
---|
| 136 | else { |
---|
| 137 | status.setRollbackOnly() |
---|
[191] | 138 | result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") |
---|
[177] | 139 | result.error = true |
---|
| 140 | return result |
---|
| 141 | } |
---|
| 142 | } |
---|
| 143 | |
---|
[191] | 144 | if(inventoryItem.save() && result.inventoryMovementInstance.save()) { |
---|
| 145 | // All went well if we get to this point. |
---|
[177] | 146 | return result |
---|
| 147 | } |
---|
| 148 | else { |
---|
| 149 | status.setRollbackOnly() |
---|
| 150 | result.error = true |
---|
| 151 | return result |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | } |
---|
| 155 | else { |
---|
| 156 | result.error = true |
---|
| 157 | return result |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | } // end withTransaction |
---|
| 161 | } // end move() |
---|
| 162 | |
---|
| 163 | } // end class |
---|