001/*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 1.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/ecl1.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.select.document.validation.impl;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.module.purap.PurapConstants;
020import org.kuali.ole.select.OleSelectConstant;
021import org.kuali.ole.select.businessobject.OlePaymentRequestItem;
022import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
023import org.kuali.ole.select.constants.OleSelectPropertyConstants;
024import org.kuali.ole.select.document.OlePaymentRequestDocument;
025import org.kuali.ole.select.document.service.OlePaymentRequestFundCheckService;
026import org.kuali.ole.sys.OLEConstants;
027import org.kuali.ole.sys.OLEPropertyConstants;
028import org.kuali.ole.sys.businessobject.SourceAccountingLine;
029import org.kuali.ole.sys.context.SpringContext;
030import org.kuali.ole.sys.document.validation.GenericValidation;
031import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
032import org.kuali.rice.core.api.util.RiceKeyConstants;
033import org.kuali.rice.core.api.util.type.KualiDecimal;
034import org.kuali.rice.core.api.util.type.KualiInteger;
035import org.kuali.rice.krad.service.BusinessObjectService;
036import org.kuali.rice.krad.util.GlobalVariables;
037
038import java.util.HashMap;
039import java.util.List;
040import java.util.Map;
041
042public class OlePaymentRequestItemValidation extends GenericValidation {
043
044    private BusinessObjectService businessObjectService;
045
046    @Override
047    public boolean validate(AttributedDocumentEvent event) {
048        boolean valid = true;
049        boolean isUnordered = false;
050        boolean orderedWithQty = true;
051        int count = 0;
052        OlePaymentRequestDocument document = (OlePaymentRequestDocument) event.getDocument();
053        if ((document.getPaymentMethod() != null) && (document.getPaymentMethod().getPaymentMethodId() != null)) {
054            document.setPaymentMethodId(document.getPaymentMethod().getPaymentMethodId());
055        }
056        List<OlePaymentRequestItem> items = document.getItems();
057        for (OlePaymentRequestItem item : items) {
058            count += 1;
059            if (item.getItemTypeCode().equalsIgnoreCase(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
060                isUnordered = true;
061                if (StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
062                    if (item.getItemQuantity() == null || item.getItemQuantity().isLessEqual(KualiDecimal.ZERO)) {
063                        GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_ITEM_QUANTITY_REQUIRED, "Item " + count);
064                        valid = false;
065                    }
066                }
067            }
068            if (item.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
069                if (StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE) || StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE)) {
070                    if (item.getItemDescription() == null || item.getItemDescription().trim().length() <= 0) {
071                        GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_REQUIRED, "Item " + count);
072                        valid = false;
073                    }
074                }
075                if (item.getItemQuantity() != null && item.getItemQuantity().isGreaterThan(KualiDecimal.ZERO)) {
076                    if (item.getItemNoOfParts() == null || (item.getItemNoOfParts()).isLessEqual(KualiInteger.ZERO)) {
077                        GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_NO_OF_PARTS_REQUIRED, "Item " + count);
078                        valid = false;
079                    }
080                }
081            }
082        }
083        if (!isUnordered) {
084            for (OlePaymentRequestItem item : items) {
085                if (item.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
086                    if (item.getItemQuantity() == null || item.getItemQuantity().isLessEqual(KualiDecimal.ZERO)) {
087                        orderedWithQty = false;
088                    } else {
089                        orderedWithQty = true;
090                    }
091                }
092                if (orderedWithQty) {
093                    break;
094                }
095            }
096        }
097        if (!orderedWithQty) {
098            GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_ATLEAST_ONE_ITEM_QTY_REQUIRED);
099            valid = false;
100        }
101
102
103        List<SourceAccountingLine> sourceAccountingLineList = document.getSourceAccountingLines();
104        for (SourceAccountingLine accLine : sourceAccountingLineList) {
105            Map searchMap = new HashMap();
106            Map<String, Object> key = new HashMap<String, Object>();
107            String chartCode = accLine.getChartOfAccountsCode();
108            String accNo = accLine.getAccountNumber();
109            String objectCd = accLine.getFinancialObjectCode();
110            key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
111            key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
112            String option = null;
113            OleSufficientFundCheck account = getBusinessObjectService().findByPrimaryKey(OleSufficientFundCheck.class,
114                    key);
115            if (account != null) {
116                option = account.getNotificationOption();
117                if (option != null) {
118                    if (option.equals(OLEPropertyConstants.BLOCK_USE)) {
119                        boolean fundCheck = SpringContext.getBean(OlePaymentRequestFundCheckService.class)
120                                .hasSufficientFundCheckRequired(accLine);
121                        if (fundCheck) {
122                            GlobalVariables.getMessageMap().putError(OLEConstants.ERROR_MSG_FOR_INSUFF_FUND,
123                                    RiceKeyConstants.ERROR_CUSTOM,
124                                    OLEConstants.INSUFF_FUND + accLine.getAccountNumber());
125                            valid = false;
126                        }
127                    } else if (option.equals(OLEPropertyConstants.WARNING_MSG)) {
128                        boolean fundcheckRequired = SpringContext.getBean(OlePaymentRequestFundCheckService.class)
129                                .hasSufficientFundCheckRequired(accLine);
130                        if (fundcheckRequired) {
131                            GlobalVariables.getMessageMap().putWarning(PurapConstants.DETAIL_TAB_ERRORS,
132                                    OleSelectPropertyConstants.INSUFF_FUND,
133                                    new String[]{accLine.getAccountNumber()});
134                        }
135                    }
136                }
137            }
138        }
139        return valid;
140
141    }
142
143
144    public void setBusinessObjectService(BusinessObjectService businessObjectService) {
145        this.businessObjectService = businessObjectService;
146    }
147
148    public BusinessObjectService getBusinessObjectService() {
149        if (businessObjectService == null) {
150            businessObjectService = SpringContext.getBean(BusinessObjectService.class);
151        }
152        return businessObjectService;
153    }
154
155}