0
def shoppingCartService

def produceShoppingCartInfo(){
    def shoppingItems = shoppingCartService.getItems()
    def summary = new ShoppingCartSummary(items: [:], total: BigDecimal.ZERO)
    shoppingItems.each { shoppingItem ->
        def part = Shoppable.findByShoppingItem(shoppingItem)
        def quantity = new BigDecimal(shoppingCartService.getQuantity(part))
        BigDecimal lineTotal = part.price.multiply(quantity)
        summary.items[part.id] = new Expando(quantity:quantity, item:part, lineTotal:lineTotal)
        summary.total = summary.total.add(lineTotal)
    }
    summary
}

def updateCartQuantities(newQuantities) {
    def summary =  produceShoppingCartInfo()
    newQuantities.each {  
        def partId = it.key
        def newQuantity = it.value
        if(summary.items[partId]) {
            def currentQuantity = summary.items[partId].quantity
            def delta = newQuantity - currentQuantity
            if(delta > 0) {
                shoppingCartService.addToShoppingCart(summary.items[partId].item, delta)
//              shoppingCartService.addToShoppingCart(summary.items[partId].item)
            } else if (delta < 0) {
                shoppingCartService.removeFromShoppingCart(summary.items[partId].item, Math.abs(delta))
//              shoppingCartService.removeFromShoppingCart(summary.items[partId].item)
            }
        }
    }
    produceShoppingCartInfo()

}

def getTotalItemsForCart(){
    def summary =  produceShoppingCartInfo()
    def totalItems = 0

    summary.items.each { partId, itemInfo  ->
        def currentItemQuantity = itemInfo.quantity
        totalItems = totalItems+currentItemQuantity
    }

    totalItems

}
4

1 に答える 1