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.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapConstants.PRQSDocumentsStrings;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
22  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
23  import org.kuali.ole.select.OleSelectConstant;
24  import org.kuali.ole.select.businessobject.OleInvoiceItem;
25  import org.kuali.ole.select.document.OleInvoiceDocument;
26  import org.kuali.ole.select.document.service.OleInvoiceService;
27  import org.kuali.ole.sys.OLEKeyConstants;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.document.validation.GenericValidation;
31  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
32  import org.kuali.ole.vnd.businessobject.OleExchangeRate;
33  import org.kuali.rice.core.api.util.type.KualiDecimal;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.rice.krad.util.ObjectUtils;
36  
37  import javax.swing.*;
38  import java.math.RoundingMode;
39  import java.util.HashMap;
40  import java.util.Iterator;
41  import java.util.List;
42  import java.util.Map;
43  
44  public class OleInvoiceValidation extends GenericValidation {
45  
46      private boolean currencyTypeIndicator = true;
47  
48      public boolean validate(AttributedDocumentEvent event) {
49          boolean valid = true;
50          OleInvoiceDocument document = (OleInvoiceDocument) event.getDocument();
51          GlobalVariables.getMessageMap().clearErrorPath();
52          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
53          PurchaseOrderDocument po = null;
54          if (ObjectUtils.isNull(document.getPurchaseOrderIdentifier())) {
55              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.PURCHASE_ORDER_ID);
56              valid &= false;
57          } else {
58              po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(document.getPurchaseOrderIdentifier());
59          }
60          if (ObjectUtils.isNull(document.getInvoiceDate())) {
61              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.INVOICE_DATE, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.INVOICE_DATE);
62              valid &= false;
63          }
64    /*      if (StringUtils.isBlank(document.getInvoiceNumber())) {
65              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.INVOICE_NUMBER, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.INVOICE_NUMBER);
66              valid &= false;
67          }*/
68          if (document.getInvoiceCurrencyType()!=null){
69              String currencyType = SpringContext.getBean(OleInvoiceService.class).getCurrencyType(document.getInvoiceCurrencyType());
70              if (StringUtils.isNotBlank(currencyType)) {
71                  if(currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
72                      currencyTypeIndicator=true;
73                  }
74                  else{
75                      currencyTypeIndicator=false;
76                  }
77              }
78          }
79  
80          if (!currencyTypeIndicator) {
81              if (ObjectUtils.isNull(document.getForeignVendorInvoiceAmount())) {
82                  GlobalVariables.getMessageMap().putError(OleSelectConstant.FOREIGN_VENDOR_INVOICE_AMOUNT, OLEKeyConstants.ERROR_REQUIRED, OleSelectConstant.FOREIGN_VENDOR_INVOICE_AMOUNT_STRING);
83                  valid &= false;
84              } else {
85                  Long currencyTypeId = po.getVendorDetail().getCurrencyType().getCurrencyTypeId();
86                  Map documentNumberMap = new HashMap();
87                  documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
88                  org.kuali.rice.krad.service.BusinessObjectService businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
89                  List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
90                  Iterator iterator = exchangeRateList.iterator();
91                  if (iterator.hasNext()) {
92                      OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
93                      document.setVendorInvoiceAmount(new KualiDecimal(document.getForeignVendorInvoiceAmount().divide(tempOleExchangeRate.getExchangeRate(), 4, RoundingMode.HALF_UP)));
94                  }
95  
96              }
97          }
98          if (ObjectUtils.isNull(document.getVendorInvoiceAmount())) {
99              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.VENDOR_INVOICE_AMOUNT, OLEKeyConstants.ERROR_REQUIRED, PRQSDocumentsStrings.VENDOR_INVOICE_AMOUNT);
100             valid &= false;
101         }
102 
103 
104             GlobalVariables.getMessageMap().clearErrorPath();
105         return valid;
106     }
107 
108 }