Ignore:
Timestamp:
May 26, 2010, 3:50:50 AM (14 years ago)
Author:
gav
Message:

Improvements to Inventory Stock Take reports, MSSQL does not like using distinct the way it was, also improved protection against too many results.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/services/InventoryReportService.groovy

    r546 r547  
    1313    def g = new org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib()
    1414
    15 //     def paramsMax = 100000
     15    def paramsMax = 250
    1616
    1717    /**
     
    6161        result.locations = result.locations.collect {it.trim()}
    6262
     63        def paginateParams = [:]
     64        paginateParams.max = Math.min(params?.max?.toInteger() ?: paramsMax, paramsMax)
     65
    6366        def namedParams = [:]
    6467        namedParams.locationList = [null] // null protects against HQL unexpected end of subtree exception with an empty list.
     
    7174        }
    7275
     76        // Return the actual locations as a string.
     77        if(namedParams.locationList.size() > 1)
     78            result.locations = namedParams.locationList[1..-1].toString()[1..-2]
     79        else
     80            result.locations = g.message(code: 'default.none.text')
     81
     82        // Inventory List.
     83        result.inventoryListQuery = "from InventoryItem as inventoryItem \
     84                                                        left join inventoryItem.inventoryLocation as inventoryLocation \
     85                                                        where (inventoryItem.isActive = true \
     86                                                                    and  inventoryItem.inventoryLocation in (:locationList) \
     87                                                                    ) "
     88
     89        result.inventoryCountQuery = "select count(distinct inventoryItem) " + result.inventoryListQuery
     90        result.inventoryItemCount = InventoryItem.executeQuery(result.inventoryCountQuery, namedParams)[0]
     91
     92        // Exit if too many results.
     93        result.countWarning = null
     94        if(result.inventoryItemCount > paramsMax) {
     95            result.countWarning = g.message(code: 'report.too.many.results.warning',
     96                                                                    args: [paramsMax],
     97                                                                    default: "Warning over ${paramsMax} results, please run report again!")
     98            result.inventoryItemList = []
     99            return result
     100        }
     101
     102        result.inventoryListQuery = "select distinct inventoryItem " + result.inventoryListQuery
     103        def inventoryList = InventoryItem.executeQuery(result.inventoryListQuery, namedParams, paginateParams)
     104
     105        // Reset namedParams for next query.
     106        namedParams = [:]
     107        namedParams.inventoryList = inventoryList
     108
    73109        // Note: HQL docs advise not using fetch aliases in where clause (or any other clause).
    74110        // Access is via the parent object, however that does not work for the order by clause in this case.
     
    79115                                        left join fetch inventoryItem.picture as picture \
    80116                                        left join fetch picture.images as Image \
    81                                         where (inventoryItem.isActive = true \
    82                                                     and  inventoryItem.inventoryLocation in (:locationList) \
     117                                        where (inventoryItem in (:inventoryList) \
    83118                                                    ) \
    84119                                        order by inventoryStore.name, inventoryLocation.name"
    85120
    86         result.query = "select distinct inventoryItem " + result.query
    87         result.queryResult = InventoryLocation.executeQuery(result.query, namedParams)
    88         result.inventoryItemCount = result.queryResult.size()
    89 
    90         // Return the actual locations as a string.
    91         if(namedParams.locationList.size() > 1)
    92             result.locations = namedParams.locationList[1..-1].toString()[1..-2]
    93         else
    94             result.locations = g.message(code: 'default.none.text')
    95 
    96         result.inventoryItemList = result.queryResult
     121        result.query = "select inventoryItem " + result.query
     122        result.inventoryItemList = InventoryItem.executeQuery(result.query, namedParams, paginateParams)
    97123
    98124        // Success.
Note: See TracChangeset for help on using the changeset viewer.