killbill-memoizeit

Prevent Fixed item to be repaired in Invoice Throw Illegal

4/29/2013 9:45:12 PM

Details

diff --git a/invoice/src/main/java/com/ning/billing/invoice/generator/DefaultInvoiceGenerator.java b/invoice/src/main/java/com/ning/billing/invoice/generator/DefaultInvoiceGenerator.java
index b690e7d..f529e68 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/generator/DefaultInvoiceGenerator.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/generator/DefaultInvoiceGenerator.java
@@ -149,6 +149,9 @@ public class DefaultInvoiceGenerator implements InvoiceGenerator {
         // Remove from both lists the items in common
         removeMatchingInvoiceItems(existingItems, proposedItems);
 
+        // We don't want the Fixed items to be repaired -- as they are setup fees that should be paid
+        removeRemainingFixedItemsFromExisting(existingItems);
+
         // Add repair items based on what is left in existing items
         addRepairItems(existingItems, proposedItems);
 
@@ -158,6 +161,16 @@ public class DefaultInvoiceGenerator implements InvoiceGenerator {
         return proposedItems.size() != 0 ? invoice : null;
     }
 
+    private void removeRemainingFixedItemsFromExisting(final List<InvoiceItem> existingItems) {
+        final Iterator<InvoiceItem> it = existingItems.iterator();
+        while (it.hasNext()) {
+            final InvoiceItem cur = it.next();
+            if (cur.getInvoiceItemType() == InvoiceItemType.FIXED) {
+                it.remove();
+            }
+        }
+    }
+
     /**
      * At this point either we have 0 existingItem left or those left need to be repaired
      *
@@ -424,8 +437,7 @@ public class DefaultInvoiceGenerator implements InvoiceGenerator {
                 return cur;
             }
         }
-        log.warn("Cannot find repaired invoice item " + repairedInvoiceItemId);
-        return null;
+        throw new IllegalStateException("Cannot find repaired invoice item " + repairedInvoiceItemId);
     }