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