killbill-aplcache

Add getOriginalAmount for invoice

4/3/2013 9:32:23 PM

Details

diff --git a/api/src/main/java/com/ning/billing/invoice/api/Invoice.java b/api/src/main/java/com/ning/billing/invoice/api/Invoice.java
index c9ef16e..402ad8d 100644
--- a/api/src/main/java/com/ning/billing/invoice/api/Invoice.java
+++ b/api/src/main/java/com/ning/billing/invoice/api/Invoice.java
@@ -28,26 +28,76 @@ import com.ning.billing.util.entity.Entity;
 
 public interface Invoice extends Entity {
 
+    /**
+     *
+     * @param item the invoice ietm to add
+     * @return true if successful
+     */
     boolean addInvoiceItem(InvoiceItem item);
 
+    /**
+     *
+     * @param items the list of ietms to add
+     * @return true is successful
+     */
     boolean addInvoiceItems(Collection<InvoiceItem> items);
 
+    /**
+     *
+     * @return the list of items on that invoice
+     */
     List<InvoiceItem> getInvoiceItems();
 
+    /**
+     *
+     * @param clazz the filter class for the items
+     * @param <T>   a InvoiceItem type
+     * @return      the list of invoice ietms on that invoice for that type
+     */
     public <T extends InvoiceItem> List<InvoiceItem> getInvoiceItems(Class<T> clazz);
 
+    /**
+     *
+     * @return the number of items on that invoice
+     */
     int getNumberOfItems();
 
+    /**
+     *
+     * @param payment the successful payment for that invoice
+     * @return        true if we were able to add the payment
+     */
     boolean addPayment(InvoicePayment payment);
 
+    /**
+     *
+     * @param payments  the list of payments to add on that invoice
+     * @return          true if we were able to add the payments
+     */
     boolean addPayments(Collection<InvoicePayment> payments);
 
+    /**
+     *
+     * @return the list of payments associated with that invoice
+     */
     List<InvoicePayment> getPayments();
 
+    /**
+     *
+     * @return the number of payments on that invoice
+     */
     int getNumberOfPayments();
 
+    /**
+     *
+     * @return the accountId
+     */
     UUID getAccountId();
 
+    /**
+     *
+     * @return the invoice number
+     */
     Integer getInvoiceNumber();
 
     /**
@@ -62,21 +112,63 @@ public interface Invoice extends Entity {
      */
     LocalDate getTargetDate();
 
+    /**
+     *
+     * @return the currency associated with that invoice
+     */
     Currency getCurrency();
 
+    /**
+     *
+     * @return the amount that was paid on that invoice
+     */
     BigDecimal getPaidAmount();
 
+    /**
+     *
+     * @return the original charged amount when the invoice was created
+     */
+    BigDecimal getOriginalChargedAmount();
+
+    /**
+     *
+     * @return the current charged amount for that invoice
+     */
     BigDecimal getChargedAmount();
 
+    /**
+     *
+     * @return the amount of account credit on that invoice
+     */
     BigDecimal getCBAAmount();
 
+    /**
+     *
+     * @return the total adjusted amount on that invoice
+     */
     BigDecimal getTotalAdjAmount();
 
+    /**
+     *
+     * @return the total credit amount on that invoice
+     */
     BigDecimal getCreditAdjAmount();
 
+    /**
+     *
+     * @return the total refunded amount on that invoice
+     */
     BigDecimal getRefundAdjAmount();
 
+    /**
+     *
+     * @return the current balance on that invoice
+     */
     BigDecimal getBalance();
 
+    /**
+     *
+     * @return true if this is a migration invoice
+     */
     boolean isMigrationInvoice();
 }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/model/DefaultInvoice.java b/invoice/src/main/java/com/ning/billing/invoice/model/DefaultInvoice.java
index 50ec651..1f4309d 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/model/DefaultInvoice.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/model/DefaultInvoice.java
@@ -189,6 +189,17 @@ public class DefaultInvoice extends EntityBase implements Invoice {
     }
 
     @Override
+    public BigDecimal getOriginalChargedAmount() {
+        BigDecimal result = BigDecimal.ZERO;
+        for (final InvoiceItem cur :  invoiceItems) {
+            if (cur.getCreatedDate().compareTo(getCreatedDate()) == 0) {
+                result = result.add(cur.getAmount());
+            }
+        }
+        return result;
+    }
+
+    @Override
     public BigDecimal getChargedAmount() {
         return invoiceItems.getChargedAmount();
     }
diff --git a/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java b/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java
index 0c020f7..f75197f 100644
--- a/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java
+++ b/invoice/src/main/java/com/ning/billing/invoice/template/formatters/DefaultInvoiceFormatter.java
@@ -183,6 +183,11 @@ public class DefaultInvoiceFormatter implements InvoiceFormatter {
     }
 
     @Override
+    public BigDecimal getOriginalChargedAmount() {
+        return round(Objects.firstNonNull(invoice.getOriginalChargedAmount(), BigDecimal.ZERO));
+    }
+
+    @Override
     public BigDecimal getCBAAmount() {
         return round(Objects.firstNonNull(invoice.getCBAAmount(), BigDecimal.ZERO));
     }
diff --git a/invoice/src/test/java/com/ning/billing/invoice/api/user/TestDefaultInvoiceUserApi.java b/invoice/src/test/java/com/ning/billing/invoice/api/user/TestDefaultInvoiceUserApi.java
index 33d66ba..d87d160 100644
--- a/invoice/src/test/java/com/ning/billing/invoice/api/user/TestDefaultInvoiceUserApi.java
+++ b/invoice/src/test/java/com/ning/billing/invoice/api/user/TestDefaultInvoiceUserApi.java
@@ -30,14 +30,22 @@ import com.ning.billing.ErrorCode;
 import com.ning.billing.ObjectType;
 import com.ning.billing.account.api.Account;
 import com.ning.billing.invoice.InvoiceTestSuiteWithEmbeddedDB;
+import com.ning.billing.invoice.api.Invoice;
 import com.ning.billing.invoice.api.InvoiceApiException;
 import com.ning.billing.invoice.api.InvoiceItem;
 import com.ning.billing.invoice.api.InvoiceItemType;
 import com.ning.billing.invoice.model.InvoicingConfiguration;
 import com.ning.billing.util.api.TagApiException;
+import com.ning.billing.util.callcontext.CallContext;
+import com.ning.billing.util.callcontext.CallContextBase;
+import com.ning.billing.util.callcontext.DefaultCallContext;
+import com.ning.billing.util.callcontext.TenantContext;
+import com.ning.billing.util.clock.ClockMock;
 import com.ning.billing.util.tag.ControlTagType;
 import com.ning.billing.util.tag.Tag;
 
+import sun.management.ThreadInfoCompositeData;
+
 import static org.testng.Assert.assertEquals;
 
 public class TestDefaultInvoiceUserApi extends InvoiceTestSuiteWithEmbeddedDB {
@@ -109,7 +117,6 @@ public class TestDefaultInvoiceUserApi extends InvoiceTestSuiteWithEmbeddedDB {
         // Verify the initial account balance
         final BigDecimal accountBalance = invoiceUserApi.getAccountBalance(accountId, callContext);
         Assert.assertEquals(accountBalance, invoiceBalance);
-
         // Post an external charge
         final BigDecimal externalChargeAmount = BigDecimal.TEN;
         final InvoiceItem externalChargeInvoiceItem = invoiceUserApi.insertExternalChargeForInvoice(accountId, invoiceId,
@@ -119,6 +126,33 @@ public class TestDefaultInvoiceUserApi extends InvoiceTestSuiteWithEmbeddedDB {
     }
 
     @Test(groups = "slow")
+    public void testOriginalAmountCharged() throws Exception {
+
+        final Invoice initialInvoice = invoiceUserApi.getInvoice(invoiceId, callContext);
+        final BigDecimal originalAmountCharged = initialInvoice.getOriginalChargedAmount();
+        final BigDecimal amountCharged = initialInvoice.getChargedAmount();
+        Assert.assertEquals(originalAmountCharged.compareTo(amountCharged), 0);
+
+        ((ClockMock) clock).addDays(1);
+
+        // Sleep at least one sec to make sure created_date for the external charge is different than the created date for the invoice itself
+        CallContext newCallContextLater = new DefaultCallContext(callContext.getTenantId(), callContext.getUserName(), callContext.getCallOrigin(), callContext.getUserType(), callContext.getUserToken(), clock);
+        // Post an external charge
+        final BigDecimal externalChargeAmount = BigDecimal.TEN;
+        final InvoiceItem externalChargeInvoiceItem = invoiceUserApi.insertExternalChargeForInvoice(accountId, invoiceId,
+                                                                                                    externalChargeAmount, UUID.randomUUID().toString(),
+                                                                                                    clock.getUTCToday(), accountCurrency, newCallContextLater);
+
+        final Invoice newInvoice = invoiceUserApi.getInvoice(invoiceId, callContext);
+        final BigDecimal newOriginalAmountCharged = newInvoice.getOriginalChargedAmount();
+        final BigDecimal newAmountCharged = newInvoice.getChargedAmount();
+        final BigDecimal expectedChargedAmount = newInvoice.getOriginalChargedAmount().add(externalChargeInvoiceItem.getAmount());
+
+        Assert.assertEquals(originalAmountCharged.compareTo(newOriginalAmountCharged), 0);
+        Assert.assertEquals(newAmountCharged.compareTo(expectedChargedAmount), 0);
+    }
+
+    @Test(groups = "slow")
     public void testPostExternalChargeForBundleOnExistingInvoice() throws Exception {
         // Verify the initial invoice balance
         final BigDecimal invoiceBalance = invoiceUserApi.getInvoice(invoiceId, callContext).getBalance();
diff --git a/payment/src/test/java/com/ning/billing/payment/MockInvoice.java b/payment/src/test/java/com/ning/billing/payment/MockInvoice.java
index 2173e16..38dbecb 100644
--- a/payment/src/test/java/com/ning/billing/payment/MockInvoice.java
+++ b/payment/src/test/java/com/ning/billing/payment/MockInvoice.java
@@ -162,6 +162,7 @@ public class MockInvoice extends EntityBase implements Invoice {
         return amountPaid;
     }
 
+
     @Override
     public BigDecimal getChargedAmount() {
         BigDecimal result = BigDecimal.ZERO;
@@ -174,6 +175,7 @@ public class MockInvoice extends EntityBase implements Invoice {
         return result;
     }
 
+
     @Override
     public BigDecimal getCreditAdjAmount() {
         BigDecimal result = BigDecimal.ZERO;
@@ -198,17 +200,22 @@ public class MockInvoice extends EntityBase implements Invoice {
 
     @Override
     public BigDecimal getCBAAmount() {
-        return null;
+        throw new UnsupportedOperationException();
     }
 
     @Override
     public BigDecimal getTotalAdjAmount() {
-        return null;
+        throw new UnsupportedOperationException();
     }
 
     @Override
     public BigDecimal getRefundAdjAmount() {
-        return null;
+        throw new UnsupportedOperationException();
+    }
+
+    @Override
+    public BigDecimal getOriginalChargedAmount() {
+        throw new UnsupportedOperationException();
     }
 }