1 | class InventoryMovementService { |
---|
2 | |
---|
3 | boolean transactional = false |
---|
4 | |
---|
5 | def reverseMove(params) { |
---|
6 | InventoryMovement.withTransaction { status -> |
---|
7 | def result = [:] |
---|
8 | result.inventoryMovementInstance = InventoryMovement.lock(params.id) |
---|
9 | if(result.inventoryMovementInstance) { |
---|
10 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
11 | result.taskId = result.inventoryMovementInstance.task.id |
---|
12 | |
---|
13 | if(!inventoryItem) { |
---|
14 | status.setRollbackOnly() |
---|
15 | result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") |
---|
16 | result.error = true |
---|
17 | return result |
---|
18 | } |
---|
19 | |
---|
20 | if(result.inventoryMovementInstance.task.taskStatus.id == 3) { |
---|
21 | status.setRollbackOnly() |
---|
22 | result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask") |
---|
23 | result.error = true |
---|
24 | return result |
---|
25 | } |
---|
26 | |
---|
27 | // Reverse the movement of inventory. |
---|
28 | if(!result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
29 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
30 | } |
---|
31 | else { |
---|
32 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
33 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
34 | } |
---|
35 | else { |
---|
36 | status.setRollbackOnly() |
---|
37 | result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") |
---|
38 | result.error = true |
---|
39 | return result |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | if(inventoryItem.save() ) { |
---|
44 | // All went well if we get to this point. |
---|
45 | result.inventoryMovementInstance.delete() |
---|
46 | return result |
---|
47 | } |
---|
48 | else { |
---|
49 | status.setRollbackOnly() |
---|
50 | result.error = true |
---|
51 | return result |
---|
52 | } |
---|
53 | |
---|
54 | } |
---|
55 | else { |
---|
56 | result.error = true |
---|
57 | return result |
---|
58 | } |
---|
59 | |
---|
60 | } // end withTransaction |
---|
61 | } //end reverseMove() |
---|
62 | |
---|
63 | def move(params) { |
---|
64 | InventoryMovement.withTransaction { status -> |
---|
65 | def result = [:] |
---|
66 | result.inventoryMovementInstance = new InventoryMovement(params) |
---|
67 | |
---|
68 | if(result.inventoryMovementInstance.validate()) { |
---|
69 | def inventoryItem = InventoryItem.lock(result.inventoryMovementInstance.inventoryItem.id) |
---|
70 | result.taskId = result.inventoryMovementInstance.task.id |
---|
71 | |
---|
72 | if(!inventoryItem) { |
---|
73 | status.setRollbackOnly() |
---|
74 | result.inventoryMovementInstance.errors.rejectValue('inventoryItem', "inventoryMovement.inventoryItem.notFound") |
---|
75 | result.error = true |
---|
76 | return result |
---|
77 | } |
---|
78 | |
---|
79 | if(result.inventoryMovementInstance.task.taskStatus.id == 3) { |
---|
80 | status.setRollbackOnly() |
---|
81 | result.inventoryMovementInstance.errors.rejectValue('task', "task.operationNotPermittedOnCompleteTask") |
---|
82 | result.error = true |
---|
83 | return result |
---|
84 | } |
---|
85 | |
---|
86 | // Perform the movement of inventory. |
---|
87 | if(result.inventoryMovementInstance.inventoryMovementType.incrementsInventory) { |
---|
88 | inventoryItem.unitsInStock += result.inventoryMovementInstance.quantity |
---|
89 | } |
---|
90 | else { |
---|
91 | if(inventoryItem.unitsInStock >= result.inventoryMovementInstance.quantity) { |
---|
92 | inventoryItem.unitsInStock -= result.inventoryMovementInstance.quantity |
---|
93 | } |
---|
94 | else { |
---|
95 | status.setRollbackOnly() |
---|
96 | result.inventoryMovementInstance.errors.rejectValue('quantity', "inventoryMovement.quantity.insufficientItemsInStock") |
---|
97 | result.error = true |
---|
98 | return result |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | if(inventoryItem.save() && result.inventoryMovementInstance.save()) { |
---|
103 | // All went well if we get to this point. |
---|
104 | return result |
---|
105 | } |
---|
106 | else { |
---|
107 | status.setRollbackOnly() |
---|
108 | result.error = true |
---|
109 | return result |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | else { |
---|
114 | result.error = true |
---|
115 | return result |
---|
116 | } |
---|
117 | |
---|
118 | } // end withTransaction |
---|
119 | } // end move() |
---|
120 | |
---|
121 | } // end class |
---|