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.businessobject.PurchaseOrderView;
023import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
024import org.kuali.ole.module.purap.service.PurapGeneralLedgerService;
025import org.kuali.ole.sys.OLEConstants;
026import org.kuali.ole.sys.businessobject.AccountingLine;
027import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
028import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntrySourceDetail;
029import org.kuali.ole.sys.context.SpringContext;
030import org.kuali.rice.kew.api.exception.WorkflowException;
031import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
032import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
033import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
034
035import java.util.ArrayList;
036import java.util.Iterator;
037import java.util.List;
038
039import static org.kuali.ole.sys.OLEConstants.GL_CREDIT_CODE;
040
041/**
042 * Purchase Order Close Document
043 */
044public class PurchaseOrderCloseDocument extends PurchaseOrderDocument {
045    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderCloseDocument.class);
046
047    /**
048     * General Ledger pending entries are not created on save for this document. They are created when the document has been finally
049     * processed. Overriding this method so that entries are not created yet.
050     *
051     * @see org.kuali.ole.module.purap.document.PurchaseOrderDocument#prepareForSave(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
052     */
053    @Override
054    public void prepareForSave(KualiDocumentEvent event) {
055        LOG.info("prepareForSave(KualiDocumentEvent) do not create gl entries");
056        setSourceAccountingLines(new ArrayList());
057        setGeneralLedgerPendingEntries(new ArrayList());
058    }
059
060    /**
061     * When Purchase Order Close document has been Processed through Workflow, the general ledger entries are created and the PO
062     * status changes to "CLOSED".
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).generateEntriesClosePurchaseOrder(this);
075
076                // update indicators
077                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForApprovedPODocuments(this);
078
079                // for app doc status
080                updateAndSaveAppDocStatus(PurchaseOrderStatuses.APPDOC_CLOSED);
081            }
082            // DOCUMENT DISAPPROVED
083            else if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isDisapproved()) {
084                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForDisapprovedChangePODocuments(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(PurapConstants.PurchaseOrderStatuses.getPurchaseOrderAppDocDisapproveStatuses().get(reqStatus));
091
092                } catch (WorkflowException e) {
093                    logAndThrowRuntimeException("Error saving routing data while saving App Doc Status " + getDocumentNumber(), e);
094                }
095
096            }
097            // DOCUMENT CANCELLED
098            else if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isCanceled()) {
099                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForCancelledChangePODocuments(this);
100                // for app doc status
101                updateAndSaveAppDocStatus(PurchaseOrderStatuses.APPDOC_CLOSED);
102            }
103        } catch (WorkflowException e) {
104            logAndThrowRuntimeException("Error saving routing data while saving document with id " + getDocumentNumber(), e);
105        }
106    }
107
108    /**
109     * @see org.kuali.module.purap.rules.PurapAccountingDocumentRuleBase#customizeExplicitGeneralLedgerPendingEntry(org.kuali.ole.sys.document.AccountingDocument,
110     *      org.kuali.ole.sys.businessobject.AccountingLine, org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry)
111     */
112    @Override
113    public void customizeExplicitGeneralLedgerPendingEntry(GeneralLedgerPendingEntrySourceDetail postable, GeneralLedgerPendingEntry explicitEntry) {
114        super.customizeExplicitGeneralLedgerPendingEntry(postable, explicitEntry);
115
116        SpringContext.getBean(PurapGeneralLedgerService.class).customizeGeneralLedgerPendingEntry(this, (AccountingLine) postable, explicitEntry, getPurapDocumentIdentifier(), GL_CREDIT_CODE, PurapDocTypeCodes.PO_DOCUMENT, true);
117
118        // don't think i should have to override this, but default isn't getting the right PO doc
119        explicitEntry.setFinancialDocumentTypeCode(PurapDocTypeCodes.PO_CLOSE_DOCUMENT);
120        explicitEntry.setFinancialDocumentApprovedCode(OLEConstants.PENDING_ENTRY_APPROVED_STATUS_CODE.APPROVED);
121    }
122
123    @Override
124    public List<GeneralLedgerPendingEntrySourceDetail> getGeneralLedgerPendingEntrySourceDetails() {
125        List<GeneralLedgerPendingEntrySourceDetail> accountingLines = new ArrayList<GeneralLedgerPendingEntrySourceDetail>();
126        if (getGlOnlySourceAccountingLines() != null) {
127            Iterator iter = getGlOnlySourceAccountingLines().iterator();
128            while (iter.hasNext()) {
129                accountingLines.add((GeneralLedgerPendingEntrySourceDetail) iter.next());
130            }
131        }
132        return accountingLines;
133    }
134
135    // MSU Contribution OLEMI-8643 DTT-3800 OLECNTRB-956
136    @Override
137    public List<String> getWorkflowEngineDocumentIdsToLock() {
138        List<String> docIdStrings = new ArrayList<String>();
139        docIdStrings.add(getDocumentNumber());
140        String currentDocumentTypeName = this.getFinancialSystemDocumentHeader().getWorkflowDocument()
141                .getDocumentTypeName();
142
143        List<PurchaseOrderView> relatedPoViews = getRelatedViews().getRelatedPurchaseOrderViews();
144        for (PurchaseOrderView poView : relatedPoViews) {
145            if (poView.isPurchaseOrderCurrentIndicator()) {
146                docIdStrings.add(poView.getDocumentNumber());
147            }
148        }
149        if (LOG.isDebugEnabled()) {
150            LOG.debug("***** getWorkflowEngineDocumentIdsToLock(" + this.documentNumber + ") = '" + docIdStrings + "'");
151        }
152        return docIdStrings;
153    }
154
155}