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