View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    *
4    * Licensed under the Educational Community License, Version 1.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/ecl1.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.coa.businessobject.Account;
20  import org.kuali.ole.module.purap.PurapConstants;
21  import org.kuali.ole.module.purap.PurapPropertyConstants;
22  import org.kuali.ole.select.OleSelectConstant;
23  import org.kuali.ole.select.businessobject.OleInvoiceItem;
24  import org.kuali.ole.select.businessobject.OleSufficientFundCheck;
25  import org.kuali.ole.select.constants.OleSelectPropertyConstants;
26  import org.kuali.ole.select.document.OleInvoiceDocument;
27  import org.kuali.ole.select.document.service.OleInvoiceFundCheckService;
28  import org.kuali.ole.select.document.service.OleInvoiceService;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.OLEKeyConstants;
31  import org.kuali.ole.sys.OLEPropertyConstants;
32  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
33  import org.kuali.ole.sys.context.SpringContext;
34  import org.kuali.ole.sys.document.validation.GenericValidation;
35  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
36  import org.kuali.rice.core.api.util.RiceKeyConstants;
37  import org.kuali.rice.core.api.util.type.KualiDecimal;
38  import org.kuali.rice.core.api.util.type.KualiInteger;
39  import org.kuali.rice.krad.service.BusinessObjectService;
40  import org.kuali.rice.krad.service.KRADServiceLocator;
41  import org.kuali.rice.krad.util.GlobalVariables;
42  import org.kuali.rice.krad.util.KRADConstants;
43  
44  import java.sql.Timestamp;
45  import java.util.ArrayList;
46  import java.util.HashMap;
47  import java.util.List;
48  import java.util.Map;
49  
50  public class OleInvoiceItemValidation extends GenericValidation {
51  
52      private BusinessObjectService businessObjectService;
53  
54      @Override
55      public boolean validate(AttributedDocumentEvent event) {
56          boolean valid = true;
57          boolean isUnordered = false;
58          boolean orderedWithQty = false;
59          int count = 0;
60          OleInvoiceDocument document = (OleInvoiceDocument) event.getDocument();
61          if ((document.getPaymentMethod() != null) && (document.getPaymentMethod().getPaymentMethodId() != null)) {
62              document.setPaymentMethodId(document.getPaymentMethod().getPaymentMethodId());
63          }
64  
65          if(document.getPaymentMethodIdentifier() != null) {
66              String paymentType = SpringContext.getBean(OleInvoiceService.class).getPaymentMethodType(document.getPaymentMethodIdentifier());
67              if (paymentType.equals(OLEConstants.DEPOSIT)) {
68                  for (OleInvoiceItem item : (List<OleInvoiceItem>) document.getItems()) {
69                      if (item.getItemTypeCode().equals("ITEM")) {
70                          if (item.getSourceAccountingLineList().size() <= 0) {
71                              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.OFFSET_ACCT_LINE, OLEKeyConstants.ERROR_REQUIRED, PurapConstants.PRQSDocumentsStrings.OFFSET_ACCT_LINE);
72                              valid = false;
73                          }
74                          if (item.getSourceAccountingLineList().size() > 0) {
75                              for (SourceAccountingLine accountingLine : item.getSourceAccountingLineList()) {
76                                  String accNo = accountingLine.getAccountNumber();
77                                  Integer vendorIdentifier = document.getVendorHeaderGeneratedIdentifier();
78                                  if ((accNo != null && accNo != "") && (vendorIdentifier != null)) {
79                                      Map accMap = new HashMap();
80                                      accMap.put("accountNumber", accNo);
81                                      accMap.put("subFundGroupCode", "CLRREV");
82                                      accMap.put("vendorHeaderGeneratedIdentifier", vendorIdentifier);
83                                      List<Account> account = (List<Account>) KRADServiceLocator.getBusinessObjectService().findMatching(Account.class, accMap);
84                                      if (account.size() <= 0) {
85                                          GlobalVariables.getMessageMap().putError(PurapPropertyConstants.OFFSET_ACCT_LINE, OleSelectConstant.ERROR_INVALID_ACC_NO, accNo);
86                                          valid = false;
87                                      }
88  
89                                  }
90  
91                              }
92  
93  
94                          }
95  
96                      }
97                  }
98              }
99          }
100 
101         List<OleInvoiceItem> items = document.getItems();
102         for (OleInvoiceItem item : items) {
103             count += 1;
104             if (item.getItemTypeCode().equalsIgnoreCase(PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
105                 isUnordered = true;
106                 if (StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE)) {
107                     if (item.getItemQuantity() == null || item.getItemQuantity().isLessEqual(KualiDecimal.ZERO)) {
108                         GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_ITEM_QUANTITY_REQUIRED, "Item " + count);
109                         valid = false;
110                     }
111                 }
112             }
113             if (item.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
114                 if (StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_UNORDERED_ITEM_CODE) || StringUtils.equalsIgnoreCase(item.getItemTypeCode(), PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE)) {
115                     if (item.getItemDescription() == null || item.getItemDescription().trim().length() <= 0) {
116                         GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_REQUIRED, "Item " + count);
117                         valid = false;
118                     }
119                 }
120                 if (item.getItemQuantity() != null && item.getItemQuantity().isGreaterThan(KualiDecimal.ZERO)) {
121                     if (item.getItemNoOfParts() == null || (item.getItemNoOfParts()).isLessEqual(KualiInteger.ZERO)) {
122                         GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_NO_OF_PARTS_REQUIRED, "Item " + count);
123                         valid = false;
124                     }
125                 }
126             }
127         }
128         if (!isUnordered) {
129             for (OleInvoiceItem item : items) {
130                 if (item.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
131                     if (item.getItemQuantity() == null || item.getItemQuantity().isLessEqual(KualiDecimal.ZERO)) {
132                         orderedWithQty = false;
133                     } else {
134                         orderedWithQty = true;
135                     }
136                 }
137                 if (orderedWithQty) {
138                     break;
139                 }
140             }
141         }
142         if (!orderedWithQty) {
143             GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, OleSelectConstant.ERROR_ATLEAST_ONE_ITEM_QTY_REQUIRED);
144             valid = false;
145         }
146 
147 
148 /*        List<SourceAccountingLine> sourceAccountingLineList = document.getSourceAccountingLines();
149         for (SourceAccountingLine accLine : sourceAccountingLineList) {
150             Map searchMap = new HashMap();
151             Map<String, Object> key = new HashMap<String, Object>();
152             String chartCode = accLine.getChartOfAccountsCode();
153             String accNo = accLine.getAccountNumber();
154             String objectCd = accLine.getFinancialObjectCode();
155             key.put(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE, chartCode);
156             key.put(OLEPropertyConstants.ACCOUNT_NUMBER, accNo);
157             String option = null;
158             OleSufficientFundCheck account = getBusinessObjectService().findByPrimaryKey(OleSufficientFundCheck.class,
159                     key);
160             if (account != null) {
161                 option = account.getNotificationOption();
162                 if (option != null) {
163                     if (option.equals(OLEPropertyConstants.BLOCK_USE)) {
164                         boolean fundCheck = SpringContext.getBean(OleInvoiceFundCheckService.class)
165                                 .hasSufficientFundCheckRequired(accLine);
166                         if (fundCheck) {
167                             GlobalVariables.getMessageMap().putError(OLEConstants.ERROR_MSG_FOR_INSUFF_FUND,
168                                     RiceKeyConstants.ERROR_CUSTOM,
169                                     OLEConstants.INSUFF_FUND + accLine.getAccountNumber());
170                             valid = false;
171                         }
172                     } else if (option.equals(OLEPropertyConstants.WARNING_MSG)) {
173                         boolean fundcheckRequired = SpringContext.getBean(OleInvoiceFundCheckService.class)
174                                 .hasSufficientFundCheckRequired(accLine);
175                         if (fundcheckRequired) {
176                             GlobalVariables.getMessageMap().putWarning(PurapConstants.DETAIL_TAB_ERRORS,
177                                     OleSelectPropertyConstants.INSUFF_FUND,
178                                     new String[]{accLine.getAccountNumber()});
179                         }
180                     }
181                 }
182             }
183         }*/
184         /*if(!(document.getInvoiceNumber()!=null && document.getInvoiceNumber().equalsIgnoreCase(""))){
185             boolean isInvoiceNoDuplicateExist = true;
186             String combinationUserString = "";
187             Map<String, Object> map = new HashMap<String, Object>();
188             if (!(document.getInvoiceNumber() != null && document.getInvoiceNumber().equalsIgnoreCase(""))) {
189                 map.put(OLEConstants.InvoiceDocument.INVOICE_NUMBER, document.getInvoiceNumber().toString());
190                 combinationUserString += document.getInvoiceNumber().toString();
191             }
192             if (!(document.getInvoiceDate() != null && document.getInvoiceDate().toString().equalsIgnoreCase(""))) {
193                 map.put(OLEConstants.InvoiceDocument.INVOICE_DATE, document.getInvoiceDate());
194                 combinationUserString += document.getInvoiceDate();
195             }
196             if (!(document.getVendorHeaderGeneratedIdentifier() != null && document.getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(""))) {
197                 map.put(OLEConstants.InvoiceDocument.VENDOR_GENERATED_IDENTIFIER, document.getVendorHeaderGeneratedIdentifier().toString());
198                 combinationUserString += document.getVendorHeaderGeneratedIdentifier().toString();
199             }
200             if (!(document.getVendorDetailAssignedIdentifier() != null && document.getVendorDetailAssignedIdentifier().toString().equalsIgnoreCase(""))) {
201                 map.put(OLEConstants.InvoiceDocument.VENDOR_DETAIL_ASSIGNED_GENERATED_IDENTIFIER, document.getVendorDetailAssignedIdentifier().toString());
202                 combinationUserString += document.getVendorDetailAssignedIdentifier().toString();
203             }
204             List<OleInvoiceDocument> documents = (List<OleInvoiceDocument>) KRADServiceLocator.getBusinessObjectService().findMatching(OleInvoiceDocument.class, map);
205             //List<String> combinationList = new ArrayList<String>();
206             Map<String, String> combinationMap = new HashMap<String, String>();
207             if (documents.size() > 0) {
208                 String combinationString = "";
209                 for (OleInvoiceDocument invoiceDocument : documents) {
210 
211                     if (!(invoiceDocument.getInvoiceNumber() != null && invoiceDocument.getInvoiceNumber().equalsIgnoreCase(""))) {
212                         combinationString += invoiceDocument.getInvoiceNumber().toString();
213                     }
214                     if (!(invoiceDocument.getInvoiceDate() != null && invoiceDocument.getInvoiceDate().toString().equalsIgnoreCase(""))) {
215                         combinationString += invoiceDocument.getInvoiceDate().toString();
216                     }
217                     if (!(invoiceDocument.getVendorHeaderGeneratedIdentifier() != null && invoiceDocument.getVendorHeaderGeneratedIdentifier().toString().equalsIgnoreCase(""))) {
218                         combinationString += invoiceDocument.getVendorHeaderGeneratedIdentifier();
219                     }
220                     if (!(invoiceDocument.getVendorDetailAssignedIdentifier() != null && invoiceDocument.getVendorDetailAssignedIdentifier().toString().equalsIgnoreCase(""))) {
221                         combinationString += invoiceDocument.getVendorDetailAssignedIdentifier();
222                     }
223                     // combinationList.add(combinationString.toString());
224                     combinationMap.put(combinationString.toString(), invoiceDocument.getApplicationDocumentStatus());
225 
226                 }
227             } else {
228                 isInvoiceNoDuplicateExist = false;
229             }
230             boolean isDocumentSaved = false;
231             if (document.getDocumentHeader() != null && document.getDocumentHeader().getWorkflowDocument() != null) {
232                 if (document.getDocumentHeader().getWorkflowDocument().isSaved()) {
233                     isDocumentSaved = true;
234                 }
235             }
236             if (!isDocumentSaved) {
237                 if (combinationMap.containsKey(combinationUserString)) {
238                     isInvoiceNoDuplicateExist = true;
239                 } else {
240                     isInvoiceNoDuplicateExist = false;
241                 }
242                 if (isInvoiceNoDuplicateExist) {
243                     GlobalVariables.getMessageMap().putError(KRADConstants.GLOBAL_ERRORS, OLEConstants.InvoiceDocument.ERROR_DUPLICATE_INVOICE_DATE_NUMBER_VND);
244                     valid &= false;
245                 }
246             }
247         }*/
248 
249 
250         return valid;
251 
252     }
253 
254 
255     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
256         this.businessObjectService = businessObjectService;
257     }
258 
259     public BusinessObjectService getBusinessObjectService() {
260         if (businessObjectService == null) {
261             businessObjectService = SpringContext.getBean(BusinessObjectService.class);
262         }
263         return businessObjectService;
264     }
265 
266 }