Ignore:
Timestamp:
Apr 16, 2010, 2:10:52 PM (15 years ago)
Author:
gav
Message:

Add recentlyUsed inventoryItem quick search.

File:
1 edited

Legend:

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

    r485 r489  
     1import grails.orm.PagedResultList
     2
    13/**
    24* Service class that encapsulates the business logic for InventoryItem searches.
     
    68    boolean transactional = false
    79
     10    def dateUtilService
    811    def messageSource
    912
     
    3740                else
    3841                    result.message = getMessage(code:"inventoryItem.search.text.below.reorder.none.found")
     42                break
     43            case "recentlyUsed":
     44                result.daysBack = params.daysBack?.isInteger() ? params.daysBack.toInteger() : 14
     45                result.inventoryItemList = getRecentlyUsed(params, result.daysBack)
     46                if(result.inventoryItemList.totalCount > 0)
     47                    result.message = getMessage(code:"inventoryItem.search.text.recently.used.description", args:[result.daysBack])
     48                else
     49                    result.message = getMessage(code:"inventoryItem.search.text.recently.used.none.found", args:[result.daysBack])
    3950                break
    4051            default:
     
    93104    } // getInventoryBelowReorder
    94105
     106    /**
     107    * Get a list of recently used inventory items.
     108    * @param params The request params.
     109    * @param daysBack The number of days back to get results for.
     110    */
     111    def getRecentlyUsed(params, daysBack) {
     112        def paginateParams = [:]
     113        paginateParams.max = Math.min(params?.max?.toInteger() ?: 10, paramsMax)
     114        paginateParams.offset = params?.offset?.toInteger() ?: 0
     115
     116        def sort = "inventoryItem." + (params?.sort ?: "name")
     117        def order = params?.order == "desc" ? "desc" : "asc"
     118        def orderBy = " order by " + sort + ' ' + order
     119
     120        def namedParams = [:]
     121        namedParams.startOfDay = dateUtilService.today - daysBack
     122
     123        def baseQuery = "from InventoryItem as inventoryItem \
     124                                        left join inventoryItem.inventoryMovements as inventoryMovement \
     125                                        where (inventoryItem.isActive = true \
     126                                                    and inventoryMovement.date > :startOfDay \
     127                                                    and inventoryMovement.inventoryMovementType = 1 \
     128                                                    )"
     129
     130        def searchQuery = "select distinct inventoryItem " + baseQuery + orderBy
     131        def list = InventoryItem.executeQuery(searchQuery, namedParams, paginateParams)
     132
     133        def countQuery = "select count(distinct inventoryItem) as inventoryItemCount " + baseQuery
     134        def totalCount = InventoryItem.executeQuery(countQuery, namedParams)[0].toInteger()
     135
     136        def inventoryItemInstanceList = new PagedResultList(list, totalCount)
     137        return inventoryItemInstanceList
     138    } // getRecentlyUsed
     139
    95140} // end class
Note: See TracChangeset for help on using the changeset viewer.