1 | class InventoryMovementService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def personService |
---|
6 | |
---|
7 | def reverseMove(params) { |
---|
8 | InventoryMovement.withTransaction { status -> |
---|
9 | def result = [:] |
---|
10 | result.inventoryMovementInstance = InventoryMovement.lock(params.id) |
---|
11 | if(result.inventoryMovementInstance) { |
---|
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 | |
---|
32 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
33 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
34 | |
---|
35 | if(!inventoryItem) { |
---|
36 | status.setRollbackOnly() |
---|
37 | result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") |
---|
38 | result.error = true |
---|
39 | return result |
---|
40 | } |
---|
41 | |
---|
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 | |
---|
49 | // Reverse the movement of inventory. |
---|
50 | if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
51 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
52 | } |
---|
53 | else { |
---|
54 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
55 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
56 | } |
---|
57 | else { |
---|
58 | status.setRollbackOnly() |
---|
59 | result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") |
---|
60 | result.error = true |
---|
61 | return result |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | if(inventoryItem.save() ) { |
---|
66 | // All went well if we get to this point. |
---|
67 | result.inventoryMovementInstance.delete() |
---|
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 = [:] |
---|
88 | result.inventoryMovementInstance = new InventoryMovement(params) |
---|
89 | |
---|
90 | result.inventoryMovementInstance.person = personService.currentUser |
---|
91 | |
---|
92 | // Used type must have a task that is not complete or in the trash |
---|
93 | if(result.inventoryMovementInstance.inventoryMovementType.id == 1) { |
---|
94 | |
---|
95 | if(!result.inventoryMovementInstance.task) { |
---|
96 | status.setRollbackOnly() |
---|
97 | result.inventoryMovementInstance.errors.rejectValue('task', "task.notFound") |
---|
98 | result.error = true |
---|
99 | return result |
---|
100 | } |
---|
101 | |
---|
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 | |
---|
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 | } |
---|
115 | } |
---|
116 | |
---|
117 | if(result.inventoryMovementInstance.validate()) { |
---|
118 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
119 | result.taskId = result.inventoryMovementInstance.task?.id |
---|
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 | |
---|
128 | // Perform the movement of inventory. |
---|
129 | if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
130 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
131 | } |
---|
132 | else { |
---|
133 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
134 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
135 | } |
---|
136 | else { |
---|
137 | status.setRollbackOnly() |
---|
138 | result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") |
---|
139 | result.error = true |
---|
140 | return result |
---|
141 | } |
---|
142 | } |
---|
143 | |
---|
144 | if(inventoryItem.save() && result.inventoryMovementInstance.save()) { |
---|
145 | // All went well if we get to this point. |
---|
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 |
---|