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.util.Date;
019    
020    import org.kuali.kfs.gl.batch.service.PostTransaction;
021    import org.kuali.kfs.gl.businessobject.Transaction;
022    import org.kuali.kfs.module.ld.LaborConstants;
023    import org.kuali.kfs.module.ld.batch.service.LaborAccountingCycleCachingService;
024    import org.kuali.kfs.module.ld.businessobject.LaborTransaction;
025    import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
026    import org.kuali.kfs.sys.KFSConstants;
027    import org.kuali.kfs.sys.service.ReportWriterService;
028    import org.kuali.rice.kns.util.KualiDecimal;
029    import org.kuali.rice.kns.util.ObjectUtils;
030    import org.springframework.transaction.annotation.Transactional;
031    
032    /**
033     * This class is used to post a transaction into Labor Ledger Balance Table
034     */
035    @Transactional
036    public class LaborLedgerBalancePoster implements PostTransaction {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborLedgerBalancePoster.class);
038    
039        private LaborAccountingCycleCachingService laborAccountingCycleCachingService;
040        /**
041         * @see org.kuali.kfs.gl.batch.service.PostTransaction#post(org.kuali.kfs.gl.businessobject.Transaction, int, java.util.Date)
042         */
043        public String post(Transaction transaction, int mode, Date postDate, ReportWriterService posterReportWriterService) {
044            String operationType = KFSConstants.OperationType.INSERT;
045            LedgerBalance ledgerBalance = new LedgerBalance((LaborTransaction) transaction);
046            // ObjectUtil.buildObject(ledgerBalance, transaction);
047    
048            LedgerBalance tempLedgerBalance = laborAccountingCycleCachingService.getLedgerBalance(ledgerBalance);
049            if (ObjectUtils.isNotNull(tempLedgerBalance)) {
050                ledgerBalance = tempLedgerBalance;
051                operationType = KFSConstants.OperationType.UPDATE;
052            }
053            KualiDecimal amount = transaction.getTransactionLedgerEntryAmount();
054            if (transaction.getBalanceType().isFinancialOffsetGenerationIndicator()) { 
055                if (!transaction.getTransactionDebitCreditCode().equals(transaction.getObjectType().getFinObjectTypeDebitcreditCd())) { 
056                    amount = amount.negated(); 
057                } 
058            } 
059    
060            ledgerBalance.addAmount(transaction.getUniversityFiscalPeriodCode(), amount);
061    
062            if (operationType.equals(KFSConstants.OperationType.INSERT)) {
063                laborAccountingCycleCachingService.insertLedgerBalance(ledgerBalance);
064            } else {
065                laborAccountingCycleCachingService.updateLedgerBalance(ledgerBalance);
066            }
067            return operationType;
068        }
069    
070        /**
071         * @see org.kuali.kfs.gl.batch.service.PostTransaction#getDestinationName()
072         */
073        public String getDestinationName() {
074            return LaborConstants.DestinationNames.LEDGER_BALANCE;
075        }
076    
077        public void setLaborAccountingCycleCachingService(LaborAccountingCycleCachingService laborAccountingCycleCachingService) {
078            this.laborAccountingCycleCachingService = laborAccountingCycleCachingService;
079        }
080    }