[177] | 1 | class InventoryMovementService { |
---|
| 2 | |
---|
| 3 | boolean transactional = false |
---|
| 4 | |
---|
[291] | 5 | def authService |
---|
[631] | 6 | def authenticateService |
---|
[217] | 7 | |
---|
[177] | 8 | def reverseMove(params) { |
---|
| 9 | InventoryMovement.withTransaction { status -> |
---|
| 10 | def result = [:] |
---|
[407] | 11 | |
---|
| 12 | def fail = { Map m -> |
---|
| 13 | status.setRollbackOnly() |
---|
| 14 | if(result.inventoryMovementInstance && m.field) |
---|
| 15 | result.inventoryMovementInstance.errors.rejectValue(m.field, m.code) |
---|
| 16 | result.error = [ code: m.code, args: ["InventoryMovement", params.id] ] |
---|
| 17 | return result |
---|
| 18 | } |
---|
| 19 | |
---|
[191] | 20 | result.inventoryMovementInstance = InventoryMovement.lock(params.id) |
---|
[225] | 21 | |
---|
[407] | 22 | if(!result.inventoryMovementInstance) |
---|
| 23 | fail(code:"default.not.found") |
---|
[225] | 24 | |
---|
[407] | 25 | // Fetch to prevent lazy initialization error. |
---|
| 26 | result.inventoryMovementInstance.inventoryItem.unitOfMeasure |
---|
[225] | 27 | |
---|
[407] | 28 | // Used type must have a task that is not complete or in the trash |
---|
| 29 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
[225] | 30 | |
---|
[407] | 31 | // Unlike move, if the task does not exist at all then deletion is still allowed. |
---|
| 32 | if(result.inventoryMovementInstance.task?.trash) |
---|
| 33 | return fail(field:"task", code:"task.operationNotPermittedOnTaskInTrash") |
---|
[177] | 34 | |
---|
[407] | 35 | if(result.inventoryMovementInstance.task?.taskStatus?.id == 3) |
---|
| 36 | return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") |
---|
[631] | 37 | |
---|
| 38 | // Check for authorisation on recurring tasks. |
---|
| 39 | if(result.inventoryMovementInstance.task?.taskRecurringSchedule) { |
---|
| 40 | if(!authenticateService.ifAnyGranted('ROLE_AppAdmin,ROLE_Manager,ROLE_TaskManager,ROLE_InventoryManager')) |
---|
| 41 | return fail(field:"task", code:"task.operationNotPermittedOnRecurringTaskWithoutAuth") |
---|
| 42 | } |
---|
| 43 | |
---|
[407] | 44 | } |
---|
[177] | 45 | |
---|
[407] | 46 | result.inventoryItemInstance = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
| 47 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
[177] | 48 | |
---|
[407] | 49 | if(!result.inventoryItemInstance) |
---|
| 50 | return fail(field:"inventoryItem", code:"inventoryMovement.inventoryItem.notFound") |
---|
| 51 | |
---|
| 52 | // Reverse the movement of inventory. |
---|
| 53 | if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
| 54 | result.inventoryItemInstance.unitsInStock += result.inventoryMovementInstance.quantity |
---|
| 55 | } |
---|
| 56 | else { |
---|
| 57 | if(result.inventoryItemInstance.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
| 58 | result.inventoryItemInstance.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
[177] | 59 | } |
---|
| 60 | else { |
---|
[407] | 61 | return fail(field:"quantity", code:"inventoryMovement.quantity.insufficientItemsInStock") |
---|
[177] | 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
[407] | 65 | if(!result.inventoryItemInstance.save()) |
---|
| 66 | return fail(code: "default.delete.failure") |
---|
| 67 | |
---|
| 68 | // Success.. |
---|
| 69 | result.inventoryMovementInstance.delete() |
---|
| 70 | return result |
---|
| 71 | |
---|
[177] | 72 | } // end withTransaction |
---|
| 73 | } //end reverseMove() |
---|
| 74 | |
---|
| 75 | def move(params) { |
---|
| 76 | InventoryMovement.withTransaction { status -> |
---|
| 77 | def result = [:] |
---|
[407] | 78 | |
---|
| 79 | def fail = { Map m -> |
---|
| 80 | status.setRollbackOnly() |
---|
| 81 | if(result.inventoryMovementInstance && m.field) |
---|
| 82 | result.inventoryMovementInstance.errors.rejectValue(m.field, m.code) |
---|
| 83 | result.error = [ code: m.code, args: ["InventoryMovement", params.id] ] |
---|
| 84 | return result |
---|
| 85 | } |
---|
| 86 | |
---|
[191] | 87 | result.inventoryMovementInstance = new InventoryMovement(params) |
---|
[177] | 88 | |
---|
[291] | 89 | result.inventoryMovementInstance.person = authService.currentUser |
---|
[217] | 90 | |
---|
[407] | 91 | // Fetch to prevent lazy initialization error. |
---|
| 92 | result.inventoryMovementInstance.inventoryItem.unitOfMeasure |
---|
| 93 | |
---|
[225] | 94 | // Used type must have a task that is not complete or in the trash |
---|
| 95 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
[177] | 96 | |
---|
[407] | 97 | if(!result.inventoryMovementInstance.task) |
---|
| 98 | return fail(field:"task", code:"task.notFound") |
---|
[177] | 99 | |
---|
[407] | 100 | if(result.inventoryMovementInstance.task.trash) |
---|
| 101 | return fail(field:"task", code:"task.operationNotPermittedOnTaskInTrash") |
---|
[225] | 102 | |
---|
[407] | 103 | if(result.inventoryMovementInstance.task.taskStatus.id == 3) |
---|
| 104 | return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") |
---|
[631] | 105 | |
---|
| 106 | // Check for authorisation on recurring tasks. |
---|
| 107 | if(result.inventoryMovementInstance.task.taskRecurringSchedule) { |
---|
| 108 | if(!authenticateService.ifAnyGranted('ROLE_AppAdmin,ROLE_Manager,ROLE_TaskManager,ROLE_InventoryManager')) |
---|
| 109 | return fail(field:"task", code:"task.operationNotPermittedOnRecurringTaskWithoutAuth") |
---|
| 110 | } |
---|
[225] | 111 | } |
---|
[191] | 112 | |
---|
[407] | 113 | // Bail early if validation fails. |
---|
| 114 | if(!result.inventoryMovementInstance.validate()) |
---|
| 115 | return fail(code:"default.create.failure") |
---|
[225] | 116 | |
---|
[407] | 117 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
| 118 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
[225] | 119 | |
---|
[407] | 120 | if(!inventoryItem) |
---|
| 121 | return fail(field:"inventoryItem", code:"inventoryMovement.inventoryItem.notFound") |
---|
[177] | 122 | |
---|
[407] | 123 | // Perform the movement of inventory. |
---|
| 124 | if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
| 125 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
| 126 | } |
---|
| 127 | else { |
---|
| 128 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
| 129 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
[177] | 130 | } |
---|
| 131 | else { |
---|
[407] | 132 | return fail(field:"quantity", code:"inventoryMovement.quantity.insufficientItemsInStock") |
---|
[177] | 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
[407] | 136 | if(!inventoryItem.save() || !result.inventoryMovementInstance.save()) |
---|
| 137 | return fail(code:"default.create.failure") |
---|
| 138 | |
---|
| 139 | // Success.. |
---|
| 140 | return result |
---|
| 141 | |
---|
[177] | 142 | } // end withTransaction |
---|
| 143 | } // end move() |
---|
| 144 | |
---|
| 145 | } // end class |
---|