View Javadoc
1   /*
2    * Copyright 2008-2009 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.module.purap.document.validation.impl;
17  
18  import org.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.businessobject.PurApAccountingLine;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.document.PurchaseOrderDocument;
23  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
24  import org.kuali.ole.sys.context.SpringContext;
25  import org.kuali.ole.sys.document.validation.GenericValidation;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  
29  import java.util.List;
30  
31  public class PurchaseOrderAmendmentAccountValidation extends GenericValidation {
32  
33      public boolean validate(AttributedDocumentEvent event) {
34  
35          boolean valid = true;
36          PurchaseOrderDocument poaDocument = (PurchaseOrderDocument) event.getDocument();
37          List<PurApItem> items = poaDocument.getItemsActiveOnly();
38  
39          PurchaseOrderDocument po = SpringContext.getBean(PurchaseOrderService.class).getCurrentPurchaseOrder(poaDocument.getPurapDocumentIdentifier());
40          List<PurApItem> poItems = po.getItems();
41  
42          for (PurApItem item : items) {
43              String identifierString = item.getItemIdentifierString();
44  
45              // check to see if the account has been expired or inactive
46              if (item.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) && item.getSourceAccountingLines() != null && item.getSourceAccountingLines().size() > 0) {
47  
48                  // check only if there are changes on the item
49                  if (isItemChanged(item, poItems)) {
50                      List<PurApAccountingLine> accountingLines = item.getSourceAccountingLines();
51                      for (PurApAccountingLine accountingLine : accountingLines) {
52                          if (accountingLine.getAccount().isExpired()) {
53                              valid = false;
54                              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNT_EXPIRED, accountingLine.getAccount().getAccountNumber());
55                              break;
56                          }
57                          if (!accountingLine.getAccount().isActive()) {
58                              valid = false;
59                              GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNT_INACTIVE, accountingLine.getAccount().getAccountNumber());
60                              break;
61                          }
62                      }
63                  }
64              }
65          }
66  
67          return valid;
68      }
69  
70      private boolean isItemChanged(PurApItem poaItem, List<PurApItem> poItems) {
71  
72          boolean changed = false;
73  
74          int poaItemId = poaItem.getItemLineNumber().intValue();
75  
76          for (PurApItem poItem : poItems) {
77  
78              if (poItem.getItemTypeCode().equals(PurapConstants.ItemTypeCodes.ITEM_TYPE_ITEM_CODE) && poaItemId == poItem.getItemLineNumber().intValue()) {
79                  // required fields so they cannot be null
80                  if (poaItem.getItemQuantity().intValue() != poItem.getItemQuantity().intValue()) {
81                      changed = true;
82                  }
83                  if (!poaItem.getItemUnitOfMeasureCode().equals(poItem.getItemUnitOfMeasureCode())) {
84                      changed = true;
85                  }
86                  if (poaItem.getItemUnitPrice().floatValue() != poItem.getItemUnitPrice().floatValue()) {
87                      changed = true;
88                  }
89                  if (poaItem.getTotalAmount().floatValue() != poItem.getTotalAmount().floatValue()) {
90                      changed = true;
91                  }
92                  if (poaItem.getItemAssignedToTradeInIndicator() != poItem.getItemAssignedToTradeInIndicator()) {
93                      changed = true;
94                  }
95                  // optional fields
96                  if ((poaItem.getItemCatalogNumber() != null && !poaItem.getItemCatalogNumber().equals(poItem.getItemCatalogNumber())) ||
97                          (poItem.getItemCatalogNumber() != null && !poItem.getItemCatalogNumber().equals(poaItem.getItemCatalogNumber()))) {
98                      changed = true;
99                  }
100                 if ((poaItem.getItemDescription() != null && !poaItem.getItemDescription().equals(poItem.getItemDescription())) ||
101                         (poItem.getItemDescription() != null && !poItem.getItemDescription().equals(poaItem.getItemDescription()))) {
102                     changed = true;
103                 }
104                 if ((poaItem.getExtendedPrice() != null && poItem.getExtendedPrice() != null && poaItem.getExtendedPrice().floatValue() != poItem.getExtendedPrice().floatValue()) ||
105                         (poaItem.getExtendedPrice() != null && poaItem.getExtendedPrice().floatValue() != 0 && poItem.getExtendedPrice() == null) ||
106                         (poaItem.getExtendedPrice() == null && poItem.getExtendedPrice() != null && poItem.getExtendedPrice().floatValue() != 0)) {
107                     changed = true;
108                 }
109                 if ((poaItem.getItemTaxAmount() != null && poItem.getItemTaxAmount() != null && poaItem.getItemTaxAmount().floatValue() != poItem.getItemTaxAmount().floatValue()) ||
110                         (poaItem.getItemTaxAmount() != null && poaItem.getItemTaxAmount().floatValue() != 0 && poItem.getItemTaxAmount() != null) ||
111                         (poaItem.getItemTaxAmount() == null && poItem.getItemTaxAmount() != null && poItem.getItemTaxAmount().floatValue() != 0)) {
112                     changed = true;
113                 }
114 
115                 break;
116             }
117         }
118 
119         return changed;
120     }
121 }