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