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.web.struts;
017    
018    import java.util.List;
019    
020    import javax.servlet.http.HttpServletRequest;
021    
022    import org.kuali.kfs.module.purap.PurapConstants;
023    import org.kuali.kfs.module.purap.PurapAuthorizationConstants.PaymentRequestEditMode;
024    import org.kuali.kfs.module.purap.PurapConstants.PaymentRequestStatuses;
025    import org.kuali.kfs.module.purap.businessobject.PurApItem;
026    import org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem;
027    import org.kuali.kfs.module.purap.document.PaymentRequestDocument;
028    import org.kuali.kfs.module.purap.document.service.PurapService;
029    import org.kuali.kfs.sys.KFSConstants;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.kuali.rice.kns.document.authorization.DocumentAuthorizer;
032    import org.kuali.rice.kns.service.DataDictionaryService;
033    import org.kuali.rice.kns.service.DocumentHelperService;
034    import org.kuali.rice.kns.service.KualiConfigurationService;
035    import org.kuali.rice.kns.util.GlobalVariables;
036    import org.kuali.rice.kns.util.KNSConstants;
037    import org.kuali.rice.kns.util.ObjectUtils;
038    import org.kuali.rice.kns.web.ui.ExtraButton;
039    import org.kuali.rice.kns.web.ui.HeaderField;
040    import org.kuali.rice.kns.workflow.service.KualiWorkflowDocument;
041    
042    /**
043     * Struts Action Form for Payment Request document.
044     */
045    public class PaymentRequestForm extends AccountsPayableFormBase {
046    
047        /**
048         * Indicates whether tax has been calculated based on the tax area data.
049         * Tax calculation is enforced before preq can be routed for tax approval.
050         */
051        protected boolean calculatedTax;
052    
053        /**
054         * Constructs a PaymentRequestForm instance and sets up the appropriately casted document.
055         */
056        public PaymentRequestForm() {
057            super();
058            this.setNewPurchasingItemLine(setupNewPurchasingItemLine());
059        }
060    
061        @Override
062        protected String getDefaultDocumentTypeName() {
063            return "PREQ";
064        }
065        
066        public boolean isCalculatedTax() {
067            return calculatedTax;
068        }
069    
070        public void setCalculatedTax(boolean calculatedTax) {
071            this.calculatedTax = calculatedTax;
072        }
073    
074        public PaymentRequestDocument getPaymentRequestDocument() {
075            return (PaymentRequestDocument) getDocument();
076        }
077    
078        public void setPaymentRequestDocument(PaymentRequestDocument purchaseOrderDocument) {
079            setDocument(purchaseOrderDocument);
080        }
081    
082        @Override
083        public void populateHeaderFields(KualiWorkflowDocument workflowDocument) {
084            super.populateHeaderFields(workflowDocument);
085            if (ObjectUtils.isNotNull(this.getPaymentRequestDocument().getPurapDocumentIdentifier())) {
086                getDocInfo().add(new HeaderField("DataDictionary.PaymentRequestDocument.attributes.purapDocumentIdentifier", ((PaymentRequestDocument) this.getDocument()).getPurapDocumentIdentifier().toString()));
087            }
088            else {
089                getDocInfo().add(new HeaderField("DataDictionary.PaymentRequestDocument.attributes.purapDocumentIdentifier", "Not Available"));
090            }
091            if (ObjectUtils.isNotNull(this.getPaymentRequestDocument().getStatus())) {
092                getDocInfo().add(new HeaderField("DataDictionary.PaymentRequestDocument.attributes.statusCode", ((PaymentRequestDocument) this.getDocument()).getStatus().getStatusDescription()));
093            }
094            else {
095                getDocInfo().add(new HeaderField("DataDictionary.PaymentRequestDocument.attributes.statusCode", "Not Available"));
096            }
097        }
098        
099        /**
100         * @see org.kuali.kfs.module.purap.document.web.struts.PurchasingFormBase#setupNewPurchasingItemLine()
101         */
102        @Override
103        public PurApItem setupNewPurchasingItemLine() {
104            return new PurchaseOrderItem();
105        }
106    
107        /**
108         * Build additional payment request specific buttons and set extraButtons list.
109         * 
110         * @return - list of extra buttons to be displayed to the user
111         */
112        @Override
113        public List<ExtraButton> getExtraButtons() {        
114            extraButtons.clear(); // clear out the extra buttons array
115            PaymentRequestDocument paymentRequestDocument = this.getPaymentRequestDocument();
116            String externalImageURL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.RICE_EXTERNALIZABLE_IMAGES_URL_KEY);
117            String appExternalImageURL = SpringContext.getBean(KualiConfigurationService.class).getPropertyString(KFSConstants.EXTERNALIZABLE_IMAGES_URL_KEY);
118            
119            if (canContinue()) {
120                addExtraButton("methodToCall.continuePREQ", externalImageURL + "buttonsmall_continue.gif", "Continue");
121                addExtraButton("methodToCall.clearInitFields", externalImageURL + "buttonsmall_clear.gif", "Clear");
122            }
123            else {
124                if (getEditingMode().containsKey(PaymentRequestEditMode.HOLD)) {
125                    addExtraButton("methodToCall.addHoldOnPayment", appExternalImageURL + "buttonsmall_hold.gif", "Hold");
126                }
127    
128                if (getEditingMode().containsKey(PaymentRequestEditMode.REMOVE_HOLD) && paymentRequestDocument.isHoldIndicator()) {
129                    addExtraButton("methodToCall.removeHoldFromPayment", appExternalImageURL + "buttonsmall_removehold.gif", "Remove");
130                }
131    
132                if (getEditingMode().containsKey(PaymentRequestEditMode.REQUEST_CANCEL)) {
133                    addExtraButton("methodToCall.requestCancelOnPayment", appExternalImageURL + "buttonsmall_requestcancel.gif", "Cancel");
134                }
135    
136                if (getEditingMode().containsKey(PaymentRequestEditMode.REMOVE_REQUEST_CANCEL) && paymentRequestDocument.isPaymentRequestedCancelIndicator()) {
137                    addExtraButton("methodToCall.removeCancelRequestFromPayment", appExternalImageURL + "buttonsmall_remreqcanc.gif", "Remove");
138                }
139    
140                if (canCalculate()) {
141                    addExtraButton("methodToCall.calculate", appExternalImageURL + "buttonsmall_calculate.gif", "Calculate");
142                }
143                
144                if (getEditingMode().containsKey(PaymentRequestEditMode.ACCOUNTS_PAYABLE_PROCESSOR_CANCEL) ||
145                        getEditingMode().containsKey(PaymentRequestEditMode.ACCOUNTS_PAYABLE_MANAGER_CANCEL)) {
146                    if (PurapConstants.PurchaseOrderStatuses.CLOSED.equals(paymentRequestDocument.getPurchaseOrderDocument().getStatusCode())) {
147                        //if the PO is CLOSED, show the 'open order' button; but only if there are no pending actions on the PO
148                        if (!paymentRequestDocument.getPurchaseOrderDocument().isPendingActionIndicator()) {
149                            addExtraButton("methodToCall.reopenPo", appExternalImageURL + "buttonsmall_openorder.gif", "Reopen PO");
150                        }
151                    }
152                    else {
153                        addExtraButton("methodToCall.cancel", externalImageURL + "buttonsmall_cancel.gif", "Cancel");
154                    }
155                }
156            }
157    
158            return extraButtons;
159        }
160        
161        /**
162         * Determines whether the current user can continue creating or clear fields of the payment request in initial status. Conditions:
163         * - the Payment Request must be in the INITIATE state; and
164         * - the user must have the authorization to initiate a Payment Request.
165         * 
166         * @return True if the current user can continue creating or clear fields of the initiated Payment Request.
167         */
168        public boolean canContinue() {
169            // preq must be in initiated status
170            boolean can = PaymentRequestStatuses.INITIATE.equals(getPaymentRequestDocument().getStatusCode());
171            
172            // check user authorization
173            if (can) {
174                DocumentAuthorizer documentAuthorizer = SpringContext.getBean(DocumentHelperService.class).getDocumentAuthorizer(getPaymentRequestDocument());
175                can = documentAuthorizer.canInitiate(KFSConstants.FinancialDocumentTypeCodes.PAYMENT_REQUEST, GlobalVariables.getUserSession().getPerson());
176            }
177                  
178            return can;        
179        }    
180    
181        /**
182         * Determine whether the current user can calculate the paymentRequest. Conditions:
183         * - Payment Request is not FullDocumentEntryCompleted, and
184         * - current user has the permission to edit the document.
185         * 
186         * @return True if the current user can calculate the Payment Request.
187         */
188        public boolean canCalculate() {
189            // preq must not be FullDocumentEntryCompleted
190            boolean can = !SpringContext.getBean(PurapService.class).isFullDocumentEntryCompleted(getPaymentRequestDocument());
191            
192            // check user authorization: whoever can edit can calculate
193            can = can && documentActions.containsKey(KNSConstants.KUALI_ACTION_CAN_EDIT);
194    
195            //FIXME this is temporary so that calculate will show up at tax
196            can = can || editingMode.containsKey(PaymentRequestEditMode.TAX_AREA_EDITABLE);
197            
198            return can;
199        }
200    
201        /**
202         * @see org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase#shouldMethodToCallParameterBeUsed(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
203         */
204        @Override
205        public boolean shouldMethodToCallParameterBeUsed(String methodToCallParameterName, String methodToCallParameterValue, HttpServletRequest request) {
206            if (KNSConstants.DISPATCH_REQUEST_PARAMETER.equals(methodToCallParameterName) && 
207               ("changeUseTaxIndicator".equals(methodToCallParameterValue))) {
208                return true;
209            }
210            return super.shouldMethodToCallParameterBeUsed(methodToCallParameterName, methodToCallParameterValue, request);
211        }
212    
213    }
214