[116] | 1 | import org.codehaus.groovy.grails.plugins.springsecurity.Secured |
---|
| 2 | |
---|
[298] | 3 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager']) |
---|
[116] | 4 | class InventoryItemDetailedController extends BaseController { |
---|
[156] | 5 | |
---|
| 6 | def filterService |
---|
[225] | 7 | def inventoryItemService |
---|
| 8 | def inventoryMovementService |
---|
[156] | 9 | |
---|
[116] | 10 | // the delete, save and update actions only accept POST requests |
---|
[225] | 11 | static allowedMethods = [delete:'POST', save:'POST', update:'POST', useInventoryItem:'POST'] |
---|
[116] | 12 | |
---|
[298] | 13 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
| 14 | def index = { redirect(action:search, params:params) } |
---|
| 15 | |
---|
| 16 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 17 | def list = { |
---|
| 18 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
| 19 | [ inventoryItemInstanceList: InventoryItem.list( params ), inventoryItemInstanceTotal: InventoryItem.count() ] |
---|
| 20 | } |
---|
| 21 | |
---|
[298] | 22 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[139] | 23 | def search = { |
---|
| 24 | params.max = Math.min( params.max ? params.max.toInteger() : 10, 100) |
---|
[156] | 25 | |
---|
| 26 | // Quick Search: |
---|
| 27 | if(!params.filter) { |
---|
[178] | 28 | return[ inventoryItemInstanceList: InventoryItem.list( params ), inventoryItemInstanceTotal: InventoryItem.count(), filterParams: params ] |
---|
[156] | 29 | } |
---|
| 30 | // filterPane: |
---|
| 31 | return[ inventoryItemInstanceList: filterService.filter( params, InventoryItem ), |
---|
| 32 | inventoryItemInstanceTotal: filterService.count( params, InventoryItem ), |
---|
| 33 | filterParams: com.zeddware.grails.plugins.filterpane.FilterUtils.extractFilterParams(params), |
---|
| 34 | params:params ] |
---|
[139] | 35 | } |
---|
| 36 | |
---|
[225] | 37 | /** |
---|
| 38 | * Simply assigns a passed in task id to a session variable and redirects to search. |
---|
| 39 | */ |
---|
[298] | 40 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 41 | def findInventoryItemForMovement = { |
---|
| 42 | if(!params.task?.id) { |
---|
| 43 | flash.message = "No task id supplied, please select a task then the inventory tab." |
---|
| 44 | redirect(controller: "taskDetailed", action: "search") |
---|
| 45 | return |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | session.inventoryMovementTaskId = params.task.id |
---|
| 49 | flash.message = "Please find and then select the inventory item." |
---|
| 50 | redirect(action: search) |
---|
| 51 | } |
---|
| 52 | |
---|
[298] | 53 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[116] | 54 | def show = { |
---|
[225] | 55 | |
---|
[139] | 56 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 57 | if(params._action_Show) |
---|
| 58 | { params.action='show' } |
---|
[116] | 59 | |
---|
[225] | 60 | if(!InventoryItem.exists(params.id)) { |
---|
[116] | 61 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[139] | 62 | redirect(action:search) |
---|
[225] | 63 | return |
---|
[116] | 64 | } |
---|
[225] | 65 | |
---|
| 66 | def result = inventoryItemService.prepareShowData(params) |
---|
| 67 | |
---|
| 68 | if(result.error) { |
---|
| 69 | flash.message = "Could not to prepare the data to show item with id: ${params.id}." |
---|
| 70 | redirect(action:search) |
---|
| 71 | return |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | def model = [ inventoryItemInstance: result.inventoryItemInstance, |
---|
| 75 | inventoryMovementList: result.inventoryMovementList, |
---|
| 76 | inventoryMovementListTotal: result.inventoryMovementListTotal, |
---|
| 77 | inventoryMovementListMax: result.inventoryMovementListMax, |
---|
| 78 | showTab: result.showTab] |
---|
| 79 | |
---|
| 80 | if(session.inventoryMovementTaskId) { |
---|
| 81 | model.inventoryMovementInstance = new InventoryMovement() |
---|
| 82 | model.inventoryMovementInstance.task = Task.get(session.inventoryMovementTaskId) |
---|
| 83 | model.inventoryMovementInstance.quantity = 1 |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | return model |
---|
[116] | 87 | } |
---|
| 88 | |
---|
| 89 | def delete = { |
---|
| 90 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
| 91 | if(inventoryItemInstance) { |
---|
| 92 | try { |
---|
[175] | 93 | inventoryItemInstance.delete(flush:true) |
---|
[116] | 94 | flash.message = "InventoryItem ${params.id} deleted" |
---|
[139] | 95 | redirect(action:search) |
---|
[116] | 96 | } |
---|
| 97 | catch(org.springframework.dao.DataIntegrityViolationException e) { |
---|
| 98 | flash.message = "InventoryItem ${params.id} could not be deleted" |
---|
| 99 | redirect(action:show,id:params.id) |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | else { |
---|
| 103 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[139] | 104 | redirect(action:search) |
---|
[116] | 105 | } |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | def edit = { |
---|
[139] | 109 | |
---|
| 110 | // In the case of an actionSubmit button, rewrite action name from 'index'. |
---|
| 111 | if(params._action_Edit) |
---|
| 112 | { params.action='edit' } |
---|
| 113 | |
---|
[116] | 114 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
| 115 | |
---|
| 116 | if(!inventoryItemInstance) { |
---|
| 117 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[139] | 118 | redirect(action:search) |
---|
[116] | 119 | } |
---|
| 120 | else { |
---|
| 121 | return [ inventoryItemInstance : inventoryItemInstance ] |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | def update = { |
---|
| 126 | def inventoryItemInstance = InventoryItem.get( params.id ) |
---|
| 127 | if(inventoryItemInstance) { |
---|
| 128 | if(params.version) { |
---|
| 129 | def version = params.version.toLong() |
---|
| 130 | if(inventoryItemInstance.version > version) { |
---|
| 131 | |
---|
| 132 | inventoryItemInstance.errors.rejectValue("version", "inventoryItem.optimistic.locking.failure", "Another user has updated this InventoryItem while you were editing.") |
---|
| 133 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
| 134 | return |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | inventoryItemInstance.properties = params |
---|
[178] | 138 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
[116] | 139 | flash.message = "InventoryItem ${params.id} updated" |
---|
| 140 | redirect(action:show,id:inventoryItemInstance.id) |
---|
| 141 | } |
---|
| 142 | else { |
---|
| 143 | render(view:'edit',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | else { |
---|
| 147 | flash.message = "InventoryItem not found with id ${params.id}" |
---|
[175] | 148 | redirect(action:search) |
---|
[116] | 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | def create = { |
---|
| 153 | def inventoryItemInstance = new InventoryItem() |
---|
| 154 | inventoryItemInstance.properties = params |
---|
| 155 | return ['inventoryItemInstance':inventoryItemInstance] |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | def save = { |
---|
| 159 | def inventoryItemInstance = new InventoryItem(params) |
---|
[178] | 160 | if(!inventoryItemInstance.hasErrors() && inventoryItemInstance.save(flush: true)) { |
---|
[116] | 161 | flash.message = "InventoryItem ${inventoryItemInstance.id} created" |
---|
| 162 | redirect(action:show,id:inventoryItemInstance.id) |
---|
| 163 | } |
---|
| 164 | else { |
---|
| 165 | render(view:'create',model:[inventoryItemInstance:inventoryItemInstance]) |
---|
| 166 | } |
---|
| 167 | } |
---|
[225] | 168 | |
---|
| 169 | /** |
---|
| 170 | * Handles the use inventory item form submit in the show view. |
---|
| 171 | */ |
---|
[298] | 172 | @Secured(['ROLE_AppAdmin', 'ROLE_Manager', 'ROLE_InventoryManager', 'ROLE_InventoryUser']) |
---|
[225] | 173 | def useInventoryItem = { |
---|
| 174 | |
---|
[226] | 175 | params.inventoryMovementType = InventoryMovementType.get(1) // Set type to "Used". |
---|
[225] | 176 | def result = inventoryMovementService.move(params) |
---|
| 177 | |
---|
| 178 | if(!result.error) { |
---|
| 179 | flash.message = "Inventory Movement for ${result.inventoryMovementInstance.inventoryItem.name.encodeAsHTML()} created." |
---|
| 180 | redirect(controller:"taskDetailed", action:"show", id: result.taskId) |
---|
| 181 | } |
---|
| 182 | else { |
---|
| 183 | if(result.inventoryMovementInstance) { |
---|
| 184 | def p = [:] |
---|
| 185 | p.id = result.inventoryMovementInstance.inventoryItem?.id |
---|
| 186 | def r = inventoryItemService.prepareShowData(p) |
---|
| 187 | |
---|
| 188 | def model = [ inventoryItemInstance: r.inventoryItemInstance, |
---|
| 189 | inventoryMovementList: r.inventoryMovementList, |
---|
| 190 | inventoryMovementListTotal: r.inventoryMovementListTotal, |
---|
| 191 | inventoryMovementListMax: r.inventoryMovementListMax, |
---|
| 192 | showTab: r.showTab] |
---|
| 193 | |
---|
| 194 | model.inventoryMovementInstance = result.inventoryMovementInstance |
---|
| 195 | |
---|
| 196 | render(view: 'show', model: model) |
---|
| 197 | } |
---|
| 198 | else { |
---|
| 199 | flash.message = "Could not create inventory movement." |
---|
| 200 | redirect(action:"search") |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
[116] | 206 | } |
---|