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.ec.service.impl;
017    
018    import java.util.List;
019    import java.util.Map;
020    import java.util.Set;
021    
022    import org.kuali.kfs.coa.businessobject.A21SubAccount;
023    import org.kuali.kfs.integration.ld.LaborLedgerBalance;
024    import org.kuali.kfs.module.ec.EffortConstants;
025    import org.kuali.kfs.module.ec.EffortConstants.SystemParameters;
026    import org.kuali.kfs.module.ec.businessobject.EffortCertificationDetailBuild;
027    import org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition;
028    import org.kuali.kfs.module.ec.service.EffortCertificationDetailBuildService;
029    import org.kuali.kfs.module.ec.util.LedgerBalanceConsolidationHelper;
030    import org.kuali.kfs.sys.KFSConstants;
031    import org.kuali.rice.kns.util.KualiDecimal;
032    import org.kuali.rice.kns.util.ObjectUtils;
033    import org.springframework.transaction.annotation.Transactional;
034    
035    /**
036     * This class provides the facilities that can generate detail line (build) for effort certification from the given labor ledger
037     * balance record
038     */
039    @Transactional
040    public class EffortCertificationDetailBuildServiceImpl implements EffortCertificationDetailBuildService {
041        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EffortCertificationDetailBuildServiceImpl.class);
042    
043        /**
044         * @see org.kuali.kfs.module.ec.service.EffortCertificationDetailBuildService#generateDetailBuild(java.lang.Integer,
045         *      org.kuali.kfs.module.ld.businessobject.LedgerBalance, org.kuali.kfs.module.ec.businessobject.EffortCertificationReportDefinition)
046         */
047        public EffortCertificationDetailBuild generateDetailBuild(Integer postingYear, LaborLedgerBalance ledgerBalance, EffortCertificationReportDefinition reportDefinition) {
048            EffortCertificationDetailBuild detailLine = new EffortCertificationDetailBuild();
049    
050            detailLine.setUniversityFiscalYear(postingYear);
051            detailLine.setAccountNumber(ledgerBalance.getAccountNumber());
052            detailLine.setChartOfAccountsCode(ledgerBalance.getChartOfAccountsCode());
053    
054            detailLine.setPositionNumber(ledgerBalance.getPositionNumber());
055            detailLine.setFinancialObjectCode(ledgerBalance.getFinancialObjectCode());
056    
057            Map<Integer, Set<String>> reportPeriods = reportDefinition.getReportPeriods();
058            KualiDecimal payrollAmount = LedgerBalanceConsolidationHelper.calculateTotalAmountWithinReportPeriod(ledgerBalance, reportPeriods);
059    
060            detailLine.setEffortCertificationPayrollAmount(payrollAmount);
061            detailLine.setEffortCertificationOriginalPayrollAmount(payrollAmount);
062    
063            detailLine.setEffortCertificationCalculatedOverallPercent(0);
064            detailLine.setEffortCertificationUpdatedOverallPercent(0);
065    
066            populateCostShareRelatedFields(detailLine, ledgerBalance);
067    
068            return detailLine;
069        }
070    
071        /**
072         * populate the cost share related fields in the given detail line
073         * 
074         * @param detailLine the given detail line
075         * @param ledgerBalance the given ledger balance
076         * @param parameters the given parameters setup in the calling client
077         */
078        protected void populateCostShareRelatedFields(EffortCertificationDetailBuild detailLine, LaborLedgerBalance ledgerBalance) {
079            List<String> expenseSubAccountTypeCodes = EffortConstants.ELIGIBLE_EXPENSE_SUB_ACCOUNT_TYPE_CODES;
080            List<String> costShareSubAccountTypeCodes = EffortConstants.ELIGIBLE_COST_SHARE_SUB_ACCOUNT_TYPE_CODES;
081    
082            A21SubAccount A21SubAccount = this.getA21SubAccount(ledgerBalance);
083            String subAccountTypeCode = ObjectUtils.isNull(A21SubAccount) ? null : A21SubAccount.getSubAccountTypeCode();
084    
085            if (ObjectUtils.isNull(subAccountTypeCode) || expenseSubAccountTypeCodes.contains(subAccountTypeCode)) {
086                detailLine.setSubAccountNumber(KFSConstants.getDashSubAccountNumber());
087                detailLine.setSourceChartOfAccountsCode(EffortConstants.DASH_CHART_OF_ACCOUNTS_CODE);
088                detailLine.setSourceAccountNumber(EffortConstants.DASH_ACCOUNT_NUMBER);
089                detailLine.setCostShareSourceSubAccountNumber(null);
090            }
091            else if (costShareSubAccountTypeCodes.contains(subAccountTypeCode)) {
092                detailLine.setSubAccountNumber(ledgerBalance.getSubAccountNumber());
093                detailLine.setSourceChartOfAccountsCode(A21SubAccount.getCostShareChartOfAccountCode());
094                detailLine.setSourceAccountNumber(A21SubAccount.getCostShareSourceAccountNumber());
095                detailLine.setCostShareSourceSubAccountNumber(A21SubAccount.getCostShareSourceSubAccountNumber());
096            }
097            else {
098                detailLine.setSubAccountNumber(ledgerBalance.getSubAccountNumber());
099                detailLine.setSourceChartOfAccountsCode(EffortConstants.DASH_CHART_OF_ACCOUNTS_CODE);
100                detailLine.setSourceAccountNumber(EffortConstants.DASH_ACCOUNT_NUMBER);
101                detailLine.setCostShareSourceSubAccountNumber(null);
102            }
103        }
104    
105        /**
106         * get the A21 sub account associated with the given ledger balance
107         * 
108         * @param ledgerBalance the given ledger balance
109         * @return the A21 sub account associated with the given ledger balance; return null if not found
110         */
111        protected A21SubAccount getA21SubAccount(LaborLedgerBalance ledgerBalance) {
112            A21SubAccount a21SubAccount = null;
113            try {
114                if (ObjectUtils.isNotNull(ledgerBalance.getSubAccount())) {
115                    a21SubAccount = ledgerBalance.getSubAccount().getA21SubAccount();
116                }
117            }
118            catch (NullPointerException npe) {
119                LOG.debug(npe);
120            }
121            return a21SubAccount;
122        }
123    }