1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.fp.document.validation.impl;
17
18 import static org.kuali.ole.sys.OLEConstants.AMOUNT_PROPERTY_NAME;
19 import static org.kuali.ole.sys.OLEConstants.BALANCE_TYPE_BASE_BUDGET;
20 import static org.kuali.ole.sys.OLEConstants.BALANCE_TYPE_CURRENT_BUDGET;
21 import static org.kuali.ole.sys.OLEConstants.BALANCE_TYPE_MONTHLY_BUDGET;
22 import static org.kuali.ole.sys.OLEConstants.CREDIT_AMOUNT_PROPERTY_NAME;
23 import static org.kuali.ole.sys.OLEConstants.DEBIT_AMOUNT_PROPERTY_NAME;
24 import static org.kuali.ole.sys.OLEConstants.GL_DEBIT_CODE;
25 import static org.kuali.ole.sys.OLEConstants.JOURNAL_LINE_HELPER_PROPERTY_NAME;
26 import static org.kuali.ole.sys.OLEConstants.NEW_SOURCE_ACCT_LINE_PROPERTY_NAME;
27 import static org.kuali.ole.sys.OLEConstants.SQUARE_BRACKET_LEFT;
28 import static org.kuali.ole.sys.OLEConstants.SQUARE_BRACKET_RIGHT;
29 import static org.kuali.ole.sys.OLEConstants.VOUCHER_LINE_HELPER_CREDIT_PROPERTY_NAME;
30 import static org.kuali.ole.sys.OLEConstants.VOUCHER_LINE_HELPER_DEBIT_PROPERTY_NAME;
31 import static org.kuali.ole.sys.OLEKeyConstants.ERROR_ZERO_AMOUNT;
32 import static org.kuali.ole.sys.OLEKeyConstants.ERROR_ZERO_OR_NEGATIVE_AMOUNT;
33 import static org.kuali.ole.sys.OLEKeyConstants.JournalVoucher.ERROR_NEGATIVE_NON_BUDGET_AMOUNTS;
34 import static org.kuali.ole.sys.OLEPropertyConstants.BALANCE_TYPE;
35
36 import org.apache.commons.lang.StringUtils;
37 import org.kuali.ole.fp.document.JournalVoucherDocument;
38 import org.kuali.ole.sys.businessobject.AccountingLine;
39 import org.kuali.ole.sys.document.validation.GenericValidation;
40 import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
41 import org.kuali.rice.core.api.util.type.KualiDecimal;
42 import org.kuali.rice.krad.util.GlobalVariables;
43
44
45
46
47 public class JournalVoucherAccountingLineAmountValidation extends GenericValidation {
48 private JournalVoucherDocument journalVoucherForValidation;
49 private AccountingLine accountingLineForValidation;
50
51
52
53
54
55
56
57
58
59 public boolean validate(AttributedDocumentEvent event) {
60 KualiDecimal amount = getAccountingLineForValidation().getAmount();
61
62 getJournalVoucherForValidation().refreshReferenceObject(BALANCE_TYPE);
63
64 if (getJournalVoucherForValidation().getBalanceType().isFinancialOffsetGenerationIndicator()) {
65
66 if (amount.isZero()) {
67 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(true), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
68 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(false), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
69
70 return false;
71 }
72 else if (amount.isNegative()) {
73 String debitCreditCode = getAccountingLineForValidation().getDebitCreditCode();
74 if (StringUtils.isNotBlank(debitCreditCode) && GL_DEBIT_CODE.equals(debitCreditCode)) {
75 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(true), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
76 }
77 else {
78 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(buildMessageMapKeyPathForDebitCreditAmount(false), ERROR_ZERO_OR_NEGATIVE_AMOUNT, "an accounting line");
79 }
80
81 return false;
82 }
83 }
84 else {
85
86 if (amount.isZero()) {
87 GlobalVariables.getMessageMap().putError(AMOUNT_PROPERTY_NAME, ERROR_ZERO_AMOUNT, "an accounting line");
88 return false;
89 }
90 else if (amount.isNegative()) {
91 if (!getAccountingLineForValidation().getBalanceTypeCode().equals(BALANCE_TYPE_BASE_BUDGET) && !getAccountingLineForValidation().getBalanceTypeCode().equals(BALANCE_TYPE_CURRENT_BUDGET) && !getAccountingLineForValidation().getBalanceTypeCode().equals(BALANCE_TYPE_MONTHLY_BUDGET)) {
92 GlobalVariables.getMessageMap().putError(AMOUNT_PROPERTY_NAME, ERROR_NEGATIVE_NON_BUDGET_AMOUNTS);
93 }
94 }
95 }
96
97 return true;
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112 protected String buildMessageMapKeyPathForDebitCreditAmount(boolean isDebit) {
113
114 boolean isNewLineAdd = GlobalVariables.getMessageMap().getErrorPath().contains(NEW_SOURCE_ACCT_LINE_PROPERTY_NAME);
115 isNewLineAdd |= GlobalVariables.getMessageMap().getErrorPath().contains(NEW_SOURCE_ACCT_LINE_PROPERTY_NAME);
116
117 if (isNewLineAdd) {
118 return isDebit ? DEBIT_AMOUNT_PROPERTY_NAME : CREDIT_AMOUNT_PROPERTY_NAME;
119 }
120 else {
121 String index = StringUtils.substringBetween(GlobalVariables.getMessageMap().getKeyPath("", true), SQUARE_BRACKET_LEFT, SQUARE_BRACKET_RIGHT);
122 String indexWithParams = SQUARE_BRACKET_LEFT + index + SQUARE_BRACKET_RIGHT;
123 return isDebit ? (JOURNAL_LINE_HELPER_PROPERTY_NAME + indexWithParams + VOUCHER_LINE_HELPER_DEBIT_PROPERTY_NAME) : (JOURNAL_LINE_HELPER_PROPERTY_NAME + indexWithParams + VOUCHER_LINE_HELPER_CREDIT_PROPERTY_NAME);
124 }
125 }
126
127
128
129
130
131 public AccountingLine getAccountingLineForValidation() {
132 return accountingLineForValidation;
133 }
134
135
136
137
138
139 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
140 this.accountingLineForValidation = accountingLineForValidation;
141 }
142
143
144
145
146
147 public JournalVoucherDocument getJournalVoucherForValidation() {
148 return journalVoucherForValidation;
149 }
150
151
152
153
154
155 public void setJournalVoucherForValidation(JournalVoucherDocument journalVoucherForValidation) {
156 this.journalVoucherForValidation = journalVoucherForValidation;
157 }
158 }