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.cab.businessobject;
017    
018    import java.util.ArrayList;
019    import java.util.List;
020    
021    import org.apache.log4j.Logger;
022    import org.kuali.kfs.module.cab.CabPropertyConstants;
023    import org.kuali.kfs.module.purap.businessobject.AccountsPayableItem;
024    import org.kuali.kfs.module.purap.businessobject.CreditMemoAccountRevision;
025    import org.kuali.kfs.module.purap.businessobject.PurApAccountingLineBase;
026    import org.kuali.kfs.module.purap.document.AccountsPayableDocumentBase;
027    import org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument;
028    import org.kuali.rice.kns.util.ObjectUtils;
029    
030    /**
031     * Accounting line grouped data for GL Line
032     */
033    public class PurApAccountLineGroup extends AccountLineGroup {
034    
035        private Logger LOG = Logger.getLogger(PurApAccountLineGroup.class);
036        private List<PurApAccountingLineBase> sourceEntries = new ArrayList<PurApAccountingLineBase>();
037    
038        /**
039         * Constructs a PurApAccountLineGroup from a PurApAccountingLineBase Line Entry
040         * 
041         * @param entry PurApAccountingLineBase Line
042         */
043        protected PurApAccountLineGroup() {
044    
045        }
046    
047        public PurApAccountLineGroup(PurApAccountingLineBase entry) {
048            entry.refreshReferenceObject(CabPropertyConstants.PURAP_ITEM);
049            AccountsPayableItem purapItem = (AccountsPayableItem) entry.getPurapItem();
050            if (ObjectUtils.isNotNull(purapItem)) {
051                purapItem.refreshReferenceObject(CabPropertyConstants.PURAP_DOCUMENT);
052                AccountsPayableDocumentBase purapDocument = ((AccountsPayableItem) entry.getPurapItem()).getPurapDocument();
053                if (ObjectUtils.isNotNull(purapDocument)) {
054                    setReferenceFinancialDocumentNumber(purapDocument.getPurchaseOrderIdentifier() != null ? purapDocument.getPurchaseOrderIdentifier().toString() : "");
055                    setDocumentNumber(purapDocument.getDocumentNumber());
056                }
057            }
058            else {
059                LOG.error("Could not load PurAP document details for " + entry.toString());
060            }
061            setUniversityFiscalYear(entry.getPostingYear());
062            setUniversityFiscalPeriodCode(entry.getPostingPeriodCode());
063            setChartOfAccountsCode(entry.getChartOfAccountsCode());
064            setAccountNumber(entry.getAccountNumber());
065            setSubAccountNumber(entry.getSubAccountNumber());
066            setFinancialObjectCode(entry.getFinancialObjectCode());
067            setFinancialSubObjectCode(entry.getFinancialSubObjectCode());
068            setOrganizationReferenceId(entry.getOrganizationReferenceId());
069            setProjectCode(entry.getProjectCode());
070            this.sourceEntries.add(entry);
071            if (CreditMemoAccountRevision.class.isAssignableFrom(entry.getClass())) {
072                setAmount(entry.getAmount().negated());
073            }
074            else {
075                setAmount(entry.getAmount());
076            }
077        }
078    
079        /**
080         * Returns true if input PurApAccountingLineBase entry belongs to this account group
081         * 
082         * @param entry PurApAccountingLineBase
083         * @return true if PurApAccountingLineBase belongs to same account line group
084         */
085        public boolean isAccounted(PurApAccountingLineBase entry) {
086            PurApAccountLineGroup test = new PurApAccountLineGroup(entry);
087            return this.equals(test);
088        }
089    
090        /**
091         * This method will combine multiple Purap account entries for the same account line group.
092         * 
093         * @param entry PurApAccountingLineBase
094         */
095        public void combineEntry(PurApAccountingLineBase newEntry) {
096            this.sourceEntries.add(newEntry);
097            if (CreditMemoAccountRevision.class.isAssignableFrom(newEntry.getClass())) {
098                this.amount = this.amount.add(newEntry.getAmount().negated());
099            }
100            else {
101                this.amount = this.amount.add(newEntry.getAmount());
102            }
103        }
104    
105    
106        /**
107         * Gets the sourceEntries attribute.
108         * 
109         * @return Returns the sourceEntries
110         */
111        public List<PurApAccountingLineBase> getSourceEntries() {
112            return sourceEntries;
113        }
114    
115        /**
116         * Sets the sourceEntries attribute.
117         * 
118         * @param sourceEntries The sourceEntries to set.
119         */
120        public void setSourceEntries(List<PurApAccountingLineBase> sourceGlEntries) {
121            this.sourceEntries = sourceGlEntries;
122        }
123    }