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.ld.batch.service.impl;
017    
018    import java.sql.Date;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.gl.batch.service.PostTransaction;
022    import org.kuali.kfs.gl.businessobject.Transaction;
023    import org.kuali.kfs.module.ld.LaborConstants;
024    import org.kuali.kfs.module.ld.businessobject.LaborGeneralLedgerEntry;
025    import org.kuali.kfs.module.ld.service.LaborGeneralLedgerEntryService;
026    import org.kuali.kfs.module.ld.util.DebitCreditUtil;
027    import org.kuali.kfs.sys.KFSConstants;
028    import org.kuali.kfs.sys.ObjectUtil;
029    import org.kuali.kfs.sys.service.ReportWriterService;
030    import org.kuali.rice.kns.util.KualiDecimal;
031    import org.springframework.transaction.annotation.Transactional;
032    
033    /**
034     * This class is used to post a transaction into labor GL entry table
035     */
036    @Transactional
037    public class LaborGLLedgerEntryPoster implements PostTransaction {
038        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborGLLedgerEntryPoster.class);
039        
040        private LaborGeneralLedgerEntryService laborGeneralLedgerEntryService;
041    
042        /**
043         * @see org.kuali.kfs.gl.batch.service.PostTransaction#post(org.kuali.kfs.gl.businessobject.Transaction, int, java.util.Date)
044         */
045        public String post(Transaction transaction, int mode, java.util.Date postDate, ReportWriterService posterReportWriterService) {
046            String operationType = KFSConstants.OperationType.INSERT;
047            LaborGeneralLedgerEntry laborGeneralLedgerEntry = new LaborGeneralLedgerEntry();
048            ObjectUtil.buildObject(laborGeneralLedgerEntry, transaction);
049    
050            laborGeneralLedgerEntry.setTransactionDate(new Date(postDate.getTime()));
051    
052            if(! transaction.getFinancialBalanceTypeCode().equalsIgnoreCase("CB")){
053                laborGeneralLedgerEntry.setTransactionDebitCreditCode(this.getDebitCreditCode(transaction));
054            }
055            laborGeneralLedgerEntry.setTransactionLedgerEntryAmount(this.getTransactionAmount(transaction));
056            
057            String encumbranceUpdateCode = this.getEncumbranceUpdateCode(transaction);
058            if(StringUtils.isNotEmpty(encumbranceUpdateCode)) {
059                laborGeneralLedgerEntry.setTransactionEncumbranceUpdateCode(encumbranceUpdateCode);
060            }
061    
062            Integer sequenceNumber = laborGeneralLedgerEntryService.getMaxSequenceNumber(laborGeneralLedgerEntry) + 1;
063            laborGeneralLedgerEntry.setTransactionLedgerEntrySequenceNumber(sequenceNumber);
064            try {
065                laborGeneralLedgerEntryService.save(laborGeneralLedgerEntry);
066            }
067            catch (Exception e) {
068                throw new RuntimeException(e);
069            }
070            return operationType;
071        }
072    
073        /**
074         * @return the debit credit code
075         */
076        protected String getDebitCreditCode(Transaction transaction) {
077            KualiDecimal transactionAmount = transaction.getTransactionLedgerEntryAmount();
078            return DebitCreditUtil.getDebitCreditCode(transactionAmount, false);
079        }
080    
081        /**
082         * @return the transaction amount
083         */
084        protected KualiDecimal getTransactionAmount(Transaction transaction) {
085            KualiDecimal transactionAmount = transaction.getTransactionLedgerEntryAmount();
086            return transactionAmount.abs();
087        }
088    
089        /**
090         * @return the encumbrance update code
091         */
092        protected String getEncumbranceUpdateCode(Transaction transaction) {
093            String encumbranceUpdateCode = transaction.getTransactionEncumbranceUpdateCode();
094            if (KFSConstants.ENCUMB_UPDT_DOCUMENT_CD.equals(encumbranceUpdateCode) || KFSConstants.ENCUMB_UPDT_REFERENCE_DOCUMENT_CD.equals(encumbranceUpdateCode)) {
095                return encumbranceUpdateCode;
096            }
097            return null;
098        }
099    
100        /**
101         * @see org.kuali.kfs.gl.batch.service.PostTransaction#getDestinationName()
102         */
103        public String getDestinationName() {
104            return LaborConstants.DestinationNames.LABOR_GL_ENTRY;
105        }
106    
107        /**
108         * Sets the laborGeneralLedgerEntryService attribute value.
109         * 
110         * @param laborGeneralLedgerEntryService The laborGeneralLedgerEntryService to set.
111         */
112        public void setLaborGeneralLedgerEntryService(LaborGeneralLedgerEntryService laborGeneralLedgerEntryService) {
113            this.laborGeneralLedgerEntryService = laborGeneralLedgerEntryService;
114        }
115    }