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    
017    package org.kuali.kfs.module.ld.businessobject;
018    
019    /**
020     * Labor Base class for parsing serialized AccountingLines for Labor LedgerJournal Voucher
021     */
022    
023    import static org.kuali.kfs.sys.KFSKeyConstants.AccountingLineParser.ERROR_INVALID_PROPERTY_VALUE;
024    import static org.kuali.kfs.sys.KFSPropertyConstants.ACCOUNT_NUMBER;
025    import static org.kuali.kfs.sys.KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE;
026    import static org.kuali.kfs.sys.KFSPropertyConstants.CREDIT;
027    import static org.kuali.kfs.sys.KFSPropertyConstants.DEBIT;
028    import static org.kuali.kfs.sys.KFSPropertyConstants.EMPLID;
029    import static org.kuali.kfs.sys.KFSPropertyConstants.EMPLOYEE_RECORD;
030    import static org.kuali.kfs.sys.KFSPropertyConstants.FINANCIAL_OBJECT_CODE;
031    import static org.kuali.kfs.sys.KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE;
032    import static org.kuali.kfs.sys.KFSPropertyConstants.OBJECT_TYPE_CODE;
033    import static org.kuali.kfs.sys.KFSPropertyConstants.ORGANIZATION_REFERENCE_ID;
034    import static org.kuali.kfs.sys.KFSPropertyConstants.POSITION_NUMBER;
035    import static org.kuali.kfs.sys.KFSPropertyConstants.PROJECT_CODE;
036    import static org.kuali.kfs.sys.KFSPropertyConstants.SUB_ACCOUNT_NUMBER;
037    import static org.kuali.kfs.sys.KFSPropertyConstants.ENCUMBRANCE_UPDATE_CODE;
038    
039    import static org.kuali.kfs.module.ld.LaborPropertyConstants.HRMS_COMPANY;
040    import static org.kuali.kfs.module.ld.LaborPropertyConstants.EARN_CODE;
041    import static org.kuali.kfs.module.ld.LaborPropertyConstants.PAY_GROUP;
042    import static org.kuali.kfs.module.ld.LaborPropertyConstants.SALARY_ADMINISTRATION_PLAN; 
043    import static org.kuali.kfs.module.ld.LaborPropertyConstants.GRADE; 
044    import static org.kuali.kfs.module.ld.LaborPropertyConstants.RUN_IDENTIFIER; 
045    import static org.kuali.kfs.module.ld.LaborPropertyConstants.PAY_PERIOD_END_DATE; 
046    import static org.kuali.kfs.module.ld.LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_YEAR; 
047    import static org.kuali.kfs.module.ld.LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_PERIOD_CODE; 
048    import static org.kuali.kfs.module.ld.LaborPropertyConstants.TRANSACTION_TOTAL_HOURS; 
049    import static org.kuali.kfs.module.ld.LaborPropertyConstants.LABORLEDGER_ORIGINAL_CHART_OF_ACCOUNTS_CODE; 
050    import static org.kuali.kfs.module.ld.LaborPropertyConstants.LABORLEDGER_ORIGINAL_ACCOUNT_NUMBER;
051    import static org.kuali.kfs.module.ld.LaborPropertyConstants.LABORLEDGER_ORIGINAL_SUB_ACCOUNT_NUMBER; 
052    import static org.kuali.kfs.module.ld.LaborPropertyConstants.LABORLEDGER_ORIGINAL_FINANCIAL_OBJECT_CODE; 
053    import static org.kuali.kfs.module.ld.LaborPropertyConstants.LABORLEDGER_ORIGINAL_FINANCIAL_SUB_OBJECT_CODE;
054    import static org.kuali.kfs.module.ld.LaborPropertyConstants.SET_ID;
055    
056    import java.util.Map;
057    
058    import org.apache.commons.lang.StringUtils;
059    import org.kuali.kfs.coa.service.BalanceTypeService;
060    import org.kuali.kfs.module.ld.LaborPropertyConstants;
061    import org.kuali.kfs.sys.KFSConstants;
062    import org.kuali.kfs.sys.KFSPropertyConstants;
063    import org.kuali.kfs.sys.businessobject.AccountingLineParserBase;
064    import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
065    import org.kuali.kfs.sys.context.SpringContext;
066    import org.kuali.kfs.sys.exception.AccountingLineParserException;
067    import org.kuali.rice.kns.util.KualiDecimal;
068    
069    public class LaborJournalVoucherAccountingLineParser extends AccountingLineParserBase {
070        private String balanceTypeCode;
071        protected static final String[] LABOR_JV_FORMAT = { CHART_OF_ACCOUNTS_CODE, ACCOUNT_NUMBER, SUB_ACCOUNT_NUMBER, FINANCIAL_OBJECT_CODE, 
072            OBJECT_TYPE_CODE, FINANCIAL_SUB_OBJECT_CODE, PROJECT_CODE, ORGANIZATION_REFERENCE_ID, POSITION_NUMBER, EMPLID, EMPLOYEE_RECORD, 
073            EARN_CODE, PAY_GROUP, SALARY_ADMINISTRATION_PLAN, GRADE, RUN_IDENTIFIER, PAY_PERIOD_END_DATE, PAYROLL_END_DATE_FISCAL_YEAR, 
074            PAYROLL_END_DATE_FISCAL_PERIOD_CODE, TRANSACTION_TOTAL_HOURS, LABORLEDGER_ORIGINAL_CHART_OF_ACCOUNTS_CODE, LABORLEDGER_ORIGINAL_ACCOUNT_NUMBER,
075            LABORLEDGER_ORIGINAL_SUB_ACCOUNT_NUMBER, LABORLEDGER_ORIGINAL_FINANCIAL_OBJECT_CODE, LABORLEDGER_ORIGINAL_FINANCIAL_SUB_OBJECT_CODE,
076            HRMS_COMPANY, ENCUMBRANCE_UPDATE_CODE, SET_ID, DEBIT, CREDIT };
077    
078        /**
079         * Constructs a JournalVoucherAccountingLineParser.java.
080         * 
081         * @param balanceTypeCode
082         */
083        public LaborJournalVoucherAccountingLineParser(String balanceTypeCode) {
084            super();
085            this.balanceTypeCode = balanceTypeCode;
086        }
087    
088        /**
089         * @see org.kuali.rice.kns.bo.AccountingLineParserBase#performCustomSourceAccountingLinePopulation(java.util.Map,
090         *      org.kuali.rice.kns.bo.SourceAccountingLine, java.lang.String)
091         */
092        @Override
093        protected void performCustomSourceAccountingLinePopulation(Map<String, String> attributeValueMap, SourceAccountingLine sourceAccountingLine, String accountingLineAsString) {
094            // chose debit/credit
095            String debitValue = attributeValueMap.remove(DEBIT);
096            String creditValue = attributeValueMap.remove(CREDIT);
097            KualiDecimal debitAmount = null;
098            try {
099                if (StringUtils.isNotBlank(debitValue)) {
100                    debitAmount = new KualiDecimal(debitValue);
101                }
102            }
103            catch (NumberFormatException e) {
104                String[] errorParameters = { debitValue, retrieveAttributeLabel(sourceAccountingLine.getClass(), DEBIT), accountingLineAsString };
105                throw new AccountingLineParserException("invalid (NaN) '" + DEBIT + "=" + debitValue + " for " + accountingLineAsString, ERROR_INVALID_PROPERTY_VALUE, errorParameters);
106            }
107            KualiDecimal creditAmount = null;
108            try {
109                if (StringUtils.isNotBlank(creditValue)) {
110                    creditAmount = new KualiDecimal(creditValue);
111                }
112            }
113            catch (NumberFormatException e) {
114                String[] errorParameters = { creditValue, retrieveAttributeLabel(sourceAccountingLine.getClass(), CREDIT), accountingLineAsString };
115                throw new AccountingLineParserException("invalid (NaN) '" + CREDIT + "=" + creditValue + " for " + accountingLineAsString, ERROR_INVALID_PROPERTY_VALUE, errorParameters);
116            }
117    
118            KualiDecimal amount = null;
119            String debitCreditCode = null;
120            if (debitAmount != null && debitAmount.isNonZero()) {
121                amount = debitAmount;
122                debitCreditCode = KFSConstants.GL_DEBIT_CODE;
123            }
124    
125            if (creditAmount != null && creditAmount.isNonZero()) {
126                amount = creditAmount;
127                debitCreditCode = KFSConstants.GL_CREDIT_CODE;
128            }
129    
130            sourceAccountingLine.setAmount(amount);
131            sourceAccountingLine.setDebitCreditCode(debitCreditCode);
132    
133            boolean isFinancialOffsetGeneration = SpringContext.getBean(BalanceTypeService.class).getBalanceTypeByCode(balanceTypeCode).isFinancialOffsetGenerationIndicator();
134            if (isFinancialOffsetGeneration || StringUtils.equals(balanceTypeCode, KFSConstants.BALANCE_TYPE_EXTERNAL_ENCUMBRANCE)) {
135                super.performCustomSourceAccountingLinePopulation(attributeValueMap, sourceAccountingLine, accountingLineAsString);
136            }
137            sourceAccountingLine.setBalanceTypeCode(balanceTypeCode);
138        }
139    
140        /**
141         * @see org.kuali.rice.kns.bo.AccountingLineParserBase#getSourceAccountingLineFormat()
142         */
143        @Override
144        public String[] getSourceAccountingLineFormat() {
145            return removeChartFromFormatIfNeeded(selectFormat());
146        }
147    
148        /**
149         * chooses line format based on balance type code
150         * 
151         * @return String []
152         */
153        private String[] selectFormat() {
154            return LABOR_JV_FORMAT;
155        }
156    }