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