1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.select.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.select.businessobject.OleCreditMemoItem;
23 import org.kuali.ole.select.document.OleVendorCreditMemoDocument;
24 import org.kuali.ole.sys.document.validation.GenericValidation;
25 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26 import org.kuali.rice.core.api.util.type.KualiDecimal;
27 import org.kuali.rice.krad.util.GlobalVariables;
28
29 import java.math.BigDecimal;
30
31 public class OleVendorCreditMemoAccountTotalValidation extends GenericValidation {
32
33 private PurApItem itemForValidation;
34
35
36
37
38 @Override
39 public boolean validate(AttributedDocumentEvent event) {
40 boolean valid = true;
41
42
43 OleVendorCreditMemoDocument cmDocument = (OleVendorCreditMemoDocument) event.getDocument();
44 BigDecimal totalAmount = BigDecimal.ZERO;
45 BigDecimal desiredAmount = (itemForValidation.getTotalAmount() == null) ? new BigDecimal(0) : itemForValidation
46 .getTotalAmount().bigDecimalValue();
47
48 KualiDecimal prorateSurcharge = KualiDecimal.ZERO;
49
50 OleCreditMemoItem crdtItem = (OleCreditMemoItem) itemForValidation;
51 if (crdtItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && crdtItem.getExtendedPrice() != null
52 && crdtItem.getExtendedPrice().compareTo(KualiDecimal.ZERO) != 0) {
53 if (crdtItem.getItemSurcharge() != null
54 && (crdtItem.getItemTypeCode().equals("ITEM") || crdtItem.getItemTypeCode()
55 .equalsIgnoreCase("UNOR"))) {
56 prorateSurcharge = new KualiDecimal(crdtItem.getItemSurcharge()).multiply(crdtItem.getItemQuantity());
57 }
58 if (prorateSurcharge.isNegative()) {
59 prorateSurcharge = prorateSurcharge.negated();
60 desiredAmount = desiredAmount.add(new BigDecimal(prorateSurcharge.toString()));
61 } else {
62 desiredAmount = desiredAmount.subtract(new BigDecimal(prorateSurcharge.toString()));
63 }
64
65 }
66 for (PurApAccountingLine account : itemForValidation.getSourceAccountingLines()) {
67 if (account.getAmount() != null) {
68 totalAmount = totalAmount.add(account.getAmount().bigDecimalValue());
69 } else {
70 totalAmount = totalAmount.add(BigDecimal.ZERO);
71 }
72 }
73 if (desiredAmount.compareTo(totalAmount) != 0 && cmDocument.getInvoiceIdentifier() == null) {
74 GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
75 PurapKeyConstants.ERROR_ITEM_ACCOUNTING_TOTAL_AMOUNT, itemForValidation.getItemIdentifierString(),
76 desiredAmount.toString());
77 valid = false;
78 }
79
80
81 return valid;
82 }
83
84
85 public PurApItem getItemForValidation() {
86 return itemForValidation;
87 }
88
89
90 public void setItemForValidation(PurApItem itemForValidation) {
91 this.itemForValidation = itemForValidation;
92 }
93
94 }