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.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.businessobject.PurchaseOrderItem;
23  import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
24  import org.kuali.ole.module.purap.document.PurchaseOrderAmendmentDocument;
25  import org.kuali.ole.module.purap.document.service.PurchaseOrderService;
26  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
27  import org.kuali.rice.core.api.util.type.KualiDecimal;
28  import org.kuali.rice.kew.api.WorkflowDocument;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  public class PurchaseOrderAmendmentNewIndividualItemValidation extends PurchaseOrderNewIndividualItemValidation {
32  
33      private PurchaseOrderService purchaseOrderService;
34  
35      /**
36       * Overrides the method in PurchaseOrderNewIndividualItemValidation to add additional validations that are specific to Amendment.
37       *
38       * @see org.kuali.ole.module.purap.document.validation.impl.PurchaseOrderDocumentRule#newIndividualItemValidation(org.kuali.ole.module.purap.document.PurchasingAccountsPayableDocument, java.lang.String, org.kuali.ole.module.purap.businessobject.PurApItem)
39       */
40      @Override
41      public boolean validate(AttributedDocumentEvent event) {
42          boolean valid = super.validate(event);
43          PurchaseOrderItem item = (PurchaseOrderItem) getItemForValidation();
44          String identifierString = item.getItemIdentifierString();
45          if ((item.getItemInvoicedTotalQuantity() != null) && (!(item.getItemInvoicedTotalQuantity()).isZero())) {
46              if (item.getItemQuantity() == null) {
47                  valid = false;
48                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMND_NULL, "Item Quantity", identifierString);
49              } else if (item.getItemQuantity().compareTo(item.getItemInvoicedTotalQuantity()) < 0) {
50                  valid = false;
51                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMND_INVALID, "Item Quantity", identifierString);
52              }
53          }
54  
55          if (item.getItemInvoicedTotalAmount() != null) {
56              KualiDecimal total = item.getTotalAmount().abs();
57              if ((total == null) || total.compareTo(item.getItemInvoicedTotalAmount().abs()) < 0) {
58                  valid = false;
59                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_AMND_INVALID_AMT, "Item Extended Price", identifierString);
60              }
61          }
62  
63          PurchaseOrderAmendmentDocument document = (PurchaseOrderAmendmentDocument) event.getDocument();
64          WorkflowDocument workflowDocument = document.getDocumentHeader().getWorkflowDocument();
65  
66          // run additional accounting line check for items added to POA via receiving line, only after document in enroute
67          if (!(workflowDocument.isInitiated() || workflowDocument.isSaved()) && purchaseOrderService.isNewUnorderedItem(item)) {
68  
69              // check to see if the account lines are empty
70              if (item.getSourceAccountingLines() == null || item.getSourceAccountingLines().size() == 0) {
71                  valid = false;
72                  GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY, PurapKeyConstants.ERROR_ITEM_ACCOUNTING_INCOMPLETE, identifierString, identifierString);
73              }
74          }
75  
76          return valid;
77      }
78  
79      /**
80       * Overrides to provide validation for PurchaseOrderAmendmentDocument.
81       *
82       * @see org.kuali.ole.module.purap.document.validation.impl.PurchasingDocumentRuleBase#validateCommodityCodes(org.kuali.ole.module.purap.businessobject.PurApItem, boolean)
83       */
84      @Override
85      protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
86          //If the item is inactive then don't need any of the following validations.
87          if (!((PurchaseOrderItem) item).isItemActiveIndicator()) {
88              return true;
89          } else {
90              return super.validateCommodityCodes(item, commodityCodeRequired);
91          }
92      }
93  
94      /**
95       * Overrides the method in PurchasingDocumentRuleBase so that we'll return true
96       * if the item has been previously saved to the database and we'll only check for
97       * the commodity code active flag if the item has not been previously saved to
98       * the database.
99       *
100      * @param item
101      * @param commodityCodeRequired
102      * @return
103      */
104     protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
105         if (item.getVersionNumber() != null) {
106             return true;
107         } else {
108             if (!((PurchasingItemBase) item).getCommodityCode().isActive()) {
109                 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
110                 return false;
111             }
112             return true;
113         }
114     }
115 
116     public PurchaseOrderService getPurchaseOrderService() {
117         return purchaseOrderService;
118     }
119 
120     public void setPurchaseOrderService(PurchaseOrderService purchaseOrderService) {
121         this.purchaseOrderService = purchaseOrderService;
122     }
123 }