001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.ar.util;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    
021    import org.kuali.kfs.module.ar.businessobject.CustomerInvoiceDetail;
022    import org.kuali.kfs.module.ar.businessobject.InvoicePaidApplied;
023    import org.kuali.kfs.module.ar.document.CustomerInvoiceDocument;
024    import org.kuali.kfs.module.ar.document.service.CustomerInvoiceDocumentService;
025    import org.kuali.kfs.sys.context.SpringContext;
026    import org.kuali.rice.kns.util.KualiDecimal;
027    
028    public class CustomerInvoiceBalanceHelper {
029    
030        private CustomerInvoiceDocument invoice;
031        private Collection<InvoicePaidApplied> invoicePaidApplieds;
032        private Collection<CustomerInvoiceDetail> customerInvoiceDetails;
033    
034        public CustomerInvoiceBalanceHelper() {
035    
036        }
037    
038        public CustomerInvoiceBalanceHelper(CustomerInvoiceDocument invoice, Collection<InvoicePaidApplied> invoicePaidApplieds) {
039            this.invoice = invoice;
040            this.invoicePaidApplieds = new ArrayList<InvoicePaidApplied>(invoicePaidApplieds);
041        }
042    
043        /**
044         * This method calculates the invoice document balance as the difference between the open amount and the total applied amount
045         * @return the balance of the customer invoice document
046         */
047        public KualiDecimal getCalculatedBalance() {
048            return invoice.getTotalDollarAmount().subtract(getTotalAppliedAmountForAppDoc());
049        }
050        
051        /**
052         * This method gets the open amount of the ustomer invoice document
053         * @return the open amount of the invoice
054         */
055        public KualiDecimal getOpenAmount() {
056            CustomerInvoiceDocumentService customerInvoiceDocumentService = SpringContext.getBean(CustomerInvoiceDocumentService.class);
057            return customerInvoiceDocumentService.getOpenAmountForCustomerInvoiceDocument(this.invoice.getDocumentNumber());
058        }
059        
060        /**
061         * This method gets the total applied amount 
062         * @return the total applied amount
063         */
064        public KualiDecimal getTotalAppliedAmountForAppDoc() {
065            KualiDecimal appliedAmount = new KualiDecimal(0);
066            for (InvoicePaidApplied invoicePaidApplied : invoicePaidApplieds) {
067                appliedAmount = appliedAmount.add(invoicePaidApplied.getInvoiceItemAppliedAmount());
068            }
069            return appliedAmount;
070        }
071       
072    
073        /**
074         * This method gets the invoice
075         * @return
076         */
077        public CustomerInvoiceDocument getInvoice() {
078            return invoice;
079        }
080    
081        /**
082         * This method sets the invoice
083         * @param invoice
084         */
085        public void setInvoice(CustomerInvoiceDocument invoice) {
086            this.invoice = invoice;
087        }
088    
089        /**
090         * This method gets the invoice paid applieds
091         * @return
092         */
093        public Collection<InvoicePaidApplied> getInvoicePaidApplieds() {
094            return invoicePaidApplieds;
095        }
096    
097        /**
098         * This method sets the invoice paid applieds
099         * @param invoicePaidApplieds
100         */
101        public void setInvoicePaidApplieds(Collection<InvoicePaidApplied> invoicePaidApplieds) {
102            this.invoicePaidApplieds = invoicePaidApplieds;
103        }
104    
105    }