1 | class InventoryMovementService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def authService |
---|
6 | def authenticateService |
---|
7 | |
---|
8 | def reverseMove(params) { |
---|
9 | InventoryMovement.withTransaction { status -> |
---|
10 | def result = [:] |
---|
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 | |
---|
20 | result.inventoryMovementInstance = InventoryMovement.lock(params.id) |
---|
21 | |
---|
22 | if(!result.inventoryMovementInstance) |
---|
23 | fail(code:"default.not.found") |
---|
24 | |
---|
25 | // Fetch to prevent lazy initialization error. |
---|
26 | result.inventoryMovementInstance.inventoryItem.unitOfMeasure |
---|
27 | |
---|
28 | // Used type must have a task that is not complete or in the trash |
---|
29 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
30 | |
---|
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") |
---|
34 | |
---|
35 | if(result.inventoryMovementInstance.task?.taskStatus?.id == 3) |
---|
36 | return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") |
---|
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 | |
---|
44 | } |
---|
45 | |
---|
46 | result.inventoryItemInstance = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
47 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
48 | |
---|
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 |
---|
59 | } |
---|
60 | else { |
---|
61 | return fail(field:"quantity", code:"inventoryMovement.quantity.insufficientItemsInStock") |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | if(!result.inventoryItemInstance.save()) |
---|
66 | return fail(code: "default.delete.failure") |
---|
67 | |
---|
68 | // Success.. |
---|
69 | result.inventoryMovementInstance.delete() |
---|
70 | return result |
---|
71 | |
---|
72 | } // end withTransaction |
---|
73 | } //end reverseMove() |
---|
74 | |
---|
75 | def move(params) { |
---|
76 | InventoryMovement.withTransaction { status -> |
---|
77 | def result = [:] |
---|
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 | |
---|
87 | result.inventoryMovementInstance = new InventoryMovement(params) |
---|
88 | |
---|
89 | result.inventoryMovementInstance.person = authService.currentUser |
---|
90 | |
---|
91 | // Fetch to prevent lazy initialization error. |
---|
92 | result.inventoryMovementInstance.inventoryItem.unitOfMeasure |
---|
93 | |
---|
94 | // Used type must have a task that is not complete or in the trash |
---|
95 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
96 | |
---|
97 | if(!result.inventoryMovementInstance.task) |
---|
98 | return fail(field:"task", code:"task.notFound") |
---|
99 | |
---|
100 | if(result.inventoryMovementInstance.task.trash) |
---|
101 | return fail(field:"task", code:"task.operationNotPermittedOnTaskInTrash") |
---|
102 | |
---|
103 | if(result.inventoryMovementInstance.task.taskStatus.id == 3) |
---|
104 | return fail(field:"task", code:"task.operationNotPermittedOnCompleteTask") |
---|
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 | } |
---|
111 | } |
---|
112 | |
---|
113 | // Bail early if validation fails. |
---|
114 | if(!result.inventoryMovementInstance.validate()) |
---|
115 | return fail(code:"default.create.failure") |
---|
116 | |
---|
117 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
118 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
119 | |
---|
120 | if(!inventoryItem) |
---|
121 | return fail(field:"inventoryItem", code:"inventoryMovement.inventoryItem.notFound") |
---|
122 | |
---|
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 |
---|
130 | } |
---|
131 | else { |
---|
132 | return fail(field:"quantity", code:"inventoryMovement.quantity.insufficientItemsInStock") |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | if(!inventoryItem.save() || !result.inventoryMovementInstance.save()) |
---|
137 | return fail(code:"default.create.failure") |
---|
138 | |
---|
139 | // Success.. |
---|
140 | return result |
---|
141 | |
---|
142 | } // end withTransaction |
---|
143 | } // end move() |
---|
144 | |
---|
145 | } // end class |
---|