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.PREQDocumentsStrings;
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.select.document.OlePaymentRequestDocument;
25  import org.kuali.ole.select.document.service.OleInvoiceService;
26  import org.kuali.ole.sys.OLEConstants;
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.service.KRADServiceLocator;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  
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 OlePaymentRequestInvoiceValidation extends GenericValidation {
45      private boolean currencyTypeIndicator = true;
46  
47      public boolean validate(AttributedDocumentEvent event) {
48          boolean valid = true;
49          OlePaymentRequestDocument document = (OlePaymentRequestDocument) event.getDocument();
50          GlobalVariables.getMessageMap().clearErrorPath();
51          GlobalVariables.getMessageMap().addToErrorPath(OLEPropertyConstants.DOCUMENT);
52          PurchaseOrderDocument po = null;
53          if (ObjectUtils.isNull(document.getPurchaseOrderIdentifier())) {
54              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.PURCHASE_ORDER_IDENTIFIER, OLEKeyConstants.ERROR_REQUIRED, PREQDocumentsStrings.PURCHASE_ORDER_ID);
55              valid &= false;
56          } else {
57              po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(document.getPurchaseOrderIdentifier());
58          }
59          if (ObjectUtils.isNull(document.getInvoiceDate())) {
60              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.INVOICE_DATE, OLEKeyConstants.ERROR_REQUIRED, PREQDocumentsStrings.INVOICE_DATE);
61              valid &= false;
62          }
63          /*if (StringUtils.isBlank(document.getInvoiceNumber())) {
64              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.INVOICE_NUMBER, OLEKeyConstants.ERROR_REQUIRED, PREQDocumentsStrings.INVOICE_NUMBER);
65              valid &= false;
66          }*/
67          if (po.getVendorDetail().getCurrencyType()!=null){
68              Integer invoiceIdentifier = document.getInvoiceIdentifier();
69              Map invoiceIdentifierMap = new HashMap();
70              invoiceIdentifierMap.put(OLEConstants.PUR_DOC_IDENTIFIER, invoiceIdentifier);
71              OleInvoiceDocument oleInvoiceDocument = (OleInvoiceDocument) KRADServiceLocator.getBusinessObjectService().findByPrimaryKey(OleInvoiceDocument.class, invoiceIdentifierMap);
72              if (ObjectUtils.isNotNull(oleInvoiceDocument)) {
73                  Long invoiceCurrencyTypeId = oleInvoiceDocument.getInvoiceCurrencyTypeId();
74                  String currencyType = SpringContext.getBean(OleInvoiceService.class).getCurrencyType(invoiceCurrencyTypeId.toString());
75                  if(currencyType.equalsIgnoreCase(OleSelectConstant.CURRENCY_TYPE_NAME)){
76                      currencyTypeIndicator=true;
77                  }
78                  else{
79                      currencyTypeIndicator=false;
80                  }
81              }
82          }
83  
84          if (po != null && (!currencyTypeIndicator)) {
85              if (ObjectUtils.isNull(document.getForeignVendorInvoiceAmount())) {
86                  GlobalVariables.getMessageMap().putError(OleSelectConstant.FOREIGN_VENDOR_INVOICE_AMOUNT, OLEKeyConstants.ERROR_REQUIRED, OleSelectConstant.FOREIGN_VENDOR_INVOICE_AMOUNT_STRING);
87                  valid &= false;
88              } else {
89                  Long currencyTypeId = po.getVendorDetail().getCurrencyType().getCurrencyTypeId();
90                  Map documentNumberMap = new HashMap();
91                  documentNumberMap.put(OleSelectConstant.CURRENCY_TYPE_ID, currencyTypeId);
92                  org.kuali.rice.krad.service.BusinessObjectService businessObjectService = SpringContext.getBean(org.kuali.rice.krad.service.BusinessObjectService.class);
93                  List<OleExchangeRate> exchangeRateList = (List) businessObjectService.findMatchingOrderBy(OleExchangeRate.class, documentNumberMap, OleSelectConstant.EXCHANGE_RATE_DATE, false);
94                  Iterator iterator = exchangeRateList.iterator();
95                  if (iterator.hasNext()) {
96                      OleExchangeRate tempOleExchangeRate = (OleExchangeRate) iterator.next();
97                      document.setVendorInvoiceAmount(new KualiDecimal(document.getForeignVendorInvoiceAmount().divide(tempOleExchangeRate.getExchangeRate(), 4, RoundingMode.HALF_UP)));
98                  }
99  
100             }
101         }
102         if (ObjectUtils.isNull(document.getVendorInvoiceAmount())) {
103             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.VENDOR_INVOICE_AMOUNT, OLEKeyConstants.ERROR_REQUIRED, PREQDocumentsStrings.VENDOR_INVOICE_AMOUNT);
104             valid &= false;
105         }
106         GlobalVariables.getMessageMap().clearErrorPath();
107         return valid;
108     }
109 
110 }