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_DEBIT_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.PurapWorkflowConstants;
027    import org.kuali.kfs.module.purap.PurapConstants.PurapDocTypeCodes;
028    import org.kuali.kfs.module.purap.document.service.PurapService;
029    import org.kuali.kfs.module.purap.document.service.PurchaseOrderService;
030    import org.kuali.kfs.module.purap.document.service.ReceivingService;
031    import org.kuali.kfs.module.purap.service.PurapGeneralLedgerService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.businessobject.AccountingLine;
034    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry;
035    import org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
036    import org.kuali.kfs.sys.context.SpringContext;
037    import org.kuali.rice.kew.dto.DocumentRouteStatusChangeDTO;
038    import org.kuali.rice.kns.rule.event.KualiDocumentEvent;
039    
040    /**
041     * Purchase Order Amendment Document
042     */
043    public class PurchaseOrderAmendmentDocument extends PurchaseOrderDocument {
044        protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderAmendmentDocument.class);
045    
046        boolean newUnorderedItem; //Used for routing
047        String receivingDeliveryCampusCode; //Used for routing
048        
049        /**
050         * Default constructor.
051         */
052        public PurchaseOrderAmendmentDocument() {
053            super();
054        }
055    
056        
057        @Override
058        public List<Long> getWorkflowEngineDocumentIdsToLock() {
059            return super.getWorkflowEngineDocumentIdsToLock();
060        }
061    
062        /**
063         * When Purchase Order Amendment document has been Processed through Workflow, the general ledger entries are created and the PO
064         * status remains "OPEN".
065         * 
066         * @see org.kuali.kfs.module.purap.document.PurchaseOrderDocument#doRouteStatusChange()
067         */
068       @Override
069            public void doRouteStatusChange(DocumentRouteStatusChangeDTO statusChangeEvent) {
070            super.doRouteStatusChange(statusChangeEvent);
071    
072            // DOCUMENT PROCESSED
073            if (getDocumentHeader().getWorkflowDocument().stateIsProcessed()) {
074                // generate GL entries
075                SpringContext.getBean(PurapGeneralLedgerService.class).generateEntriesApproveAmendPurchaseOrder(this);
076    
077                // update indicators
078                SpringContext.getBean(PurchaseOrderService.class).completePurchaseOrderAmendment(this);
079    
080                // update vendor commodity code by automatically spawning vendor maintenance document
081                SpringContext.getBean(PurchaseOrderService.class).updateVendorCommodityCode(this);
082                
083                // set purap status
084                SpringContext.getBean(PurapService.class).updateStatus(this, PurapConstants.PurchaseOrderStatuses.OPEN);
085    
086            }
087            // DOCUMENT DISAPPROVED
088            else if (getDocumentHeader().getWorkflowDocument().stateIsDisapproved()) {
089                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForDisapprovedChangePODocuments(this);
090                SpringContext.getBean(PurapService.class).saveDocumentNoValidation(this);
091            }
092            // DOCUMENT CANCELED
093            else if (getDocumentHeader().getWorkflowDocument().stateIsCanceled()) {
094                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForCancelledChangePODocuments(this);
095                SpringContext.getBean(PurapService.class).saveDocumentNoValidation(this);
096            }
097       }
098    
099       /**
100        * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.kfs.sys.document.AccountingDocument,
101        *      org.kuali.kfs.sys.businessobject.AccountingLine, org.kuali.kfs.sys.businessobject.GeneralLedgerPendingEntry)
102        */
103       @Override
104       public void customizeExplicitGeneralLedgerPendingEntry(GeneralLedgerPendingEntrySourceDetail postable, GeneralLedgerPendingEntry explicitEntry) {
105           super.customizeExplicitGeneralLedgerPendingEntry(postable, explicitEntry);
106    
107           SpringContext.getBean(PurapGeneralLedgerService.class).customizeGeneralLedgerPendingEntry(this, (AccountingLine)postable, explicitEntry, getPurapDocumentIdentifier(), GL_DEBIT_CODE, PurapDocTypeCodes.PO_DOCUMENT, true);
108    
109           // don't think i should have to override this, but default isn't getting the right PO doc
110           explicitEntry.setFinancialDocumentTypeCode(PurapDocTypeCodes.PO_AMENDMENT_DOCUMENT);
111           explicitEntry.setFinancialDocumentApprovedCode(KFSConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
112       }
113    
114       @Override
115       public List<GeneralLedgerPendingEntrySourceDetail> getGeneralLedgerPendingEntrySourceDetails() {
116           List<GeneralLedgerPendingEntrySourceDetail> accountingLines = new ArrayList<GeneralLedgerPendingEntrySourceDetail>();
117           if (getGlOnlySourceAccountingLines() != null) {
118               Iterator iter = getGlOnlySourceAccountingLines().iterator();
119               while (iter.hasNext()) {
120                   accountingLines.add((GeneralLedgerPendingEntrySourceDetail) iter.next());
121               }
122           }
123           return accountingLines;
124       }
125    
126       
127       @Override
128       public void populateDocumentForRouting() {
129           newUnorderedItem = SpringContext.getBean(PurchaseOrderService.class).hasNewUnorderedItem(this);
130           receivingDeliveryCampusCode = SpringContext.getBean(ReceivingService.class).getReceivingDeliveryCampusCode(this);
131           super.populateDocumentForRouting();
132       }
133    
134        public boolean isNewUnorderedItem() {
135            return newUnorderedItem;
136        }
137        
138        public void setNewUnorderedItem(boolean newUnorderedItem) {
139            this.newUnorderedItem = newUnorderedItem;
140        }
141    
142        public String getReceivingDeliveryCampusCode() {
143            return receivingDeliveryCampusCode;
144        }
145    
146        public void setReceivingDeliveryCampusCode(String receivingDeliveryCampusCode) {
147            this.receivingDeliveryCampusCode = receivingDeliveryCampusCode;
148        }
149        
150        @Override
151        public boolean answerSplitNodeQuestion(String nodeName) throws UnsupportedOperationException {
152            if (nodeName.equals(PurapWorkflowConstants.HAS_NEW_UNORDERED_ITEMS)) return isNewUnorderedItem();
153            throw new UnsupportedOperationException("Cannot answer split question for this node you call \""+nodeName+"\"");
154        } 
155    
156    }