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.PurchaseOrderStatuses;
021import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
022import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
023import org.kuali.ole.sys.context.SpringContext;
024import org.kuali.rice.core.api.datetime.DateTimeService;
025import org.kuali.rice.core.api.util.type.KualiDecimal;
026import org.kuali.rice.kew.api.exception.WorkflowException;
027import org.kuali.rice.kew.framework.postprocessor.DocumentRouteStatusChange;
028import org.kuali.rice.krad.rules.rule.event.KualiDocumentEvent;
029import org.kuali.rice.krad.workflow.service.WorkflowDocumentService;
030
031import java.math.BigDecimal;
032import java.util.List;
033
034/**
035 * Purchase Order Retransmit Document
036 */
037public class PurchaseOrderRetransmitDocument extends PurchaseOrderDocument {
038    protected static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PurchaseOrderRetransmitDocument.class);
039
040    protected boolean shouldDisplayRetransmitTab;
041
042    /**
043     * Default constructor.
044     */
045    public PurchaseOrderRetransmitDocument() {
046        super();
047    }
048
049    /**
050     * General Ledger pending entries are not created for this document. Overriding this method so that entries are not created.
051     *
052     * @see org.kuali.ole.module.purap.document.PurchaseOrderDocument#customPrepareForSave(org.kuali.rice.krad.rule.event.KualiDocumentEvent)
053     */
054    @Override
055    public void customPrepareForSave(KualiDocumentEvent event) {
056        // do not set the accounts in sourceAccountingLines; this document should not create GL entries
057    }
058
059    /**
060     * Adds up the total amount of the items selected by the user for retransmit, then return the amount.
061     *
062     * @return KualiDecimal the total amount of the items selected by the user for retransmit.
063     */
064    public KualiDecimal getTotalDollarAmountForRetransmit() {
065        // We should only add up the amount of the items that were selected for retransmit.
066        KualiDecimal total = new KualiDecimal(BigDecimal.ZERO);
067        for (PurchaseOrderItem item : (List<PurchaseOrderItem>) getItems()) {
068            if (item.isItemSelectedForRetransmitIndicator()) {
069                KualiDecimal totalAmount = item.getTotalAmount();
070                KualiDecimal itemTotal = (totalAmount != null) ? totalAmount : KualiDecimal.ZERO;
071                total = total.add(itemTotal);
072            }
073        }
074
075        return total;
076    }
077
078    public KualiDecimal getTotalPreTaxDollarAmountForRetransmit() {
079        // We should only add up the amount of the items that were selected for retransmit.
080        KualiDecimal total = new KualiDecimal(BigDecimal.ZERO);
081        for (PurchaseOrderItem item : (List<PurchaseOrderItem>) getItems()) {
082            if (item.isItemSelectedForRetransmitIndicator()) {
083                KualiDecimal extendedPrice = item.getExtendedPrice();
084                KualiDecimal itemTotal = (extendedPrice != null) ? extendedPrice : KualiDecimal.ZERO;
085                total = total.add(itemTotal);
086            }
087        }
088
089        return total;
090    }
091
092    public KualiDecimal getTotalTaxDollarAmountForRetransmit() {
093        // We should only add up the amount of the items that were selected for retransmit.
094        KualiDecimal total = new KualiDecimal(BigDecimal.ZERO);
095        for (PurchaseOrderItem item : (List<PurchaseOrderItem>) getItems()) {
096            if (item.isItemSelectedForRetransmitIndicator()) {
097                KualiDecimal taxAmount = item.getItemTaxAmount();
098                KualiDecimal itemTotal = (taxAmount != null) ? taxAmount : KualiDecimal.ZERO;
099                total = total.add(itemTotal);
100            }
101        }
102
103        return total;
104    }
105
106    /**
107     * When Purchase Order Retransmit document has been Processed through Workflow, the PO status remains to "OPEN" and the last
108     * transmit date is updated.
109     *
110     * @see org.kuali.ole.module.purap.document.PurchaseOrderDocument#doRouteStatusChange()
111     */
112    @Override
113    public void doRouteStatusChange(DocumentRouteStatusChange statusChangeEvent) {
114        super.doRouteStatusChange(statusChangeEvent);
115
116        try {
117            // DOCUMENT PROCESSED
118            if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isProcessed()) {
119                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForApprovedPODocuments(this);
120                setPurchaseOrderLastTransmitTimestamp(SpringContext.getBean(DateTimeService.class).getCurrentTimestamp());
121                updateAndSaveAppDocStatus(PurchaseOrderStatuses.APPDOC_OPEN);
122            }
123            // DOCUMENT DISAPPROVED
124            else if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isDisapproved()) {
125                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForDisapprovedChangePODocuments(this);
126
127                // for app doc status
128                try {
129                    String nodeName = SpringContext.getBean(WorkflowDocumentService.class).getCurrentRouteLevelName(this.getFinancialSystemDocumentHeader().getWorkflowDocument());
130                    String reqStatus = PurapConstants.PurchaseOrderStatuses.getPurchaseOrderAppDocDisapproveStatuses().get(nodeName);
131                    updateAndSaveAppDocStatus(PurapConstants.PurchaseOrderStatuses.getPurchaseOrderAppDocDisapproveStatuses().get(reqStatus));
132                } catch (WorkflowException e) {
133                    logAndThrowRuntimeException("Error saving routing data while saving App Doc Status " + getDocumentNumber(), e);
134                }
135            }
136            // DOCUMENT CANCELED
137            else if (this.getFinancialSystemDocumentHeader().getWorkflowDocument().isCanceled()) {
138                SpringContext.getBean(PurchaseOrderService.class).setCurrentAndPendingIndicatorsForCancelledChangePODocuments(this);
139                // for app doc status
140                updateAndSaveAppDocStatus(PurapConstants.PurchaseOrderStatuses.APPDOC_CANCELLED);
141            }
142        } catch (WorkflowException e) {
143            logAndThrowRuntimeException("Error saving routing data while saving document with id " + getDocumentNumber(), e);
144        }
145    }
146
147    public boolean isShouldDisplayRetransmitTab() {
148        return shouldDisplayRetransmitTab;
149    }
150
151    public void setShouldDisplayRetransmitTab(boolean shouldDisplayRetransmitTab) {
152        this.shouldDisplayRetransmitTab = shouldDisplayRetransmitTab;
153    }
154
155}