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.service.impl;
017    
018    import java.util.Collection;
019    import java.util.HashMap;
020    import java.util.Iterator;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.kuali.kfs.module.ld.businessobject.LaborLedgerPendingEntry;
025    import org.kuali.kfs.module.ld.dataaccess.LaborLedgerPendingEntryDao;
026    import org.kuali.kfs.module.ld.document.LaborLedgerPostingDocument;
027    import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.businessobject.AccountingLine;
030    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySequenceHelper;
031    import org.kuali.kfs.sys.context.SpringContext;
032    import org.kuali.rice.kns.service.BusinessObjectService;
033    import org.kuali.rice.kns.service.KualiRuleService;
034    import org.kuali.rice.kns.service.LookupService;
035    import org.springframework.transaction.annotation.Transactional;
036    
037    /**
038     * Service implementation of LaborLedgerPendingEntryService.
039     */
040    @Transactional
041    public class LaborLedgerPendingEntryServiceImpl implements LaborLedgerPendingEntryService {
042        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LaborLedgerPendingEntryServiceImpl.class);
043    
044        private LaborLedgerPendingEntryDao laborLedgerPendingEntryDao;
045        private BusinessObjectService businessObjectService;
046    
047        /**
048         * @see org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService#hasPendingLaborLedgerEntry(org.kuali.kfs.coa.businessobject.Account)
049         */
050        public boolean hasPendingLaborLedgerEntry(String chartOfAccountsCode, String accountNumber) {
051            Map fieldValues = new HashMap();
052            fieldValues.put("chartOfAccountsCode", chartOfAccountsCode);
053            fieldValues.put("accountNumber", accountNumber);
054    
055            return businessObjectService.countMatching(LaborLedgerPendingEntry.class, fieldValues) > 0;
056        }
057    
058        /**
059         * @see org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService#hasPendingLaborLedgerEntry(java.util.Map)
060         */
061        public boolean hasPendingLaborLedgerEntry(Map fieldValues) {
062            LOG.info("hasPendingLaborLedgerEntry(Map fieldValues) started");
063    
064            Collection<LaborLedgerPendingEntry> pendingEntries = SpringContext.getBean(LookupService.class).findCollectionBySearch(LaborLedgerPendingEntry.class, fieldValues);
065    
066            // exclude the pending labor ledger transaction has been processed
067            for (LaborLedgerPendingEntry pendingLedgerEntry : pendingEntries) {
068                String approvedCode = pendingLedgerEntry.getFinancialDocumentApprovedCode();
069                if (!KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.PROCESSED.equals(approvedCode)) {
070                    return true;
071                }
072            }
073            return false;
074        }
075    
076        /**
077         * Invokes generateEntries method on the salary expense transfer document.
078         * 
079         * @param document - document whose pending entries need generated
080         * @return whether the business rules succeeded
081         */
082        public boolean generateLaborLedgerPendingEntries(LaborLedgerPostingDocument document) {
083            LOG.info("generateLaborLedgerPendingEntries() started");
084            boolean success = true;
085    
086            // we must clear them first before creating new ones
087            document.getLaborLedgerPendingEntries().clear();
088    
089            LOG.info("deleting existing labor ledger pending ledger entries for document " + document.getDocumentNumber());
090            delete(document.getDocumentNumber());
091    
092            LOG.info("generating labor ledger pending ledger entries for document " + document.getDocumentNumber());
093            GeneralLedgerPendingEntrySequenceHelper sequenceHelper = new GeneralLedgerPendingEntrySequenceHelper();
094    
095            // process accounting lines, generate labor ledger pending entries
096            List<AccountingLine> sourceAccountingLines = document.getSourceAccountingLines();
097            for (AccountingLine accountingLine : sourceAccountingLines) {
098                success &= document.generateLaborLedgerPendingEntries(accountingLine, sequenceHelper);
099            }
100    
101            List<AccountingLine> targetAccountingLines = document.getTargetAccountingLines();
102            for (AccountingLine accountingLine : targetAccountingLines) {
103                success &= document.generateLaborLedgerPendingEntries(accountingLine, sequenceHelper);
104            }
105    
106            // compare source and target accounting lines, and generate benefit clearing lines as needed
107            success &= document.generateLaborLedgerBenefitClearingPendingEntries(sequenceHelper);
108    
109            return success;
110        }
111    
112        public void delete(String documentHeaderId) {
113            LOG.debug("delete() started");
114    
115            laborLedgerPendingEntryDao.delete(documentHeaderId);
116        }
117    
118        public Collection findPendingEntries(Map fieldValues, boolean isApproved) {
119            LOG.debug("findPendingEntries() started");
120    
121            return laborLedgerPendingEntryDao.findPendingEntries(fieldValues, isApproved);
122        }
123    
124        /**
125         * @see org.kuali.module.gl.service.GeneralLedgerPendingEntryService#findPendingLedgerEntriesForAccountBalance(java.util.Map,
126         *      boolean, boolean)
127         */
128        public Iterator findPendingLedgerEntriesForLedgerBalance(Map fieldValues, boolean isApproved) {
129            LOG.debug("findPendingLedgerEntriesForAccountBalance() started");
130            return laborLedgerPendingEntryDao.findPendingLedgerEntriesForLedgerBalance(fieldValues, isApproved);
131        }
132    
133        /**
134         * @see org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService#findApprovedPendingLedgerEntries()
135         */
136        public Iterator<LaborLedgerPendingEntry> findApprovedPendingLedgerEntries() {
137            return laborLedgerPendingEntryDao.findApprovedPendingLedgerEntries();
138        }
139    
140        /**
141         * @see org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService#deleteByFinancialDocumentApprovedCode(java.lang.String)
142         */
143        public void deleteByFinancialDocumentApprovedCode(String financialDocumentApprovedCode) {
144            laborLedgerPendingEntryDao.deleteByFinancialDocumentApprovedCode(financialDocumentApprovedCode);
145        }
146    
147        /**
148         * Sets the laborLedgerPendingEntryDao attribute value.
149         * @param laborLedgerPendingEntryDao The laborLedgerPendingEntryDao to set.
150         */
151        public void setLaborLedgerPendingEntryDao(LaborLedgerPendingEntryDao laborLedgerPendingEntryDao) {
152            this.laborLedgerPendingEntryDao = laborLedgerPendingEntryDao;
153        }
154    
155        /**
156         * Sets the businessObjectService attribute value.
157         * @param businessObjectService The businessObjectService to set.
158         */
159        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
160            this.businessObjectService = businessObjectService;
161        }
162    }