| | 245 | /** |
| | 246 | * Get the data for the inventory overiew value. |
| | 247 | * @param params The request params, may contain params to specify the search. |
| | 248 | * @param locale The locale to use when generating result.message. |
| | 249 | */ |
| | 250 | def getInventoryValueOverview(params, locale) { |
| | 251 | def result = [:] |
| | 252 | |
| | 253 | result.inventoryItemCount = 0 |
| | 254 | result.inventoryItemTotalValue = new BigDecimal(0) |
| | 255 | result.currency = null |
| | 256 | result.errorMessage = null |
| | 257 | result.summaryOfCalculationMethod = "This report does not convert between different currency.\n" |
| | 258 | result.summaryOfCalculationMethod += "Therefore all item's are checked to ensure that currency is the same." |
| | 259 | |
| | 260 | result.site = Site.get(params.site.id.toLong()) |
| | 261 | |
| | 262 | if(params.inventoryTypes) |
| | 263 | result.inventoryTypes = params.inventoryTypes.collect { InventoryType.get(it.toInteger()) } |
| | 264 | else |
| | 265 | result.inventoryTypes = InventoryType.findAllByIsActive(true, [max:254, sort:'name']) |
| | 266 | |
| | 267 | if(params.inventoryGroups) |
| | 268 | result.inventoryGroups = params.inventoryGroups.collect { InventoryGroup.get(it.toInteger()) } |
| | 269 | else |
| | 270 | result.inventoryGroups = InventoryGroup.findAllByIsActive(true, [max:254, sort:'name']) |
| | 271 | |
| | 272 | def fail = { Map m -> |
| | 273 | result.error = [ code: m.code, args: m.args ] |
| | 274 | result.errorMessage = g.message(result.error) |
| | 275 | result.currency = null |
| | 276 | //result.inventoryItemTotalValue = new BigDecimal(0) |
| | 277 | return result |
| | 278 | } |
| | 279 | |
| | 280 | // Base query. |
| | 281 | def q = new HqlBuilder().query { |
| | 282 | select '' |
| | 283 | from 'InventoryItem as inventoryItem', |
| | 284 | 'left join inventoryItem.inventoryLocation as inventoryLocation', |
| | 285 | 'left join inventoryLocation.inventoryStore as inventoryStore' |
| | 286 | where 'inventoryItem.isActive = true' |
| | 287 | namedParams.siteId = result.site.id |
| | 288 | and 'inventoryStore.site.id = :siteId' |
| | 289 | } |
| | 290 | def baseWhereLogic = new ArrayList(q.whereClauseTerms) |
| | 291 | |
| | 292 | // Count the inventoryItems. |
| | 293 | q.select = 'count(distinct inventoryItem)' |
| | 294 | result.inventoryItemCount = InventoryItem.executeQuery(q.query, q.namedParams, q.paginateParams)[0] |
| | 295 | |
| | 296 | // Get the first currency found on this site. |
| | 297 | q.paginateParams.max = 1 |
| | 298 | q.select = 'inventoryItem.estimatedUnitPriceCurrency' |
| | 299 | result.currency = InventoryItem.executeQuery(q.query, q.namedParams, q.paginateParams)[0] |
| | 300 | |
| | 301 | // Count the distinct currency found. |
| | 302 | q.select = 'count(distinct inventoryItem.estimatedUnitPriceCurrency)' |
| | 303 | def currencyCount = InventoryItem.executeQuery(q.query, q.namedParams)[0] |
| | 304 | |
| | 305 | // Get total value. |
| | 306 | q.select = 'sum (inventoryItem.estimatedUnitPriceAmount * inventoryItem.unitsInStock)' |
| | 307 | result.inventoryItemTotalValue = InventoryItem.executeQuery(q.query, q.namedParams)[0] |
| | 308 | |
| | 309 | // Get values for each group. |
| | 310 | q.and 'inventoryItem.inventoryGroup.id = :groupId' |
| | 311 | def tempGroups = [] |
| | 312 | result.inventoryGroups.each() { group -> |
| | 313 | q.namedParams.groupId = group.id |
| | 314 | def groupValue = InventoryItem.executeQuery(q.query, q.namedParams)[0] ?: 0 |
| | 315 | tempGroups << [name: group.name, value: groupValue] |
| | 316 | } |
| | 317 | |
| | 318 | // Cleanup and reset query. |
| | 319 | q.namedParams.remove('groupId') |
| | 320 | q.whereClauseTerms = baseWhereLogic |
| | 321 | result.inventoryGroups = tempGroups |
| | 322 | |
| | 323 | // Get values for each type. |
| | 324 | q.and 'inventoryItem.inventoryType.id = :typeId' |
| | 325 | def tempTypes = [] |
| | 326 | result.inventoryTypes.each() { type -> |
| | 327 | q.namedParams.typeId = type.id |
| | 328 | def typeValue = InventoryItem.executeQuery(q.query, q.namedParams)[0] ?: 0 |
| | 329 | tempTypes << [name: type.name, value: typeValue] |
| | 330 | } |
| | 331 | |
| | 332 | // Cleanup and reset query. |
| | 333 | q.namedParams.remove('typeId') |
| | 334 | q.whereClauseTerms = baseWhereLogic |
| | 335 | result.inventoryTypes = tempTypes |
| | 336 | |
| | 337 | if(currencyCount != 1) |
| | 338 | fail(code:'report.error.multiple.currency.found') // No return, populate errors but continue report. |
| | 339 | |
| | 340 | // Success. |
| | 341 | return result |
| | 342 | |
| | 343 | } // getInventoryValueOverview() |
| | 344 | |