001/*
002 * Copyright 2007 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
017package org.kuali.ole.module.purap.document;
018
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.PurapConstants.PurapDocTypeCodes;
021import org.kuali.ole.module.purap.PurapConstants.PurchaseOrderStatuses;
022import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
023import org.kuali.ole.module.purap.service.PurapGeneralLedgerService;
024import org.kuali.ole.sys.OLEConstants;
025import org.kuali.ole.sys.businessobject.AccountingLine;
026import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
027import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
028import org.kuali.ole.sys.context.SpringContext;
029import org.kuali.rice.kew.api.exception.WorkflowException;
030import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
031import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
032import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
033
034import java.util.ArrayList;
035import java.util.Iterator;
036import java.util.List;
037
038import static org.kuali.ole.sys.OLEConstants.GL_DEBIT_CODE;
039
040/**
041 * Purchase Order Reopen Document
042 */
043public class PurchaseOrderReopenDocument extends PurchaseOrderDocument {
044    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderReopenDocument.class);
045
046    /**
047     * General Ledger pending entries are not created on save for this document. They are created when the document has been finally
048     * processed. Overriding this method so that entries are not created yet.
049     *
050     * @see org.kuali.ole.module.purap.document.PurchaseOrderDocument#prepareForSave(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
051     */
052    @Override
053    public void prepareForSave(KualiDocumentEvent event) {
054        LOG.info("prepareForSave(KualiDocumentEvent) do not create gl entries");
055       // setSourceAccountingLines(new ArrayList());
056       // setGeneralLedgerPendingEntries(new ArrayList());
057        super.prepareForSave(event);
058    }
059
060    /**
061     * When Purchase Order Reopen document has been processed through Workflow, the general ledger entries are created and the PO
062     * status changes to "OPEN".
063     *
064     * @see org.kuali.ole.module.purap.document.PurchaseOrderDocument#doRouteStatusChange()
065     */
066    @Override
067    public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
068        super.doRouteStatusChange(statusChangeEvent);
069
070        try {
071            // DOCUMENT PROCESSED
072            if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isProcessed()) {
073                // generate GL entries
074                SpringContext.getBean(PurapGeneralLedgerService.class).generateEntriesReopenPurchaseOrder(this);
075
076                // update indicators
077                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForApprovedPODocuments(this);
078
079                // set purap status
080                updateAndSaveAppDocStatus(PurchaseOrderStatuses.APPDOC_OPEN);
081            }
082            // DOCUMENT DISAPPROVED
083            else if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isDisapproved()) {
084                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForDisapprovedReopenPODocuments(this);
085
086                // for app doc status
087                try {
088                    String nodeName = SpringContext.getBean(WorkflowDocumentService.class).getCurrentRouteLevelName(this.getFinancialSystemDocumentHeader().getWorkflowDocument());
089                    String reqStatus = PurapConstants.PurchaseOrderStatuses.getPurchaseOrderAppDocDisapproveStatuses().get(nodeName);
090                    updateAndSaveAppDocStatus(reqStatus);
091                } catch (WorkflowException e) {
092                    logAndThrowRuntimeException("Error saving routing data while saving App Doc Status " + getDocumentNumber(), e);
093                }
094            }
095            // DOCUMENT CANCELED
096            else if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isCanceled()) {
097                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForCancelledReopenPODocuments(this);
098                // for app doc status
099                updateAndSaveAppDocStatus(PurchaseOrderStatuses.APPDOC_CANCELLED);
100            }
101        } catch (WorkflowException e) {
102            logAndThrowRuntimeException("Error saving routing data while saving document with id " + getDocumentNumber(), e);
103        }
104    }
105
106    /**
107     * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.ole.sys.document.AccountingDocument,
108     *      org.kuali.ole.sys.businessobject.AccountingLine, org.kuali.ole.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_DEBIT_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_REOPEN_DOCUMENT);
118        explicitEntry.setFinancialDocumentApprovedCode(OLEConstants.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
134}