View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.select.document.validation.impl;
17  
18  import org.kuali.ole.module.purap.PurapConstants.PRQSDocumentsStrings;
19  import org.kuali.ole.module.purap.PurapPropertyConstants;
20  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
21  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
22  import org.kuali.ole.select.OleSelectConstant;
23  import org.kuali.ole.select.document.OleInvoiceDocument;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.context.SpringContext;
27  import org.kuali.ole.sys.document.validation.GenericValidation;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.ole.vnd.businessobject.OleExchangeRate;
30  import org.kuali.rice.core.api.util.type.KualiDecimal;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  import org.kuali.rice.krad.util.ObjectUtils;
33  
34  import java.math.RoundingMode;
35  import java.util.HashMap;
36  import java.util.Iterator;
37  import java.util.List;
38  import java.util.Map;
39  
40  public class OleInvoiceValidation extends GenericValidation {
41  
42      public boolean validate(AttributedDocumentEvent event) {
43          boolean valid = true;
44          OleInvoiceDocument document = (OleInvoiceDocument) event.getDocument();
45          GlobalVariables.getMessageMap().clearErrorPath();
46          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
47          PurchaseOrderDocument po = null;
48          if (ObjectUtils.isNull(document.getPurchaseOrderIdentifier())) {
49              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.PURCHASE_ORDER_ID);
50              valid &= false;
51          } else {
52              po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(document.getPurchaseOrderIdentifier());
53          }
54          if (ObjectUtils.isNull(document.getInvoiceDate())) {
55              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.INVOICE_DATE, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.INVOICE_DATE);
56              valid &= false;
57          }
58    /*      if (StringUtils.isBlank(document.getInvoiceNumber())) {
59              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.INVOICE_NUMBER, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.INVOICE_NUMBER);
60              valid &= false;
61          }*/
62          if (po != null && po.getVendorDetail().getVendorHeader().getVendorForeignIndicator()) {
63              if (ObjectUtils.isNull(document.getForeignVendorInvoiceAmount())) {
64                  GlobalVariables.getMessageMap().putError(OleSelectConstant.FOREIGN_VENDOR_INVOICE_AMOUNT, OLEKeyConstants.ERROR_REQUIRED, OleSelectConstant.FOREIGN_VENDOR_INVOICE_AMOUNT_STRING);
65                  valid &= false;
66              } else {
67                  Long currencyTypeId = po.getVendorDetail().getCurrencyType().getCurrencyTypeId();
68                  Map documentNumberMap = new HashMap();
69                  documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
70                  org.kuali.rice.krad.service.BusinessObjectService businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
71                  List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
72                  Iterator iterator = exchangeRateList.iterator();
73                  if (iterator.hasNext()) {
74                      OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
75                      document.setVendorInvoiceAmount(new KualiDecimal(document.getForeignVendorInvoiceAmount().divide(tempOleExchangeRate.getExchangeRate(), 4, RoundingMode.HALF_UP)));
76                  }
77  
78              }
79          }
80          if (ObjectUtils.isNull(document.getVendorInvoiceAmount())) {
81              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.VENDOR_INVOICE_AMOUNT, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.VENDOR_INVOICE_AMOUNT);
82              valid &= false;
83          }
84          GlobalVariables.getMessageMap().clearErrorPath();
85          return valid;
86      }
87  
88  }