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.fp.document;
017    
018    import org.kuali.kfs.fp.businessobject.IndirectCostAdjustmentDocumentAccountingLineParser;
019    import org.kuali.kfs.fp.document.validation.impl.IndirectCostAdjustmentDocumentRuleConstants;
020    import org.kuali.kfs.sys.KFSConstants;
021    import org.kuali.kfs.sys.businessobject.AccountingLine;
022    import org.kuali.kfs.sys.businessobject.AccountingLineParser;
023    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
024    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
025    import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
026    import org.kuali.kfs.sys.context.SpringContext;
027    import org.kuali.kfs.sys.document.AccountingDocumentBase;
028    import org.kuali.kfs.sys.document.AmountTotaling;
029    import org.kuali.kfs.sys.document.Correctable;
030    import org.kuali.kfs.sys.document.service.DebitDeterminerService;
031    import org.kuali.rice.kns.document.Copyable;
032    import org.kuali.rice.kns.exception.InfrastructureException;
033    import org.kuali.rice.kns.service.ParameterService;
034    
035    public class IndirectCostAdjustmentDocument extends AccountingDocumentBase implements Copyable, Correctable, AmountTotaling {
036    
037        /**
038         * Constructs a IndirectCostAdjustmentDocument.java.
039         */
040        public IndirectCostAdjustmentDocument() {
041            super();
042        }
043    
044        /**
045         * @see org.kuali.kfs.sys.document.AccountingDocument#getSourceAccountingLinesSectionTitle()
046         */
047        @Override
048        public String getSourceAccountingLinesSectionTitle() {
049            return KFSConstants.GRANT;
050        }
051    
052        /**
053         * @see org.kuali.kfs.sys.document.AccountingDocument#getTargetAccountingLinesSectionTitle()
054         */
055        @Override
056        public String getTargetAccountingLinesSectionTitle() {
057            return KFSConstants.ICR;
058        }
059    
060        /**
061         * per ICA specs, adds a target(receipt) line when an source(grant) line is added using the following logic to populate the
062         * target line.
063         * <ol>
064         * <li>receipt line's chart = chart from grant line
065         * <li>receipt line's account = ICR account for the account entered on the grant line
066         * <li>receipt line's object code = Financial System Parameter APC for the document global receipt line object code (see APC
067         * setion below)
068         * <li>receipt line's amount = amount from grant line
069         * </ol>
070         * 
071         * @see org.kuali.kfs.sys.document.AccountingDocumentBase#addSourceAccountingLine(SourceAccountingLine)
072         */
073        @Override
074        public void addSourceAccountingLine(SourceAccountingLine line) {
075            // add source
076            super.addSourceAccountingLine(line);
077            // create and populate target line
078            TargetAccountingLine targetAccountingLine = null;
079            try {
080                targetAccountingLine = (TargetAccountingLine) getTargetAccountingLineClass().newInstance();
081            }
082            catch (Exception e) {
083                throw new InfrastructureException("unable to create a target accounting line", e);
084            }
085            // get apc object code value
086            String objectCode = SpringContext.getBean(ParameterService.class).getParameterValue(IndirectCostAdjustmentDocument.class, IndirectCostAdjustmentDocumentRuleConstants.RECEIPT_OBJECT_CODE);
087            targetAccountingLine.setFinancialObjectCode(objectCode);
088            targetAccountingLine.setAccountNumber(line.getAccount().getIndirectCostRecoveryAcctNbr());
089            targetAccountingLine.setChartOfAccountsCode(line.getAccount().getIndirectCostRcvyFinCoaCode());
090            targetAccountingLine.setDocumentNumber(line.getDocumentNumber());
091            targetAccountingLine.setPostingYear(line.getPostingYear());
092            targetAccountingLine.setAmount(line.getAmount());
093            // refresh reference objects
094    
095            targetAccountingLine.refresh();
096            // add target line
097            addTargetAccountingLine(targetAccountingLine);
098        }
099        
100        /**
101         * Same logic as <code>IsDebitUtils#isDebitConsideringType(FinancialDocumentRuleBase, FinancialDocument, AccountingLine)</code>
102         * but has the following accounting line restrictions: 
103         * 
104         * for grant lines(source):
105         * <ol>
106         * <li>only allow expense object type codes
107         * </ol>
108         * for receipt lines(target):
109         * <ol>
110         * <li>only allow income object type codes
111         * </ol>
112         * 
113         * @param transactionDocument The document associated with the accounting line being reviewed to determine if it's a debit.
114         * @param accountingLine The accounting line being reviewed to determine if it's a debit line.
115         * @return True if the accounting line is a debit.  See IsDebitUtils.isDebitConsideringType().
116         * @throws IllegalStateException Thrown if the accounting line given is a source accounting line representing an expense
117         * or is a target accounting line representing an income.
118         * 
119         * @see IsDebitUtils#isDebitConsideringType(FinancialDocumentRuleBase, FinancialDocument, AccountingLine)
120         * @see org.kuali.rice.kns.rule.AccountingLineRule#isDebit(org.kuali.rice.kns.document.FinancialDocument,
121         *      org.kuali.rice.kns.bo.AccountingLine)
122         */
123        public boolean isDebit(GeneralLedgerPendingEntrySourceDetail postable) throws IllegalStateException {
124            AccountingLine accountingLine = (AccountingLine)postable;
125            DebitDeterminerService isDebitUtils = SpringContext.getBean(DebitDeterminerService.class);
126            if (!(accountingLine.isSourceAccountingLine() && isDebitUtils.isExpense(accountingLine)) && !(accountingLine.isTargetAccountingLine() && isDebitUtils.isIncome(accountingLine))) {
127                throw new IllegalStateException(isDebitUtils.getDebitCalculationIllegalStateExceptionMessage());
128            }
129    
130            return isDebitUtils.isDebitConsideringType(this, accountingLine);
131        }
132    }