View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.fp.document;
20  
21  import java.math.BigDecimal;
22  
23  import org.kuali.kfs.coa.businessobject.IndirectCostRecoveryAccount;
24  import org.kuali.kfs.fp.document.validation.impl.IndirectCostAdjustmentDocumentRuleConstants;
25  import org.kuali.kfs.sys.KFSConstants;
26  import org.kuali.kfs.sys.businessobject.AccountingLine;
27  import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
28  import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
29  import org.kuali.kfs.sys.businessobject.TargetAccountingLine;
30  import org.kuali.kfs.sys.context.SpringContext;
31  import org.kuali.kfs.sys.document.AccountingDocumentBase;
32  import org.kuali.kfs.sys.document.AmountTotaling;
33  import org.kuali.kfs.sys.document.Correctable;
34  import org.kuali.kfs.sys.document.service.DebitDeterminerService;
35  import org.kuali.rice.core.api.util.type.KualiDecimal;
36  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
37  import org.kuali.rice.krad.document.Copyable;
38  import org.kuali.rice.krad.exception.InfrastructureException;
39  import org.kuali.rice.krad.util.GlobalVariables;
40  import org.kuali.rice.krad.util.ObjectUtils;
41  
42  public class IndirectCostAdjustmentDocument extends AccountingDocumentBase implements Copyable, Correctable, AmountTotaling {
43  
44      /**
45       * Constructs a IndirectCostAdjustmentDocument.java.
46       */
47      public IndirectCostAdjustmentDocument() {
48          super();
49      }
50  
51      /**
52       * @see org.kuali.kfs.sys.document.AccountingDocument#getSourceAccountingLinesSectionTitle()
53       */
54      @Override
55      public String getSourceAccountingLinesSectionTitle() {
56          return KFSConstants.GRANT;
57      }
58  
59      /**
60       * @see org.kuali.kfs.sys.document.AccountingDocument#getTargetAccountingLinesSectionTitle()
61       */
62      @Override
63      public String getTargetAccountingLinesSectionTitle() {
64          return KFSConstants.ICR;
65      }
66  
67      /**
68       * per ICA specs, adds a target(receipt) line when an source(grant) line is added using the following logic to populate the
69       * target line.
70       * <ol>
71       * <li>receipt line's chart = chart from grant line
72       * <li>receipt line's account = ICR account for the account entered on the grant line
73       * <li>receipt line's object code = Financial System Parameter APC for the document global receipt line object code (see APC
74       * setion below)
75       * <li>receipt line's amount = amount from grant line
76       * </ol>
77       *
78       * @see org.kuali.kfs.sys.document.AccountingDocumentBase#addSourceAccountingLine(SourceAccountingLine)
79       */
80      @Override
81      public void addSourceAccountingLine(SourceAccountingLine line) {
82          // add source
83          super.addSourceAccountingLine(line);
84  
85          if (GlobalVariables.getMessageMap().hasNoMessages()) {
86              if  (line != null && ObjectUtils.isNotNull(line.getAccount()) && line.getAccount().getActiveIndirectCostRecoveryAccounts() != null && line.getAccount().getActiveIndirectCostRecoveryAccounts().size() > 0) {
87                  for (IndirectCostRecoveryAccount icrAccount : line.getAccount().getActiveIndirectCostRecoveryAccounts()) {
88  
89                      KualiDecimal percentDecimal = new KualiDecimal(icrAccount.getAccountLinePercent().divide(new BigDecimal(100)));
90  
91                      // create and populate target line
92                      TargetAccountingLine targetAccountingLine = null;
93                      try {
94                          targetAccountingLine = (TargetAccountingLine) getTargetAccountingLineClass().newInstance();
95                      }
96                      catch (Exception e) {
97                          throw new InfrastructureException("unable to create a target accounting line", e);
98                      }
99                      // get apc object code value
100                     String objectCode = SpringContext.getBean(ParameterService.class).getParameterValueAsString(IndirectCostAdjustmentDocument.class, IndirectCostAdjustmentDocumentRuleConstants.RECEIPT_OBJECT_CODE);
101                     targetAccountingLine.setFinancialObjectCode(objectCode);
102                     targetAccountingLine.setAccountNumber(icrAccount.getIndirectCostRecoveryAccountNumber());
103                     targetAccountingLine.setChartOfAccountsCode(icrAccount.getIndirectCostRecoveryFinCoaCode());
104                     targetAccountingLine.setDocumentNumber(line.getDocumentNumber());
105                     targetAccountingLine.setPostingYear(line.getPostingYear());
106                     targetAccountingLine.setAmount(line.getAmount().multiply(percentDecimal));
107                     // refresh reference objects
108 
109                     targetAccountingLine.refresh();
110                     // add target line
111                     addTargetAccountingLine(targetAccountingLine);
112                 }
113             }
114         }
115     }
116 
117     /**
118      * Same logic as <code>IsDebitUtils#isDebitConsideringType(FinancialDocumentRuleBase, FinancialDocument, AccountingLine)</code>
119      * but has the following accounting line restrictions:
120      *
121      * for grant lines(source):
122      * <ol>
123      * <li>only allow expense object type codes
124      * </ol>
125      * for receipt lines(target):
126      * <ol>
127      * <li>only allow income object type codes
128      * </ol>
129      *
130      * @param transactionDocument The document associated with the accounting line being reviewed to determine if it's a debit.
131      * @param accountingLine The accounting line being reviewed to determine if it's a debit line.
132      * @return True if the accounting line is a debit.  See IsDebitUtils.isDebitConsideringType().
133      * @throws IllegalStateException Thrown if the accounting line given is a source accounting line representing an expense
134      * or is a target accounting line representing an income.
135      *
136      * @see IsDebitUtils#isDebitConsideringType(FinancialDocumentRuleBase, FinancialDocument, AccountingLine)
137      * @see org.kuali.rice.krad.rule.AccountingLineRule#isDebit(org.kuali.rice.krad.document.FinancialDocument,
138      *      org.kuali.rice.krad.bo.AccountingLine)
139      */
140     @Override
141     public boolean isDebit(GeneralLedgerPendingEntrySourceDetail postable) throws IllegalStateException {
142         AccountingLine accountingLine = (AccountingLine)postable;
143         DebitDeterminerService isDebitUtils = SpringContext.getBean(DebitDeterminerService.class);
144         if (!(accountingLine.isSourceAccountingLine() && isDebitUtils.isExpense(accountingLine)) && !(accountingLine.isTargetAccountingLine() && isDebitUtils.isIncome(accountingLine))) {
145             throw new IllegalStateException(isDebitUtils.getDebitCalculationIllegalStateExceptionMessage());
146         }
147 
148         return isDebitUtils.isDebitConsideringType(this, accountingLine);
149     }
150 }