1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
2 | |
---|
3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
4 | class InventoryMovementDetailedController extends BaseController { |
---|
5 | |
---|
6 | def inventoryMovementService |
---|
7 | |
---|
8 | // the delete, save and update actions only accept POST requests |
---|
9 | static allowedMethods = [delete:'POST', save:'POST'] |
---|
10 | |
---|
11 | def index = { redirect(action:list,params:params) } |
---|
12 | |
---|
13 | def list = { |
---|
14 | flash.message = flash.message |
---|
15 | redirect(controller:"taskDetailed", action:"search") |
---|
16 | } |
---|
17 | |
---|
18 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
19 | def show = { |
---|
20 | def inventoryMovementInstance = InventoryMovement.get( params.id ) |
---|
21 | |
---|
22 | if(!inventoryMovementInstance) { |
---|
23 | flash.message = "InventoryMovement not found with id ${params.id}" |
---|
24 | redirect(action:list) |
---|
25 | } |
---|
26 | else { return [ inventoryMovementInstance : inventoryMovementInstance ] } |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * List the inventory movements for an InventoryItem. |
---|
31 | * @param params.inventoryItem.id The id of an existing inventory item. |
---|
32 | */ |
---|
33 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
34 | def listInventoryMovements = { |
---|
35 | def inventoryItemInstance |
---|
36 | if(params.id) |
---|
37 | inventoryItemInstance = InventoryItem.get(params.id) |
---|
38 | else if(params.inventoryItem?.id) |
---|
39 | inventoryItemInstance = InventoryItem.get(params.inventoryItem.id) |
---|
40 | |
---|
41 | if(!inventoryItemInstance) { |
---|
42 | flash.message = "Inventory item not found with id ${params.id}" |
---|
43 | redirect(controller:'inventoryItemDetailed', action: 'search') |
---|
44 | return |
---|
45 | } |
---|
46 | |
---|
47 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
48 | def inventoryMovementList = InventoryMovement.findAllByInventoryItem(inventoryItemInstance, params) |
---|
49 | def inventoryMovementListTotal = InventoryMovement.countByInventoryItem(inventoryItemInstance) |
---|
50 | |
---|
51 | [ inventoryMovementList: inventoryMovementList, |
---|
52 | inventoryMovementListTotal: inventoryMovementListTotal, |
---|
53 | inventoryItemInstance: inventoryItemInstance] |
---|
54 | } |
---|
55 | |
---|
56 | def delete = { |
---|
57 | def result = inventoryMovementService.reverseMove(params) |
---|
58 | |
---|
59 | if(!result.error) { |
---|
60 | flash.message = "InventoryMovement ${params.id} deleted" |
---|
61 | if(result.taskId) |
---|
62 | redirect(controller:"taskDetailed", action:"show", id:result.taskId) |
---|
63 | redirect(controller:"inventoryItemDetailed", action:"show", id:result.inventoryItemInstance?.id) |
---|
64 | } |
---|
65 | else { |
---|
66 | if(result.inventoryMovementInstance) { |
---|
67 | render(view:'show',model:[inventoryMovementInstance:result.inventoryMovementInstance.attach()]) |
---|
68 | } |
---|
69 | else { |
---|
70 | flash.message = "Could not delete inventory movement." |
---|
71 | redirect(controller:"taskDetailed", action:"search") |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | def create = { |
---|
77 | def inventoryMovementInstance = new InventoryMovement() |
---|
78 | inventoryMovementInstance.properties = params |
---|
79 | def inventoryMovementTypeList = InventoryMovementType.withCriteria { gt("id", 1L) } // Don't include "Used". |
---|
80 | |
---|
81 | return ['inventoryMovementInstance': inventoryMovementInstance, |
---|
82 | inventoryMovementTypeList: inventoryMovementTypeList] |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * Handles all inventory movements except those of type "Used". |
---|
87 | * The "Used" type is handled directly by InventoryItemDetailedController and useInventoryItem. |
---|
88 | */ |
---|
89 | def save = { |
---|
90 | def result = inventoryMovementService.move(params) |
---|
91 | |
---|
92 | if(!result.error) { |
---|
93 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name} created." |
---|
94 | if(result.taskId) { |
---|
95 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
96 | return |
---|
97 | } |
---|
98 | redirect(controller:"inventoryItemDetailed", action:"show", id: result.inventoryMovementInstance.inventoryItem.id) |
---|
99 | } |
---|
100 | else { |
---|
101 | if(result.inventoryMovementInstance) { |
---|
102 | def inventoryMovementTypeList = InventoryMovementType.withCriteria { gt("id", 1L) } // Don't include "Used". |
---|
103 | render(view:'create', model:[inventoryMovementInstance: result.inventoryMovementInstance, |
---|
104 | inventoryMovementTypeList: inventoryMovementTypeList]) |
---|
105 | } |
---|
106 | else { |
---|
107 | flash.message = "Could not create inventory movement." |
---|
108 | redirect(controller:"taskDetailed", action:"search", id:result.taskId) |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | } |
---|