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    package org.kuali.kfs.module.purap.document.authorization;
017    
018    import java.util.Set;
019    
020    import org.apache.commons.lang.StringUtils;
021    import org.kuali.kfs.module.purap.PurapAuthorizationConstants;
022    import org.kuali.kfs.module.purap.PurapConstants;
023    import org.kuali.kfs.module.purap.PurapParameterConstants;
024    import org.kuali.kfs.module.purap.PurapAuthorizationConstants.CreditMemoEditMode;
025    import org.kuali.kfs.module.purap.PurapConstants.CreditMemoStatuses;
026    import org.kuali.kfs.module.purap.document.VendorCreditMemoDocument;
027    import org.kuali.kfs.module.purap.document.service.PurapService;
028    import org.kuali.kfs.sys.KFSConstants;
029    import org.kuali.kfs.sys.context.SpringContext;
030    import org.kuali.kfs.sys.service.impl.KfsParameterConstants;
031    import org.kuali.rice.kns.document.Document;
032    import org.kuali.rice.kns.service.ParameterService;
033    import org.kuali.rice.kns.util.ObjectUtils;
034    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
035    
036    
037    public class VendorCreditMemoDocumentPresentationController extends PurchasingAccountsPayableDocumentPresentationController {
038    
039        @Override
040        protected boolean canSave(Document document) {
041            VendorCreditMemoDocument vendorCreditMemoDocument = (VendorCreditMemoDocument) document;
042    
043            if (StringUtils.equals(vendorCreditMemoDocument.getStatusCode(), PurapConstants.CreditMemoStatuses.INITIATE)) {
044                return false;
045            }
046    
047            if (canEditPreExtraction(vendorCreditMemoDocument)) {
048                return true;
049            }
050    
051            return super.canSave(document);
052        }
053    
054        @Override
055        protected boolean canReload(Document document) {
056            VendorCreditMemoDocument vendorCreditMemoDocument = (VendorCreditMemoDocument) document;
057    
058            if (StringUtils.equals(vendorCreditMemoDocument.getStatusCode(), PurapConstants.CreditMemoStatuses.INITIATE)) {
059                return false;
060            }
061            
062            if (canEditPreExtraction(vendorCreditMemoDocument)) {
063                return true;
064            }
065    
066            return super.canReload(document);
067        }
068    
069        @Override
070        protected boolean canCancel(Document document) {
071            //controlling the cancel button through getExtraButtons in CreditMemoForm
072            return false;
073        }
074    
075        @Override
076        protected boolean canDisapprove(Document document) {
077            //disapprove is never allowed for Credit Memo
078            return false;
079        }
080    
081        /**
082         * 
083         * @see org.kuali.rice.kns.document.authorization.DocumentPresentationControllerBase#canEdit(org.kuali.rice.kns.document.Document)
084         */
085        @Override
086        protected boolean canEdit(Document document) {
087            if (SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted((VendorCreditMemoDocument) document)) {
088                return false;
089            }
090    
091            return super.canEdit(document);
092        }
093    
094        /**
095         * 
096         * @see org.kuali.rice.kns.document.authorization.TransactionalDocumentPresentationControllerBase#getEditModes(org.kuali.rice.kns.document.Document)
097         */
098        @Override
099        public Set<String> getEditModes(Document document) {
100            Set<String> editModes = super.getEditModes(document);
101            KualiWorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
102            VendorCreditMemoDocument vendorCreditMemoDocument = (VendorCreditMemoDocument)document;
103    
104            if (canCancel(vendorCreditMemoDocument)) {
105                editModes.add(CreditMemoEditMode.ACCOUNTS_PAYABLE_PROCESSOR_CANCEL);
106            }
107            
108            if (canHold(vendorCreditMemoDocument)) {
109                editModes.add(CreditMemoEditMode.HOLD);
110            }
111    
112            if (canRemoveHold(vendorCreditMemoDocument)) {
113                editModes.add(CreditMemoEditMode.REMOVE_HOLD);
114            }
115    
116            if (SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(vendorCreditMemoDocument)) {
117                editModes.add(CreditMemoEditMode.FULL_DOCUMENT_ENTRY_COMPLETED);
118            }
119            else {
120                if (ObjectUtils.isNotNull(vendorCreditMemoDocument.getPurchaseOrderDocument()) && 
121                        !vendorCreditMemoDocument.isSourceVendor() && 
122                        PurapConstants.PurchaseOrderStatuses.CLOSED.equals(vendorCreditMemoDocument.getPurchaseOrderDocument().getStatusCode())) {
123                    // TODO hjs-is this right? check to see if the checkbox is showing up for non-AP folks
124                    editModes.add(CreditMemoEditMode.ALLOW_REOPEN_PURCHASE_ORDER);
125                }
126            }
127    
128            if (StringUtils.equals(vendorCreditMemoDocument.getStatusCode(), PurapConstants.CreditMemoStatuses.INITIATE)) {
129                editModes.add(CreditMemoEditMode.DISPLAY_INIT_TAB);
130            }
131            
132            if (canEditPreExtraction(vendorCreditMemoDocument)) {
133                editModes.add(CreditMemoEditMode.EDIT_PRE_EXTRACT);
134            }
135    
136            if (!vendorCreditMemoDocument.isSourceDocumentPaymentRequest()) {
137                editModes.add(CreditMemoEditMode.LOCK_VENDOR_ENTRY);
138            }
139    
140            // See if purap tax is enabled
141            boolean salesTaxInd = SpringContext.getBean(ParameterService.class).getIndicatorParameter(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.ENABLE_SALES_TAX_IND);
142            if (salesTaxInd) {
143                editModes.add(PurapAuthorizationConstants.PURAP_TAX_ENABLED);
144                
145                if (vendorCreditMemoDocument.isUseTaxIndicator()) {
146                    // only allow tax editing if doc is not using use tax
147                    editModes.add(CreditMemoEditMode.LOCK_TAX_AMOUNT_ENTRY);
148                }
149                else {
150                    // display the "clear all taxes" button if doc is not using use tax
151                    editModes.add(CreditMemoEditMode.CLEAR_ALL_TAXES);
152                }
153            }
154    
155            // Remove editBank edit mode if the document has been extracted
156            if (vendorCreditMemoDocument.isExtracted()) {
157                editModes.remove(KFSConstants.BANK_ENTRY_EDITABLE_EDITING_MODE);
158            }
159    
160            return editModes;
161        }
162    
163        /**
164         * Determines if the document can be put on hold. Credit memo not already on hold, extracted date is null, and credit memo 
165         * status approved or complete.
166         * 
167         * @param cmDocument - credit memo document to hold.
168         * @return boolean - true if hold can occur, false if not allowed.
169         */
170        protected boolean canHold(VendorCreditMemoDocument cmDocument) {
171            return !cmDocument.isHoldIndicator() && !cmDocument.isExtracted() && !PurapConstants.CreditMemoStatuses.STATUSES_DISALLOWING_HOLD.contains(cmDocument.getStatusCode());
172        }
173    
174        /**
175         * Determines if the document can be taken off hold.  Credit memo must be on hold.
176         * 
177         * @param cmDocument - credit memo document that is on hold.
178         * @return boolean - true if document can be taken off hold, false if it cannot.
179         */
180        protected boolean canRemoveHold(VendorCreditMemoDocument cmDocument) {
181            return cmDocument.isHoldIndicator();
182        }
183    
184        /**
185         * Determines if the document can be canceled. Document can be canceled if not in canceled status already, extracted date is
186         * null, and hold indicator is false.
187         * 
188         * @param cmDocument - credit memo document to cancel.
189         * @return boolean - true if document can be canceled, false if it cannot be.
190         */
191        protected boolean canCancel(VendorCreditMemoDocument cmDocument) {
192            return !CreditMemoStatuses.CANCELLED_STATUSES.contains(cmDocument.getStatusCode()) && !cmDocument.isExtracted() && !cmDocument.isHoldIndicator();
193        }
194    
195        protected boolean canEditPreExtraction(VendorCreditMemoDocument vendorCreditMemoDocument) {
196            return (!vendorCreditMemoDocument.isExtracted() && 
197                    !vendorCreditMemoDocument.getDocumentHeader().getWorkflowDocument().isAdHocRequested() &&
198                    !PurapConstants.CreditMemoStatuses.CANCELLED_STATUSES.contains(vendorCreditMemoDocument.getStatusCode()));
199        }
200        
201    }