001/*
002 * Copyright 2009 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 */
016package org.kuali.ole.module.purap.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.module.purap.PurapConstants.PaymentRequestStatuses;
021import org.kuali.ole.module.purap.PurapKeyConstants;
022import org.kuali.ole.module.purap.PurapParameterConstants;
023import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
024import org.kuali.ole.module.purap.businessobject.PurApItem;
025import org.kuali.ole.module.purap.document.PaymentRequestDocument;
026import org.kuali.ole.sys.context.SpringContext;
027import org.kuali.ole.sys.document.AccountingDocument;
028import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
029import org.kuali.ole.sys.service.impl.OleParameterConstants;
030import org.kuali.rice.coreservice.framework.parameter.ParameterService;
031import org.kuali.rice.krad.service.DataDictionaryService;
032import org.kuali.rice.krad.util.GlobalVariables;
033import org.kuali.rice.krad.util.ObjectUtils;
034
035public class PaymentRequestNonZeroAccountingLineAmountValidation extends PurchasingAccountsPayableAccountingLineAccessibleValidation {
036
037    private PurApItem itemForValidation;
038    private ParameterService parameterService;
039
040    public boolean validate(AttributedDocumentEvent event) {
041        boolean valid = true;
042        String status = ((PaymentRequestDocument) event.getDocument()).getApplicationDocumentStatus();
043
044        AccountingDocument accountingDocument = (AccountingDocument) event.getDocument();
045        this.setAccountingDocumentForValidation(accountingDocument);
046        this.setDataDictionaryService(SpringContext.getBean(DataDictionaryService.class));
047
048        //Do this for AFOA only
049        if (StringUtils.equals(PaymentRequestStatuses.APPDOC_AWAITING_FISCAL_REVIEW, status)) {
050            for (PurApAccountingLine acct : itemForValidation.getSourceAccountingLines()) {
051                this.setAccountingLineForValidation(acct);
052                final boolean lineIsAccessible = lookupAccountingLineAuthorizer().hasEditPermissionOnAccountingLine(accountingDocument, acct, getAccountingLineCollectionProperty(), GlobalVariables.getUserSession().getPerson(), true);
053                // if the current logged in user has edit permissions on this line then validate the line..
054                if (lineIsAccessible) {
055                    //if amount is null, throw an error.  If amount is zero, check system parameter and determine
056                    //if the line can be approved.
057                    if (ObjectUtils.isNull(acct.getAmount())) {
058                        GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_AMOUNT_INVALID, itemForValidation.getItemIdentifierString());
059                        valid &= false;
060                    } else {
061                        if (acct.getAmount().isZero()) {
062                            if (!canApproveAccountingLinesWithZeroAmount()) {
063                                GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_AMOUNT_INVALID, itemForValidation.getItemIdentifierString());
064                                valid &= false;
065                            }
066                        }
067                    }
068                }
069            }
070        }
071
072        return valid;
073    }
074
075    /**
076     * checks if an accounting line with zero dollar amount can be approved.  This will check
077     * the system parameter APPROVE_ACCOUNTING_LINES_WITH_ZERO_DOLLAR_AMOUNT_IND and determines if the
078     * line can be approved or not.
079     *
080     * @return true if the system parameter value is Y else returns N.
081     */
082    public boolean canApproveAccountingLinesWithZeroAmount() {
083        boolean canApproveLine = false;
084
085        // get parameter to see if accounting line with zero dollar amount can be approved.
086        String approveZeroAmountLine = SpringContext.getBean(ParameterService.class).getParameterValueAsString(OleParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.APPROVE_ACCOUNTING_LINES_WITH_ZERO_DOLLAR_AMOUNT_IND);
087
088        if ("Y".equalsIgnoreCase(approveZeroAmountLine)) {
089            return true;
090        }
091
092        return canApproveLine;
093    }
094
095    public PurApItem getItemForValidation() {
096        return itemForValidation;
097    }
098
099    public void setItemForValidation(PurApItem itemForValidation) {
100        this.itemForValidation = itemForValidation;
101    }
102
103    /**
104     * Gets the parameterService attribute.
105     *
106     * @return Returns the parameterService
107     */
108
109    public ParameterService getParameterService() {
110        return parameterService;
111    }
112
113    /**
114     * Sets the parameterService attribute.
115     *
116     * @param parameterService The parameterService to set.
117     */
118    public void setParameterService(ParameterService parameterService) {
119        this.parameterService = parameterService;
120    }
121}