Index: trunk/grails-app/services/InventoryPurchaseService.groovy
===================================================================
--- trunk/grails-app/services/InventoryPurchaseService.groovy	(revision 596)
+++ trunk/grails-app/services/InventoryPurchaseService.groovy	(revision 597)
@@ -11,5 +11,6 @@
     * @param order An inventory puchase that was the source of the order.
     * @returns A result map containing the totalOrdered, totalReceived, totalRemaining, thisOrderRemaining,
-    *                 totalOrderedAmount, totalReceivedAmount, totalRemainingAmount, thisOrderRemainingAmount.
+    *                 totalOrderedAmount, totalReceivedAmount, totalRemainingAmount, thisOrderRemainingAmount,
+    *                 totalPaymentApproved.
     */
     def calcQuantities(order) {
@@ -19,29 +20,22 @@
         result.totalOrdered = 0
         result.totalOrderedAmount = 0
+        result.totalReceived = 0
+        result.totalReceivedAmount = 0
+        result.totalPaymentApproved = 0
         InventoryItemPurchase.withCriteria {
             eq("inventoryItem", order.inventoryItem)
             eq("purchaseOrderNumber", order.purchaseOrderNumber)
-            inventoryItemPurchaseType {
-                    eq("id", 1L)
-            }
         }.each() {
-            result.totalOrdered += it.quantity
-            result.totalOrderedAmount += it.orderValueAmount
-        }
-
-        result.totalReceived = 0
-        result.totalReceivedAmount = 0
-        InventoryItemPurchase.withCriteria {
-            eq("inventoryItem", order.inventoryItem)
-            eq("purchaseOrderNumber", order.purchaseOrderNumber)
-            inventoryItemPurchaseType {
-                or {
-                    eq("id", 2L)
-                    eq("id", 3L)
-                }
-            }
-        }.each() {
-            result.totalReceived += it.quantity
-            result.totalReceivedAmount += it.orderValueAmount
+            if(it.inventoryItemPurchaseType.id == 1L) { // Orders.
+                result.totalOrdered += it.quantity
+                result.totalOrderedAmount += it.orderValueAmount
+            }
+            if(it.inventoryItemPurchaseType.id == 2L || it.inventoryItemPurchaseType.id == 3L) { // Received B/order and Complete.
+                result.totalReceived += it.quantity
+                result.totalReceivedAmount += it.orderValueAmount
+            }
+            if(it.inventoryItemPurchaseType.id == 4L) { // Approved.
+                result.totalPaymentApproved += it.orderValueAmount
+            }
         }
 
@@ -71,4 +65,26 @@
 
         return result
+    }
+
+    /**
+    * Get the original order for an inventoryItemPurchase and InventoryItem.
+    * @param inventoryItemPurchase An inventory puchase.
+    * @returns The order.
+    */
+    def getOriginalOrder(inventoryItemPurchase) {
+
+        def namedParams = [:]
+
+        namedParams.inventoryItem = inventoryItemPurchase.inventoryItem
+        namedParams.purchaseOrderNumber = inventoryItemPurchase.purchaseOrderNumber
+        namedParams.orderPlaced = InventoryItemPurchaseType.read(1)
+
+        def order = InventoryItemPurchase.find("from InventoryItemPurchase as p \
+                                                                                where( p.inventoryItem = :inventoryItem \
+                                                                                            and p.purchaseOrderNumber = :purchaseOrderNumber \
+                                                                                            and p.inventoryItemPurchaseType = :orderPlaced )",
+                                                                            namedParams)
+
+        return order
     }
 
@@ -93,6 +109,6 @@
 
             // Handle Invoice Payment Approved.
-            // Find and mark all orders as invoicePaymentApproved = false.
             if(purchaseTypeId == 4) {
+                // Find and mark all orders as invoicePaymentApproved = false.
                 InventoryItemPurchase.withCriteria {
                     eq("inventoryItem", result.inventoryItemPurchaseInstance.inventoryItem)
@@ -104,4 +120,15 @@
                     it.invoicePaymentApproved = false
                 }
+                // Find and mark last orderReceived as invoicePaymentApproved = false.
+                InventoryItemPurchase.withCriteria {
+                    eq("inventoryItem", result.inventoryItemPurchaseInstance.inventoryItem)
+                    eq("purchaseOrderNumber", result.inventoryItemPurchaseInstance.purchaseOrderNumber)
+                    inventoryItemPurchaseType {
+                        or {
+                            eq("id", 2L) // Received B/order To Come
+                            eq("id", 3L) // Received Complete
+                        }
+                    }
+                }.last().invoicePaymentApproved = false
             }
 
@@ -129,5 +156,5 @@
                     between("date", startOfDay, startOfDay+1)
                     inventoryMovementType {
-                        eq("id", 3L) //purchaseReceived.
+                        eq("id", 3L) // purchaseReceived.
                     }
                     order('id', 'desc') // The newest one will be first.
@@ -324,10 +351,13 @@
             }
 
-            def order = InventoryItemPurchase.get(params.orderId)
+            def received = InventoryItemPurchase.get(params.receivedId)
+            if(!received)
+                return fail(code:"default.not.found")
+            result.receivedId = received.id
+
+            def order = getOriginalOrder(received)
             if(!order)
                 return fail(code:"default.not.found")
             result.orderId = order.id
-
-            result.inventoryItemPurchaseInstance = new InventoryItemPurchase(params)
 
             result.inventoryItemPurchaseInstance = new InventoryItemPurchase(params)
@@ -338,6 +368,16 @@
             result.inventoryItemPurchaseInstance.inventoryItemPurchaseType = InventoryItemPurchaseType.read(4) // Approve.
 
-            order.invoicePaymentApproved = true
+            received.invoicePaymentApproved = true
             result.inventoryItemPurchaseInstance.invoicePaymentApproved = true
+
+            // Update orderValueAmount and invoicePaymentApproved if processing a receivedComplete.
+            if(received.inventoryItemPurchaseType.id == 3L) {
+                order.invoicePaymentApproved = true
+                result.inventoryItemPurchaseInstance.receivedComplete = true
+                def calcQuantities = calcQuantities(order)
+                if(result.inventoryItemPurchaseInstance.orderValueAmount)
+                    calcQuantities.totalPaymentApproved += result.inventoryItemPurchaseInstance.orderValueAmount
+                order.orderValueAmount = calcQuantities.totalPaymentApproved
+            }
 
             // Fetch to prevent lazy initialization error.
@@ -347,6 +387,4 @@
                 return fail(field:"invoiceNumber", code:"inventoryItemPurchase.invoiceNumber.required")
 
-            order.invoicePaymentApproved = true
-
             if(order.hasErrors() || !order.save())
                 return fail(code:"default.create.failure")
