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.purap.document;
018    
019    import static org.kuali.kfs.sys.KFSConstants.GL_CREDIT_CODE;
020    
021    import java.util.ArrayList;
022    import java.util.Iterator;
023    import java.util.List;
024    
025    import org.kuali.kfs.module.purap.PurapConstants.PurapDocTypeCodes;
026    import org.kuali.kfs.module.purap.PurapConstants.PurchaseOrderStatuses;
027    import org.kuali.kfs.module.purap.document.service.PurapService;
028    import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
029    import org.kuali.kfs.module.purap.service.PurapGeneralLedgerService;
030    import org.kuali.kfs.sys.KFSConstants;
031    import org.kuali.kfs.sys.businessobject.AccountingLine;
032    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry;
033    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
034    import org.kuali.kfs.sys.context.SpringContext;
035    import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
036    import org.kuali.rice.kns.rule.event.KualiDocumentEvent;
037    
038    /**
039     * Purchase Order Close Document
040     */
041    public class PurchaseOrderCloseDocument extends PurchaseOrderDocument {
042        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderCloseDocument.class);
043    
044        /**
045         * Default constructor.
046         */
047        public PurchaseOrderCloseDocument() {
048            super();
049        }
050    
051        /**
052         * General Ledger pending entries are not created on save for this document. They are created when the document has been finally
053         * processed. Overriding this method so that entries are not created yet.
054         * 
055         * @see org.kuali.kfs.module.purap.document.PurchaseOrderDocument#prepareForSave(org.kuali.rice.kns.rule.event.KualiDocumentEvent)
056         */
057        @Override
058        public void prepareForSave(KualiDocumentEvent event) {
059            LOG.info("prepareForSave(KualiDocumentEvent) do not create gl entries");
060            setSourceAccountingLines(new ArrayList());
061            setGeneralLedgerPendingEntries(new ArrayList());
062        }
063    
064        @Override
065        public List<Long> getWorkflowEngineDocumentIdsToLock() {
066            return super.getWorkflowEngineDocumentIdsToLock();
067        }
068    
069        /**
070         * When Purchase Order Close document has been Processed through Workflow, the general ledger entries are created and the PO
071         * status changes to "CLOSED".
072         * 
073         * @see org.kuali.kfs.module.purap.document.PurchaseOrderDocument#doRouteStatusChange()
074         */
075        @Override
076        public void doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent) {
077            super.doRouteStatusChange(statusChangeEvent);
078    
079            // DOCUMENT PROCESSED
080            if (getDocumentHeader().getWorkflowDocument().stateIsProcessed()) {
081                // generate GL entries
082                SpringContext.getBean(PurapGeneralLedgerService.class).generateEntriesClosePurchaseOrder(this);
083    
084                // update indicators
085                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForApprovedPODocuments(this);
086    
087                // set purap status
088                SpringContext.getBean(PurapService.class).updateStatus(this, PurchaseOrderStatuses.CLOSED);
089            }
090            // DOCUMENT DISAPPROVED
091            else if (getDocumentHeader().getWorkflowDocument().stateIsDisapproved()) {
092                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForDisapprovedChangePODocuments(this);
093            }
094            // DOCUMENT CANCELLED
095            else if (getDocumentHeader().getWorkflowDocument().stateIsCanceled()) {
096                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForCancelledChangePODocuments(this);
097            }
098    
099        }
100    
101        /**
102         * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.kfs.sys.document.AccountingDocument,
103         *      org.kuali.kfs.sys.businessobject.AccountingLine, org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry)
104         */
105        @Override
106        public void customizeExplicitGeneralLedgerPendingEntry(GeneralLedgerPendingEntrySourceDetail postable, GeneralLedgerPendingEntry explicitEntry) {
107            super.customizeExplicitGeneralLedgerPendingEntry(postable, explicitEntry);
108    
109            SpringContext.getBean(PurapGeneralLedgerService.class).customizeGeneralLedgerPendingEntry(this, (AccountingLine)postable, explicitEntry, getPurapDocumentIdentifier(), GL_CREDIT_CODE, PurapDocTypeCodes.PO_DOCUMENT, true);
110    
111            // don't think i should have to override this, but default isn't getting the right PO doc
112            explicitEntry.setFinancialDocumentTypeCode(PurapDocTypeCodes.PO_CLOSE_DOCUMENT);
113            explicitEntry.setFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
114        }
115        
116        @Override
117        public List<GeneralLedgerPendingEntrySourceDetail> getGeneralLedgerPendingEntrySourceDetails() {
118            List<GeneralLedgerPendingEntrySourceDetail> accountingLines = new ArrayList<GeneralLedgerPendingEntrySourceDetail>();
119            if (getGlOnlySourceAccountingLines() != null) {
120                Iterator iter = getGlOnlySourceAccountingLines().iterator();
121                while (iter.hasNext()) {
122                    accountingLines.add((GeneralLedgerPendingEntrySourceDetail) iter.next());
123                }
124            }
125            return accountingLines;
126        }
127    
128    
129    }