1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.module.purap.document.validation.impl;
17
18 import org.kuali.ole.module.purap.PurapKeyConstants;
19 import org.kuali.ole.module.purap.PurapPropertyConstants;
20 import org.kuali.ole.module.purap.businessobject.CreditMemoItem;
21 import org.kuali.ole.module.purap.document.VendorCreditMemoDocument;
22 import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
23 import org.kuali.ole.sys.OLEKeyConstants;
24 import org.kuali.ole.sys.OLEPropertyConstants;
25 import org.kuali.ole.sys.document.validation.GenericValidation;
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.kns.service.DataDictionaryService;
29 import org.kuali.rice.krad.util.GlobalVariables;
30
31 public class VendorCreditMemoItemQuantityValidation extends GenericValidation {
32
33 private DataDictionaryService dataDictionaryService;
34 private CreditMemoItem itemForValidation;
35
36 public boolean validate(AttributedDocumentEvent event) {
37 boolean valid = true;
38 OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
39 String errorKeyPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" + (itemForValidation.getItemLineNumber() - 1) + "].";
40 String errorKey = errorKeyPrefix + PurapPropertyConstants.QUANTITY;
41
42 if (itemForValidation.getItemQuantity() != null) {
43 if (itemForValidation.getItemQuantity().isNegative() && cmDocument.getInvoiceIdentifier() == null) {
44 String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.QUANTITY);
45 GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_AMOUNT_NONPOSITIVE, label);
46 valid = false;
47 }
48
49
50 KualiDecimal invoicedQuantity = getSourceTotalInvoiceQuantity(cmDocument, itemForValidation);
51 if (itemForValidation.getItemQuantity().isGreaterThan(invoicedQuantity) && cmDocument.getInvoiceIdentifier() == null) {
52 GlobalVariables.getMessageMap().putError(errorKey, PurapKeyConstants.ERROR_CREDIT_MEMO_ITEM_QUANTITY_TOOMUCH);
53 valid = false;
54 }
55 } else {
56
57 KualiDecimal invoicedQuantity = getSourceTotalInvoiceQuantity(cmDocument, itemForValidation);
58 if (cmDocument.getInvoiceIdentifier() == null && itemForValidation.getItemType().isQuantityBasedGeneralLedgerIndicator() && (invoicedQuantity != null && invoicedQuantity.isGreaterThan(KualiDecimal.ZERO)) && (itemForValidation.getExtendedPrice() != null && itemForValidation.getExtendedPrice().isGreaterThan(KualiDecimal.ZERO))) {
59 String label = dataDictionaryService.getAttributeErrorLabel(CreditMemoItem.class, PurapPropertyConstants.QUANTITY);
60 GlobalVariables.getMessageMap().putError(errorKey, OLEKeyConstants.ERROR_REQUIRED, label);
61 valid = false;
62 }
63 }
64
65 return valid;
66 }
67
68
69
70
71
72
73
74
75 protected KualiDecimal getSourceTotalInvoiceQuantity(VendorCreditMemoDocument cmDocument, CreditMemoItem item) {
76 KualiDecimal invoicedQuantity = null;
77
78 if (cmDocument.isSourceDocumentPurchaseOrder()) {
79 invoicedQuantity = item.getPoInvoicedTotalQuantity();
80 } else {
81 invoicedQuantity = item.getPreqInvoicedTotalQuantity();
82 }
83
84 return invoicedQuantity;
85 }
86
87 public DataDictionaryService getDataDictionaryService() {
88 return dataDictionaryService;
89 }
90
91 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
92 this.dataDictionaryService = dataDictionaryService;
93 }
94
95 public CreditMemoItem getItemForValidation() {
96 return itemForValidation;
97 }
98
99 public void setItemForValidation(CreditMemoItem itemForValidation) {
100 this.itemForValidation = itemForValidation;
101 }
102
103 }